Merge pull request #2563 from AlanCoding/old_timer_types

Strict type enforcement of deprecated launch fields
This commit is contained in:
Alan Rominger
2018-07-17 11:53:17 -04:00
committed by GitHub

View File

@@ -3036,10 +3036,10 @@ class JobTemplateLaunch(RetrieveAPIView):
existing_credentials = obj.credentials.all() existing_credentials = obj.credentials.all()
template_credentials = list(existing_credentials) # save copy of existing template_credentials = list(existing_credentials) # save copy of existing
new_credentials = [] new_credentials = []
for key, conditional in ( for key, conditional, _type, type_repr in (
('credential', lambda cred: cred.credential_type.kind != 'ssh'), ('credential', lambda cred: cred.credential_type.kind != 'ssh', int, 'pk value'),
('vault_credential', lambda cred: cred.credential_type.kind != 'vault'), ('vault_credential', lambda cred: cred.credential_type.kind != 'vault', int, 'pk value'),
('extra_credentials', lambda cred: cred.credential_type.kind not in ('cloud', 'net')) ('extra_credentials', lambda cred: cred.credential_type.kind not in ('cloud', 'net'), Iterable, 'a list')
): ):
if key in modern_data: if key in modern_data:
# if a specific deprecated key is specified, remove all # if a specific deprecated key is specified, remove all
@@ -3048,6 +3048,13 @@ class JobTemplateLaunch(RetrieveAPIView):
existing_credentials = filter(conditional, existing_credentials) existing_credentials = filter(conditional, existing_credentials)
prompted_value = modern_data.pop(key) prompted_value = modern_data.pop(key)
# validate type, since these are not covered by a serializer
if not isinstance(prompted_value, _type):
msg = _(
"Incorrect type. Expected {}, received {}."
).format(type_repr, prompted_value.__class__.__name__)
raise ParseError({key: [msg], 'credentials': [msg]})
# add the deprecated credential specified in the request # add the deprecated credential specified in the request
if not isinstance(prompted_value, Iterable) or isinstance(prompted_value, basestring): if not isinstance(prompted_value, Iterable) or isinstance(prompted_value, basestring):
prompted_value = [prompted_value] prompted_value = [prompted_value]