Skip to main content

AWS IAM policy statement generator

Project description

IAM Floyd

Source Docs GitHub

npm package PyPI package NuGet package Maven package

AWS IAM policy statement generator.

This is an early version of the package. The signature of methods will change while I implement new features. Therefore make sure you use an exact version in your package.json before it reaches 1.0.0.

If you see something off, think something could be done better or have any other suggestion, speak up. :-)

While method chaining is not seen a lot in CDK-land, this library's goal is to provide a way to generate policy statements in a single chain. Code completion FTW!

Usage

The package contains a statement provider for each AWS service, e.g. Ec2. A statement provider is an extension of the original PolicyStatement of the @aws-cdk/aws-iam package, so you can use it as drop-in replacement.

A statement provider has methods for every single action of a service. Calling such method will add the related action to the list of actions of the statement:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_iam as iam
import iam_floyd as statement

statement.Ec2().start_instances()

Every method returns the statement provider, so you can chain method calls:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().start_instances().stop_instances()

The default effect of any statement is Allow. To add some linguistic sugar you can explicitly call the allow() method:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().start_instances().stop_instances()

And of course deny():

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().deny().start_instances().stop_instances()

If you don't want to be verbose and add every single action manually to the statement, you discovered the reason why this package was created. You can work with access levels!

There are 5 access levels you can use: LIST, READ, WRITE, PERMISSION_MANAGEMENT and TAGGING:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().all_actions(statement.AccessLevel.LIST, statement.AccessLevel.READ)

The allActions() method also accepts regular expressions which test against the action name:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().deny().all_actions(/vpn/i)

If no value is passed, all actions (ec2:*) will be added:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().all_actions()

To add conditions to the statement you can use if():

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().start_instances().if("StringEquals",
    aws:_request_tag/_owner="${aws:username}"
)

By default the statement applies to all resources. To limit to specific resources, add them via on*().

For every resource type an on*() method exists:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.S3().allow().all_actions().on_bucket("some-bucket").on_object("some-bucket", "some/path/*")

If instead you have an ARN ready, use the on() method:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.S3().allow().all_actions().on("arn:aws:s3:::some-bucket", "arn:aws:s3:::another-bucket")

What about notAction? Yes, simply add a not() to the chain. Though it is important that you add it before you add actions.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.S3().allow().not().delete_bucket().on_bucket("some-bucket")

Examples

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
iam.PolicyDocument(
    statements=[
        statement.Ec2().allow().start_instances().if("StringEquals",
            aws:_request_tag/_owner="${aws:username}"
        ),
        statement.Ec2().allow().stop_instances().if("StringEquals",
            ec2:_resource_tag/_owner="${aws:username}"
        ),
        statement.Ec2().allow().all_actions(statement.AccessLevel.LIST, statement.AccessLevel.READ)
    ]
)
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
iam.PolicyDocument(
    statements=[
        statement.Cloudformation().allow().all_actions(),
        statement.All().allow().all_actions().if("ForAnyValue:StringEquals",
            aws:_called_via="cloudformation.amazonaws.com"
        ),
        statement.S3().allow().all_actions().on("arn:aws:s3:::cdktoolkit-stagingbucket-*"),
        statement.Account().deny().all_actions(statement.AccessLevel.PERMISSION_MANAGEMENT, statement.AccessLevel.WRITE),
        statement.Organizations().deny().all_actions(statement.AccessLevel.PERMISSION_MANAGEMENT, statement.AccessLevel.WRITE)
    ]
)

Methods

allow

Sets the Effect of the statement to Allow.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().stop_instances()

deny

Sets the Effect of the statement to Deny.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().deny().stop_instances()

allActions

This method allows you to add multiple actions at once. If called without parameters, it adds all actions of the service.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().all_actions()

The method can take regular expressions and access levels as options and will add only the matching actions:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().all_actions(/vpn/i)
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().all_actions(statement.AccessLevel.LIST, statement.AccessLevel.READ)

There exist 5 access levels:

  • LIST
  • READ
  • WRITE
  • PERMISSION_MANAGEMENT
  • TAGGING

if

Adds a condition to the statement.

This is basically the same as addCondition() of the original iam.PolicyStatement. Only difference is, it returns the statement so you can use it with method chaining.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().start_instances().if("StringEquals",
    aws:_request_tag/_owner="${aws:username}"
)

on, on*

Limit statement to specified resources.

For every resource type an on*() method exists:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.S3().allow().all_actions().on_bucket("some-bucket")

If instead you have an ARN ready, use the on() method:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.S3().allow().all_actions().on("arn:aws:s3:::some-bucket")

If no resources are applied to the statement, it defaults to all resources (*). You can also be verbose and set this yourself:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.S3().allow().all_actions().on("*")

not

Switches the policy provider to use notAction. Calling this method will change the behavior of all successive called action methods. It will not modify actions that have been added before the call.

Correct: s3:DeleteBucket will be added to the list of NotAction

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.S3().allow().not().delete_bucket().on_bucket("some-bucket")

Wrong: s3:DeleteBucket will be added to the list of Action

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.S3().allow().delete_bucket().not().on_bucket("some-bucket")

But I don't use CDK. Can I still use this package?

Yes. While the package is designed to be used within CDK you can also just use it to generate policy statements in JSON format:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().start_instances().stop_instances().on("*").to_jSON()

iam.PolicyDocument(
    statements=[
        statement.Ec2().allow().start_instances().stop_instances().on("*")
    ]
).to_jSON()

Roadmap

  • Support for conditions (also in action methods?)
  • Support for NotResources
  • Compile action list down to the smallest possible pattern
  • Add useful standard conditions as methods
  • Add useful action collections based on common use cases

Floyd?

George Floyd has been murdered by racist police officers on May 25th, 2020.

This package is not named after him to just remind you of him and his death. I want this package to be of great help to you and I want you to use it on a daily base. Every time you use it, I want you to remember our society is ill and needs change. The riots will stop. The news will fade. The issue persists!

If this statement annoys you, this package is not for you.

Legal

The code contained in the lib folder is generated from the AWS documentation. The class- and function-names and their description therefore are property of AWS.

AWS and their services are trademarks, registered trademarks or trade dress of AWS in the U.S. and/or other countries.

This project is not affiliated, funded, or in any way associated with AWS.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iam-floyd-0.9.0.tar.gz (2.5 MB view hashes)

Uploaded Source

Built Distribution

iam_floyd-0.9.0-py3-none-any.whl (2.5 MB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page