enforce variable name syntax and uniqueness for Credential Types

see: #6158
This commit is contained in:
Ryan Petrello 2017-05-04 11:51:37 -04:00
parent f3591b81a7
commit ef09744b75
2 changed files with 20 additions and 2 deletions

View File

@ -363,6 +363,8 @@ class JSONSchemaField(JSONBField):
super(JSONSchemaField, self).validate(value, model_instance)
errors = []
for error in Draft4Validator(self.schema(model_instance)).iter_errors(value):
if error.validator == 'pattern' and 'error' in error.schema:
error.message = error.schema['error'] % error.instance
errors.append(error)
if errors:
@ -471,7 +473,11 @@ class CredentialTypeInputField(JSONSchemaField):
'items': {'type': 'string'},
'uniqueItems': True
},
'id': {'type': 'string'},
'id': {
'type': 'string',
'pattern': '^[a-zA-Z_]+[a-zA-Z0-9_]*$',
'error': '%s is an invalid variable name',
},
'label': {'type': 'string'},
'help_text': {'type': 'string'},
'multiline': {'type': 'boolean'},
@ -490,14 +496,24 @@ class CredentialTypeInputField(JSONSchemaField):
value, model_instance
)
ids = {}
for field in value.get('fields', []):
if field.get('id') == 'tower':
id_ = field.get('id')
if id_ == 'tower':
raise django_exceptions.ValidationError(
_('"tower" is a reserved field name'),
code='invalid',
params={'value': value},
)
if id_ in ids:
raise django_exceptions.ValidationError(
_('field IDs must be unique (%s)' % id_),
code='invalid',
params={'value': value},
)
ids[id_] = True
class CredentialTypeInjectorField(JSONSchemaField):

View File

@ -58,6 +58,8 @@ def test_cloud_kind_uniqueness():
({'fields': [{'id': 'username', 'label': 'Username', 'type': 'string'}]}, True),
({'fields': [{'id': 'username', 'label': 'Username', 'help_text': 1}]}, False),
({'fields': [{'id': 'username', 'label': 'Username', 'help_text': 'Help Text'}]}, True), # noqa
({'fields': [{'id': 'username', 'label': 'Username'}, {'id': 'username', 'label': 'Username 2'}]}, False), # noqa
({'fields': [{'id': '$invalid$', 'label': 'Invalid'}]}, False), # noqa
({'fields': [{'id': 'password', 'label': 'Password', 'type': 'number'}]}, True),
({'fields': [{'id': 'ssh_key', 'label': 'SSH Key', 'type': 'ssh_private_key'}]}, True), # noqa
({'fields': [{'id': 'other', 'label': 'Other', 'type': 'boolean'}]}, False),