-
Notifications
You must be signed in to change notification settings - Fork 274
feat(python): add TLS and auth args to getting-started examples #2754
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
saie-ch
wants to merge
11
commits into
apache:master
Choose a base branch
from
saie-ch:feature/add-tls-args-to-example
base: master
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 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
861c7ef
NA
acd0c40
Merge branch 'apache:master' into feature/add-tls-args-to-example
saie-ch b7f1a67
Add TLS integration tests using testcontainers and TLS pass to shell …
2ba8d14
Fix CI lint failures
saie-ch 77bbef9
Merge branch 'master' into feature/add-tls-args-to-example
saie-ch f2f9a9e
Fix Python formatting
saie-ch 73a3d77
Fix test_tls.py formatting
saie-ch da90833
Merge branch 'master' into feature/add-tls-args-to-example
saie-ch f411c5b
Fix mypy lint for testcontainers imports
saie-ch 37c2de2
Address review comments: remove login_user, fix negative test, instal…
saie-ch 1adf317
Merge branch 'master' into feature/add-tls-args-to-example
mmodzelewski 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
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
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,189 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """ | ||
| Integration tests for TLS connectivity using testcontainers. | ||
|
|
||
| These tests spin up a TLS-enabled Iggy server in a Docker container | ||
| so they are fully self-contained and work in CI without a pre-running | ||
| TLS server. | ||
|
|
||
| Requirements: | ||
| - Docker running locally | ||
| - testcontainers[docker] installed (in [testing-docker] extras) | ||
| - CA certificate available at core/certs/iggy_ca_cert.pem | ||
| """ | ||
|
|
||
| import os | ||
| import uuid | ||
|
|
||
| import pytest | ||
| from apache_iggy import IggyClient, PollingStrategy | ||
| from apache_iggy import SendMessage as Message | ||
| from testcontainers.core.container import DockerContainer | ||
| from testcontainers.core.waiting_utils import wait_for_logs | ||
|
|
||
| from .utils import wait_for_ping, wait_for_server | ||
|
|
||
| # Paths resolved relative to this file → repo root | ||
| REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..")) | ||
| CERTS_DIR = os.path.join(REPO_ROOT, "core", "certs") | ||
| CA_FILE = os.path.join(CERTS_DIR, "iggy_ca_cert.pem") | ||
|
|
||
| IGGY_IMAGE = os.environ.get("IGGY_SERVER_DOCKER_IMAGE", "apache/iggy:edge") | ||
| CONTAINER_TCP_PORT = 8090 | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def tls_container(): | ||
| """Start a TLS-enabled Iggy server in Docker.""" | ||
| container = ( | ||
| DockerContainer(IGGY_IMAGE) | ||
| .with_exposed_ports(CONTAINER_TCP_PORT) | ||
| .with_env("IGGY_ROOT_USERNAME", "iggy") | ||
| .with_env("IGGY_ROOT_PASSWORD", "iggy") | ||
| .with_env("IGGY_TCP_TLS_ENABLED", "true") | ||
| .with_env("IGGY_TCP_TLS_CERT_FILE", "/app/certs/iggy_cert.pem") | ||
| .with_env("IGGY_TCP_TLS_KEY_FILE", "/app/certs/iggy_key.pem") | ||
| .with_env("IGGY_TCP_ADDRESS", f"0.0.0.0:{CONTAINER_TCP_PORT}") | ||
| .with_volume_mapping(CERTS_DIR, "/app/certs", "ro") | ||
| .with_kwargs(privileged=True) | ||
| ) | ||
| container.start() | ||
| # Wait for the server to be ready inside the container | ||
| wait_for_logs(container, "Iggy server is running", timeout=60) | ||
| yield container | ||
| container.stop() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| async def tls_client(tls_container) -> IggyClient: | ||
| """Create an authenticated client connected to the TLS container.""" | ||
| host = "localhost" | ||
| port = tls_container.get_exposed_port(CONTAINER_TCP_PORT) | ||
|
|
||
| wait_for_server(host, int(port)) | ||
|
|
||
| conn_str = ( | ||
| f"iggy+tcp://iggy:iggy@{host}:{port}" | ||
| f"?tls=true&tls_domain={host}&tls_ca_file={CA_FILE}" | ||
| ) | ||
| client = IggyClient.from_connection_string(conn_str) | ||
| await client.connect() | ||
| await wait_for_ping(client) | ||
| await client.login_user("iggy", "iggy") | ||
| return client | ||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| class TestTlsConnectivity: | ||
| """Test TLS connection establishment and basic operations.""" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ping_over_tls(self, tls_client: IggyClient): | ||
| """Test that the server responds to ping over a TLS connection.""" | ||
| await tls_client.ping() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_client_not_none(self, tls_client: IggyClient): | ||
| """Test that the TLS client fixture is properly initialized.""" | ||
| assert tls_client is not None | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_create_stream_over_tls(self, tls_client: IggyClient): | ||
| """Test creating and getting a stream over TLS.""" | ||
| stream_name = f"tls-test-stream-{uuid.uuid4().hex[:8]}" | ||
| await tls_client.create_stream(stream_name) | ||
| stream = await tls_client.get_stream(stream_name) | ||
| assert stream is not None | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_produce_and_consume_over_tls(self, tls_client: IggyClient): | ||
| """Test producing and consuming messages over TLS.""" | ||
| stream_name = f"tls-msg-stream-{uuid.uuid4().hex[:8]}" | ||
| topic_name = "tls-test-topic" | ||
| partition_id = 0 | ||
|
|
||
| # Create stream and topic | ||
| await tls_client.create_stream(stream_name) | ||
| await tls_client.create_topic( | ||
| stream_name, topic_name, partitions_count=1 | ||
| ) | ||
|
|
||
| # Produce messages | ||
| test_messages = [f"tls-message-{i}" for i in range(3)] | ||
| messages = [Message(msg) for msg in test_messages] | ||
| await tls_client.send_messages( | ||
| stream=stream_name, | ||
| topic=topic_name, | ||
| partitioning=partition_id, | ||
| messages=messages, | ||
| ) | ||
|
|
||
| # Consume messages | ||
| polled = await tls_client.poll_messages( | ||
| stream=stream_name, | ||
| topic=topic_name, | ||
| partition_id=partition_id, | ||
| polling_strategy=PollingStrategy.First(), | ||
| count=10, | ||
| auto_commit=True, | ||
| ) | ||
| assert len(polled) >= len(test_messages) | ||
|
|
||
| # Verify message payloads | ||
| for i, expected_msg in enumerate(test_messages): | ||
| if i < len(polled): | ||
| assert polled[i].payload().decode("utf-8") == expected_msg | ||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| class TestTlsConnectionString: | ||
| """Test TLS connection string variations.""" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_connection_string_with_tls_params(self, tls_container): | ||
| """Test creating a client from a connection string with TLS parameters.""" | ||
| host = "localhost" | ||
| port = tls_container.get_exposed_port(CONTAINER_TCP_PORT) | ||
|
|
||
| wait_for_server(host, int(port)) | ||
|
|
||
| conn_str = ( | ||
| f"iggy+tcp://iggy:iggy@{host}:{port}" | ||
| f"?tls=true&tls_domain={host}&tls_ca_file={CA_FILE}" | ||
| ) | ||
| client = IggyClient.from_connection_string(conn_str) | ||
| await client.connect() | ||
| await wait_for_ping(client) | ||
| await client.ping() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_connect_without_tls_should_fail(self, tls_container): | ||
| """Test that connecting without TLS to a TLS-enabled server fails.""" | ||
| host = "localhost" | ||
| port = tls_container.get_exposed_port(CONTAINER_TCP_PORT) | ||
|
|
||
| wait_for_server(host, int(port)) | ||
|
|
||
| # Connect without TLS to a TLS-enabled server | ||
| # IggyClient constructor requires IP address, not hostname | ||
| client = IggyClient(f"127.0.0.1:{port}") | ||
| await client.connect() | ||
|
||
|
|
||
| with pytest.raises(RuntimeError): | ||
| await client.login_user("iggy", "iggy") | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the credentials are provided in the connection string, then auto_login is enabled, so calling login is no longer needed as a separate step