-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate with AWS Services: MSK Kafka, Secrets Manager, KMS
- Loading branch information
1 parent
56409f6
commit f3138d5
Showing
8 changed files
with
145 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM public.ecr.aws/sam/build-python3.10:latest-x86_64 | ||
|
||
ENV PATH="/sbin:/usr/sbin:${PATH}" | ||
|
||
COPY confluent.repo /etc/yum.repos.d/confluent.repo | ||
RUN rpm --import https://packages.confluent.io/rpm/7.0/archive.key && \ | ||
yum install -y librdkafka-devel && \ | ||
pip install --upgrade pip | ||
|
||
ARG USER_ID | ||
ARG GROUP_ID | ||
RUN groupadd -g $GROUP_ID mygroup && \ | ||
useradd -m -u $USER_ID -g $GROUP_ID myuser | ||
|
||
USER myuser | ||
WORKDIR /var/task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[Confluent-Clients] | ||
name=Confluent Clients repository | ||
baseurl=https://packages.confluent.io/clients/rpm/centos/7/x86_64 | ||
gpgcheck=1 | ||
gpgkey=https://packages.confluent.io/clients/rpm/archive.key | ||
enabled=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
import json | ||
from abc import ABC, abstractmethod | ||
import attr | ||
from confluent_kafka import Producer | ||
|
||
|
||
def stdout(message): | ||
print(json.dumps(message)) | ||
@attr.s | ||
class BaseProducer(ABC): | ||
@abstractmethod | ||
def produce(self, topic, message): | ||
pass | ||
|
||
|
||
class StdoutProducer(BaseProducer): | ||
def produce(self, topic, message): | ||
print(message) | ||
|
||
|
||
class KafkaProducer(BaseProducer): | ||
def __init__(self, config): | ||
self.producer = Producer(config) | ||
print("KafkaProducer initialized") | ||
|
||
def produce(self, topic, key, value): | ||
delivery_reports = [] | ||
|
||
def delivery_report(err, msg): | ||
if err is not None: | ||
print(f"Delivery failed for message {msg.key()}: {err}") | ||
else: | ||
print(f"Message {msg.key()} successfully delivered to {msg.topic()} [{msg.partition()}] at offset {msg.offset()}") | ||
delivery_reports.append((err, msg)) | ||
|
||
self.producer.produce(topic=topic, key=key, value=value, callback=delivery_report) | ||
self.producer.flush() | ||
return delivery_reports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import json | ||
import boto3 | ||
import urllib3 | ||
|
||
|
||
def get_secret(config): | ||
client = boto3.client("secretsmanager", region_name=config.get("region_name")) | ||
try: | ||
response = client.get_secret_value(SecretId=config.get("secret_name")) | ||
if "SecretString" in response: | ||
secret = response["SecretString"] | ||
else: | ||
secret = response["SecretBinary"] | ||
secret_dict = json.loads(secret) | ||
return secret_dict | ||
except Exception as e: | ||
print(f"Error retrieving secret: {e}") | ||
raise e | ||
|
||
|
||
def load_access_control_policy(url): | ||
http = urllib3.PoolManager() | ||
response = http.request("GET", url) | ||
if response.status == 200: | ||
print("Access Control Policy loaded") | ||
return json.loads(response.data.decode("utf-8")) | ||
else: | ||
return {} |