-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
Add resource "aws_iam_policy_attachment" to attach a managed policy to users, roles, or groups #2395
Merged
Merged
Add resource "aws_iam_policy_attachment" to attach a managed policy to users, roles, or groups #2395
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2b7e3d0
commit create and delete with a start of test
73e8191
add to provider
2135ff0
add works but need tests
7bf27da
seems to work
3266c44
rename to more intuitive name and finish tests
24e2cfb
test works
fa0573b
add doc link to aws.erb
d1325b0
fix whitespace
8abca07
add markdown
c375a72
check for empty string instead of nil
0f5c9c0
check length of slices instead of using incorrect type
28506c3
add composeErrors function to only expose errors that happened
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
316 changes: 316 additions & 0 deletions
316
builtin/providers/aws/resource_aws_iam_policy_attachment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,316 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/iam" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsIamPolicyAttachment() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsIamPolicyAttachmentCreate, | ||
Read: resourceAwsIamPolicyAttachmentRead, | ||
Update: resourceAwsIamPolicyAttachmentUpdate, | ||
Delete: resourceAwsIamPolicyAttachmentDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"users": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
"roles": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
"groups": &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Set: schema.HashString, | ||
}, | ||
"policy_arn": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsIamPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iamconn | ||
|
||
name := d.Get("name").(string) | ||
arn := d.Get("policy_arn").(string) | ||
users := expandStringList(d.Get("users").(*schema.Set).List()) | ||
roles := expandStringList(d.Get("roles").(*schema.Set).List()) | ||
groups := expandStringList(d.Get("groups").(*schema.Set).List()) | ||
|
||
if users == nil && roles == nil && groups == nil { | ||
return fmt.Errorf("[WARN] No Users, Roles, or Groups specified for %s", name) | ||
} else { | ||
var userErr, roleErr, groupErr error | ||
if users != nil { | ||
userErr = attachPolicyToUsers(conn, users, arn) | ||
} | ||
if roles != nil { | ||
roleErr = attachPolicyToRoles(conn, roles, arn) | ||
} | ||
if groups != nil { | ||
groupErr = attachPolicyToGroups(conn, groups, arn) | ||
} | ||
if userErr != nil || roleErr != nil || groupErr != nil { | ||
return fmt.Errorf("[WARN] Error attaching policy with IAM Policy Attach (%s), error:\n users - %v\n roles - %v\n groups - %v", name, userErr, roleErr, groupErr) | ||
} | ||
} | ||
d.SetId(d.Get("name").(string)) | ||
return resourceAwsIamPolicyAttachmentRead(d, meta) | ||
} | ||
|
||
func resourceAwsIamPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iamconn | ||
arn := d.Get("policy_arn").(string) | ||
name := d.Get("name").(string) | ||
|
||
_, err := conn.GetPolicy(&iam.GetPolicyInput{ | ||
PolicyARN: aws.String(arn), | ||
}) | ||
|
||
if err != nil { | ||
if awsErr, ok := err.(awserr.Error); ok { | ||
if awsErr.Code() == "NoSuchIdentity" { | ||
d.SetId("") | ||
return nil | ||
} | ||
} | ||
return err | ||
} | ||
|
||
policyEntities, err := conn.ListEntitiesForPolicy(&iam.ListEntitiesForPolicyInput{ | ||
PolicyARN: aws.String(arn), | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
ul := make([]string, 0, len(policyEntities.PolicyUsers)) | ||
rl := make([]string, 0, len(policyEntities.PolicyRoles)) | ||
gl := make([]string, 0, len(policyEntities.PolicyGroups)) | ||
|
||
for _, u := range policyEntities.PolicyUsers { | ||
ul = append(ul, *u.UserName) | ||
} | ||
|
||
for _, r := range policyEntities.PolicyRoles { | ||
rl = append(rl, *r.RoleName) | ||
} | ||
|
||
for _, g := range policyEntities.PolicyGroups { | ||
gl = append(gl, *g.GroupName) | ||
} | ||
|
||
userErr := d.Set("users", ul) | ||
roleErr := d.Set("roles", rl) | ||
groupErr := d.Set("groups", gl) | ||
|
||
if userErr != nil || roleErr != nil || groupErr != nil { | ||
return fmt.Errorf("[WARN} Error setting user, role, or group list from IAM Policy Attach (%s):\n user error - %s\n role error - %s\n group error - %s", name, userErr, roleErr, groupErr) | ||
} | ||
|
||
return nil | ||
} | ||
func resourceAwsIamPolicyAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iamconn | ||
name := d.Get("name").(string) | ||
var userErr, roleErr, groupErr error | ||
|
||
if d.HasChange("users") { | ||
userErr = updateUsers(conn, d, meta) | ||
} | ||
if d.HasChange("roles") { | ||
roleErr = updateRoles(conn, d, meta) | ||
} | ||
if d.HasChange("groups") { | ||
groupErr = updateGroups(conn, d, meta) | ||
} | ||
if userErr != nil || roleErr != nil || groupErr != nil { | ||
return fmt.Errorf("[WARN] Error updating user, role, or group list from IAM Policy Attach (%s):\n user error - %s\n role error - %s\n group error - %s", name, userErr, roleErr, groupErr) | ||
} | ||
return resourceAwsIamPolicyAttachmentRead(d, meta) | ||
} | ||
|
||
func resourceAwsIamPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).iamconn | ||
name := d.Get("name").(string) | ||
arn := d.Get("policy_arn").(string) | ||
users := expandStringList(d.Get("users").(*schema.Set).List()) | ||
roles := expandStringList(d.Get("roles").(*schema.Set).List()) | ||
groups := expandStringList(d.Get("groups").(*schema.Set).List()) | ||
|
||
var userErr, roleErr, groupErr error | ||
if users != nil { | ||
userErr = detachPolicyFromUsers(conn, users, arn) | ||
} | ||
if roles != nil { | ||
roleErr = detachPolicyFromRoles(conn, roles, arn) | ||
} | ||
if groups != nil { | ||
groupErr = detachPolicyFromGroups(conn, groups, arn) | ||
} | ||
if userErr != nil || roleErr != nil || groupErr != nil { | ||
return fmt.Errorf("[WARN] Error removing user, role, or group list from IAM Policy Detach (%s), error:\n users - %v\n roles - %v\n groups - %v", name, userErr, roleErr, groupErr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we try something like this? errMsg := fmt.Sprintf("[WARN] Error removing user, role, or group list from IAM Policy Detach (%s), error(s):", name)
errs := []error{userErr, roleErr, groupErr}
for _, e := range errs {
if e!= nil {
errMsg = errMsg + "\n– " + e.Error()
}
}
if userErr != nil || roleErr != nil || groupErr != nil {
return fmt.Errorf(errMsg)
} That will omit and errors that didn't happen, say if we only had a user error and no others. Just thinking out loud |
||
} | ||
return nil | ||
} | ||
func attachPolicyToUsers(conn *iam.IAM, users []*string, arn string) error { | ||
for _, u := range users { | ||
_, err := conn.AttachUserPolicy(&iam.AttachUserPolicyInput{ | ||
UserName: u, | ||
PolicyARN: aws.String(arn), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
func attachPolicyToRoles(conn *iam.IAM, roles []*string, arn string) error { | ||
for _, r := range roles { | ||
_, err := conn.AttachRolePolicy(&iam.AttachRolePolicyInput{ | ||
RoleName: r, | ||
PolicyARN: aws.String(arn), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
func attachPolicyToGroups(conn *iam.IAM, groups []*string, arn string) error { | ||
for _, g := range groups { | ||
_, err := conn.AttachGroupPolicy(&iam.AttachGroupPolicyInput{ | ||
GroupName: g, | ||
PolicyARN: aws.String(arn), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
func updateUsers(conn *iam.IAM, d *schema.ResourceData, meta interface{}) error { | ||
arn := d.Get("policy_arn").(string) | ||
o, n := d.GetChange("users") | ||
if o == nil { | ||
o = new(schema.Set) | ||
} | ||
if n == nil { | ||
n = new(schema.Set) | ||
} | ||
os := o.(*schema.Set) | ||
ns := n.(*schema.Set) | ||
remove := expandStringList(os.Difference(ns).List()) | ||
add := expandStringList(ns.Difference(os).List()) | ||
|
||
if rErr := detachPolicyFromUsers(conn, remove, arn); rErr != nil { | ||
return rErr | ||
} | ||
if aErr := attachPolicyToUsers(conn, add, arn); aErr != nil { | ||
return aErr | ||
} | ||
return nil | ||
} | ||
func updateRoles(conn *iam.IAM, d *schema.ResourceData, meta interface{}) error { | ||
arn := d.Get("policy_arn").(string) | ||
o, n := d.GetChange("roles") | ||
if o == nil { | ||
o = new(schema.Set) | ||
} | ||
if n == nil { | ||
n = new(schema.Set) | ||
} | ||
os := o.(*schema.Set) | ||
ns := n.(*schema.Set) | ||
remove := expandStringList(os.Difference(ns).List()) | ||
add := expandStringList(ns.Difference(os).List()) | ||
|
||
if rErr := detachPolicyFromRoles(conn, remove, arn); rErr != nil { | ||
return rErr | ||
} | ||
if aErr := attachPolicyToRoles(conn, add, arn); aErr != nil { | ||
return aErr | ||
} | ||
return nil | ||
} | ||
func updateGroups(conn *iam.IAM, d *schema.ResourceData, meta interface{}) error { | ||
arn := d.Get("policy_arn").(string) | ||
o, n := d.GetChange("groups") | ||
if o == nil { | ||
o = new(schema.Set) | ||
} | ||
if n == nil { | ||
n = new(schema.Set) | ||
} | ||
os := o.(*schema.Set) | ||
ns := n.(*schema.Set) | ||
remove := expandStringList(os.Difference(ns).List()) | ||
add := expandStringList(ns.Difference(os).List()) | ||
|
||
if rErr := detachPolicyFromGroups(conn, remove, arn); rErr != nil { | ||
return rErr | ||
} | ||
if aErr := attachPolicyToGroups(conn, add, arn); aErr != nil { | ||
return aErr | ||
} | ||
return nil | ||
|
||
} | ||
func detachPolicyFromUsers(conn *iam.IAM, users []*string, arn string) error { | ||
for _, u := range users { | ||
_, err := conn.DetachUserPolicy(&iam.DetachUserPolicyInput{ | ||
UserName: u, | ||
PolicyARN: aws.String(arn), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
func detachPolicyFromRoles(conn *iam.IAM, roles []*string, arn string) error { | ||
for _, r := range roles { | ||
_, err := conn.DetachRolePolicy(&iam.DetachRolePolicyInput{ | ||
RoleName: r, | ||
PolicyARN: aws.String(arn), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
func detachPolicyFromGroups(conn *iam.IAM, groups []*string, arn string) error { | ||
for _, g := range groups { | ||
_, err := conn.DetachGroupPolicy(&iam.DetachGroupPolicyInput{ | ||
GroupName: g, | ||
PolicyARN: aws.String(arn), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method
expandStringList
callsmake([]*string, ...)
and returns the result. Even that slice is empty, it's notnil
, so this block will never evaluate totrue
, yeah?