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

Remove principal type AWS from IAM policy wildcard normalization #4248

Merged
merged 4 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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