1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 15:21:13 +03:00

treat tower as a reserved field name for custom CredentialTypes

This commit is contained in:
Ryan Petrello 2017-04-28 11:26:18 -04:00
parent 89f239ce66
commit 03460b311e
2 changed files with 22 additions and 2 deletions

View File

@ -673,6 +673,19 @@ class CredentialTypeInputField(JSONSchemaField):
}
}
def validate(self, value, model_instance):
super(CredentialTypeInputField, self).validate(
value, model_instance
)
for field in value.get('fields', []):
if field.get('id') == 'tower':
raise django_exceptions.ValidationError(
_('"tower" is a reserved field name'),
code='invalid',
params={'value': value},
)
class CredentialTypeInjectorField(JSONSchemaField):
@ -723,8 +736,14 @@ class CredentialTypeInjectorField(JSONSchemaField):
value, model_instance
)
# make sure the inputs are clean first
CredentialTypeInputField().validate(model_instance.inputs, model_instance)
# make sure the inputs are valid first
try:
CredentialTypeInputField().validate(model_instance.inputs, model_instance)
except django_exceptions.ValidationError:
# If `model_instance.inputs` itself is invalid, we can't make an
# estimation as to whether our Jinja templates contain valid field
# names; don't continue
return
# In addition to basic schema validation, search the injector fields
# for template variables and make sure they match the fields defined in

View File

@ -71,6 +71,7 @@ def test_cloud_kind_uniqueness():
({'fields': [{'id': 'become_method', 'label': 'Become', 'choices': []}]}, False),
({'fields': [{'id': 'become_method', 'label': 'Become', 'choices': ['su', 'sudo']}]}, True), # noqa
({'fields': [{'id': 'become_method', 'label': 'Become', 'choices': ['dup', 'dup']}]}, False), # noqa
({'fields': [{'id': 'tower', 'label': 'Reserved!', }]}, False), # noqa
])
def test_cred_type_input_schema_validity(input_, valid):
type_ = CredentialType(