Skip to content

Commit

Permalink
Fail Credentials validation if it contains extra data
Browse files Browse the repository at this point in the history
  • Loading branch information
dblanchette committed Mar 7, 2024
1 parent d0dfc75 commit 54c55fd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
13 changes: 11 additions & 2 deletions credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,20 @@ func ParseSingleCredentials(credentialsMap map[string]interface{}) (Credentials,
default:
return nil, fmt.Errorf("entry %s: unknown credentials type: %s", id, credentialsType)
}
err := mapstructure.Decode(credentialsMap, credentials)
var validationErrors error
delete(credentialsMap, "type")
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
ErrorUnused: true,
Metadata: nil,
Result: credentials})
if err != nil {
validationErrors = multierror.Append(validationErrors, fmt.Errorf("entry %s: unable to create a decoder: %v", id, err))
}
if err := decoder.Decode(credentialsMap); err != nil {
return nil, fmt.Errorf("entry %s: invalid credentials data: %v", id, err)
}
var validationErrors error
credentialsMap["type"] = credentialsType

if err := credentials.BaseValidate(); err != nil {
validationErrors = multierror.Append(validationErrors, err)
}
Expand Down
51 changes: 51 additions & 0 deletions credentials/credentials_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func TestAwsCredentialsValidationErrors(t *testing.T) {
delete(credMap, "access_key")
_, err = ParseSingleCredentials(credMap)
assert.Error(t, err)

// Extra data
credMap["extra"] = "data"
_, err = ParseSingleCredentials(credMap)
assert.Error(t, err)
}

func TestAwsCredentialsToString(t *testing.T) {
Expand All @@ -66,3 +71,49 @@ func TestAwsCredentialsToString(t *testing.T) {
assert.Equal(t, "test -> Type: Amazon Web Services - key:********", cred.ToString(false))
assert.Equal(t, "test -> Type: Amazon Web Services - key:secret", cred.ToString(true))
}

func TestCredentialWithTargetTags(t *testing.T) {
credMap := map[string]interface{}{
"id": "test",
"type": "aws",
"description": "test-desc",
"access_key": "key",
"secret_key": "secret_key",
"target_tags": map[string]interface{}{
"do_match": map[string]interface{}{
"tag1": "value1",
},
},
}

cred, err := ParseSingleCredentials(credMap)
assert.Nil(t, err)

assert.Equal(t, &AmazonWebServicesCredentials{
Base: Base{
ID: "test",
CredType: "Amazon Web Services",
Description: "test-desc",
TargetTags: targetTagsMatcher{DoMatch: map[string]interface{}{"tag1": "value1"}},
},
AccessKey: "key",
SecretKey: "secret_key",
}, cred)
}

func TestCredentialWithTargetTagsMalformed(t *testing.T) {
credMap := map[string]interface{}{
"id": "test",
"type": "aws",
"access_key": "key",
"secret_key": "secret_key",
"target_tags": map[string]interface{}{
"match": map[string]interface{}{
"tag1": "value1",
},
},
}

_, err := ParseSingleCredentials(credMap)
assert.Error(t, err)
}

0 comments on commit 54c55fd

Please sign in to comment.