mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 01:17:37 -02:30
Force custom inventory scripts to start with a hashbang
This commit is contained in:
@@ -979,6 +979,12 @@ class CustomInventoryScriptSerializer(BaseSerializer):
|
|||||||
model = CustomInventoryScript
|
model = CustomInventoryScript
|
||||||
fields = ('*', "script", "organization")
|
fields = ('*', "script", "organization")
|
||||||
|
|
||||||
|
def validate_script(self, attrs, source):
|
||||||
|
script_contents = attrs.get(source, '')
|
||||||
|
if not script_contents.startswith("#!"):
|
||||||
|
raise serializers.ValidationError('Script must begin with a hashbang sequence: i.e.... #!/usr/bin/env python')
|
||||||
|
return attrs
|
||||||
|
|
||||||
def to_native(self, obj):
|
def to_native(self, obj):
|
||||||
ret = super(CustomInventoryScriptSerializer, self).to_native(obj)
|
ret = super(CustomInventoryScriptSerializer, self).to_native(obj)
|
||||||
if obj is None:
|
if obj is None:
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ from awx.main.tests.base import BaseTest, BaseTransactionTest
|
|||||||
__all__ = ['InventoryTest', 'InventoryUpdatesTest']
|
__all__ = ['InventoryTest', 'InventoryUpdatesTest']
|
||||||
|
|
||||||
TEST_SIMPLE_INVENTORY_SCRIPT = "#!/usr/bin/env python\nimport json\nprint json.dumps({'hosts': ['ahost-01', 'ahost-02', 'ahost-03', 'ahost-04']})"
|
TEST_SIMPLE_INVENTORY_SCRIPT = "#!/usr/bin/env python\nimport json\nprint json.dumps({'hosts': ['ahost-01', 'ahost-02', 'ahost-03', 'ahost-04']})"
|
||||||
|
TEST_SIMPLE_INVENTORY_SCRIPT_WITHOUT_HASHBANG = "import json\nprint json.dumps({'hosts': ['ahost-01', 'ahost-02', 'ahost-03', 'ahost-04']})"
|
||||||
|
|
||||||
TEST_UNICODE_INVENTORY_SCRIPT = u"""#!/usr/bin/env python
|
TEST_UNICODE_INVENTORY_SCRIPT = u"""#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
@@ -299,6 +300,10 @@ class InventoryTest(BaseTest):
|
|||||||
new_failed_script = dict(name="Shouldfail", description="This test should fail", script=TEST_SIMPLE_INVENTORY_SCRIPT, organization=self.organizations[0].id)
|
new_failed_script = dict(name="Shouldfail", description="This test should fail", script=TEST_SIMPLE_INVENTORY_SCRIPT, organization=self.organizations[0].id)
|
||||||
self.post(inventory_scripts, data=new_failed_script, expect=403, auth=self.get_normal_credentials())
|
self.post(inventory_scripts, data=new_failed_script, expect=403, auth=self.get_normal_credentials())
|
||||||
|
|
||||||
|
failed_no_shebang = dict(name="ShouldAlsoFail", descript="This test should also fail", script=TEST_SIMPLE_INVENTORY_SCRIPT_WITHOUT_HASHBANG,
|
||||||
|
organization=self.organizations[0].id)
|
||||||
|
self.post(inventory_scripts, data=failed_no_shebang, expect=400, auth=self.get_super_credentials())
|
||||||
|
|
||||||
def test_main_line(self):
|
def test_main_line(self):
|
||||||
|
|
||||||
# some basic URLs...
|
# some basic URLs...
|
||||||
|
|||||||
Reference in New Issue
Block a user