Skip to content

Commit

Permalink
Merge pull request #4248 from spirius/feature/iam-wildcard-pricipal
Browse files Browse the repository at this point in the history
Remove principal type AWS from IAM policy wildcard normalization
  • Loading branch information
bflad authored May 11, 2018
2 parents 693223a + bcf4ebd commit d8d67df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
8 changes: 6 additions & 2 deletions aws/data_source_aws_iam_policy_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ var testAccAWSIAMPolicyDocumentExpectedJSON = `{
"Sid": "",
"Effect": "Allow",
"Action": "kinesis:*",
"Principal": "*"
"Principal": {
"AWS": "*"
}
},
{
"Sid": "",
Expand Down Expand Up @@ -395,7 +397,9 @@ var testAccAWSIAMPolicyDocumentSourceExpectedJSON = `{
"Sid": "",
"Effect": "Allow",
"Action": "kinesis:*",
"Principal": "*"
"Principal": {
"AWS": "*"
}
},
{
"Sid": "",
Expand Down
11 changes: 6 additions & 5 deletions aws/iam_policy_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ func (self *IAMPolicyDoc) Merge(newDoc *IAMPolicyDoc) {
func (ps IAMPolicyStatementPrincipalSet) MarshalJSON() ([]byte, error) {
raw := map[string]interface{}{}

// As a special case, IAM considers the string value "*" to be
// equivalent to "AWS": "*", and normalizes policies as such.
// We'll follow their lead and do the same normalization here.
// IAM also considers {"*": "*"} to be equivalent to this.
// Although IAM documentation says, that "*" and {"AWS": "*"} are equivalent
// (https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_principal.html),
// in practice they are not for IAM roles. IAM will return an error if trust
// policy have "*" or {"*": "*"} as principal, but will accept {"AWS": "*"}.
// Only {"*": "*"} should be normalized to "*".
if len(ps) == 1 {
p := ps[0]
if p.Type == "AWS" || p.Type == "*" {
if p.Type == "*" {
if sv, ok := p.Identifiers.(string); ok && sv == "*" {
return []byte(`"*"`), nil
}
Expand Down
10 changes: 10 additions & 0 deletions website/docs/d/iam_policy_document.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ uses `${...}`-style syntax that is in conflict with Terraform's interpolation
syntax, so this data source instead uses `&{...}` syntax for interpolations that
should be processed by AWS rather than by Terraform.

## Wildcard Principal

In order to define wildcard principal (a.k.a. anonymous user) use `type = "*"` and
`identifiers = ["*"]`. In that case the rendered json will contain `"Principal": "*"`.
Note, that even though the [IAM Documentation](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_principal.html)
states that `"Principal": "*"` and `"Principal": {"AWS": "*"}` are equivalent,
those principals have different behavior for IAM Role Trust Policy. Therefore
Terraform will normalize the principal field only in above-mentioned case and principals
like `type = "AWS"` and `identifiers = ["*"]` will be rendered as `"Principal": {"AWS": "*"}`.

## Attributes Reference

The following attribute is exported:
Expand Down

0 comments on commit d8d67df

Please sign in to comment.