Skip to content

Commit

Permalink
Add support for ARNs with paths.
Browse files Browse the repository at this point in the history
IAM ARNs can have paths (ex.
arn:aws:iam::123456789012:assumed-role/org/team/rolename/session).  When
normalizing the assumed role the output only contained the first part of
the path after assumed-role.

Signed-off-by: Matt Landis <matlan@amazon.com>
  • Loading branch information
mattlandis committed Jun 20, 2018
1 parent bae670c commit b5938b9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/arn/arn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ func Canonicalize(arn string) (string, error) {
case "federated-user":
return arn, nil
case "assumed-role":
if len(parts) < 2 {
if len(parts) < 3 {
return "", fmt.Errorf("assumed-role arn '%s' does not have a role", arn)
}
role := parts[1]
// IAM ARNs can contain paths, part[0] is resource, parts[len(parts)] is the SessionName.
role := strings.Join(parts[1:len(parts)-1], "/")
return fmt.Sprintf("arn:%s:iam::%s:role/%s", parsed.Partition, parsed.AccountID, role), nil
default:
return "", fmt.Errorf("unrecognized resource %s for service sts", parsed.Resource)
Expand Down
1 change: 1 addition & 0 deletions pkg/arn/arn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var arnTests = []struct {
{"arn:aws:sts::123456789012:assumed-role/Admin/Session", "arn:aws:iam::123456789012:role/Admin", nil},
{"arn:aws:sts::123456789012:federated-user/Bob", "arn:aws:sts::123456789012:federated-user/Bob", nil},
{"arn:aws:iam::123456789012:root", "arn:aws:iam::123456789012:root", nil},
{"arn:aws:sts::123456789012:assumed-role/Org/Team/Admin/Session", "arn:aws:iam::123456789012:role/Org/Team/Admin", nil},
}

func TestUserARN(t *testing.T) {
Expand Down

0 comments on commit b5938b9

Please sign in to comment.