Skip to content

Commit ebb40ed

Browse files
committed
Add sqs_policy_statement to iam_utils.py
1 parent 2f51cea commit ebb40ed

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/aibs_informatics_cdk_lib/common/aws/iam_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,22 @@ def ssm_policy_statement(
467467
return iam.PolicyStatement(
468468
sid=sid, actions=actions, effect=iam.Effect.ALLOW, resources=[build_arn(service="ssm")]
469469
)
470+
471+
472+
def sqs_policy_statement(
473+
env_base: Optional[EnvBase] = None,
474+
actions: List[str] = SQS_FULL_ACCESS_ACTIONS,
475+
sid: str = "SQSFullAccess",
476+
) -> iam.PolicyStatement:
477+
return iam.PolicyStatement(
478+
sid=sid,
479+
actions=actions,
480+
effect=iam.Effect.ALLOW,
481+
resources=[
482+
build_arn(
483+
service="sqs",
484+
resource_id=f"{env_base or ''}*",
485+
resource_type="*",
486+
)
487+
],
488+
)

test/aibs_informatics_cdk_lib/common/aws/test_iam_utils.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import re
2+
from typing import Optional
3+
14
import aws_cdk as cdk
25
import pytest
6+
from aibs_informatics_core.env import EnvBase
37

48
from aibs_informatics_cdk_lib.common.aws.iam_utils import (
59
SECRETSMANAGER_READ_ONLY_ACTIONS,
610
SECRETSMANAGER_READ_WRITE_ACTIONS,
11+
SQS_FULL_ACCESS_ACTIONS,
712
secretsmanager_policy_statement,
13+
sqs_policy_statement,
814
)
915

1016

@@ -48,3 +54,54 @@ def test__secrets_manager_policy_args(generate_policy_args, expected_resource, e
4854
generated_policy_statement = secretsmanager_policy_statement(**generate_policy_args)
4955
assert generated_policy_statement.resources == [expected_resource]
5056
assert set(generated_policy_statement.actions) == set(expected_actions)
57+
58+
59+
@pytest.mark.parametrize(
60+
"env_base, expected_actions, expected_resource_patterns",
61+
[
62+
pytest.param(
63+
# env_base
64+
None,
65+
# expected_actions
66+
SQS_FULL_ACCESS_ACTIONS,
67+
# expected_resource_patterns
68+
[
69+
r"arn:aws:sqs:\$\{Token\[AWS\.Region\.[\d]+\]\}:\$\{Token\[AWS\.AccountId\.[\d]+\]\}:\*:\*"
70+
],
71+
id="Test SQS policystatment (env_base=None)",
72+
),
73+
pytest.param(
74+
# env_base
75+
EnvBase("dev"),
76+
# expected_actions
77+
SQS_FULL_ACCESS_ACTIONS,
78+
# expected_resource_patterns
79+
[
80+
r"arn:aws:sqs:\$\{Token\[AWS\.Region\.[\d]+\]\}:\$\{Token\[AWS\.AccountId\.[\d]+\]\}:\*:dev\*"
81+
],
82+
id="Test SQS policystatment (env_base=dev)",
83+
),
84+
pytest.param(
85+
# env_base
86+
EnvBase("test"),
87+
# expected_actions
88+
SQS_FULL_ACCESS_ACTIONS,
89+
# expected_resource_patterns
90+
[
91+
r"arn:aws:sqs:\$\{Token\[AWS\.Region\.[\d]+\]\}:\$\{Token\[AWS\.AccountId\.[\d]+\]\}:\*:test\*"
92+
],
93+
id="Test SQS policystatment (env_base=test)",
94+
),
95+
],
96+
)
97+
def test__sqs_policy_statement(
98+
env_base: Optional[EnvBase], expected_actions, expected_resource_patterns
99+
):
100+
obt = sqs_policy_statement(env_base=env_base)
101+
102+
assert expected_actions == obt.actions
103+
for indx, expected_pattern in enumerate(expected_resource_patterns):
104+
obt_resource = obt.resources[indx]
105+
assert re.fullmatch(
106+
expected_pattern, obt_resource
107+
), f"expected_pattern: {expected_pattern}, obt: {obt_resource}"

0 commit comments

Comments
 (0)