Skip to content

Commit

Permalink
Merge pull request #58 from moj-analytical-services/add-kms-permissions
Browse files Browse the repository at this point in the history
added kms functionality
  • Loading branch information
lalithanagarur authored Apr 18, 2024
2 parents 0320014 + fca82ae commit 5f1d220
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
__pycache__/
venv
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## v4.3.0

- added kms permissions

## v4.2.2

- updated pypi action for trusted publisher
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ s3:

deny:
- test_bucket_read_write/sensitive_table/*

kms:
- test_kms_key_arn
```
Whilst the example json (`iam_config.json`) looks like this:
Expand All @@ -93,7 +96,8 @@ Whilst the example json (`iam_config.json`) looks like this:
"test_bucket_read_write/*",
"test_bucket_read_only/write_folder/*"
]
}
},
"kms": ["test_kms_key_arn"]
}
```
- **iam_role_name:** The role name of your airflow job; required if you want to run glue jobs or access secrets.
Expand All @@ -115,6 +119,10 @@ Whilst the example json (`iam_config.json`) looks like this:
- **read_write:** A list of s3 paths that the iam_role should be able to access (read and write). Each item in the list should either be a path to a object or finish with `/*` to denote that it can access everything within that directory. _Note the S3 paths don't start with `s3://` in the config._

- **deny:** A list of s3 paths that the iam_role should _not_ be able to access. This should be used to add exceptions to wildcarded access to folders, for example excluding sensitive tables in order to provide basic access to a database. Each item in the list should either be a path to a object or finish with `/*` to denote that it can access everything within that directory. _Note the S3 paths don't start with `s3://` in the config._

- **kms:**: A list of kms arns that the iam_role should be able to access. Can call the DescribeKey, GenerateDataKey, Decrypt, Encrypt and ReEncrypt
operations.

## How to update

When updating IAM builder, make sure to change the version number in `pyproject.toml` and describe the change in `CHANGELOG.md`.
Expand Down
6 changes: 5 additions & 1 deletion examples/iam_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
"deny": [
"test_bucket_read_write/sensitive_table/*"
]
}
},
"kms": [
"arn:aws:kms:test_region:test_account:key/test_key",
"arn:aws:kms:test_region_2:test_account:key/test_key_2"
]
}
4 changes: 4 additions & 0 deletions examples/iam_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ s3:

deny:
- test_bucket_read_write/sensitive_table/*

kms:
- arn:aws:kms:test_region:test_account:key/test_key
- arn:aws:kms:test_region_2:test_account:key/test_key_2
15 changes: 15 additions & 0 deletions examples/iam_policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,21 @@
"Resource": [
"arn:aws:kms:::key/*"
]
},
{
"Sid": "kmsPermissions",
"Action": [
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Encrypt",
"kms:DescribeKey",
"kms:Decrypt"
],
"Effect": "Allow",
"Resource": [
"arn:aws:kms:test_region:test_account:key/test_key",
"arn:aws:kms:test_region_2:test_account:key/test_key_2"
]
}
]
}
6 changes: 6 additions & 0 deletions iam_builder/iam_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
get_deny_policy,
get_s3_list_bucket_policy,
get_secrets,
get_kms_permissions
)
from iam_builder.iam_schema import validate_iam

Expand Down Expand Up @@ -90,4 +91,9 @@ def build_iam_policy(config: dict) -> dict: # noqa: C901
iam["Statement"].append(secrets_statement)
iam["Statement"].extend(iam_lookup["decrypt_statement"])

if "kms" in config:
kms_arns = config["kms"]
kms_permissions = get_kms_permissions(kms_arns)
iam["Statement"].append(kms_permissions)

return iam
7 changes: 7 additions & 0 deletions iam_builder/schemas/iam_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@
}
}
},
"kms": {
"description": "A list of kms key arns that the iam_role should be able to acces.",
"type": "array",
"items": {
"type": "string"
}
},
"role_duration_seconds":{
"description": "Max duration role can be assumed for in seconds",
"type": "integer"
Expand Down
15 changes: 15 additions & 0 deletions iam_builder/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,18 @@ def get_secrets(iam_role: str, write=False) -> dict:
]
}
return statement

def get_kms_permissions(kms_arns: list) -> dict:
policy = {
"Sid": "kmsPermissions",
"Action": [
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Encrypt",
"kms:DescribeKey",
"kms:Decrypt"
],
"Effect": "Allow",
"Resource": kms_arns,
}
return policy
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "iam_builder"
version = "4.2.2"
version = "4.3.0"
description = "A lil python package to generate iam policies"
authors = ["Karik Isichei <[email protected]>"]
license = "MIT"
Expand Down
15 changes: 15 additions & 0 deletions tests/expected_policy/all_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@
"arn:aws:s3:::test_bucket_read_write",
"arn:aws:s3:::test_bucket_write_only"
]
},
{
"Sid": "kmsPermissions",
"Action": [
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Encrypt",
"kms:DescribeKey",
"kms:Decrypt"
],
"Effect": "Allow",
"Resource": [
"arn:aws:kms:test_region:test_account:key/test_key",
"arn:aws:kms:test_region_2:test_account:key/test_key_2"
]
}
]
}
4 changes: 4 additions & 0 deletions tests/test_config/all_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ s3:
read_write:
- test_bucket_read_write/*
- test_bucket_read_only/write_folder/*

kms:
- arn:aws:kms:test_region:test_account:key/test_key
- arn:aws:kms:test_region_2:test_account:key/test_key_2

0 comments on commit 5f1d220

Please sign in to comment.