Last active
May 20, 2026 03:55
-
-
Save bored-engineer/9426a558e0ce44ba9b3a018389aca236 to your computer and use it in GitHub Desktop.
Normalizes an AWS IAM policy converting all possible single-value elements into their array equivalents
This file contains hidden or 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
| # Wraps the provided elements in an array if the input not already contained in one (ex: "abc" -> ["abc"]) | |
| # TODO: If the input is a literal 'null', this (incorrectly) removes the key when used with the '|=' operator | |
| def normalize: | |
| if type == "null" then empty | |
| elif type == "array" then . | |
| else [.] end; | |
| # Convert the Statement into an array (if needed), then normalize the contents of each | |
| .Statement |= (normalize | map( | |
| (.Principal, .NotPrincipal) |= ( | |
| # TODO: Technically it's not _always_ valid to map "*" to {"AWS": "*"} | |
| if . == "*" then {"AWS": ["*"]} | |
| elif type == "null" then empty | |
| else map_values(normalize) end | |
| ) | | |
| (.Action, .NotAction, .Resource, .NotResource) |= normalize | | |
| .Condition |= ( | |
| if type == "null" then empty | |
| else map_values(map_values(normalize)) end | |
| ) | |
| )) |
This file contains hidden or 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
| alias iam-normalize=jq 'def normalize: if type == "null" then empty elif type == "array" then . else [.] end; .Statement |= (normalize | map((.Principal, .NotPrincipal) |= (if . == "*" then {"AWS": ["*"]} elif type == "null" then empty else map_values(normalize) end) | (.Action, .NotAction, .Resource, .NotResource) |= normalize | .Condition |= (if type == "null" then empty else map_values(map_values(normalize)) end)))' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment