2019-08-23 12:38:38 +02:00
|
|
|
- module_defaults:
|
|
|
|
group/aws:
|
|
|
|
region: "{{ aws_region }}"
|
|
|
|
aws_access_key: "{{ aws_access_key }}"
|
|
|
|
aws_secret_key: "{{ aws_secret_key }}"
|
|
|
|
security_token: "{{ security_token | default(omit) }}"
|
|
|
|
block:
|
|
|
|
# ============================================================
|
|
|
|
# PREPARATION
|
|
|
|
#
|
|
|
|
# Get some information about who we are before starting our tests
|
|
|
|
# we'll need this as soon as we start working on the policies
|
|
|
|
- name: get ARN of calling user
|
|
|
|
aws_caller_info:
|
|
|
|
register: aws_caller_info
|
2019-02-13 04:06:58 +01:00
|
|
|
|
2019-08-23 12:38:38 +02:00
|
|
|
# IAM Roles completes before the Role is fully instantiated, create it here
|
|
|
|
# to ensure it exists when we need it for updating the policies
|
|
|
|
- name: create an IAM role that can do nothing
|
|
|
|
iam_role:
|
|
|
|
name: "{{ resource_prefix }}-kms-role"
|
|
|
|
state: present
|
|
|
|
assume_role_policy_document: '{"Version": "2012-10-17", "Statement": {"Action": "sts:AssumeRole", "Principal": {"Service": "ec2.amazonaws.com"}, "Effect": "Deny"} }'
|
|
|
|
register: iam_role_result
|
2019-02-13 04:06:58 +01:00
|
|
|
# ============================================================
|
2019-08-23 12:38:38 +02:00
|
|
|
# TESTS
|
2019-02-13 04:06:58 +01:00
|
|
|
- name: See whether key exists and its current state
|
2019-06-10 15:52:49 +02:00
|
|
|
aws_kms_info:
|
2019-02-13 04:06:58 +01:00
|
|
|
filters:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
|
|
|
|
- name: create a key
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
2019-02-28 19:54:18 +01:00
|
|
|
tags:
|
|
|
|
Hello: World
|
2019-02-13 04:06:58 +01:00
|
|
|
state: present
|
|
|
|
enabled: yes
|
|
|
|
register: create_kms
|
|
|
|
|
|
|
|
- name: assert that state is enabled
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- create_kms.key_state == "Enabled"
|
2019-02-28 19:54:18 +01:00
|
|
|
- create_kms.tags['Hello'] == 'World'
|
2019-02-13 04:06:58 +01:00
|
|
|
|
|
|
|
- name: find facts about the key
|
2019-06-10 15:52:49 +02:00
|
|
|
aws_kms_info:
|
2019-02-13 04:06:58 +01:00
|
|
|
filters:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
register: new_key
|
|
|
|
|
|
|
|
- name: check that a key was found
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- new_key["keys"]|length == 1
|
|
|
|
|
2019-08-23 12:38:38 +02:00
|
|
|
- name: Update Policy on key to match AWS Console generate policy
|
|
|
|
aws_kms:
|
2019-08-24 01:56:45 +02:00
|
|
|
key_id: '{{ new_key["keys"][0]["key_id"] }}'
|
2019-08-23 12:38:38 +02:00
|
|
|
policy: "{{ lookup('template', 'console-policy.j2') | to_json }}"
|
|
|
|
register: kms_policy_changed
|
|
|
|
|
|
|
|
- name: Policy should have been changed
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- kms_policy_changed is changed
|
|
|
|
|
|
|
|
- name: Attempt to re-assert the same policy
|
|
|
|
aws_kms:
|
2019-08-24 01:56:45 +02:00
|
|
|
alias: "alias/{{ resource_prefix }}-kms"
|
2019-08-23 12:38:38 +02:00
|
|
|
policy: "{{ lookup('template', 'console-policy.j2') | to_json }}"
|
|
|
|
register: kms_policy_changed
|
|
|
|
|
|
|
|
- name: Policy should not have changed since it was last set
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- kms_policy_changed is succeeded
|
2019-02-13 04:06:58 +01:00
|
|
|
|
|
|
|
- name: grant user-style access to production secrets
|
|
|
|
aws_kms:
|
|
|
|
mode: grant
|
2019-08-24 01:56:45 +02:00
|
|
|
alias: "alias/{{ resource_prefix }}-kms"
|
2019-02-13 04:06:58 +01:00
|
|
|
role_name: "{{ resource_prefix }}-kms-role"
|
|
|
|
grant_types: "role,role grant"
|
|
|
|
|
|
|
|
- name: find facts about the key
|
2019-06-10 15:52:49 +02:00
|
|
|
aws_kms_info:
|
2019-02-13 04:06:58 +01:00
|
|
|
filters:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
register: new_key
|
|
|
|
|
|
|
|
- name: remove access to production secrets from role
|
|
|
|
aws_kms:
|
|
|
|
mode: deny
|
2019-08-24 01:56:45 +02:00
|
|
|
alias: "alias/{{ resource_prefix }}-kms"
|
2019-02-13 04:06:58 +01:00
|
|
|
role_arn: "{{ iam_role_result.iam_role.arn }}"
|
|
|
|
|
|
|
|
- name: find facts about the key
|
2019-06-10 15:52:49 +02:00
|
|
|
aws_kms_info:
|
2019-02-13 04:06:58 +01:00
|
|
|
filters:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
register: new_key
|
|
|
|
|
|
|
|
- name: Allow the IAM role to use a specific Encryption Context
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
purge_grants: yes
|
|
|
|
purge_tags: yes
|
|
|
|
grants:
|
|
|
|
- name: test_grant
|
|
|
|
grantee_principal: "{{ iam_role_result.iam_role.arn }}"
|
2019-08-20 13:15:40 +02:00
|
|
|
retiring_principal: "{{ aws_caller_info.arn }}"
|
2019-02-13 04:06:58 +01:00
|
|
|
constraints:
|
|
|
|
encryption_context_equals:
|
|
|
|
environment: test
|
|
|
|
application: testapp
|
|
|
|
operations:
|
|
|
|
- Decrypt
|
|
|
|
- RetireGrant
|
|
|
|
register: grant_one
|
|
|
|
|
|
|
|
- name: assert grant added
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- grant_one.changed
|
|
|
|
- grant_one.grants|length == 1
|
|
|
|
|
|
|
|
- name: Add a second grant
|
2019-02-28 19:54:18 +01:00
|
|
|
aws_kms:
|
2019-02-13 04:06:58 +01:00
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
grants:
|
|
|
|
- name: another_grant
|
|
|
|
grantee_principal: "{{ iam_role_result.iam_role.arn }}"
|
2019-08-20 13:15:40 +02:00
|
|
|
retiring_principal: "{{ aws_caller_info.arn }}"
|
2019-02-13 04:06:58 +01:00
|
|
|
constraints:
|
|
|
|
encryption_context_equals:
|
|
|
|
Environment: second
|
|
|
|
Application: anotherapp
|
|
|
|
operations:
|
|
|
|
- Decrypt
|
|
|
|
- RetireGrant
|
|
|
|
register: grant_two
|
|
|
|
|
|
|
|
- name: assert grant added
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- grant_two.changed
|
|
|
|
- grant_two.grants|length == 2
|
|
|
|
|
|
|
|
- name: Add a second grant again
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
grants:
|
|
|
|
- name: another_grant
|
|
|
|
grantee_principal: "{{ iam_role_result.iam_role.arn }}"
|
2019-08-20 13:15:40 +02:00
|
|
|
retiring_principal: "{{ aws_caller_info.arn }}"
|
2019-02-13 04:06:58 +01:00
|
|
|
constraints:
|
|
|
|
encryption_context_equals:
|
|
|
|
Environment: second
|
|
|
|
Application: anotherapp
|
|
|
|
operations:
|
|
|
|
- Decrypt
|
|
|
|
- RetireGrant
|
|
|
|
register: grant_two_again
|
|
|
|
|
|
|
|
- name: assert grant added
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- not grant_two_again.changed
|
|
|
|
- grant_two_again.grants|length == 2
|
|
|
|
|
|
|
|
- name: Update the grants with purge_grants set
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
purge_grants: yes
|
|
|
|
grants:
|
|
|
|
- name: third_grant
|
|
|
|
grantee_principal: "{{ iam_role_result.iam_role.arn }}"
|
2019-08-20 13:15:40 +02:00
|
|
|
retiring_principal: "{{ aws_caller_info.arn }}"
|
2019-02-13 04:06:58 +01:00
|
|
|
constraints:
|
|
|
|
encryption_context_equals:
|
|
|
|
environment: third
|
|
|
|
application: onemoreapp
|
|
|
|
operations:
|
|
|
|
- Decrypt
|
|
|
|
- RetireGrant
|
|
|
|
register: grant_three
|
|
|
|
|
|
|
|
- name: assert grants replaced
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- grant_three.changed
|
|
|
|
- grant_three.grants|length == 1
|
|
|
|
|
|
|
|
- name: update third grant to change encryption context equals to subset
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
grants:
|
|
|
|
- name: third_grant
|
|
|
|
grantee_principal: "{{ iam_role_result.iam_role.arn }}"
|
2019-08-20 13:15:40 +02:00
|
|
|
retiring_principal: "{{ aws_caller_info.arn }}"
|
2019-02-13 04:06:58 +01:00
|
|
|
constraints:
|
|
|
|
encryption_context_subset:
|
|
|
|
environment: third
|
|
|
|
application: onemoreapp
|
|
|
|
operations:
|
|
|
|
- Decrypt
|
|
|
|
- RetireGrant
|
|
|
|
register: grant_three_update
|
|
|
|
|
|
|
|
- name: assert grants replaced
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "grant_three_update.changed"
|
|
|
|
- "grant_three_update.grants|length == 1"
|
|
|
|
- "'encryption_context_equals' not in grant_three_update.grants[0].constraints"
|
|
|
|
- "'encryption_context_subset' in grant_three_update.grants[0].constraints"
|
|
|
|
|
|
|
|
- name: tag encryption key
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
tags:
|
|
|
|
tag_one: tag_one
|
|
|
|
tag_two: tag_two
|
|
|
|
register: tag_kms
|
|
|
|
|
|
|
|
- name: assert tags added and grants remain in place
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "tag_kms.changed"
|
|
|
|
- "tag_kms.grants|length == 1"
|
|
|
|
- "'tag_one' in tag_kms.tags"
|
|
|
|
- "'tag_two' in tag_kms.tags"
|
|
|
|
|
|
|
|
- name: add, replace, remove tags
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
purge_tags: yes
|
|
|
|
tags:
|
|
|
|
tag_two: tag_two_updated
|
|
|
|
tag_three: tag_three
|
|
|
|
register: tag_kms_update
|
|
|
|
|
|
|
|
- name: assert tags correctly changed
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "tag_kms_update.changed"
|
|
|
|
- "'tag_one' not in tag_kms_update.tags"
|
|
|
|
- "'tag_two' in tag_kms_update.tags"
|
|
|
|
- "tag_kms_update.tags.tag_two == 'tag_two_updated'"
|
|
|
|
- "'tag_three' in tag_kms_update.tags"
|
|
|
|
|
|
|
|
- name: make no real tag change
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
register: tag_kms_no_update
|
|
|
|
|
|
|
|
- name: assert no change to tags
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "not tag_kms_no_update.changed"
|
|
|
|
- "'tag_one' not in tag_kms_no_update.tags"
|
|
|
|
- "'tag_two' in tag_kms_no_update.tags"
|
|
|
|
- "tag_kms_no_update.tags.tag_two == 'tag_two_updated'"
|
|
|
|
- "'tag_three' in tag_kms_no_update.tags"
|
|
|
|
|
|
|
|
- name: update the key's description and disable it
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
description: test key for testing
|
|
|
|
enabled: no
|
|
|
|
register: update_key
|
|
|
|
|
|
|
|
- name: assert that state is enabled
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- update_key.description == "test key for testing"
|
|
|
|
- update_key.key_state == "Disabled"
|
|
|
|
- update_key.changed
|
|
|
|
|
|
|
|
- name: delete the key
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: absent
|
|
|
|
register: delete_kms
|
|
|
|
|
|
|
|
- name: assert that state is pending deletion
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- delete_kms.key_state == "PendingDeletion"
|
|
|
|
- delete_kms.changed
|
|
|
|
|
2019-08-24 01:56:45 +02:00
|
|
|
- name: re-delete the key
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: absent
|
|
|
|
register: delete_kms
|
|
|
|
|
|
|
|
- name: assert that state is pending deletion
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- delete_kms.key_state == "PendingDeletion"
|
|
|
|
- delete_kms is not changed
|
|
|
|
|
2019-02-13 04:06:58 +01:00
|
|
|
- name: undelete and enable the key
|
|
|
|
aws_kms:
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
state: present
|
|
|
|
enabled: yes
|
|
|
|
register: undelete_kms
|
|
|
|
|
|
|
|
- name: assert that state is enabled
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- undelete_kms.key_state == "Enabled"
|
|
|
|
- undelete_kms.changed
|
|
|
|
|
2019-08-24 01:56:45 +02:00
|
|
|
- name: delete a non-existant key
|
|
|
|
aws_kms:
|
|
|
|
key_id: '00000000-0000-0000-0000-000000000000'
|
|
|
|
state: absent
|
|
|
|
register: delete_kms
|
|
|
|
|
|
|
|
- name: assert that state is unchanged
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- delete_kms is not changed
|
|
|
|
|
2019-02-13 04:06:58 +01:00
|
|
|
always:
|
|
|
|
# ============================================================
|
2019-08-23 12:38:38 +02:00
|
|
|
# CLEAN-UP
|
2019-02-13 04:06:58 +01:00
|
|
|
- name: finish off by deleting key
|
|
|
|
aws_kms:
|
|
|
|
state: absent
|
|
|
|
alias: "{{ resource_prefix }}-kms"
|
|
|
|
register: destroy_result
|
|
|
|
|
|
|
|
- name: remove the IAM role
|
|
|
|
iam_role:
|
|
|
|
name: "{{ resource_prefix }}-kms-role"
|
|
|
|
state: absent
|
|
|
|
register: iam_role_result
|