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

fix: Kafka Auth Mechanism "SERVER_ROOT_CA_CERTIFICATE" and other Auth can't be used together #3341

Merged
merged 5 commits into from
Sep 12, 2023
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
18 changes: 13 additions & 5 deletions samtranslator/model/eventsources/pull.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABCMeta, abstractmethod
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Tuple

from samtranslator.internal.deprecation_control import deprecated
from samtranslator.metrics.method_decorator import cw_timer
Expand Down Expand Up @@ -520,7 +520,6 @@ class SelfManagedKafka(PullEventSource):
"SASL_SCRAM_512_AUTH",
"BASIC_AUTH",
"CLIENT_CERTIFICATE_TLS_AUTH",
"SERVER_ROOT_CA_CERTIFICATE",
]

def get_event_source_arn(self) -> Optional[PassThrough]:
Expand Down Expand Up @@ -558,11 +557,15 @@ def get_policy_statements(self) -> Optional[List[Dict[str, Any]]]:

def generate_policy_document(self, source_access_configurations: List[Any]): # type: ignore[no-untyped-def]
statements = []
authentication_uri, has_vpc_config = self.get_secret_key(source_access_configurations)
authentication_uri, authentication_uri_2, has_vpc_config = self.get_secret_key(source_access_configurations)
if authentication_uri:
secret_manager = self.get_secret_manager_secret(authentication_uri) # type: ignore[no-untyped-call]
statements.append(secret_manager)

if authentication_uri_2:
secret_manager = self.get_secret_manager_secret(authentication_uri) # type: ignore[no-untyped-call]
statements.append(secret_manager)

if has_vpc_config:
vpc_permissions = self.get_vpc_permission()
statements.append(vpc_permissions)
Expand All @@ -580,10 +583,11 @@ def generate_policy_document(self, source_access_configurations: List[Any]): #
"PolicyName": "SelfManagedKafkaExecutionRolePolicy",
}

def get_secret_key(self, source_access_configurations: List[Any]): # type: ignore[no-untyped-def]
def get_secret_key(self, source_access_configurations: List[Any]) -> Tuple[Optional[str], Optional[str], bool]:
authentication_uri = None
has_vpc_subnet = False
has_vpc_security_group = False
authentication_uri_2 = None

if not isinstance(source_access_configurations, list):
raise InvalidEventException(
Expand All @@ -609,6 +613,10 @@ def get_secret_key(self, source_access_configurations: List[Any]): # type: igno
self.validate_uri(config.get("URI"), "auth mechanism")
authentication_uri = config.get("URI")

elif config.get("Type") == "SERVER_ROOT_CA_CERTIFICATE":
self.validate_uri(config.get("URI"), "SERVER_ROOT_CA_CERTIFICATE")
authentication_uri_2 = config.get("URI")

else:
raise InvalidEventException(
self.relative_id,
Expand All @@ -620,7 +628,7 @@ def get_secret_key(self, source_access_configurations: List[Any]): # type: igno
self.relative_id,
"VPC_SUBNET and VPC_SECURITY_GROUP in SourceAccessConfigurations for SelfManagedKafka must be both provided.",
)
return authentication_uri, (has_vpc_subnet and has_vpc_security_group)
return authentication_uri, authentication_uri_2, (has_vpc_subnet and has_vpc_security_group)
ConnorRobertson marked this conversation as resolved.
Show resolved Hide resolved

def validate_uri(self, uri: Optional[Any], msg: str) -> None:
if not uri:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
AWSTemplateFormatVersion: '2010-09-09'
Parameters: {}
Resources:
KafkaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/kafka.zip
Handler: index.kafka_handler
Runtime: python3.9
Events:
MyKafkaCluster:
Type: SelfManagedKafka
Properties:
KafkaBootstrapServers:
- abc.xyz.com:9092
- 123.45.67.89:9096
Topics:
- Topic1
SourceAccessConfigurations:
- Type: SASL_SCRAM_512_AUTH
URI: arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c
- Type: SERVER_ROOT_CA_CERTIFICATE
URI: arn:aws:secretsmanager:us-west-2:123456789012:secret:test-root-certificate-abcdef
- Type: VPC_SUBNET
URI: subnet:subnet-12345
- Type: VPC_SECURITY_GROUP
URI: security_group:sg-67890
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {},
"Resources": {
"KafkaFunction": {
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "kafka.zip"
},
"Handler": "index.kafka_handler",
"Role": {
"Fn::GetAtt": [
"KafkaFunctionRole",
"Arn"
]
},
"Runtime": "python3.9",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::Lambda::Function"
},
"KafkaFunctionMyKafkaCluster": {
"Properties": {
"FunctionName": {
"Ref": "KafkaFunction"
},
"SelfManagedEventSource": {
"Endpoints": {
"KafkaBootstrapServers": [
"abc.xyz.com:9092",
"123.45.67.89:9096"
]
}
},
"SourceAccessConfigurations": [
{
"Type": "SASL_SCRAM_512_AUTH",
"URI": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
},
{
"Type": "SERVER_ROOT_CA_CERTIFICATE",
"URI": "arn:aws:secretsmanager:us-west-2:123456789012:secret:test-root-certificate-abcdef"
},
{
"Type": "VPC_SUBNET",
"URI": "subnet:subnet-12345"
},
{
"Type": "VPC_SECURITY_GROUP",
"URI": "security_group:sg-67890"
}
],
"Topics": [
"Topic1"
]
},
"Type": "AWS::Lambda::EventSourceMapping"
},
"KafkaFunctionRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Policies": [
{
"PolicyDocument": {
"Statement": [
{
"Action": [
"secretsmanager:GetSecretValue"
],
"Effect": "Allow",
"Resource": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
},
{
"Action": [
"secretsmanager:GetSecretValue"
],
"Effect": "Allow",
"Resource": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
},
{
"Action": [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"ec2:DescribeVpcs",
"ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
},
"PolicyName": "SelfManagedKafkaExecutionRolePolicy"
}
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {},
"Resources": {
"KafkaFunction": {
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "kafka.zip"
},
"Handler": "index.kafka_handler",
"Role": {
"Fn::GetAtt": [
"KafkaFunctionRole",
"Arn"
]
},
"Runtime": "python3.9",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::Lambda::Function"
},
"KafkaFunctionMyKafkaCluster": {
"Properties": {
"FunctionName": {
"Ref": "KafkaFunction"
},
"SelfManagedEventSource": {
"Endpoints": {
"KafkaBootstrapServers": [
"abc.xyz.com:9092",
"123.45.67.89:9096"
]
}
},
"SourceAccessConfigurations": [
{
"Type": "SASL_SCRAM_512_AUTH",
"URI": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
},
{
"Type": "SERVER_ROOT_CA_CERTIFICATE",
"URI": "arn:aws:secretsmanager:us-west-2:123456789012:secret:test-root-certificate-abcdef"
},
{
"Type": "VPC_SUBNET",
"URI": "subnet:subnet-12345"
},
{
"Type": "VPC_SECURITY_GROUP",
"URI": "security_group:sg-67890"
}
],
"Topics": [
"Topic1"
]
},
"Type": "AWS::Lambda::EventSourceMapping"
},
"KafkaFunctionRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Policies": [
{
"PolicyDocument": {
"Statement": [
{
"Action": [
"secretsmanager:GetSecretValue"
],
"Effect": "Allow",
"Resource": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
},
{
"Action": [
"secretsmanager:GetSecretValue"
],
"Effect": "Allow",
"Resource": "arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c"
},
{
"Action": [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"ec2:DescribeVpcs",
"ec2:DescribeSubnets",
"ec2:DescribeSecurityGroups"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
},
"PolicyName": "SelfManagedKafkaExecutionRolePolicy"
}
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
}
}
}
Loading