improve sanitation of empty credential values to match API v1 behavior

This is mostly backwards compatability to avoid surprises: in 3.1.x
if you submit a field value with `null` or an empty string to
a CharField, it's treated as an empty string (and SSH key validation
is skipped).  For boolean field values (`net.authorize`), `null` and
empty string are coerced to `False`.

see: #7216
see: #7218
This commit is contained in:
Ryan Petrello
2017-07-21 11:21:40 -04:00
parent 825dfc9df9
commit a640d6afec
3 changed files with 72 additions and 3 deletions

View File

@@ -420,15 +420,15 @@ def format_ssh_private_key(value):
# These end in a unicode-encoded final character that gets double
# escaped due to being in a Python 2 bytestring, and that causes
# Python's key parsing to barf. Detect this issue and correct it.
if value == '$encrypted$':
return value
if not value or value == '$encrypted$':
return True
if r'\u003d' in value:
value = value.replace(r'\u003d', '=')
try:
validate_ssh_private_key(value)
except django_exceptions.ValidationError as e:
raise jsonschema.exceptions.FormatError(e.message)
return value
return True
class CredentialInputField(JSONSchemaField):
@@ -478,6 +478,13 @@ class CredentialInputField(JSONSchemaField):
return super(CredentialInputField, self).validate(value,
model_instance)
# Backwards compatability: in prior versions, if you submit `null` for
# a credential field value, it just considers the value an empty string
for unset in [key for key, v in model_instance.inputs.items() if not v]:
default_value = model_instance.credential_type.default_for_field(unset)
if default_value is not None:
model_instance.inputs[unset] = default_value
decrypted_values = {}
for k, v in value.items():
if all([