Skip to content

Instantly share code, notes, and snippets.

@ajkerrigan
Last active June 22, 2023 11:25
Show Gist options
  • Save ajkerrigan/f7879cdbbb0a3d285567d8e07e26a723 to your computer and use it in GitHub Desktop.
Save ajkerrigan/f7879cdbbb0a3d285567d8e07e26a723 to your computer and use it in GitHub Desktop.
Get Cloud Custodian Policy Permissions

Get Cloud Custodian IAM Actions

Overview

This script is intended to serve as one piece of the puzzle when building out IAM roles that will execute Cloud Custodian policies.

In the best case, the output of the script can be combined with the base IAM setup information in the official documentation.

This still may not cover all required permissions. For example:

  • Permission annotations in Cloud Custodian resources, filters or actions may be incomplete.
  • This static scan of policies cannot account for all possible triggering mechanisms or AssumeRole operations.

Still, it's a useful starting point.

Usage

As this script is performing policy validations and permission checks using Cloud Custodian modules, it must be executed from a Python environment where Cloud Custodian is already installed.

From there, run:

python /path/to/get_c7n_iam_actions.py -p <policy file/directory>

Or for more information:

python /path/to/get_c7n_iam_actions.py --help
"""
Determine the set of IAM actions required to run a set of Cloud Custodian
policy files.
"""
import json
from pathlib import Path
import sys
import c7n.policy
import click
from c7n.config import Config
from c7n.exceptions import PolicyValidationError
def load_policies(policy_files):
"""
Given a collection of policy files, attempt to load them
as Cloud Custodian policies.
Return a tuple containing loaded policies and errors.
"""
policies = []
errors = []
for f in policy_files:
try:
policy_collection = c7n.policy.load(Config.empty(), f)
policies.extend([*policy_collection])
except PolicyValidationError as err:
errors.append(f)
return policies, errors
def get_permission_set(policies):
"""
Given a collection of Cloud Custodian policies, scan and merge the
permission annotations associated with their resources, filters
and actions.
Return a set of IAM actions.
"""
if not policies:
return set()
return set.union(*(pol.get_permissions() for pol in policies))
@click.command(
help="Output a list of IAM actions associated with a given set of policies"
)
@click.option(
"-p",
"--policy-path",
required=True,
type=click.Path(),
help="Policy file or parent directory containing policy files",
)
@click.option(
"-o",
"--output-file",
type=click.File(mode="w"),
default="-",
help="Where to write IAM action output [default: stdout]",
)
def cli(policy_path, output_file):
policy_path = Path(policy_path).expanduser()
if policy_path.is_dir():
policy_files = policy_path.rglob("*.y*ml")
else:
policy_files = [str(policy_path)]
policies, errors = load_policies(policy_files)
perms = get_permission_set(policies)
if errors:
print(
f"The following policy files failed validation and were skipped:",
*(f"\t{err}" for err in sorted(errors)),
sep="\n",
file=sys.stderr,
)
json.dump(sorted(perms), output_file, indent=4)
if __name__ == "__main__":
cli()
MIT License
Copyright (c) 2021 AJ Kerrigan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@deanmalan
Copy link

Very useful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment