Skip to content

Commit 6625df9

Browse files
authored
fix: Add validation for SecretsManagerKmsKeyId (#2323)
1 parent 97794f0 commit 6625df9

7 files changed

+161
-0
lines changed

samtranslator/model/eventsources/pull.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ def _validate_filter_criteria(self):
188188
if list(self.FilterCriteria.keys()) not in [[], ["Filters"]]:
189189
raise InvalidEventException(self.relative_id, "FilterCriteria field has a wrong format")
190190

191+
def validate_secrets_manager_kms_key_id(self):
192+
if self.SecretsManagerKmsKeyId and not isinstance(self.SecretsManagerKmsKeyId, str):
193+
raise InvalidEventException(
194+
self.relative_id,
195+
"Provided SecretsManagerKmsKeyId should be of type str.",
196+
)
197+
191198

192199
class Kinesis(PullEventSource):
193200
"""Kinesis event source."""
@@ -304,6 +311,7 @@ def get_policy_statements(self):
304311
},
305312
}
306313
if self.SecretsManagerKmsKeyId:
314+
self.validate_secrets_manager_kms_key_id()
307315
kms_policy = {
308316
"Action": "kms:Decrypt",
309317
"Effect": "Allow",
@@ -367,6 +375,7 @@ def generate_policy_document(self):
367375
statements.append(vpc_permissions)
368376

369377
if self.SecretsManagerKmsKeyId:
378+
self.validate_secrets_manager_kms_key_id()
370379
kms_policy = self.get_kms_policy()
371380
statements.append(kms_policy)
372381

tests/model/eventsources/test_mq_event_source.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from unittest import TestCase
22
from samtranslator.model.eventsources.pull import MQ
3+
from samtranslator.model.exceptions import InvalidEventException
4+
from parameterized import parameterized
35

46

57
class MQEventSource(TestCase):
@@ -40,3 +42,57 @@ def test_get_policy_statements(self):
4042
}
4143
]
4244
self.assertEqual(policy_statements, expected_policy_document)
45+
46+
@parameterized.expand(
47+
[
48+
(1,),
49+
(True,),
50+
(["1abc23d4-567f-8ab9-cde0-1fab234c5d67"],),
51+
({"KmsKeyId": "1abc23d4-567f-8ab9-cde0-1fab234c5d67"},),
52+
]
53+
)
54+
def test_must_validate_secrets_manager_kms_key_id(self, kms_key_id_value):
55+
self.mq_event_source.SourceAccessConfigurations = [{"Type": "BASIC_AUTH", "URI": "SECRET_URI"}]
56+
self.mq_event_source.Broker = "BROKER_ARN"
57+
self.mq_event_source.SecretsManagerKmsKeyId = kms_key_id_value
58+
error_message = "(None, 'Provided SecretsManagerKmsKeyId should be of type str.')"
59+
with self.assertRaises(InvalidEventException) as error:
60+
self.mq_event_source.get_policy_statements()
61+
self.assertEqual(error_message, str(error.exception))
62+
63+
def test_get_policy_statements_with_secrets_manager_kms_key_id(self):
64+
self.mq_event_source.SourceAccessConfigurations = [{"Type": "BASIC_AUTH", "URI": "SECRET_URI"}]
65+
self.mq_event_source.Broker = "BROKER_ARN"
66+
self.mq_event_source.SecretsManagerKmsKeyId = "1abc23d4-567f-8ab9-cde0-1fab234c5d67"
67+
policy_statements = self.mq_event_source.get_policy_statements()
68+
expected_policy_document = [
69+
{
70+
"PolicyName": "SamAutoGeneratedAMQPolicy",
71+
"PolicyDocument": {
72+
"Statement": [
73+
{
74+
"Action": [
75+
"secretsmanager:GetSecretValue",
76+
],
77+
"Effect": "Allow",
78+
"Resource": "SECRET_URI",
79+
},
80+
{
81+
"Action": [
82+
"mq:DescribeBroker",
83+
],
84+
"Effect": "Allow",
85+
"Resource": "BROKER_ARN",
86+
},
87+
{
88+
"Action": "kms:Decrypt",
89+
"Effect": "Allow",
90+
"Resource": {
91+
"Fn::Sub": "arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:key/1abc23d4-567f-8ab9-cde0-1fab234c5d67"
92+
},
93+
},
94+
]
95+
},
96+
}
97+
]
98+
self.assertEqual(policy_statements, expected_policy_document)

tests/model/eventsources/test_self_managed_kafka_event_source.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest import TestCase
22
from samtranslator.model.eventsources.pull import SelfManagedKafka
33
from samtranslator.model.exceptions import InvalidEventException
4+
from parameterized import parameterized
45

56

67
class SelfManagedKafkaEventSource(TestCase):
@@ -294,3 +295,27 @@ def test_must_raise_for_wrong_source_access_configurations_uri(self):
294295
self.kafka_event_source.SourceAccessConfigurations = config
295296
with self.assertRaises(InvalidEventException):
296297
self.kafka_event_source.get_policy_statements()
298+
299+
@parameterized.expand(
300+
[
301+
(1,),
302+
(True,),
303+
(["1abc23d4-567f-8ab9-cde0-1fab234c5d67"],),
304+
({"KmsKeyId": "1abc23d4-567f-8ab9-cde0-1fab234c5d67"},),
305+
]
306+
)
307+
def test_must_validate_secrets_manager_kms_key_id(self, kms_key_id_value):
308+
self.kafka_event_source.SourceAccessConfigurations = [
309+
{"Type": "SASL_SCRAM_256_AUTH", "URI": "SECRET_URI"},
310+
{"Type": "VPC_SUBNET", "URI": "SECRET_URI"},
311+
{"Type": "VPC_SECURITY_GROUP", "URI": "SECRET_URI"},
312+
]
313+
self.kafka_event_source.Topics = ["Topics"]
314+
self.kafka_event_source.KafkaBootstrapServers = ["endpoint1", "endpoint2"]
315+
self.kafka_event_source.Enabled = True
316+
self.kafka_event_source.BatchSize = 1
317+
self.kafka_event_source.SecretsManagerKmsKeyId = kms_key_id_value
318+
error_message = "(None, 'Provided SecretsManagerKmsKeyId should be of type str.')"
319+
with self.assertRaises(InvalidEventException) as error:
320+
self.kafka_event_source.get_policy_statements()
321+
self.assertEqual(error_message, str(error.exception))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Parameter:
3+
SecretsManagerKmsKeyIdValue:
4+
Type: String
5+
Default: 1abc23d4-567f-8ab9-cde0-1fab234c5d67
6+
Resources:
7+
KafkaFunction:
8+
Type: 'AWS::Serverless::Function'
9+
Properties:
10+
CodeUri: s3://sam-demo-bucket/kafka.zip
11+
Handler: index.kafka_handler
12+
Runtime: python3.9
13+
Events:
14+
MyKafkaCluster:
15+
Type: SelfManagedKafka
16+
Properties:
17+
KafkaBootstrapServers:
18+
- "abc.xyz.com:9092"
19+
- "123.45.67.89:9096"
20+
Topics:
21+
- "Topic1"
22+
SourceAccessConfigurations:
23+
- Type: SASL_SCRAM_512_AUTH
24+
URI: arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c
25+
- Type: VPC_SUBNET
26+
URI: subnet:subnet-12345
27+
- Type: VPC_SECURITY_GROUP
28+
URI: security_group:sg-67890
29+
SecretsManagerKmsKeyId:
30+
Ref: SecretsManagerKmsKeyIdValue
31+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Parameter:
2+
SecretsManagerKmsKeyIdValue:
3+
Type: String
4+
Default: 1abc23d4-567f-8ab9-cde0-1fab234c5d67
5+
6+
Resources:
7+
MQFunction:
8+
Type: 'AWS::Serverless::Function'
9+
Properties:
10+
CodeUri: s3://sam-demo-bucket/queues.zip
11+
Handler: queue.mq_handler
12+
Runtime: python2.7
13+
Events:
14+
MyMQQueue:
15+
Type: MQ
16+
Properties:
17+
Broker: arn:aws:mq:us-east-2:123456789012:broker:MyBroker:b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9
18+
Queues:
19+
- "Queue1"
20+
SourceAccessConfigurations:
21+
- Type: BASIC_AUTH
22+
URI: arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c
23+
SecretsManagerKmsKeyId:
24+
Ref: SecretsManagerKmsKeyIdValue
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [KafkaFunction] is invalid. Event with id [MyKafkaCluster] is invalid. Provided SecretsManagerKmsKeyId should be of type str.",
3+
"errors": [
4+
{
5+
"errorMessage": "Resource with id [KafkaFunction] is invalid. Event with id [MyKafkaCluster] is invalid. Provided SecretsManagerKmsKeyId should be of type str."
6+
}
7+
]
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MQFunction] is invalid. Event with id [MyMQQueue] is invalid. Provided SecretsManagerKmsKeyId should be of type str.",
3+
"errors": [
4+
{
5+
"errorMessage": "Resource with id [MQFunction] is invalid. Event with id [MyMQQueue] is invalid. Provided SecretsManagerKmsKeyId should be of type str."
6+
}
7+
]
8+
}

0 commit comments

Comments
 (0)