mirror of
https://github.com/ansible/awx.git
synced 2026-02-20 04:30:05 -03:30
do not accept array as extra_vars
related to #5645 * Since we pass extra_vars to ansible as -e; we require that the top most data-structure be a key-value. NOT an array.
This commit is contained in:
@@ -376,7 +376,7 @@ def test_edit_nonsenstive(patch, job_template_factory, alice):
|
|||||||
'forks': 14,
|
'forks': 14,
|
||||||
'limit': 'something',
|
'limit': 'something',
|
||||||
'verbosity': 5,
|
'verbosity': 5,
|
||||||
'extra_vars': '--',
|
'extra_vars': '---',
|
||||||
'job_tags': 'sometags',
|
'job_tags': 'sometags',
|
||||||
'force_handlers': True,
|
'force_handlers': True,
|
||||||
'skip_tags': 'thistag,thattag',
|
'skip_tags': 'thistag,thattag',
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from awx.main.validators import (
|
|||||||
validate_private_key,
|
validate_private_key,
|
||||||
validate_certificate,
|
validate_certificate,
|
||||||
validate_ssh_private_key,
|
validate_ssh_private_key,
|
||||||
|
vars_validate_or_raise,
|
||||||
)
|
)
|
||||||
from awx.main.tests.data.ssh import (
|
from awx.main.tests.data.ssh import (
|
||||||
TEST_SSH_RSA1_KEY_DATA,
|
TEST_SSH_RSA1_KEY_DATA,
|
||||||
@@ -12,6 +13,7 @@ from awx.main.tests.data.ssh import (
|
|||||||
TEST_OPENSSH_KEY_DATA_LOCKED,
|
TEST_OPENSSH_KEY_DATA_LOCKED,
|
||||||
TEST_SSH_CERT_KEY,
|
TEST_SSH_CERT_KEY,
|
||||||
)
|
)
|
||||||
|
from rest_framework.serializers import ValidationError as RestValidationError
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -96,3 +98,33 @@ def test_cert_with_key():
|
|||||||
assert pem_objects[0]['type'] == 'CERTIFICATE'
|
assert pem_objects[0]['type'] == 'CERTIFICATE'
|
||||||
assert pem_objects[1]['key_type'] == 'rsa'
|
assert pem_objects[1]['key_type'] == 'rsa'
|
||||||
assert not pem_objects[1]['key_enc']
|
assert not pem_objects[1]['key_enc']
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("var_str", [
|
||||||
|
'{"a": "b"}',
|
||||||
|
'---\na: b\nc: d',
|
||||||
|
'',
|
||||||
|
'""',
|
||||||
|
])
|
||||||
|
def test_valid_vars(var_str):
|
||||||
|
vars_validate_or_raise(var_str)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("var_str", [
|
||||||
|
'["a": "b"]',
|
||||||
|
'["a", "b"]',
|
||||||
|
"('a=4', 'c=5')",
|
||||||
|
'"',
|
||||||
|
"''",
|
||||||
|
"5",
|
||||||
|
"6.74",
|
||||||
|
"hello",
|
||||||
|
"OrderedDict([('a', 'b')])",
|
||||||
|
"True",
|
||||||
|
"False",
|
||||||
|
])
|
||||||
|
def test_invalid_vars(var_str):
|
||||||
|
with pytest.raises(RestValidationError):
|
||||||
|
vars_validate_or_raise(var_str)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -179,15 +179,21 @@ def vars_validate_or_raise(vars_str):
|
|||||||
job templates, inventories, or hosts are either an acceptable
|
job templates, inventories, or hosts are either an acceptable
|
||||||
blank string, or are valid JSON or YAML dict
|
blank string, or are valid JSON or YAML dict
|
||||||
"""
|
"""
|
||||||
try:
|
if isinstance(vars_str, dict) or (isinstance(vars_str, basestring) and vars_str == '""'):
|
||||||
json.loads((vars_str or '').strip() or '{}')
|
|
||||||
return vars_str
|
return vars_str
|
||||||
|
|
||||||
|
try:
|
||||||
|
r = json.loads((vars_str or '').strip() or '{}')
|
||||||
|
if isinstance(r, dict):
|
||||||
|
return vars_str
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
r = yaml.safe_load(vars_str)
|
r = yaml.safe_load(vars_str)
|
||||||
if not (isinstance(r, basestring) and r.startswith('OrderedDict(')):
|
# Can be None if '---'
|
||||||
|
if isinstance(r, dict) or r is None:
|
||||||
return vars_str
|
return vars_str
|
||||||
except yaml.YAMLError:
|
except yaml.YAMLError:
|
||||||
pass
|
pass
|
||||||
raise RestValidationError(_('Must be valid JSON or YAML.'))
|
raise RestValidationError(_('Must be valid JSON or YAML.'))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user