Skip to content

Instantly share code, notes, and snippets.

@bored-engineer
Last active May 20, 2026 03:55
Show Gist options
  • Select an option

  • Save bored-engineer/9426a558e0ce44ba9b3a018389aca236 to your computer and use it in GitHub Desktop.

Select an option

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
# 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
)
))
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