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

resource/aws_ssm_document: Update SSM Document Permission Updates to Clear Permission Before Updating #5685

Merged
merged 3 commits into from
Aug 29, 2018

Conversation

brandonstevens
Copy link
Contributor

Reference: #5308

Changes proposed in this pull request:

  • Update SSM Document Permission Updates to Clear Permission Before Updating

Output from acceptance testing:

$  make testacc TESTARGS='-run=TestAccAWSSSMDocument'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -run=TestAccAWSSSMDocument -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccAWSSSMDocument_basic
--- PASS: TestAccAWSSSMDocument_basic (20.38s)
=== RUN   TestAccAWSSSMDocument_update
--- PASS: TestAccAWSSSMDocument_update (28.27s)
=== RUN   TestAccAWSSSMDocument_permission_public
--- PASS: TestAccAWSSSMDocument_permission_public (15.73s)
=== RUN   TestAccAWSSSMDocument_permission_private
--- PASS: TestAccAWSSSMDocument_permission_private (15.64s)
=== RUN   TestAccAWSSSMDocument_permission_change
--- PASS: TestAccAWSSSMDocument_permission_change (26.85s)
=== RUN   TestAccAWSSSMDocument_params
--- PASS: TestAccAWSSSMDocument_params (15.34s)
=== RUN   TestAccAWSSSMDocument_automation
--- PASS: TestAccAWSSSMDocument_automation (29.21s)
=== RUN   TestAccAWSSSMDocument_DocumentFormat_YAML
--- PASS: TestAccAWSSSMDocument_DocumentFormat_YAML (27.88s)
=== RUN   TestAccAWSSSMDocument_Tags
--- PASS: TestAccAWSSSMDocument_Tags (38.03s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	217.371s

@bflad bflad added bug Addresses a defect in current functionality. service/ssm Issues and PRs that pertain to the ssm service. labels Aug 27, 2018
// Since AccountIdsToRemove has higher priority than AccountIdsToAdd,
// we update permissions in two steps: we remove all existing permissions
// before adding new permissions.
o, _ := d.GetChange("permissions")
Copy link
Contributor

@bflad bflad Aug 27, 2018

Choose a reason for hiding this comment

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

It seems like we should be performing a difference against the old and new permissions so updates do not intentionally create "downtime", especially if the second ModifyDocumentPermission API call fails.

I would recommend setting this up using something like the below which should perform this update in one API call (in pseudo-code):

if d.HasChange("permissions") {
  o, n := d.GetChange("permissions")
  oldPermissions := o.(map[string]interface{})
  newPermissions := n.(map[string]interface{})
  oldPermissionsAccountIds := make([]string, 0)
  if v, ok := oldPermissions["account_ids"]; ok && v.(string) != "" {
    oldPermissionsAccountIds = strings.Split(v.(string), ",")
  }
  newPermissionsAccountIds := make([]string, 0)
  if v, ok := newPermissions["account_ids"]; ok && v.(string) != "" {
    newPermissionsAccountIds = strings.Split(v.(string), ",")
  }

  accountIdsToRemove := make([]string, 0)
  for _, oldPermissionsAccountId := range oldPermissionsAccountIds {
    if _, contains := sliceContainsString(newPermissionsAccountIds, oldPermissionsAccountId); !contains {
      accountIdsToRemove = append(accountIdsToRemove, oldPermissionsAccountId)
    }
  }
  accountIdsToAdd := make([]string, 0)
  for _, newPermissionsAccountId := range newPermissionsAccountIds {
    if _, contains := sliceContainsString(oldPermissionsAccountIds, newPermissionsAccountId); !contains {
      accountIdsToAdd = append(accountIdsToAdd, newPermissionsAccountId)
    }
  }

  input := &ssm.ModifyDocumentPermissionInput{
    Name:               aws.String(d.Get("name").(string)),
    PermissionType:     aws.String("Share"),
    AccountIdsToAdd: aws.StringSlice(accountIdsToAdd),
    AccountIdsToRemove: aws.StringSlice(accountIdsToRemove),
  }

  log.Printf("[DEBUG] Modifying SSM document permissions: %s", input)
  _, err := ssmconn.ModifyDocumentPermission(input)
  if err != nil {
    return fmt.Errorf("error modifying SSM document permissions: %s", err)
  }
}

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Hi @brandonstevens 👋 Thanks for submitting this. I left some initial feedback below. Can you please take a look and let us know if you have any questions or do not have time to implement?

_, err := ssmconn.ModifyDocumentPermission(permClearInput)

if err != nil {
return errwrap.Wrapf("[ERROR] Error removing permissions for SSM document: {{err}}", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Regardless of other changes, we prefer fmt.Errorf() over errwrap.Wrapf() as we do not need the complexity introduced by errwrap when returning simple error messages. We are trying to clean this up across the codebase. 😄

Steps: []resource.TestStep{
{
Config: testAccAWSSSMDocumentPrivatePermissionConfig(name, initial_ids),
Check: resource.ComposeTestCheckFunc(
Copy link
Contributor

Choose a reason for hiding this comment

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

This check should verify the value of the account IDs either via resource.TestCheckResourceAttr("aws_ssm_document.foo", "permissions.account_ids", initialIds), or writing a new testAccCheck function that verifies it from the API.

"aws_ssm_document.foo", "permissions.type", "Share"),
),
},
},
Copy link
Contributor

Choose a reason for hiding this comment

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

We should have another TestStep that adds IDs and verifies they were updated. 👍

@@ -108,6 +108,35 @@ func TestAccAWSSSMDocument_permission_private(t *testing.T) {
})
}

func TestAccAWSSSMDocument_permission_change(t *testing.T) {
name := acctest.RandString(10)
initial_ids := "123456789012,123456789013"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Go styling recommends camelCase naming instead of underscores.

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Aug 27, 2018
@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Aug 28, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @brandonstevens! 🚀

9 tests passed (all tests)
--- PASS: TestAccAWSSSMDocument_basic (11.08s)
--- PASS: TestAccAWSSSMDocument_update (15.30s)
--- PASS: TestAccAWSSSMDocument_params (17.94s)
--- PASS: TestAccAWSSSMDocument_automation (21.10s)
--- PASS: TestAccAWSSSMDocument_permission_private (27.91s)
--- PASS: TestAccAWSSSMDocument_DocumentFormat_YAML (28.49s)
--- PASS: TestAccAWSSSMDocument_permission_change (31.82s)
--- PASS: TestAccAWSSSMDocument_Tags (34.76s)
--- PASS: TestAccAWSSSMDocument_permission_public (55.92s)

@bflad bflad added this to the v1.34.0 milestone Aug 29, 2018
@bflad bflad merged commit 872ccab into hashicorp:master Aug 29, 2018
bflad added a commit that referenced this pull request Aug 29, 2018
@bflad
Copy link
Contributor

bflad commented Aug 30, 2018

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

@brandonstevens brandonstevens deleted the ssm-doc-fix-permission-updates branch September 27, 2018 23:35
@ghost
Copy link

ghost commented Apr 3, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/ssm Issues and PRs that pertain to the ssm service.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants