Skip to content

Commit b5ddd1f

Browse files
authored
fix: Support new CodeDeploy ManagedPolicy (#1858)
* fix: Support new CodeDeploy MangedPolicy in regions without AWSCodeDeployRoleForLambda CodeDeploy is migrating from AWSCodeDeployRoleForLambda to AWSCodeDeployRoleForLambdaLimited. Some partitions do not support AWSCodeDeployRoleForLambda and therefore we need to use the newer one in those partitions. We cannot widely update to AWSCodeDeployRoleForLambdaLimited since this can cause customer's stacks to fail unexpectedly. * Forgot to commit unit tests * Handle PR feedback Co-authored-by: Jacob Fuss <[email protected]>
1 parent f97cdda commit b5ddd1f

File tree

6 files changed

+151
-10
lines changed

6 files changed

+151
-10
lines changed

samtranslator/model/preferences/deployment_preference_collection.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,17 @@ def _codedeploy_iam_role(self):
101101
}
102102
],
103103
}
104-
iam_role.ManagedPolicyArns = [
105-
ArnGenerator.generate_aws_managed_policy_arn("service-role/AWSCodeDeployRoleForLambda")
106-
]
104+
105+
# CodeDeploy has a new managed policy. We cannot update any existing partitions, without customer reach out
106+
# that support AWSCodeDeployRoleForLambda since this could regress stacks that are currently deployed.
107+
if ArnGenerator.get_partition_name() in ["aws-iso", "aws-iso-b"]:
108+
iam_role.ManagedPolicyArns = [
109+
ArnGenerator.generate_aws_managed_policy_arn("service-role/AWSCodeDeployRoleForLambdaLimited")
110+
]
111+
else:
112+
iam_role.ManagedPolicyArns = [
113+
ArnGenerator.generate_aws_managed_policy_arn("service-role/AWSCodeDeployRoleForLambda")
114+
]
107115

108116
return iam_role
109117

samtranslator/region_configuration.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class RegionConfiguration(object):
77
class abstracts all region/partition specific configuration.
88
"""
99

10-
partitions = {"govcloud": "aws-us-gov", "china": "aws-cn"}
11-
1210
@classmethod
1311
def is_apigw_edge_configuration_supported(cls):
1412
"""
@@ -18,4 +16,9 @@ def is_apigw_edge_configuration_supported(cls):
1816
:return: True, if API Gateway does not support Edge configuration
1917
"""
2018

21-
return ArnGenerator.get_partition_name() not in [cls.partitions["govcloud"], cls.partitions["china"]]
19+
return ArnGenerator.get_partition_name() not in [
20+
"aws-us-gov",
21+
"aws-iso",
22+
"aws-iso-b",
23+
"aws-cn",
24+
]

samtranslator/translator/arn_generator.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,23 @@ def get_partition_name(cls, region=None):
3939
:param region: Optional name of the region
4040
:return: Partition name
4141
"""
42+
4243
if region is None:
4344
# Use Boto3 to get the region where code is running. This uses Boto's regular region resolution
4445
# mechanism, starting from AWS_DEFAULT_REGION environment variable.
4546
region = boto3.session.Session().region_name
4647

48+
# setting default partition to aws, this will be overwritten by checking the region below
49+
partition = "aws"
50+
4751
region_string = region.lower()
4852
if region_string.startswith("cn-"):
49-
return "aws-cn"
53+
partition = "aws-cn"
54+
elif region_string.startswith("us-iso-"):
55+
partition = "aws-iso"
56+
elif region_string.startswith("us-isob"):
57+
partition = "aws-iso-b"
5058
elif region_string.startswith("us-gov"):
51-
return "aws-us-gov"
52-
else:
53-
return "aws"
59+
partition = "aws-us-gov"
60+
61+
return partition
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from unittest import TestCase
2+
3+
from mock import patch
4+
from parameterized import parameterized
5+
6+
from samtranslator.model.preferences.deployment_preference_collection import DeploymentPreferenceCollection
7+
8+
9+
class TestDeploymentPreferenceCollection(TestCase):
10+
@parameterized.expand(
11+
[
12+
["aws-iso"],
13+
["aws-iso-b"],
14+
]
15+
)
16+
def test_codedeploy_iam_role_contains_AWSCodeDeployRoleForLambdaLimited_managedpolicy(self, partition):
17+
18+
with patch(
19+
"samtranslator.translator.arn_generator.ArnGenerator.get_partition_name"
20+
) as get_partition_name_patch:
21+
get_partition_name_patch.return_value = partition
22+
23+
iam_role = DeploymentPreferenceCollection().codedeploy_iam_role
24+
25+
self.assertIn(
26+
"arn:{}:iam::aws:policy/service-role/AWSCodeDeployRoleForLambdaLimited".format(partition),
27+
iam_role.ManagedPolicyArns,
28+
)
29+
30+
@parameterized.expand(
31+
[
32+
["aws"],
33+
["aws-cn"],
34+
["aws-us-gov"],
35+
]
36+
)
37+
def test_codedeploy_iam_role_contains_AWSCodeDeployRoleForLambda_managedpolicy(self, partition):
38+
39+
with patch(
40+
"samtranslator.translator.arn_generator.ArnGenerator.get_partition_name"
41+
) as get_partition_name_patch:
42+
get_partition_name_patch.return_value = partition
43+
44+
iam_role = DeploymentPreferenceCollection().codedeploy_iam_role
45+
46+
self.assertIn(
47+
"arn:{}:iam::aws:policy/service-role/AWSCodeDeployRoleForLambda".format(partition),
48+
iam_role.ManagedPolicyArns,
49+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from unittest import TestCase
2+
3+
from mock import patch
4+
from parameterized import parameterized
5+
6+
from samtranslator.region_configuration import RegionConfiguration
7+
8+
9+
class TestRegionConfiguration(TestCase):
10+
@parameterized.expand(
11+
[
12+
["aws"],
13+
]
14+
)
15+
def test_when_apigw_edge_configuration_supported(self, partition):
16+
17+
with patch(
18+
"samtranslator.translator.arn_generator.ArnGenerator.get_partition_name"
19+
) as get_partition_name_patch:
20+
get_partition_name_patch.return_value = partition
21+
22+
self.assertTrue(RegionConfiguration.is_apigw_edge_configuration_supported())
23+
24+
@parameterized.expand(
25+
[
26+
["aws-cn"],
27+
["aws-us-gov"],
28+
["aws-iso"],
29+
["aws-iso-b"],
30+
]
31+
)
32+
def test_when_apigw_edge_configuration_is_not_supported(self, partition):
33+
with patch(
34+
"samtranslator.translator.arn_generator.ArnGenerator.get_partition_name"
35+
) as get_partition_name_patch:
36+
get_partition_name_patch.return_value = partition
37+
38+
self.assertFalse(RegionConfiguration.is_apigw_edge_configuration_supported())
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from unittest import TestCase
2+
3+
from mock import patch
4+
from parameterized import parameterized
5+
6+
from samtranslator.translator.arn_generator import ArnGenerator
7+
8+
9+
class TestArnGenerator(TestCase):
10+
@parameterized.expand(
11+
[
12+
["us-east-1", "aws"],
13+
["eu-west-1", "aws"],
14+
["cn-north-1", "aws-cn"],
15+
["us-gov-west-1", "aws-us-gov"],
16+
["us-iso-east-1", "aws-iso"],
17+
["us-isob-east-1", "aws-iso-b"],
18+
]
19+
)
20+
def test_get_partition_name(self, region, expected_partition):
21+
self.assertEqual(expected_partition, ArnGenerator.get_partition_name(region=region))
22+
23+
@parameterized.expand(
24+
[
25+
["us-east-1", "aws"],
26+
["eu-west-1", "aws"],
27+
["cn-north-1", "aws-cn"],
28+
["us-gov-west-1", "aws-us-gov"],
29+
["us-iso-east-1", "aws-iso"],
30+
["us-isob-east-1", "aws-iso-b"],
31+
]
32+
)
33+
def test_get_partition_name_when_region_not_provided(self, region, expected_partition):
34+
with patch("boto3.session.Session.region_name", region):
35+
self.assertEqual(expected_partition, ArnGenerator.get_partition_name())

0 commit comments

Comments
 (0)