8000 Add tests to test urlencoded IAM policies by dfangl · Pull Request #11580 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

Add tests to test urlencoded IAM policies #11580

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

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ def iam_api():
TransformerUtility.key_value("RoleName"),
TransformerUtility.key_value("PolicyName"),
TransformerUtility.key_value("PolicyId"),
TransformerUtility.key_value("GroupName"),
]

@staticmethod
Expand Down
117 changes: 117 additions & 0 deletions tests/aws/services/iam/test_iam.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from urllib.parse import quote_plus

import pytest
from botocore.exceptions import ClientError
Expand Down Expand Up @@ -742,3 +743,119 @@ def test_user_attach_policy(self, snapshot, aws_client, create_user, create_poli
UserName=user_name, PolicyArn=policy_arn
)
snapshot.match("valid_policy_arn", attach_policy_response)


class TestIAMPolicyEncoding:
@markers.aws.validated
def test_put_user_policy_encoding(self, snapshot, aws_client, create_user, region_name):
snapshot.add_transformer(snapshot.transform.iam_api())

target_arn = quote_plus(f"arn:aws:apigateway:{region_name}::/restapis/aaeeieije")
policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["apigatway:PUT"],
"Resource": [f"arn:aws:apigateway:{region_name}::/tags/{target_arn}"],
}
],
}

user_name = f"test-user-{short_uid()}"
policy_name = f"test-policy-{short_uid()}"
create_user(UserName=user_name)

aws_client.iam.put_user_policy(
UserName=user_name, PolicyName=policy_name, PolicyDocument=json.dumps(policy_document)
)
get_policy_response = aws_client.iam.get_user_policy(
UserName=user_name, PolicyName=policy_name
)
snapshot.match("get-policy-response", get_policy_response)

@markers.aws.validated
def test_put_role_policy_encoding(self, snapshot, aws_client, create_role, region_name):
snapshot.add_transformer(snapshot.transform.iam_api())

target_arn = quote_plus(f"arn:aws:apigateway:{region_name}::/restapis/aaeeieije")
policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["apigatway:PUT"],
"Resource": [f"arn:aws:apigateway:{region_name}::/tags/{target_arn}"],
}
],
}
assume_policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {"Service": "lambda.amazonaws.com"},
"Effect": "Allow",
"Condition": {"StringEquals": {"aws:SourceArn": target_arn}},
}
],
}

role_name = f"test-role-{short_uid()}"
policy_name = f"test-policy-{short_uid()}"
path = f"/{short_uid()}/"
snapshot.add_transformer(snapshot.transform.key_value("Path"))
create_role_response = create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(assume_policy_document),
Path=path,
)
snapshot.match("create-role-response", create_role_response)

aws_client.iam.put_role_policy(
RoleName=role_name, PolicyName=policy_name, PolicyDocument=json.dumps(policy_document)
)
get_policy_response = aws_client.iam.get_role_policy(
RoleName=role_name, PolicyName=policy_name
)
snapshot.match("get-policy-response", get_policy_response)

get_role_response = aws_client.iam.get_role(RoleName=role_name)
snapshot.match("get-role-response", get_role_response)

list_roles_response = aws_client.iam.list_roles(PathPrefix=path)
snapshot.match("list-roles-response", list_roles_response)

@markers.aws.validated
def test_put_group_policy_encoding( 8000 self, snapshot, aws_client, region_name, cleanups):
snapshot.add_transformer(snapshot.transform.iam_api())

# create quoted target arn
target_arn = quote_plus(f"arn:aws:apigateway:{region_name}::/restapis/aaeeieije")
policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["apigatway:PUT"],
"Resource": [f"arn:aws:apigateway:{region_name}::/tags/{target_arn}"],
}
],
}

group_name = f"test-group-{short_uid()}"
policy_name = f"test-policy-{short_uid()}"
aws_client.iam.create_group(GroupName=group_name)
cleanups.append(lambda: aws_client.iam.delete_group(GroupName=group_name))

aws_client.iam.put_group_policy(
GroupName=group_name, PolicyName=policy_name, PolicyDocument=json.dumps(policy_document)
)
cleanups.append(
lambda: aws_client.iam.delete_group_policy(GroupName=group_name, PolicyName=policy_name)
)

get_policy_response = aws_client.iam.get_group_policy(
GroupName=group_name, PolicyName=policy_name
)
snapshot.match("get-policy-response", get_policy_response)
177 changes: 177 additions & 0 deletions tests/aws/services/iam/test_iam.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,182 @@
}
}
}
},
"tests/aws/services/iam/test_iam.py::TestIAMPolicyEncoding::test_put_user_policy_encoding": {
"recorded-date": "25-09-2024, 15:10:45",
"recorded-content": {
"get-policy-response": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"apigatway:PUT"
],
"Effect": "Allow",
"Resource": [
"arn:<partition>:apigateway:<region>::/tags/arn%3Aaws%3Aapigateway%3A<region>%3A%3A%2Frestapis%2Faaeeieije"
]
}
],
"Version": "2012-10-17"
},
"PolicyName": "<policy-name:1>",
"UserName": "<user-name:1>",
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
}
}
},
"tests/aws/services/iam/test_iam.py::TestIAMPolicyEncoding::test_put_role_policy_encoding": {
"recorded-date": "30-09-2024, 15:17:42",
"recorded-content": {
"create-role-response": {
"Role": {
"Arn": "arn:<partition>:iam::111111111111:role<path:1><role-name:1>",
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"aws:SourceArn": "arn%3Aaws%3Aapigateway%3A<region>%3A%3A%2Frestapis%2Faaeeieije"
}
},
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"CreateDate": "datetime",
"Path": "<path:1>",
"RoleId": "<role-id:1>",
"RoleName": "<role-name:1>"
},
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
},
"get-policy-response": {
"PolicyDocument": {
"Statement": [
{
"Action": [
"apigatway:PUT"
],
"Effect": "Allow",
"Resource": [
"arn:<partition>:apigateway:<region>::/tags/arn%3Aaws%3Aapigateway%3A<region>%3A%3A%2Frestapis%2Faaeeieije"
]
}
],
"Version": "2012-10-17"
},
"PolicyName": "<policy-name:1>",
"RoleName": "<role-name:1>",
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
},
"get-role-response": {
"Role": {
"Arn": "arn:<partition>:iam::111111111111:role<path:1><role-name:1>",
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"aws:SourceArn": "arn%3Aaws%3Aapigateway%3A<region>%3A%3A%2Frestapis%2Faaeeieije"
}
},
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"CreateDate": "datetime",
"MaxSessionDuration": 3600,
"Path": "<path:1>",
"RoleId": "<role-id:1>",
"RoleLastUsed": {},
"RoleName": "<role-name:1>"
},
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
},
"list-roles-response": {
"IsTruncated": false,
"Roles": [
{
"Arn": "arn:<partition>:iam::111111111111:role<path:1><role-name:1>",
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"aws:SourceArn": "arn%3Aaws%3Aapigateway%3A<region>%3A%3A%2Frestapis%2Faaeeieije"
}
},
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"CreateDate": "datetime",
"MaxSessionDuration": 3600,
"Path": "<path:1>",
"RoleId": "<role-id:1>",
"RoleName": "<role-name:1>"
}
],
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
}
}
},
"tests/aws/services/iam/test_iam.py::TestIAMPolicyEncoding::test_put_group_policy_encoding": {
"recorded-date": "25-09-2024, 15:10:47",
"recorded-content": {
"get-policy-response": {
"GroupName": "<group-name:1>",
"PolicyDocument": {
"Statement": [
{
"Action": [
"apigatway:PUT"
],
"Effect": "Allow",
"Resource": [
"arn:<partition>:apigateway:<region>::/tags/arn%3Aaws%3Aapigateway%3A<region>%3A%3A%2Frestapis%2Faaeeieije"
]
}
],
"Version": "2012-10-17"
},
"PolicyName": "<policy-name:1>",
"ResponseMetadata": {
"HTTPHeaders": {},
"HTTPStatusCode": 200
}
}
}
}
}
9 changes: 9 additions & 0 deletions tests/aws/services/iam/test_iam.validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@
},
"tests/aws/services/iam/test_iam.py::TestIAMIntegrations::test_user_attach_policy": {
"last_validated_date": "2023-09-14T15:42:45+00:00"
},
"tests/aws/services/iam/test_iam.py::TestIAMPolicyEncoding::test_put_group_policy_encoding": {
"last_validated_date": "2024-09-25T15:12:26+00:00"
},
"tests/aws/services/iam/test_iam.py::TestIAMPolicyEncoding::test_put_role_policy_encoding": {
"last_validated_date": "2024-09-30T15:17:41+00:00"
},
"tests/aws/services/iam/test_iam.py::TestIAMPolicyEncoding::test_put_user_policy_encoding": {
"last_validated_date": "2024-09-25T15:12:23+00:00"
}
}
Loading
0