Skip to content

Commit 861c7ef

Browse files
Sai EswarSai Eswar
authored andcommitted
NA
1 parent 8cb4805 commit 861c7ef

File tree

2 files changed

+108
-11
lines changed

2 files changed

+108
-11
lines changed

examples/python/getting-started/consumer.py

100644100755
Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
PARTITION_ID = 0
3333
BATCHES_LIMIT = 5
3434

35-
ArgNamespace = namedtuple("ArgNamespace", ["tcp_server_address"])
35+
ArgNamespace = namedtuple("ArgNamespace", ["tcp_server_address", "tls", "tls_ca_file", "username", "password"])
3636

3737

3838
class ValidateUrl(argparse.Action):
@@ -57,23 +57,71 @@ def parse_args():
5757
action=ValidateUrl,
5858
default="127.0.0.1:8090",
5959
)
60-
return parser.parse_args()
61-
60+
parser.add_argument(
61+
"--tls",
62+
action="store_true",
63+
default=False,
64+
help="Enable TLS for TCP connection",
65+
)
66+
parser.add_argument(
67+
"--tls-ca-file",
68+
default="",
69+
help="Path to TLS CA certificate file",
70+
)
71+
parser.add_argument(
72+
"--username",
73+
default="iggy",
74+
help="Username for authentication",
75+
)
76+
parser.add_argument(
77+
"--password",
78+
default="iggy",
79+
help="Password for authentication",
80+
)
81+
args = parser.parse_args()
82+
83+
# Validate TLS requirements
84+
if args.tls and not args.tls_ca_file:
85+
parser.error("--tls requires --tls-ca-file")
86+
87+
return ArgNamespace(**vars(args))
88+
89+
def build_connection_string(args) -> str:
90+
"""Build a connection string with TLS support."""
91+
92+
conn_str = f"iggy://{args.username}:{args.password}@{args.tcp_server_address}"
93+
94+
if args.tls:
95+
# Extract domain from server address (host:port -> host)
96+
host = args.tcp_server_address.split(":")[0]
97+
query_params = [f"tls=true", f"tls_domain={host}"]
98+
99+
# Add CA file if provided
100+
if args.tls_ca_file:
101+
query_params.append(f"tls_ca_file={args.tls_ca_file}")
102+
conn_str += "?" + "&".join(query_params)
103+
104+
return conn_str
62105

63106
async def main():
64107
args: ArgNamespace = parse_args()
65-
client = IggyClient(args.tcp_server_address)
108+
109+
# Build connection string with TLS support
110+
connection_string = build_connection_string(args)
111+
logger.info(f"Connection string: {connection_string}")
112+
113+
client = IggyClient.from_connection_string(connection_string)
66114
try:
67115
logger.info("Connecting to IggyClient...")
68116
await client.connect()
69117
logger.info("Connected. Logging in user...")
70-
await client.login_user("iggy", "iggy")
118+
await client.login_user(args.username, args.password)
71119
logger.info("Logged in.")
72120
await consume_messages(client)
73121
except Exception as error:
74122
logger.exception("Exception occurred in main function: {}", error)
75123

76-
124+
77125
async def consume_messages(client: IggyClient):
78126
interval = 0.5 # 500 milliseconds in seconds for asyncio.sleep
79127
logger.info(

examples/python/getting-started/producer.py

100644100755
Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
PARTITION_ID = 0
3434
BATCHES_LIMIT = 5
3535

36-
ArgNamespace = namedtuple("ArgNamespace", ["tcp_server_address"])
36+
ArgNamespace = namedtuple("ArgNamespace", ["tcp_server_address", "tls", "tls_ca_file", "username", "password"])
3737

3838

3939
class ValidateUrl(argparse.Action):
@@ -58,16 +58,65 @@ def parse_args():
5858
action=ValidateUrl,
5959
default="127.0.0.1:8090",
6060
)
61-
return parser.parse_args()
62-
61+
parser.add_argument(
62+
"--tls",
63+
action="store_true",
64+
default=False,
65+
help="Enable TLS for TCP connection",
66+
)
67+
parser.add_argument(
68+
"--tls-ca-file",
69+
default="",
70+
help="Path to TLS CA certificate file",
71+
)
72+
parser.add_argument(
73+
"--username",
74+
default="iggy",
75+
help="Username for authentication",
76+
)
77+
parser.add_argument(
78+
"--password",
79+
default="iggy",
80+
help="Password for authentication",
81+
)
82+
args = parser.parse_args()
83+
84+
# Validate TLS requirements
85+
if args.tls and not args.tls_ca_file:
86+
parser.error("--tls requires --tls-ca-file")
87+
88+
return ArgNamespace(**vars(args))
89+
90+
def build_connection_string(args) -> str:
91+
"""Build a connection string with TLS support."""
92+
93+
conn_str = f"iggy://{args.username}:{args.password}@{args.tcp_server_address}"
94+
95+
if args.tls:
96+
# Extract domain from server address (host:port -> host)
97+
host = args.tcp_server_address.split(":")[0]
98+
query_params = [f"tls=true", f"tls_domain={host}"]
99+
100+
# Add CA file if provided
101+
if args.tls_ca_file:
102+
query_params.append(f"tls_ca_file={args.tls_ca_file}")
103+
conn_str += "?" + "&".join(query_params)
104+
105+
return conn_str
63106

64107
async def main():
65108
args: ArgNamespace = parse_args()
66-
client = IggyClient(args.tcp_server_address)
109+
# Build connection string with TLS support
110+
connection_string = build_connection_string(args)
111+
logger.info(f"Connection string: {connection_string}")
112+
logger.info(f"Connecting to {args.tcp_server_address} (TLS: {args.tls})")
113+
114+
115+
client = IggyClient.from_connection_string(connection_string)
67116
logger.info("Connecting to IggyClient")
68117
await client.connect()
69118
logger.info("Connected. Logging in user...")
70-
await client.login_user("iggy", "iggy")
119+
await client.login_user(args.username, args.password)
71120
logger.info("Logged in.")
72121
await init_system(client)
73122
await produce_messages(client)

0 commit comments

Comments
 (0)