generated from it-at-m/oss-repository-en-template
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 70 write documents to kafka #88
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
Open
Tuckerle
wants to merge
56
commits into
main
Choose a base branch
from
70-write-documents-to-kafka
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
f62fb38
feat: publish document id to kafka
Tuckerle b34ec24
Merge branch 'main' into 70-write-documents-to-kafka
Tuckerle a4e2026
fix: avoid infinite loop
Tuckerle 76c2cad
fix: debugging
Tuckerle 025b7f2
fix: debug
Tuckerle dd29f48
exper: remove filehandling
Tuckerle 62048bf
experi:: renable filehandller
Tuckerle e8dabad
experi: simplified handler
Tuckerle 24e0867
fix: messagetype
Tuckerle 983bb33
fix: fix message
Tuckerle e81e983
fix: reenable file downloads
Tuckerle f349f9e
experi: always publish id
Tuckerle 106cebf
experi: try blocking call
Tuckerle aeca0c7
fix: message
Tuckerle 7a61139
fix: broker
Tuckerle 6cdc344
experi: more logging
Tuckerle 8502079
fix: await publishing
Tuckerle 2c6b49b
fix: logging
Tuckerle de7ade8
refactor: code quality
Tuckerle 46725db
fix: logging
Tuckerle bae3214
experi: debug
Tuckerle 5705302
experi: logging
Tuckerle 49fcb9e
fix: kafka publishing
Tuckerle 98fcebb
fix: publishing
Tuckerle d28bfdd
refactor: code rabbit
Tuckerle 99b5164
refactor: code rabbit
Tuckerle 4575e83
refactor: code rabbit
Tuckerle 6ee0c47
fix: async code
Tuckerle ea21fb9
feat: add close
Tuckerle bbb78ff
refactor: broker async
Tuckerle 343f156
refactor: code rabbit
Tuckerle a801b69
Merge branch 'main' into 70-write-documents-to-kafka
Tuckerle 4cf2078
ci: code rabbit
Tuckerle 978112a
Merge branch 'main' into 70-write-documents-to-kafka
Tuckerle 3a6cdb2
chore: faststream[kafka] upgrade
Tuckerle dadbcde
fix: ensure closed broker
Tuckerle 453cc9f
Merge branch 'main' into 70-write-documents-to-kafka
Tuckerle 938eed4
Merge branch '95-filehandler-async-support' into 70-write-documents-t…
Tuckerle 59a5d73
Merge branch 'main' into 70-write-documents-to-kafka
Tuckerle 1e1d251
feat: kafka settings
Tuckerle 8d1b489
feat: kafka in core package
Tuckerle da0b856
feat: kafka security integration
Tuckerle 241d611
fix: avoid redundant downloads
Tuckerle e4f849e
Merge branch 'main' into 70-write-documents-to-kafka
Tuckerle 412178e
ci: required env vars
Tuckerle 89e11c8
fix: remove method
Tuckerle a42979e
fix: async tests
Tuckerle 6d75428
fix: assert publish
Tuckerle da81cac
fix: assert omit calling publish
Tuckerle 1c77a0c
fix: logger as non Async Mock
Tuckerle af2227c
Merge branch 'main' into 70-write-documents-to-kafka
Tuckerle 265aecc
Merge branch 'main' into 70-write-documents-to-kafka
Tuckerle ba4fce0
chore: dependency up
Tuckerle 44ae6c9
fix: ruff issues
Tuckerle 59dc17f
fix: ruff issues
Tuckerle 47d3525
fix: code rabbit
Tuckerle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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,81 @@ | ||
| """Security helpers for configuring Kafka TLS/mTLS.""" | ||
|
|
||
| from base64 import b64decode | ||
| from logging import Logger | ||
| from pathlib import Path | ||
| from ssl import SSLContext, create_default_context | ||
| from tempfile import TemporaryDirectory | ||
|
|
||
| from cryptography.hazmat.primitives import serialization | ||
| from cryptography.hazmat.primitives.serialization import pkcs12 | ||
| from faststream.security import BaseSecurity | ||
|
|
||
| from src.logtools import getLogger | ||
|
|
||
| logger: Logger = getLogger(__name__) | ||
|
|
||
|
|
||
| def setup_security(kafka_pkcs12_data: str, kafka_pkcs12_pw: str, kafka_ca_b64: str) -> BaseSecurity: | ||
| """Set up Kafka security configuration based on application settings. | ||
|
|
||
| Returns: | ||
| BaseSecurity: The configured security object for Kafka. | ||
|
|
||
| Raises: | ||
| ValueError: If required environment variables are not set or if there are issues | ||
| loading the PKCS#12 file. | ||
| """ | ||
| with TemporaryDirectory() as tempdir: | ||
| tempdir_path: Path = Path(tempdir) | ||
| logger.debug("Setting up Kafka mTLS security using environment variables.") | ||
|
|
||
| # Unpack CA file | ||
| ca_data: str | None = kafka_ca_b64 | ||
| ca_data = b64decode(s=ca_data).decode(encoding="utf-8") | ||
|
|
||
| # Unpack PKCS#12 file | ||
| pkcs12_data: str | None = kafka_pkcs12_data | ||
| pkcs12_bytes = b64decode(s=pkcs12_data) | ||
|
|
||
| # Unpack PKCS#12 password | ||
| pkcs12_pw: str | None = kafka_pkcs12_pw | ||
| pkcs12_pw_bytes: bytes = b64decode(s=pkcs12_pw) | ||
|
|
||
| # Extract the private key and certificate from the PKCS#12 file | ||
| try: | ||
| private_key, certificate, _ = pkcs12.load_key_and_certificates( | ||
| data=pkcs12_bytes, | ||
| password=pkcs12_pw_bytes, | ||
| ) | ||
| except Exception as e: | ||
| raise ValueError(f"Failed to load PKCS#12 file: {e}") | ||
|
|
||
| if private_key is None or certificate is None: | ||
| raise ValueError("PKCS#12 file does not contain a private key and certificate.") | ||
|
|
||
| # Write cert and key to temporary files | ||
| cert_file: Path = tempdir_path / "kafka.cert" | ||
| key_file: Path = tempdir_path / "kafka.key" | ||
| with open(file=cert_file, mode="wb") as f: | ||
| f.write(certificate.public_bytes(encoding=serialization.Encoding.PEM)) | ||
| with open(file=key_file, mode="wb") as f: | ||
| f.write( | ||
| private_key.private_bytes( | ||
| encoding=serialization.Encoding.PEM, | ||
| format=serialization.PrivateFormat.TraditionalOpenSSL, | ||
| encryption_algorithm=serialization.NoEncryption(), | ||
| ) | ||
| ) | ||
|
|
||
| # Create SSL context | ||
| ssl_context: SSLContext = create_default_context( | ||
| cadata=ca_data, | ||
| ) | ||
|
|
||
| # Load client cert and key | ||
| ssl_context.load_cert_chain( | ||
| certfile=cert_file, | ||
| keyfile=key_file, | ||
| ) | ||
|
|
||
| return BaseSecurity(ssl_context=ssl_context, use_ssl=True) |
This file contains hidden or 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 hidden or 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,31 @@ | ||
| from pydantic import BaseModel, Field | ||
|
|
||
|
|
||
| class KafkaSettings(BaseModel): | ||
| """ | ||
| Kafka configuration settings. | ||
| """ | ||
|
|
||
| # === Kafka Settings === | ||
| server: str = Field( | ||
| description="Kafka Server URL", | ||
| ) | ||
| topic: str = Field( | ||
| default="lhm-riski-parse", | ||
| description="Kafka Topic Name", | ||
| ) | ||
| security: bool = Field( | ||
| description="Enable mTLS security for accessing Kafka Server", | ||
| ) | ||
| ca_b64: str | None = Field( | ||
| default=None, | ||
| description="Kafka Server CA (B64 Encoded)", | ||
| ) | ||
| pkcs12_data: str | None = Field( | ||
| default=None, | ||
| description="Kafka P12 (B64 Encoded)", | ||
| ) | ||
| pkcs12_pw: str | None = Field( | ||
| default=None, | ||
| description="Kafka P12 Password (B64 Encoded)", | ||
| ) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Message(BaseModel): | ||
| content: str | ||
| republished: bool = False |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.