Skip to main content

AWS IAM policy statement generator with fluent interface for AWS CDK

Project description

IAM Floyd

Source iam-floyd GitHub Maintainability CDKio

AWS IAM policy statement generator with fluent interface.

Support for:

  • 238 Services
  • 7844 Actions
  • 773 Resource Types
  • 456 Conditions

EXPERIMENTAL
This is an early version of the package. The API 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.

Auto completion demo

Packages

There are two different package variants available:

  • iam-floyd: Can be used in AWS SDK, Boto 3 or for whatever you need an IAM policy statement for
    npm PyPI NuGet
  • cdk-iam-floyd: Integrates into AWS CDK and extends iam.PolicyStatement
    npm PyPI NuGet

Usage

The package contains a statement provider for each AWS service, e.g. Ec2. A statement provider is a class with methods for each and every available action, resource type and condition. Calling such method will add the action/resource/condition to the statement:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
# for use without AWS CDK use the iam-floyd package
import iam_floyd as statement

# for use with CDK use the cdk-iam-floyd package
import cdk_iam_floyd as statement

statement.Ec2().to_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().to_start_instances().to_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().to_start_instances().to_stop_instances()

And of course deny():

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

If you don't want to be verbose and add every single action manually to the statement, you can work with access levels. For every access level there is a distinct method available to add all related actions to the statement:

  • allListActions()
  • allReadActions()
  • allWriteActions()
  • allPermissionManagementActions()
  • allTaggingActions()
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().deny().all_permission_management_actions()

statement.Ec2().allow().all_list_actions().all_read_actions()

To add actions based on regular expressions, use the method allMatchingActions().

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

To add all actions (e.g. ec2:*), call the allActions() method:

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

For every available condition key, there are if*() methods available.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().to_start_instances().if_encrypted().if_instance_type(["t3.micro", "t3.nano"]).if_associate_public_ip_address(False).if_aws_request_tag("Owner", "John")

If you want to add a condition not covered by the available methods, you can define just any condition yourself via if():

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().to_start_instances().if("aws:RequestTag/Owner", "John")

The default operator for conditions of type String is StringLike.

Most of the if*() methods allow an optional operator as last argument:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().to_start_instances().if("aws:RequestTag/Owner", "*John*", "StringEquals")

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

To invert the policy you can use notActions() and notResources():

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

Examples

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
policy = {
    "Version": "2012-10-17",
    "Statement": [
        statement.Ec2().allow().to_start_instances().if_aws_request_tag("Owner", "${aws:username}"),
        statement.Ec2().allow().to_stop_instances().if_resource_tag("Owner", "${aws:username}"),
        statement.Ec2().allow().all_list_actions().all_read_actions()
    ]
}
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
policy = {
    "Version": "2012-10-17",
    "Statement": [
        statement.Cloudformation().allow().all_actions(),
        statement.All().allow().all_actions().if_aws_called_via("cloudformation.amazonaws.com"),
        statement.S3().allow().all_actions().on("arn:aws:s3:::cdktoolkit-stagingbucket-*"),
        statement.Account().deny().all_permission_management_actions().all_write_actions(),
        statement.Organizations().deny().all_permission_management_actions().all_write_actions()
    ]
}

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().to_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().to_stop_instances()

to*, to

For every available action, there are to*() methods available.

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

allActions

This method adds all actions of the related service to the statement, e.g. ec2:*

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

allMatchingActions

Adds all actions matching regular expressions to the statement.

The regular expressions need to be in Perl/JavaScript literal style and need to be passed as strings:

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

allListActions

Adds all actions with access level list to the statement.

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

allReadActions

Adds all actions with access level read to the statement.

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

allWriteActions

Adds all actions with access level write to the statement.

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

allPermissionManagementActions

Adds all actions with access level permission management to the statement.

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

allTaggingActions

Adds all actions with access level tagging to the statement.

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

if*, if

For every available condition key, there are if*() methods available.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().to_start_instances().if_encrypted().if_instance_type(["t3.micro", "t3.nano"]).if_associate_public_ip_address(False).if_aws_request_tag("Owner", "John")

Most of them allow an optional operator as last argument:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Ec2().allow().to_start_instances().if_instance_type("*.nano", "StringLike")

Global conditions are prefixed with ifAws, e.g. ifAwsRequestedRegion()

If you want to add a condition not covered by the available methods, you can define just any condition yourself via if():

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

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("*")

notActions

Switches the policy provider to use NotAction.

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

notResources

Switches the policy provider to use NotResource.

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

notPrincipals

Switches the policy provider to use NotPrincipal.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Sts().deny().not_principals().to_assume_role().for_user("1234567890", "Bob")

for*

To create assume policies, use the for*() methods. There are methods available for any type of principal:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Sts().allow().to_assume_role().for_account("1234567890")

statement.Sts().allow().to_assume_role_with_sAML().for_service("lambda.amazonaws.com")

statement.Sts().allow().to_assume_role().for_user("1234567890", "Bob")

statement.Sts().allow().to_assume_role().for_role("1234567890", "role-name")

statement.Sts().allow().to_assume_role_with_sAML().for_federated_cognito()

statement.Sts().allow().to_assume_role_with_sAML().for_federated_amazon()

statement.Sts().allow().to_assume_role_with_sAML().for_federated_google()

statement.Sts().allow().to_assume_role_with_sAML().for_federated_facebook()

statement.Sts().allow().to_assume_role_with_sAML().for_saml("1234567890", "saml-provider")

statement.Sts().allow().to_assume_role().for_public()

statement.Sts().allow().to_assume_role().for_assumed_role_session("123456789", "role-name", "session-name")

statement.Sts().allow().to_assume_role().for_canonical_user("userID")

statement.Sts().allow().to_assume_role().for("arn:foo:bar")

To reverse the assume policy you can call the notPrincipals() method:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Sts().deny().not_principals().to_assume_role().for_user("1234567890", "Bob")

If you use the cdk variant of the package you should not have the need to manually create assume policies. But if you do, there is an additional method forCdkPrincipal() which takes any number of iam.IPrincipal objects:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
statement.Sts().allow().to_assume_role().for_cdk_principal(
    iam.ServicePrincipal("sns.amazonaws.com"),
    iam.ServicePrincipal("lambda.amazonaws.com"))

Collections

The package provides commonly used statement collections. These can be called via new statement.Collection().allowEc2InstanceDeleteByOwner(). Collections return a list of statements, which then can be used in a policy like this:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
policy = {
    "Version": "2012-10-17",
    "Statement": [
        (SpreadElement ...new statement.Collection().allowEc2InstanceDeleteByOwner()
          statement.Collection().allow_ec2_instance_delete_by_owner())
    ]
}

Available collections are:

  • allowEc2InstanceDeleteByOwner: Allows stopping EC2 instance only for the user who started them

allowEc2InstanceDeleteByOwner

Allows stopping EC2 instance only for the user who started them.

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.

Similar projects

Legal

The code contained in the lib/generated 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

cdk-iam-floyd-0.58.0.tar.gz (3.9 MB view details)

Uploaded Source

Built Distribution

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

cdk_iam_floyd-0.58.0-py3-none-any.whl (3.9 MB view details)

Uploaded Python 3

File details

Details for the file cdk-iam-floyd-0.58.0.tar.gz.

File metadata

  • Download URL: cdk-iam-floyd-0.58.0.tar.gz
  • Upload date:
  • Size: 3.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 cdk-iam-floyd-0.58.0.tar.gz
Algorithm Hash digest
SHA256 57b1629ba7cebb3836d95f2479a3f0daf4e8cab3cd124963c7941f4f8c1aae07
MD5 675b59bf62c82be3b716b61764ed0006
BLAKE2b-256 bbd7ac957e19a80ccf9d272772dd065fc912db7b150c31efac705067db1e3806

See more details on using hashes here.

File details

Details for the file cdk_iam_floyd-0.58.0-py3-none-any.whl.

File metadata

  • Download URL: cdk_iam_floyd-0.58.0-py3-none-any.whl
  • Upload date:
  • Size: 3.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 cdk_iam_floyd-0.58.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e47665980dfe79f050b3c4aaeafbaf18fb42bf8963b2ff2ede737db46303b69d
MD5 8b8102fc2a8edd76899f633773548c19
BLAKE2b-256 68a444fdfb342c21d07de100f034c91d02bf658a57d4cdf2756e8eec7093849b

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