iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔍

Finding Matching IAM Policies with iam-policy-finder - fujiwara-ware 2024 day 15

に公開

This article is for Day 15 of the fujiwara-ware advent calendar 2024.

What is iam-policy-finder

https://github.com/fujiwara/iam-policy-finder

iam-policy-finder is a tool to find AWS IAM policies that match specified conditions.

Why I created it

Occasionally, AWS sends out announcements regarding IAM policies.

A common scenario is a notification stating that due to specification changes, certain IAM policy configurations will no longer support actions that were previously possible, and thus require updates. In such cases, you need to find the relevant policies based on specific criteria (such as whether they possess certain permissions), but doing this manually is a daunting task.

That's why I created a tool to find policies that match specified conditions.

Usage

Search by policy name

$ iam-policy-finder 'Name == "AmazonEC2FullAccess"'

The argument provided is a CEL expression. CEL will be discussed later.

Compare policy JSON as strings

Document.matches('"lambda:[Gg]et[Ff]unction"')
&&
!Document.matches('"lambda:[Ll]ist[Tt]ags"')

In iam-policy-finder, you can access the policy's JSON string through the variable Document. You can specify conditions on this string using string functions such as matches or contains.

Search normalized policy statements

Statement.exists(s, (s.Action.exists(a, a == "s3:*") && s.Effect == "Allow"))

Statement is a list of normalized statements. Use Statement.exists to search through the contents of the statements. Please refer to the documentation for more details.

Options

--filter (-f)

Pre-filters by policy type. By default, it searches all types.

You can specify "User", "Group", "Role", "LocalManagedPolicy", or "AWSManagedPolicy".

--dump

Dumps the JSON of the found policies to standard output.

--skip-evaluation-error

Skips evaluation errors. Even if an evaluation error occurs, the process will continue to the next policy instead of terminating.
By default, the process stops if an evaluation error occurs.

--lc

Converts Statement[].Action and Statement[].NotAction to lowercase during normalization.

IAM policy actions are case-insensitive, but CEL comparisons are case-sensitive. Use the --lc option if you want to compare actions regardless of case.

What is CEL

https://cel.dev/

CEL (Common Expression Language) is a fast, safe, and non-Turing complete expression language specialized for expression evaluation. It can be embedded in Go, Java, and C++, and is already in use by Google Cloud's Certificate Authority Service, Kubernetes, Istio, and others.

iam-policy-finder uses CEL to evaluate IAM policy statements flexibly. IAM policies are too complex to be evaluated simply as strings. CEL allows you to easily write expressions that match policies with specific conditions.

Also, I simply wanted to try using it. It's risky to adopt a new technology for the first time in a large project. By using it in a small tool first, you can deepen your understanding of the technology.

Summary

iam-policy-finder is a tool to find IAM policies that match specified conditions. Since it uses CEL to evaluate policies flexibly, you can search for policies under a variety of conditions.

While it was a tool created to respond to notifications from AWS, it might be useful for various other purposes. Please give it a try.

Well then, stay tuned for tomorrow's post!

References

https://techblog.kayac.com/iam-policy-finder

https://speakerdeck.com/fujiwara3/iam-policy-finder

Discussion