Skeletal aws_kms_keys resource

Signed-off-by: Rony Xavier <rx294@nyu.edu>
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2018-02-01 10:21:54 -05:00
parent 6ae80ad6f7
commit e2e9915aa4
5 changed files with 226 additions and 0 deletions

View file

@ -0,0 +1,70 @@
---
title: About the aws_kms_keys Resource
---
# aws_kms_keys
Use the `aws_kms_keys` InSpec audit resource to test properties of some or all AWS KMS Keys.
AWS Key Management Service (KMS) is a managed service that makes it easy for you to create and control the encryption keys used to encrypt your data, and uses Hardware Security Modules (HSMs) to protect the security of your keys. AWS Key Management Service is integrated with several other AWS services to help you protect the data you store with these services.
Each AWS KMS Key is uniquely identified by its key-id or key-arn.
<br>
## Syntax
An `aws_kms_keys` resource block uses an optional filter to select a group of KMS Keys and then tests that group.
# Verify the number of KMS keys in the AWS account
describe aws_kms_keys do
its('entries.count') { should cmp 10 }
end
<br>
## Examples
The following examples show how to use this InSpec audit resource.
As this is the initial release of `aws_kms_keys`, its limited functionality precludes examples.
<br>
## Matchers
### exists
The control will pass if the filter returns at least one result. Use should_not if you expect zero matches.
# Verify that at least one KMS Key exists.
describe aws_kms_keys
it { should exist }
end
## Properties
### key_arns
Provides a list of key arns for all KMS Keys in the AWS account.
describe aws_kms_keys do
its('key_arns') { should include('arn:aws:kms:us-east-1::key/key-id') }
end
### key_ids
Provides a list of key ids for all KMS Keys in the AWS account.
describe aws_kms_keys do
its('key_ids') { should include('fd7e608b-f435-4186-b8b5-111111111111') }
end
### entries
Provides access to the raw results of the query. This can be useful for checking counts and other advanced operations.
# Allow at most 100 KMS Keys on the account
describe aws_kms_keys do
its('entries.count') { should be <= 100}
end

View file

@ -60,4 +60,8 @@ class AWSConnection
def s3_client
@s3_client ||= Aws::S3::Client.new
end
def kms_client
@kms_client ||= Aws::KMS::Client.new
end
end

44
libraries/aws_kms_keys.rb Normal file
View file

@ -0,0 +1,44 @@
class AwsKmsKeys < Inspec.resource(1)
name 'aws_kms_keys'
desc 'Verifies settings for AWS KMS Keys in bulk'
example '
describe aws_kms_keys do
it { should exist }
end
'
# Underlying FilterTable implementation.
filter = FilterTable.create
filter.add_accessor(:entries)
.add(:exists?) { |x| !x.entries.empty? }
.add(:key_arns, field: :key_arn)
.add(:key_ids, field: :key_id)
filter.connect(self, :key_data)
def key_data
@table
end
def to_s
'KMS Keys'
end
def initialize
backend = AwsKmsKeys::BackendFactory.create
@table = backend.list_keys({ limit: 1000 }).to_h[:keys] # max value for limit is 1000
end
class BackendFactory
extend AwsBackendFactoryMixin
end
class Backend
class AwsClientApi
BackendFactory.set_default_backend(self)
def list_keys(query = {})
AWSConnection.new.kms_client.list_keys(query)
end
end
end
end

View file

@ -0,0 +1,5 @@
control "aws_kms_keys recall" do
describe aws_kms_keys do
it { should exist }
end
end

View file

@ -0,0 +1,103 @@
require 'helper'
require 'aws_kms_keys'
# MAKKPB = MockAwsKmsKeysPluralBackend
# Abbreviation not used outside this file
#=============================================================================#
# Constructor Tests
#=============================================================================#
class AwsKmsKeysConstructorTest < Minitest::Test
def setup
AwsKmsKeys::BackendFactory.select(MAKKPB::Empty)
end
def test_empty_params_ok
AwsKmsKeys.new
end
def test_rejects_unrecognized_params
assert_raises(ArgumentError) { AwsKmsKeys.new(shoe_size: 9) }
end
end
#=============================================================================#
# Search / Recall
#=============================================================================#
class AwsKmsKeysRecallEmptyTest < Minitest::Test
def setup
AwsKmsKeys::BackendFactory.select(MAKKPB::Empty)
end
def test_search_miss_key_empty_kms_key_list
refute AwsKmsKeys.new.exists?
end
end
class AwsKmsKeysRecallBasicTest < Minitest::Test
def setup
AwsKmsKeys::BackendFactory.select(MAKKPB::Basic)
end
def test_search_hit_via_empty_filter
assert AwsKmsKeys.new.exists?
end
end
#=============================================================================#
# Properties
#=============================================================================#
class AwsKmsKeysProperties < Minitest::Test
def setup
AwsKmsKeys::BackendFactory.select(MAKKPB::Basic)
end
def test_property_key_ids
basic = AwsKmsKeys.new
assert_kind_of(Array, basic.key_ids)
assert(basic.key_ids.include?('012026a4-b657-42bf-99ae-111111111111'))
refute(basic.key_ids.include?(nil))
end
def test_property_key_arns
basic = AwsKmsKeys.new
assert_kind_of(Array, basic.key_arns)
assert(basic.key_arns.include?('arn:aws:kms:us-east-1::key/012026a4-b657-42bf-99ae-111111111111'))
refute(basic.key_arns.include?(nil))
end
end
#=============================================================================#
# Test Fixtures
#=============================================================================#
module MAKKPB
class Empty < AwsKmsKeys::Backend
def list_keys(query = {})
OpenStruct.new({ keys: [] })
end
end
class Basic < AwsKmsKeys::Backend
def list_keys(query = {})
fixtures = [
OpenStruct.new({
key_id: '012026a4-b657-42bf-99ae-111111111111',
key_arn: 'arn:aws:kms:us-east-1::key/012026a4-b657-42bf-99ae-111111111111',
}),
OpenStruct.new({
key_id: '012026a4-b657-42bf-99ae-222222222222',
key_arn: 'arn:aws:kms:us-east-1::key/012026a4-b657-42bf-99ae-222222222222',
}),
OpenStruct.new({
key_id: '012026a4-b657-42bf-99ae-333333333333',
key_arn: 'arn:aws:kms:us-east-1::key/012026a4-b657-42bf-99ae-333333333333',
}),
]
OpenStruct.new({ keys: fixtures })
end
end
end