Skip to content

Commit

Permalink
Fix polaris_aws_cnp_permissions id (#208)
Browse files Browse the repository at this point in the history
The data source's id was accidentally calculated for the complete set
of role keys and not just the specified role key.
  • Loading branch information
johan3141592 authored Nov 26, 2024
1 parent 9eabb2d commit 3432040
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
4 changes: 4 additions & 0 deletions docs/guides/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ page_title: "Changelog"

# Changelog

## v0.10.0-beta.9
* Fix a bug in the `polaris_aws_cnp_permissions` data source where the data source's id was accidentally calculated for
the complete set of role keys and not just the specified role key.

## v0.10.0-beta.8
* Add the `permissions` field to the `polaris_aws_cnp_account_attachments` resource. The `permissions` field should be
used with the `id` field of the `polaris_aws_cnp_permissions` data source to trigger an update of the resource
Expand Down
5 changes: 3 additions & 2 deletions docs/resources/aws_cnp_account_attachments.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ resource "polaris_aws_cnp_account_attachments" "attachments" {
dynamic "role" {
for_each = aws_iam_role.role
content {
key = role.key
arn = role.value["arn"]
key = role.key
arn = role.value["arn"]
permissions = data.polaris_aws_cnp_permissions.permissions[role.key].id
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ resource "polaris_aws_cnp_account_attachments" "attachments" {
dynamic "role" {
for_each = aws_iam_role.role
content {
key = role.key
arn = role.value["arn"]
key = role.key
arn = role.value["arn"]
permissions = data.polaris_aws_cnp_permissions.permissions[role.key].id
}
}
}
21 changes: 10 additions & 11 deletions internal/provider/data_source_aws_cnp_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func awsPermissionsRead(ctx context.Context, d *schema.ResourceData, m interface
return diag.FromErr(err)
}

// The hash is created from customer managed policies and managed policies
// matching the role key.
hash := sha256.New()

var customerPoliciesAttr []map[string]string
for _, policy := range customerPolicies {
if roleKey == policy.Artifact {
Expand All @@ -179,6 +183,10 @@ func awsPermissionsRead(ctx context.Context, d *schema.ResourceData, m interface
keyName: policy.Name,
keyPolicy: policy.Policy,
})
hash.Write([]byte(policy.Artifact))
hash.Write([]byte(policy.Feature.Name))
hash.Write([]byte(policy.Name))
hash.Write([]byte(policy.Policy))
}
}
if err := d.Set(keyCustomerManagedPolicies, customerPoliciesAttr); err != nil {
Expand All @@ -189,23 +197,14 @@ func awsPermissionsRead(ctx context.Context, d *schema.ResourceData, m interface
for _, policy := range managedPolicies {
if roleKey == policy.Artifact {
managedPoliciesAttr = append(managedPoliciesAttr, policy.Name)
hash.Write([]byte(policy.Artifact))
hash.Write([]byte(policy.Name))
}
}
if err := d.Set(keyManagedPolicies, managedPoliciesAttr); err != nil {
return diag.FromErr(err)
}

hash := sha256.New()
for _, policy := range customerPolicies {
hash.Write([]byte(policy.Artifact))
hash.Write([]byte(policy.Feature.Name))
hash.Write([]byte(policy.Name))
hash.Write([]byte(policy.Policy))
}
for _, policy := range managedPolicies {
hash.Write([]byte(policy.Artifact))
hash.Write([]byte(policy.Name))
}
d.SetId(fmt.Sprintf("%x", hash.Sum(nil)))

return nil
Expand Down
4 changes: 4 additions & 0 deletions templates/guides/changelog.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ page_title: "Changelog"

# Changelog

## v0.10.0-beta.9
* Fix a bug in the `polaris_aws_cnp_permissions` data source where the data source's id was accidentally calculated for
the complete set of role keys and not just the specified role key.

## v0.10.0-beta.8
* Add the `permissions` field to the `polaris_aws_cnp_account_attachments` resource. The `permissions` field should be
used with the `id` field of the `polaris_aws_cnp_permissions` data source to trigger an update of the resource
Expand Down

0 comments on commit 3432040

Please sign in to comment.