-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#335 Adding warning if a KMS key allows wildcarded principals in its …
…policy (#338) * #335 Adding warning if a KMS key allows wildcarded principals in its policy. * #335 Changing to failure, and adding logic to catch when AWS subkey is set to wildcard. * #335 Modifying KMS key wildcard principal rule to use new KMS key model from cfn-model and included tests for nested hash wildcard principal. * #335 Updating cfn-model version.
- Loading branch information
Showing
12 changed files
with
381 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'cfn-nag/violation' | ||
require_relative 'base' | ||
|
||
class KMSKeyWildcardPrincipalRule < BaseRule | ||
def rule_text | ||
'KMS key should not allow * principal ' \ | ||
'(https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html)' | ||
end | ||
|
||
def rule_type | ||
Violation::FAILING_VIOLATION | ||
end | ||
|
||
def rule_id | ||
'F76' | ||
end | ||
|
||
def audit_impl(cfn_model) | ||
# Select all AWS::KMS::Key resources to audit | ||
violating_keys = cfn_model.resources_by_type('AWS::KMS::Key').select do |key| | ||
# Return key if wildcard_allowed_principals boolean is not empty | ||
!key.key_policy.policy_document.wildcard_allowed_principals.empty? | ||
end | ||
|
||
violating_keys.map(&:logical_resource_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'spec_helper' | ||
require 'cfn-nag/custom_rules/KMSKeyWildcardPrincipalRule' | ||
require 'cfn-model' | ||
|
||
describe KMSKeyWildcardPrincipalRule do | ||
describe 'AWS::KMS::Key' do | ||
context 'when a principal is not set to a wildcard' do | ||
it 'does not return an offending logical resource id' do | ||
cfn_model = CfnParser.new.parse read_test_template('json/kms/kms_key_without_wildcard_principal.json') | ||
actual_logical_resource_ids = KMSKeyWildcardPrincipalRule.new.audit_impl cfn_model | ||
|
||
expect(actual_logical_resource_ids).to eq [] | ||
end | ||
end | ||
context 'when a principal is set to a wildcard' do | ||
it 'returns an offending logical resource id' do | ||
cfn_model = CfnParser.new.parse read_test_template('json/kms/kms_key_with_wildcard_principal.json') | ||
actual_logical_resource_ids = KMSKeyWildcardPrincipalRule.new.audit_impl cfn_model | ||
|
||
expect(actual_logical_resource_ids).to eq ['myKeyWildcardPrincipal'] | ||
end | ||
end | ||
context 'when a principal\'s AWS key is set to a wildcard' do | ||
it 'returns an offending logical resource id' do | ||
cfn_model = CfnParser.new.parse read_test_template('json/kms/kms_key_with_aws_wildcard_principal.json') | ||
actual_logical_resource_ids = KMSKeyWildcardPrincipalRule.new.audit_impl cfn_model | ||
|
||
expect(actual_logical_resource_ids).to eq ['myKeyAwsWildcardPrincipal'] | ||
end | ||
end | ||
context 'when a principal\'s AWS key is an array and contains a wildcard' do | ||
it 'returns an offending logical resource id' do | ||
cfn_model = CfnParser.new.parse read_test_template('json/kms/kms_key_with_aws_array_wildcard_principal.json') | ||
actual_logical_resource_ids = KMSKeyWildcardPrincipalRule.new.audit_impl cfn_model | ||
|
||
expect(actual_logical_resource_ids).to eq ['myKeyAwsArrayWildcardPrincipal'] | ||
end | ||
end | ||
end | ||
end |
44 changes: 44 additions & 0 deletions
44
spec/test_templates/json/kms/kms_key_with_aws_array_wildcard_principal.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"Resources": { | ||
"myKeyAwsArrayWildcardPrincipal" : { | ||
"Type" : "AWS::KMS::Key", | ||
"Properties" : { | ||
"Description" : "An example CMK", | ||
"EnableKeyRotation": "true", | ||
"KeyPolicy" : { | ||
"Version": "2012-10-17", | ||
"Id": "key-default-1", | ||
"Statement": [ | ||
{ | ||
"Sid": "Enable IAM User Permissions", | ||
"Effect": "Allow", | ||
"Principal": {"AWS": "arn:aws:iam::111122223333:root"}, | ||
"Action": "kms:*", | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow administration of the key", | ||
"Effect": "Allow", | ||
"Principal": {"AWS": ["arn:aws:iam::111122223333:root", "*"]}, | ||
"Action": [ | ||
"kms:Create*", | ||
"kms:CancelKeyDeletion" | ||
], | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow use of the key", | ||
"Effect": "Allow", | ||
"Principal": {"AWS": "arn:aws:iam::123456789012:user/Alice"}, | ||
"Action": [ | ||
"kms:GenerateDataKey", | ||
"kms:GenerateDataKeyWithoutPlaintext" | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
spec/test_templates/json/kms/kms_key_with_aws_wildcard_principal.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"Resources": { | ||
"myKeyAwsWildcardPrincipal" : { | ||
"Type" : "AWS::KMS::Key", | ||
"Properties" : { | ||
"Description" : "An example CMK", | ||
"EnableKeyRotation": "true", | ||
"KeyPolicy" : { | ||
"Version": "2012-10-17", | ||
"Id": "key-default-1", | ||
"Statement": [ | ||
{ | ||
"Sid": "Enable IAM User Permissions", | ||
"Effect": "Allow", | ||
"Principal": {"AWS": "arn:aws:iam::111122223333:root"}, | ||
"Action": "kms:*", | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow administration of the key", | ||
"Effect": "Allow", | ||
"Principal": {"AWS": "*"}, | ||
"Action": [ | ||
"kms:Create*", | ||
"kms:CancelKeyDeletion" | ||
], | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow use of the key", | ||
"Effect": "Allow", | ||
"Principal": {"AWS": "arn:aws:iam::123456789012:user/Alice"}, | ||
"Action": [ | ||
"kms:GenerateDataKey", | ||
"kms:GenerateDataKeyWithoutPlaintext" | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
spec/test_templates/json/kms/kms_key_with_wildcard_principal.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"Resources": { | ||
"myKeyWildcardPrincipal" : { | ||
"Type" : "AWS::KMS::Key", | ||
"Properties" : { | ||
"Description" : "An example CMK", | ||
"EnableKeyRotation": "true", | ||
"KeyPolicy" : { | ||
"Version": "2012-10-17", | ||
"Id": "key-default-1", | ||
"Statement": [ | ||
{ | ||
"Sid": "Enable IAM User Permissions", | ||
"Effect": "Allow", | ||
"Principal": {"AWS": "arn:aws:iam::111122223333:root"}, | ||
"Action": "kms:*", | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow administration of the key", | ||
"Effect": "Allow", | ||
"Principal": { "CanonicalUser": "79a59df900b949e55d96a1e698fbacedfd6e09d98eacf8f8d5218e7cd47ef2be" }, | ||
"Action": [ | ||
"kms:Create*", | ||
"kms:CancelKeyDeletion" | ||
], | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow use of the key", | ||
"Effect": "Allow", | ||
"Principal": "*", | ||
"Action": [ | ||
"kms:GenerateDataKey", | ||
"kms:GenerateDataKeyWithoutPlaintext" | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
spec/test_templates/json/kms/kms_key_without_wildcard_principal.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"Resources": { | ||
"myKeyNoWildcardPrincipal" : { | ||
"Type" : "AWS::KMS::Key", | ||
"Properties" : { | ||
"Description" : "An example CMK", | ||
"EnableKeyRotation": "true", | ||
"KeyPolicy" : { | ||
"Version": "2012-10-17", | ||
"Id": "key-default-1", | ||
"Statement": [ | ||
{ | ||
"Sid": "Enable IAM User Permissions", | ||
"Effect": "Allow", | ||
"Principal": {"AWS": "arn:aws:iam::111122223333:root"}, | ||
"Action": "kms:*", | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow administration of the key", | ||
"Effect": "Allow", | ||
"Principal": { "AWS": "arn:aws:iam::123456789012:user/Alice" }, | ||
"Action": [ | ||
"kms:Create*", | ||
"kms:CancelKeyDeletion" | ||
], | ||
"Resource": "*" | ||
}, | ||
{ | ||
"Sid": "Allow use of the key", | ||
"Effect": "Allow", | ||
"Principal": { "AWS": "arn:aws:iam::123456789012:user/Bob" }, | ||
"Action": [ | ||
"kms:GenerateDataKey", | ||
"kms:GenerateDataKeyWithoutPlaintext" | ||
], | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
spec/test_templates/yaml/kms/kms_key_with_aws_array_wildcard_principal.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
Resources: | ||
myKeyAwsArrayWildcardPrincipal: | ||
Type: AWS::KMS::Key | ||
Properties: | ||
Description: An example CMK | ||
EnableKeyRotation: true | ||
KeyPolicy: | ||
Version: 2012-10-17 | ||
Id: key-default-1 | ||
Statement: | ||
- Sid: Enable IAM User Permissions | ||
Effect: Allow | ||
Principal: | ||
AWS: arn:aws:iam::111122223333:root | ||
Action: kms:* | ||
Resource: '*' | ||
- Sid: Allow administration of the key | ||
Effect: Allow | ||
Principal: | ||
AWS: | ||
- arn:aws:iam::111122223333:root | ||
- '*' | ||
Action: | ||
- kms:Create* | ||
- kms:CancelKeyDeletion | ||
Resource: '*' | ||
- Sid: Allow use of the key | ||
Effect: Allow | ||
Principal: | ||
AWS: arn:aws:iam::123456789012:user/Alice | ||
Action: | ||
- kms:GenerateDataKey | ||
- kms:GenerateDataKeyWithoutPlaintext | ||
Resource: '*' |
33 changes: 33 additions & 0 deletions
33
spec/test_templates/yaml/kms/kms_key_with_aws_wildcard_principal.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
Resources: | ||
myKeyAwsWildcardPrincipal: | ||
Type: AWS::KMS::Key | ||
Properties: | ||
Description: An example CMK | ||
EnableKeyRotation: true | ||
KeyPolicy: | ||
Version: 2012-10-17 | ||
Id: key-default-1 | ||
Statement: | ||
- Sid: Enable IAM User Permissions | ||
Effect: Allow | ||
Principal: | ||
AWS: arn:aws:iam::111122223333:root | ||
Action: kms:* | ||
Resource: '*' | ||
- Sid: Allow administration of the key | ||
Effect: Allow | ||
Principal: | ||
AWS: '*' | ||
Action: | ||
- kms:Create* | ||
- kms:CancelKeyDeletion | ||
Resource: '*' | ||
- Sid: Allow use of the key | ||
Effect: Allow | ||
Principal: | ||
AWS: arn:aws:iam::123456789012:user/Alice | ||
Action: | ||
- kms:GenerateDataKey | ||
- kms:GenerateDataKeyWithoutPlaintext | ||
Resource: '*' |
32 changes: 32 additions & 0 deletions
32
spec/test_templates/yaml/kms/kms_key_with_wildcard_principal.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
Resources: | ||
myKeyWildcardPrincipal: | ||
Type: AWS::KMS::Key | ||
Properties: | ||
Description: An example CMK | ||
EnableKeyRotation: true | ||
KeyPolicy: | ||
Version: 2012-10-17 | ||
Id: key-default-1 | ||
Statement: | ||
- Sid: Enable IAM User Permissions | ||
Effect: Allow | ||
Principal: | ||
AWS: arn:aws:iam::111122223333:root | ||
Action: kms:* | ||
Resource: '*' | ||
- Sid: Allow administration of the key | ||
Effect: Allow | ||
Principal: | ||
CanonicalUser: 79a59df900b949e55d96a1e698fbacedfd6e09d98eacf8f8d5218e7cd47ef2be | ||
Action: | ||
- kms:Create* | ||
- kms:CancelKeyDeletion | ||
Resource: '*' | ||
- Sid: Allow use of the key | ||
Effect: Allow | ||
Principal: '*' | ||
Action: | ||
- kms:GenerateDataKey | ||
- kms:GenerateDataKeyWithoutPlaintext | ||
Resource: '*' |
Oops, something went wrong.