Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more IAM Actions based on needs for aibs-informatics-workflows. #8

Merged
merged 2 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 124 additions & 29 deletions src/aibs_informatics_cdk_lib/common/aws/iam_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
The list of actions for each service is incomplete and based on our needs so far.
A helpful resource to research actions is:
https://www.awsiamactions.io/
"""

from typing import List, Optional, Union

from aibs_informatics_core.env import EnvBase
Expand All @@ -12,6 +18,28 @@
build_sfn_arn,
)

#
# utils
#


def grant_managed_policies(
role: Optional[iam.IRole],
*managed_policies: Union[str, iam.ManagedPolicy],
):
if not role:
return

for mp in managed_policies:
role.add_managed_policy(
iam.ManagedPolicy.from_aws_managed_policy_name(mp) if isinstance(mp, str) else mp
)


#
# policy action lists
#

BATCH_READ_ONLY_ACTIONS = [
"batch:Describe*",
"batch:List*",
Expand All @@ -25,6 +53,25 @@
"batch:*",
]

CLOUDWATCH_READ_ACTIONS = [
"logs:DescribeLogGroups",
"logs:GetLogEvents",
"logs:GetLogGroupFields",
"logs:GetLogRecord",
"logs:GetQueryResults",
]

CLOUDWATCH_WRITE_ACTIONS = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
]

CLOUDWATCH_FULL_ACCESS_ACTIONS = [
*CLOUDWATCH_READ_ACTIONS,
*CLOUDWATCH_WRITE_ACTIONS,
]

DYNAMODB_READ_ACTIONS = [
"dynamodb:BatchGet*",
"dynamodb:DescribeStream",
Expand All @@ -50,51 +97,82 @@

EC2_ACTIONS = ["ec2:DescribeAvailabilityZones"]

ECS_READ_ACTIONS = [
"ecs:DescribeContainerInstances",
"ecs:DescribeTaskDefinition",
"ecs:DescribeTasks",
"ecs:ListTasks",
]

ECS_WRITE_ACTIONS = [
"ecs:RegisterTaskDefinition",
]

ECS_RUN_ACTIONS = [
"ecs:RunTask",
]

ECS_FULL_ACCESS_ACTIONS = [
*ECS_READ_ACTIONS,
*ECS_WRITE_ACTIONS,
*ECS_RUN_ACTIONS,
]

ECR_READ_ACTIONS = [
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:DescribeImageScanFindings",
"ecr:DescribeImages",
"ecr:DescribeRepositories",
"ecr:GetAuthorizationToken",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:DescribeImages",
"ecr:BatchGetImage",
"ecr:ListTagsForResource",
"ecr:DescribeImageScanFindings",
]

ECR_WRITE_ACTIONS = [
"ecr:CompleteLayerUpload",
"ecr:CreateRepository",
"ecr:DeleteRepository",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:PutImage",
"ecr:PutLifecyclePolicy",
"ecr:UploadLayerPart",
]

ECR_TAGGING_ACTIONS = [
"ecr:TagResource",
"ecr:UntagResource",
]

ECR_FULL_ACCESS_ACTIONS = [*ECR_READ_ACTIONS, *ECR_WRITE_ACTIONS, *ECR_TAGGING_ACTIONS]
ECR_FULL_ACCESS_ACTIONS = [
*ECR_READ_ACTIONS,
*ECR_TAGGING_ACTIONS,
*ECR_WRITE_ACTIONS,
]

ECS_READ_ACTIONS = ["ecs:DescribeContainerInstances"]
KMS_READ_ACTIONS = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add constants in alphabetical order?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that not everything is ordered alphabetically, but it would be helpful if you tried to order. even better if you ordered others by alphabet and type of constant (action lists vs policy variables)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to alphabetize and reorganize the file. Let me know if that does what you were requesting.

"kms:Decrypt",
"kms:DescribeKey",
]

KMS_WRITE_ACTIONS = [
"kms:Encrypt",
"kms:GenerateDataKey*",
"kms:PutKeyPolicy",
]

CODE_BUILD_IAM_POLICY = iam.PolicyStatement(
actions=[
*EC2_ACTIONS,
*ECR_FULL_ACCESS_ACTIONS,
],
resources=["*"],
)
KMS_FULL_ACCESS_ACTIONS = [
*KMS_READ_ACTIONS,
*KMS_WRITE_ACTIONS,
]


LAMBDA_FULL_ACCESS_ACTIONS = ["lambda:*"]

S3_FULL_ACCESS_ACTIONS = ["s3:*"]

S3_READ_ONLY_ACCESS_ACTIONS = [
"s3:Get*",
"s3:List*",
Expand Down Expand Up @@ -122,13 +200,43 @@

SNS_FULL_ACCESS_ACTIONS = ["sns:*"]

SQS_READ_ACTIONS = [
"sqs:GetQueueAttributes",
"sqs:GetQueueUrl",
"sqs:ReceiveMessage",
"sqs:SendMessage",
]

SQS_WRITE_ACTIONS = [
"sqs:ChangeMessageVisibility",
"sqs:DeleteMessage",
]

SQS_FULL_ACCESS_ACTIONS = [
*SQS_READ_ACTIONS,
*SQS_WRITE_ACTIONS,
]

SSM_READ_ACTIONS = [
"ssm:GetParameter",
"ssm:GetParameters",
"ssm:GetParametersByPath",
]


#
# policy statement constants and builders
#

CODE_BUILD_IAM_POLICY = iam.PolicyStatement(
actions=[
*EC2_ACTIONS,
*ECR_FULL_ACCESS_ACTIONS,
],
resources=["*"],
)


def batch_policy_statement(
env_base: Optional[EnvBase] = None,
actions: List[str] = BATCH_FULL_ACCESS_ACTIONS,
Expand Down Expand Up @@ -295,16 +403,3 @@ def ssm_policy_statement(
return iam.PolicyStatement(
sid=sid, actions=actions, effect=iam.Effect.ALLOW, resources=[build_arn(service="ssm")]
)


def grant_managed_policies(
role: Optional[iam.IRole],
*managed_policies: Union[str, iam.ManagedPolicy],
):
if not role:
return

for mp in managed_policies:
role.add_managed_policy(
iam.ManagedPolicy.from_aws_managed_policy_name(mp) if isinstance(mp, str) else mp
)
Loading