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

New resource: aws_ssoadmin_customer_managed_policy_attachment #25915

Merged
merged 34 commits into from
Sep 8, 2022
Merged

New resource: aws_ssoadmin_customer_managed_policy_attachment #25915

merged 34 commits into from
Sep 8, 2022

Conversation

oakbramble
Copy link

@oakbramble oakbramble commented Jul 21, 2022

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for pull request followers and do not help prioritize the request

Closes #25904.
Closes #26068.

This is my first contribution and first time working with Go, so thank you in advance for your feedback.
I have added aws_ssoadmin_customer_managed_policy_attachment and manually tested that it is working, creating the following resource:

resource "aws_ssoadmin_customer_managed_policy_attachment" "example" {
  customer_managed_policy_name         = aws_iam_policy.example.name
  customer_managed_policy_path         = "/" # optional
  instance_arn       = tolist(data.aws_ssoadmin_instances.example.arns)[0]
  permission_set_arn = aws_ssoadmin_permission_set.example.arn
}

I based the acceptance tests on managed_policy_attachment_test.go. They are passing (see below), however there are a couple of areas I am unsure about:

  • I have used some random names in the test resources being created, to avoid conflicts, however am unsure if I'm doing this properly?
  • Not all of the resources are being destroyed after the tests, I assume this could be related to the above and testAccCheckCustomerManagedPolicyAttachmentDestroy?

Update: The tests are working and all resources being destroyed properly. I have marked the PR ready for review

Thanks for bearing with me as I get used to the repo and I hope this PR is helpful.

Output from acceptance testing:

$ make testacc TESTS=TestAccSSOAdminCustomerManagedPolicyAttachment PKG=ssoadmin

==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/ssoadmin/... -v -count 1 -parallel 20 -run='TestAccSSOAdminCustomerManagedPolicyAttachment'  -timeout 5m
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_basic
=== PAUSE TestAccSSOAdminCustomerManagedPolicyAttachment_basic
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew
=== PAUSE TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_disappears
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_disappears (27.91s)
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_Disappears_permissionSet
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_Disappears_permissionSet (22.14s)
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies
=== PAUSE TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies
=== CONT  TestAccSSOAdminCustomerManagedPolicyAttachment_basic
=== CONT  TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies
=== CONT  TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_basic (31.19s)
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies (51.96s)
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew (56.06s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/ssoadmin	106.228s

@github-actions github-actions bot added needs-triage Waiting for first response or review from a maintainer. provider Pertains to the provider itself, rather than any interaction with AWS. service/ssoadmin Issues and PRs that pertain to the ssoadmin service. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. size/XL Managed by automation to categorize the size of a PR. labels Jul 21, 2022
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Welcome @oakbramble 👋

It looks like this is your first Pull Request submission to the Terraform AWS Provider! If you haven’t already done so please make sure you have checked out our CONTRIBUTING guide and FAQ to make sure your contribution is adhering to best practice and has all the necessary elements in place for a successful approval.

Also take a look at our FAQ which details how we prioritize Pull Requests for inclusion.

Thanks again, and welcome to the community! 😃

@github-actions github-actions bot added the documentation Introduces or discusses updates to documentation. label Jul 22, 2022
@oakbramble
Copy link
Author

oakbramble commented Jul 22, 2022

Ok, I had the tests working, however now I am having trouble attaching multiple policies, both when testing manually and in the acc tests.

  Error: error attaching Customer Managed Policy to SSO Permission Set (arn:aws:sso:::permissionSet/ssoins-68049fb1eee8a70d/ps-69539f9a26bf4698): ConflictException: Conflicting operation occurred on Permission Set with Id: ps-69539f9a26bf4698 and SSOInstanceArn: arn:aws:sso:::instance/ssoins-68049fb1eee8a70d.

It has a conflict when adding or deleting more than one aws_ssoadmin_customer_managed_policy_attachment resource at the same time. Adding them sequentially is ok. It's producing a ConflictException when calling AttachCustomerManagedPolicyReferenceToPermissionSet for the second policy attachment.

Any advice on how I can make sure these calls are handled properly, so they do not cause conflict, would be much apprecated.

Update: I think this was an issue in my environment. Everything is working now and I have tested repeatedly, both manually and the acc tests.

@oakbramble oakbramble marked this pull request as ready for review July 22, 2022 12:33
@oakbramble oakbramble changed the title [WIP] New resource: aws_ssoadmin_customer_managed_policy_attachment New resource: aws_ssoadmin_customer_managed_policy_attachment Jul 22, 2022
@csanders-git
Copy link

csanders-git commented Jul 24, 2022

Tested with your branch and I can reproduce the same error on both additions and deletions. When adding or detaching multiple CMPolicies. This is def a raise condition documented in the code.

│ Error: error detaching Customer Managed Policy (potato2) from SSO Permission Set (arn:aws:sso:::permissionSet/ssoins-722321efc6569e0f/ps-e49642a3f1f3d8ca): ConflictException: Conflicting operation occurred on Permission Set with Id: ps-e49642a3f1f3d8ca and SSOInstanceArn: arn:aws:sso:::instance/ssoins-722321efc6569e0f.

The suggested mitigation in the AWS docs is to perform a backoff and retry (https://github.com/aws/aws-sdk-go/blob/ea9ec95e2434772e82b415093f9b660bd849322f/service/ssoadmin/api.go#L92).

Initial testing has shown the nicer solution might be to querying ListCustomerManagedPolicyReferencesInPermissionSetInput until the result is returned (or not returned), with some sane limits on the amount of times this would be tried before returning the conflict error to the user.

       input := &ssoadmin.ListCustomerManagedPolicyReferencesInPermissionSetInput{
                InstanceArn: aws.String(instanceArn),
                MaxResults: aws.Int64(1),
                PermissionSetArn: aws.String(permissionSetArn),
       }

@oakbramble
Copy link
Author

Thanks @csanders-git for taking a look and providing feedback. I have added retry conditions to the create and delete attachment functions (I know you mention a nicer solution, but this is what I could get working). I find that the tests are now consistently passing and with manual testing I can attach and detach multiple customer managed policies at the same time.

(Side note: I do wonder why this conflict doesn't seem to be an issue for the AWS managed policy attachments, unless I am missing a different retry logic around them?)

Please let me know if there is anything else you see issue with, or if you do not find that this solution for multiple attachments is working.

@justinretzolk justinretzolk added new-resource Introduces a new resource. and removed needs-triage Waiting for first response or review from a maintainer. labels Jul 25, 2022
@nomeelnoj
Copy link

The AWS CLI and API docs both use a config block for the CustomerManagedPolicyReference. We should do the same here (easy to extend in the future if/when they allow searching for the policy by tags, etc.)

I also think we should call it what it is, which is a reference to a policy. Please also make sure in the docs it is clear that in order for this to work, the customer managed policy has to first be present in the account that is getting assigned to the permission set.

resource "aws_ssoadmin_customer_managed_policy_attachment" "example" {
  customer_managed_policy_reference { # This should end in `reference`, as well as be a config block
    name         = aws_iam_policy.example.name
    path         = aws_iam_policy.example.path # Optional
  }
  instance_arn       = tolist(data.aws_ssoadmin_instances.example.arns)[0]
  permission_set_arn = aws_ssoadmin_permission_set.example.arn
}

This would be more inline with the AWS API docs and be easier to understand and support.

Also, doing it with a config block allows for better terraform logic with the ability to use dynamics to find and attach policies only for permission sets that are deployed in accounts where the policies exist.

@paulschwarzenberger
Copy link

paulschwarzenberger commented Jul 26, 2022

(Side note: I do wonder why this conflict doesn't seem to be an issue for the AWS managed policy attachments, unless I am missing a different retry logic around them?)

@oakbramble thank you so much for working on this, I'll be using the new feature as soon as it's released!

I believe I've seen the equivalent issue with AWS managed policy attachments, resulting in timeouts and failure to apply when trying to attach managed policies to large numbers of accounts and groups at the same time via a module. I worked around the issue by adding an otherwise unnecessary for_each statement to the resource.

@oakbramble
Copy link
Author

Thanks for the feedback @nomeelnoj . I had been thinking from a user perspective it would be more readable without a config block, particularly when path will likely not be used so often, but I understand the reasoning to make it more like the API.

I have not had much time to look at this today, however have reconfigured to use the config block and tested manually that this is working (basic, multiple policies, force new and delete). I have not sorted the tests or docs yet, but will look at them tomorrow. In the meantime, I have pushed the WIP, just in case there are any further tips forthcoming 😄 (I am getting to grips with the repo/ Go, thank you for bearing with me.)

@paulschwarzenberger thanks for the info about the managed policy attachments - I'll watch out for that :)

@oakbramble
Copy link
Author

Following @nomeelnoj 's comments, there is now a functioning customer_managed_policy_reference config block and the tests have been updated to reflect this.

@ColinHarrington thanks for the review. I will look at removing the path default and updating documentation tomorrow morning.

@oakbramble
Copy link
Author

Ok, docs have been updated and all looks to be in order. To update on the initial post, this is what the final resource looks like:

resource "aws_ssoadmin_customer_managed_policy_attachment" "example" {
  instance_arn       = tolist(data.aws_ssoadmin_instances.example.arns)[0]
  permission_set_arn = aws_ssoadmin_permission_set.example.arn
  customer_managed_policy_reference {
    name       = aws_iam_policy.example.name
    path         = "/" # optional
  }
}

This is the current output from the acceptance testing:

$ make testacc TESTS=TestAccSSOAdminCustomerManagedPolicyAttachment PKG=ssoadmin

==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/ssoadmin/... -v -count 1 -parallel 20 -run='TestAccSSOAdminCustomerManagedPolicyAttachment'  -timeout 180m
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_basic
=== PAUSE TestAccSSOAdminCustomerManagedPolicyAttachment_basic
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew
=== PAUSE TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_disappears
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_disappears (22.10s)
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_Disappears_permissionSet
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_Disappears_permissionSet (49.79s)
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies
=== PAUSE TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies
=== CONT  TestAccSSOAdminCustomerManagedPolicyAttachment_basic
=== CONT  TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies
=== CONT  TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_basic (24.02s)
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies (39.16s)
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew (42.87s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/ssoadmin	114.810s

@oakbramble
Copy link
Author

After the pipeline tests were run last week, I had some errors to correct (sorry I did not see all these extra tests in the make file beforehand). Below is summary of the corrections I have made, based on these errors.

  • Remove a trailing whitespace in documentation (here)
  • Switch from depreciated acctest.ProviderFactories to acctest.ProtoV5ProviderFactories (here)
  • Add TimedOut to a Retry and remove unnecessary type conversions (here)
  • Correct import statement in docs (here)
  • Update tf code in tests to pass terrafmt (here)

In addition to the acc tests, the following are now also passing when I run locally:

  • make semgrep
  • terrafmt
  • make fmt
  • make importlint
  • make golang-ci-lint
  • make docs-lint

I'm having trouble running make providerlint however the provider lint tests did pass in the pipeline already.

Please let me know if there are any more concerns, or if there's anything else I should be doing to get this merged.

(I know the branch doesn't match the naming convention, but I didn't see this until it was too late; will do better next time.)

@csanders-git
Copy link

csanders-git commented Aug 18, 2022

I have retested the latest version of the PR, it works flawlessly.

The contribution guidelines indicate that comments are also an important part of prioritizing PRs, so here we go:

Let me take a stab at discussing why this is so critical to many orgs. AWS-SSO allows for a proper SCIM based integration with IdP's such as Okta. This is only available in a hacky manner via the IAM SAML integration because it doesn't speak SCIM AND it doesn't provide an OIDC Authorization Server for accessing credentials via the CLI which leads to terrible command line tools to assume roles via CLI.
The most recent change corrects a long standing issue with AWS-SSO that effectively made it incompatible with how IAM is supposed to be used in a secure way. Traditionally, IAM encourages organizations to make fine grained reusable policies (customer managed policies) to augment the preset AWS managed policies and provide fine-grained, least priv access.
AWS-SSO broke this model because it (previously) ONLY supported inline (considered to be bad security practice) and AWS Managed Policies. Not only did this make it difficult for most organizations that were properly using IAM to migrate, it also meant there was a functional limit on the size of policies that was different from IAM (i.e one inline policy can only hold N chars where N is less than N*10 customer managed policies).
AWS fixed this issue recently by allowing (this admittedly sorta hacky solution) to let users associate existing customer managed policies with a permission set (translation between SCIM groups/users and Roles).

This change, that @oakbramble so quickly added support for, frees up many organizations to migrate to AWS-SSO and GREATLY improve their security posture.

@tom-henderson
Copy link
Contributor

Going to add another vote here to help prioritise this. Naturally AWS released this feature about 2 weeks after we complete our migration to SSO managed inline policies, but this would still be very useful to us.

@josemdav
Copy link

In addition to what @csanders-git mentions, another business need that support for customer managed policies in permission sets is providing flexibility to application owners / developers to directly manage the permissions at the AWS account level without having to modify the centrally defined permission set. For organizations in which an internal shared responsibility model is implemented, this adds agility.

@bgupta
Copy link

bgupta commented Aug 19, 2022

Please note that many AWS customers were waiting for Amazon to release this feature, and many are now blocked from deploying if they are Terraform customers, even if they don't realize it yet. The inability to manage AWS permissions in Okta via Terraform has been a long-standing pain point, and we're very much looking forward to seeing this feature implemented!

@c0mput3rj0n3s
Copy link

This is a huge deal for us as we're in the midst of a migration from IAM users to AWS SSO in our org. We can't be fine-grained with the SSO inline policies, and we easily bust the 10KB limits if we try, and this feature appearing when it did would have been excellent had the Terraform provider been updated to support it. This would solve so many problems that I'd say it's desperately needed.

@raizyr
Copy link

raizyr commented Aug 26, 2022

I am in the same boat as @c0mput3rj0n3s. We are trying to migrate from IAM to AWS SSO and have similar concerns. This terraform resource is badly needed.

Copy link
Member

@bschaatsbergen bschaatsbergen left a comment

Choose a reason for hiding this comment

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

Overall the contribution looks great, thanks @oakbramble 👏 . I've left a few comments here and there to make things a bit more clear.

Regarding tests; I seem to be unable to run the tests as I'm getting an access denied exception (something I have to sort out).

Next to the above, I do think that the id we construct isn't that pretty - especially as the terraform import requires this identifier (TestPolicy,/,arn:aws:sso:::permissionSet/ssoins-2938j0x8920sbj72/ps-80383020jr9302rk,arn:aws:sso:::instance/ssoins-2938j0x8920sbj72) - What are your thoughts on this? @ewbankkit

I believe that @ewbankkit will perform an additional review before we merge this.

@oakbramble
Copy link
Author

Thanks for the comments @bschaatsbergen ☺️ I have updated everything you mentioned. If you have any further feedback, just let me know.

@oakbramble
Copy link
Author

oakbramble commented Sep 8, 2022

Ok, the semgrep check failed on the last run. I saw that several semgrep changes were merged yesterday and it looks like that was the cause of the failure. I have rebased again to remedy this. As ever, if you think that isn't the case and I have missed something, please let me know.

Copy link
Contributor

@ewbankkit ewbankkit left a comment

Choose a reason for hiding this comment

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

LGTM 🚀.

% make testacc TESTARGS='-run=TestAccSSOAdminCustomerManagedPolicyAttachment_' PKG=ssoadmin ACCTEST_PARALLELISM=2 
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/ssoadmin/... -v -count 1 -parallel 2  -run=TestAccSSOAdminCustomerManagedPolicyAttachment_ -timeout 180m
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_basic
=== PAUSE TestAccSSOAdminCustomerManagedPolicyAttachment_basic
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew
=== PAUSE TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_disappears
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_disappears (29.85s)
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_Disappears_permissionSet
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_Disappears_permissionSet (20.95s)
=== RUN   TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies
=== PAUSE TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies
=== CONT  TestAccSSOAdminCustomerManagedPolicyAttachment_basic
=== CONT  TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_basic (32.87s)
=== CONT  TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_multipleManagedPolicies (52.84s)
--- PASS: TestAccSSOAdminCustomerManagedPolicyAttachment_forceNew (56.37s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/ssoadmin	143.892s

@bschaatsbergen
Copy link
Member

Nice work @oakbramble 👍

@ewbankkit ewbankkit added the enhancement Requests to existing resources that expand the functionality or scope. label Sep 8, 2022
@ewbankkit ewbankkit merged commit 0689eaf into hashicorp:main Sep 8, 2022
@github-actions github-actions bot added this to the v4.30.0 milestone Sep 8, 2022
@oakbramble oakbramble deleted the add-ssoadmin-customer-managed-policies branch September 8, 2022 14:12
@github-actions
Copy link

github-actions bot commented Sep 9, 2022

This functionality has been released in v4.30.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

github-actions bot commented Oct 9, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. enhancement Requests to existing resources that expand the functionality or scope. new-resource Introduces a new resource. provider Pertains to the provider itself, rather than any interaction with AWS. service/ssoadmin Issues and PRs that pertain to the ssoadmin service. size/XL Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet