Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_kms: Update policy on existing keys (when passed) #60059

Merged
merged 4 commits into from
Aug 23, 2019

Conversation

tremble
Copy link
Contributor

@tremble tremble commented Aug 5, 2019

SUMMARY

Update policy on existing keys when policy is passed as an option.

Fixes #59987

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

lib/ansible/modules/cloud/amazon/aws_kms.py

ADDITIONAL INFORMATION

@willthames PR as promised on Friday

@ansibot
Copy link
Contributor

ansibot commented Aug 5, 2019

@tremble, just so you are aware we have a dedicated Working Group for aws.
You can find other people interested in this in #ansible-aws on Freenode IRC
For more information about communities, meetings and agendas see https://github.com/ansible/community

click here for bot help

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 aws bug This issue/PR relates to a bug. cloud community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. labels Aug 5, 2019
@ansibot
Copy link
Contributor

ansibot commented Aug 5, 2019

The test ansible-test sanity --test pylint [explain] failed with 1 error:

lib/ansible/modules/cloud/amazon/aws_kms.py:654:97: bad-whitespace No space allowed before :         if json.dumps(original_policy, sort_keys=True) != json.dumps(new_policy, sort_keys=True) :                                                                                                  ^

The test ansible-test sanity --test pep8 [explain] failed with 1 error:

lib/ansible/modules/cloud/amazon/aws_kms.py:654:97: E203 whitespace before ':'

click here for bot help

@ansibot ansibot added ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Aug 5, 2019
@willthames
Copy link
Contributor

@tremble this change looks reasonable to me but needs the test suite updated to match (so that it would have caught this problem)

@ansibot
Copy link
Contributor

ansibot commented Aug 5, 2019

The test ansible-test sanity --test pylint [explain] failed with 1 error:

lib/ansible/modules/cloud/amazon/aws_kms.py:654:97: bad-whitespace No space allowed before :         if json.dumps(original_policy, sort_keys=True) != json.dumps(new_policy, sort_keys=True) :                                                                                                  ^

The test ansible-test sanity --test pep8 [explain] failed with 1 error:

lib/ansible/modules/cloud/amazon/aws_kms.py:654:97: E203 whitespace before ':'

click here for bot help

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Aug 5, 2019
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Aug 5, 2019
@tremble
Copy link
Contributor Author

tremble commented Aug 5, 2019

@willthames the integration tests appear to be broken. This may be due to the older version of Boto3 I'm running (rhel 7), however, I'm seeing things like

The full traceback is:
Traceback (most recent call last):
  File "/home/mchappel/.ansible/tmp/ansible-tmp-1565005313.24-218693380161805/AnsiballZ_aws_kms.py", line 139, in <module>
    _ansiballz_main()
  File "/home/mchappel/.ansible/tmp/ansible-tmp-1565005313.24-218693380161805/AnsiballZ_aws_kms.py", line 131, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/mchappel/.ansible/tmp/ansible-tmp-1565005313.24-218693380161805/AnsiballZ_aws_kms.py", line 68, in invoke_module
    imp.load_module('__main__', mod, module, MOD_DESC)
  File "/tmp/ansible_aws_kms_payload_JEyiic/__main__.py", line 937, in <module>
  File "/tmp/ansible_aws_kms_payload_JEyiic/__main__.py", line 903, in main
KeyError: 'key_arn'

Which looks like some refactoring around aliases hasn't done what's expected.

@tremble
Copy link
Contributor Author

tremble commented Aug 8, 2019

See also #60206 for initial test fixes

@omonnig
Copy link

omonnig commented Aug 12, 2019

This PR looks reasonable, but do we care if the policy matches? I wasted way too much time trying to debug my policy before checking the module source and finding the root cause. Just write the policy and be done with it.

How do we handle the BypassPolicyLockoutSafetyCheck? It should default to False, only set to True by the user. This behavior can be dangerous, but allowed IMHO.

@willthames
Copy link
Contributor

We do care if the policy matches, otherwise how do we know whether to report changed correctly?

@tremble
Copy link
Contributor Author

tremble commented Aug 13, 2019

@omonnig Am I understanding correctly that this PR falsely flagged your new policy as not changing anything? Or are you just worried about the additional complexity?

As @willthames mentioned it's there so that "changed" can be correctly reported. Personally I try to ensure that my playbooks are idempotent and "changed" only shows up when something's really changed.

re BypassPolicyLockoutSafetyCheck that would likely want to be an additional option, and for the sake of keeping PRs simple I'd rather not pull it into this PR.

@tremble
Copy link
Contributor Author

tremble commented Aug 13, 2019

@willthames Should I rebase this change on top of #60206 and see if I can test the shape / policy pieces as a part of this PR built on top of #60206 ?

@omonnig
Copy link

omonnig commented Aug 13, 2019

Sorry, @willthames, I was not thinking straight. The Changed functionality is very important and should be included in this PR.

I will put the BypassPolicyLockoutSafetyCheck in another PR.

@tremble
Copy link
Contributor Author

tremble commented Aug 19, 2019

@willthames @omonnig https://github.com/tremble/ansible/tree/aws_kms_keyid_only now uses compare_policies instead of json.dumps I'll update this PR once @jillr or @willthames can give me an idea which order we're going to try and merge everything in.

@tremble
Copy link
Contributor Author

tremble commented Aug 20, 2019

And just for the curious, the reason a simple json dump doesn't work is that it would appear Amazon mangle the Principal lists. My basic testing seems to show that when you add a new principal to the list then where-ever in the list you add it Amazon will add it to the end of the list.

@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Aug 20, 2019
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Aug 20, 2019
Copy link
Contributor

@jillr jillr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test suite passes for me, change looks good. Thanks @tremble.

@willthames
Copy link
Contributor

Looks like security-policy.json is missing a permission:

The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_aws_kms_payload_gnmjc6sa/__main__.py", line 685, in update_key
    connection.put_key_policy(KeyId=key['key_id'], PolicyName='default', Policy=policy)
  File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AccessDeniedException) when calling the PutKeyPolicy operation: User: arn:aws:iam::864163117881:user/AnsibleTestUser is not authorized to perform: kms:PutKeyPolicy on resource: arn:aws:kms:us-west-2:864163117881:key/57be8964-e790-44fb-afe1-2307e8febccd

@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. support:core This issue/PR relates to code supported by the Ansible Engineering Team. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Aug 21, 2019
@tremble
Copy link
Contributor Author

tremble commented Aug 22, 2019

@willthames added

@willthames willthames merged commit 77e4371 into ansible:devel Aug 23, 2019
@willthames
Copy link
Contributor

Tests pass for me locally now, thanks @tremble!

@tremble tremble deleted the kms_policy branch August 23, 2019 19:42
adharshsrivatsr pushed a commit to adharshsrivatsr/ansible that referenced this pull request Sep 3, 2019
* aws_kms: (integration tests) Use module_defaults to reduce the copy and paste

* aws_kms: (integration tests) make sure policy option functions.

* aws_kms: (integration tests) Move iam_role creation to start of playbook.

iam_roles aren't fully created when iam_role completes, there's a delay on the Amazon side before they're fully recognised.

* aws_kms: Update policy on existing keys (when passed)
@ansible ansible locked and limited conversation to collaborators Sep 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 aws bug This issue/PR relates to a bug. cloud core_review In order to be merged, this PR must follow the core review workflow. has_issue module This issue/PR relates to a module. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws_kms doesn't update policy
5 participants