-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More general IAM policy normalization
Some earlier work in #6956 implemented IAM policy normalization within the aws_iam_policy_document data source. Some other (unmerged) work in #7785 implemented normalization across many different IAM policy attributes in the provider. The two are in conflict due to a difference in approach. This is an attempt to reconcile the two by generalizing the normalization already implemented in #6956 and then applying it to the various places that were addressed by #7785.
- Loading branch information
1 parent
e37dbef
commit 1cc5353
Showing
2 changed files
with
214 additions
and
0 deletions.
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
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,84 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNormalizeIAMPolicyJSON(t *testing.T) { | ||
type testCase struct { | ||
Input string | ||
Expected string | ||
ExpectError bool | ||
} | ||
|
||
tests := []testCase{ | ||
{ | ||
`{}`, | ||
`{"Statement":[]}`, | ||
false, | ||
}, | ||
{ | ||
`{"Statement":[]}`, | ||
`{"Statement":[]}`, | ||
false, | ||
}, | ||
{ | ||
// Single action string becomes single-item list | ||
`{"Statement":[{"Action":"foo:Baz"}]}`, | ||
`{"Statement":[{"Sid":"","Action":["foo:Baz"]}]}`, | ||
false, | ||
}, | ||
{ | ||
// Multiple actions are sorted | ||
`{"Statement":[{"Sid":"","Action":["foo:Zeek","foo:Baz"]}]}`, | ||
`{"Statement":[{"Sid":"","Action":["foo:Baz","foo:Zeek"]}]}`, | ||
false, | ||
}, | ||
{ | ||
`{"Statement":[{"Sid":"","NotAction":"foo:Zeek"}]}`, | ||
`{"Statement":[{"Sid":"","NotAction":["foo:Zeek"]}]}`, | ||
false, | ||
}, | ||
{ | ||
`{"Statement":[{"Sid":"","Resource":"foo:Zeek"}]}`, | ||
`{"Statement":[{"Sid":"","Resource":["foo:Zeek"]}]}`, | ||
false, | ||
}, | ||
{ | ||
`{"Statement":[{"Sid":"","NotResource":"foo:Zeek"}]}`, | ||
`{"Statement":[{"Sid":"","NotResource":["foo:Zeek"]}]}`, | ||
false, | ||
}, | ||
{ | ||
// Statement attribute order is normalized | ||
`{"Statement":[{"Sid":"","NotAction":["foo:Zeek"],"Action":["foo:Baz"]}]}`, | ||
`{"Statement":[{"Sid":"","Action":["foo:Baz"],"NotAction":["foo:Zeek"]}]}`, | ||
false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
result, err := NormalizeIAMPolicyJSON(test.Input) | ||
|
||
if test.ExpectError { | ||
if err == nil { | ||
t.Errorf("%s normalized successfully; want error", test.Input) | ||
continue | ||
} | ||
|
||
if result != test.Input { | ||
t.Errorf("%s\nproduced %s\nshould match input", test.Input, result) | ||
continue | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("%s returned error; want success\n%s", test.Input, err) | ||
continue | ||
} | ||
|
||
if result != test.Expected { | ||
t.Errorf("%s\nproduced %s\n want %s", test.Input, result, test.Expected) | ||
} | ||
} | ||
} | ||
} |
1cc5353
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.
This seems more complicated than #7785...