Skip to content

Commit

Permalink
don't allow users to specify $encrypted$ for encrypted credential fields
Browse files Browse the repository at this point in the history
this keyword only has value when you _update_ an existing credential
  • Loading branch information
ryanpetrello committed Jul 1, 2020
1 parent e3e69b4 commit 1434e58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions awx/main/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,14 @@ def validate(self, value, model_instance):
else:
decrypted_values[k] = v

# don't allow secrets with $encrypted$ on new object creation
if not model_instance.pk:
for field in model_instance.credential_type.secret_fields:
if value.get(field) == '$encrypted$':
raise serializers.ValidationError({
self.name: [f'$encrypted$ is a reserved keyword, and cannot be used for {field}.']
})

super(JSONSchemaField, self).validate(decrypted_values, model_instance)
errors = {}
for error in Draft4Validator(
Expand Down
16 changes: 16 additions & 0 deletions awx/main/tests/functional/api/test_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,22 @@ def _change_credential_type():
assert response.status_code == 200


@pytest.mark.django_db
@pytest.mark.parametrize('field', ['password', 'ssh_key_data'])
def test_secret_fields_cannot_be_special_encrypted_variable(post, organization, admin, credentialtype_ssh, field):
params = {
'name': 'Best credential ever',
'credential_type': credentialtype_ssh.id,
'inputs': {
'username': 'joe',
field: '$encrypted$',
},
'organization': organization.id,
}
response = post(reverse('api:credential_list'), params, admin, status=400)
assert str(response.data['inputs'][0]) == f'$encrypted$ is a reserved keyword, and cannot be used for {field}.'


@pytest.mark.django_db
def test_ssh_unlock_needed(put, organization, admin, credentialtype_ssh):
params = {
Expand Down

0 comments on commit 1434e58

Please sign in to comment.