Skip to main content

Helper library for CDK to easily generate AWS IAM policy statements

Project description

IAM Floyd

Source Docs npm version PyPI version NuGet version GitHub

Helper library for CDK to easily generate AWS IAM policy statements.

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 again 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 withCondition():

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

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

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.S3().allow().all_actions().on_resources("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_resources("arn:aws:s3:::some-bucket")

Examples

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
iam.PolicyDocument(
    statements=[
        statement.Ec2().allow().start_instances().with_condition("StringEquals",
            aws:_request_tag/_owner="${aws:username}"
        ),
        statement.Ec2().allow().stop_instances().with_condition("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().with_condition("ForAnyValue:StringEquals",
            aws:_called_via="cloudformation.amazonaws.com"
        ),
        statement.S3().allow().all_actions().on_resources("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 exists 5 access levels:

  • LIST
  • READ
  • WRITE
  • PERMISSION_MANAGEMENT
  • TAGGING

withCondition

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().with_condition("StringEquals",
    aws:_request_tag/_owner="${aws:username}"
)

onResources

Limit statement to specified resources.

This is basically the same as addResources() 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.S3().allow().all_actions().on_resources("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_resources("*")

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_resources("arn:aws:s3:::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_resources("arn:aws:s3:::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_resources("*").to_jSON()

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

Roadmap

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

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

This version

0.2.0

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.2.0.tar.gz (1.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iam_floyd-0.2.0-py3-none-any.whl (1.9 MB view details)

Uploaded Python 3

File details

Details for the file iam-floyd-0.2.0.tar.gz.

File metadata

  • Download URL: iam-floyd-0.2.0.tar.gz
  • Upload date:
  • Size: 1.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for iam-floyd-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cecf48efaf66dd26916346f699cec530c230a1cd6f7454e9cb2ce7ea998d1c4a
MD5 b4f5e01cb7d461c6a52cac058e2c0991
BLAKE2b-256 f7e0e57d31493351764373fbcbb50a5e9f14f8f17f54a46e6424011a178de01c

See more details on using hashes here.

File details

Details for the file iam_floyd-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: iam_floyd-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for iam_floyd-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 657dd04fbb243647e3da7755c363d4f895e6875d1110426ea11e513624a0eac5
MD5 77c572f2e3289769022361391ee6f490
BLAKE2b-256 725486ce28901d842052961f4e9dad3dcf9c79b0d08bae46de5102d0545dc194

See more details on using hashes here.

Supported by

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