-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (92 loc) · 3.75 KB
/
Copy pathProgram.cs
File metadata and controls
101 lines (92 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Demonstrates TLS and SASL authentication configuration.
//
// Shows how to connect to a Streamline server with:
// - TLS encryption (SSL/mTLS)
// - SASL authentication (PLAIN, SCRAM-SHA-256, SCRAM-SHA-512)
//
// Run with:
// dotnet run --project examples/SecurityUsage/SecurityUsage.csproj
using Streamline.Client;
var bootstrapServers = Environment.GetEnvironmentVariable("STREAMLINE_BOOTSTRAP_SERVERS") ?? "localhost:9092";
// =====================================================================
// Example 1: TLS only (server certificate validation)
// =====================================================================
Console.WriteLine("=== TLS Connection ===");
try
{
await using var client = new StreamlineClient(bootstrapServers, new StreamlineOptions
{
SecurityProtocol = SecurityProtocol.Ssl,
Tls = new TlsOptions
{
CaCertificatePath = "/path/to/ca-cert.pem",
// For mutual TLS (mTLS), also set:
// ClientCertificatePath = "/path/to/client-cert.pem",
// ClientKeyPath = "/path/to/client-key.pem",
SkipCertificateVerification = false, // true only for self-signed in dev
},
});
await using var producer = client.CreateProducer<string, string>();
var metadata = await producer.SendAsync("secure-topic", "key", "Hello over TLS!");
Console.WriteLine($"Message sent over TLS at offset={metadata.Offset}");
}
catch (StreamlineException ex)
{
Console.WriteLine($"TLS example: {ex.Message}");
Console.WriteLine("(Expected if server is not configured for TLS)");
}
// =====================================================================
// Example 2: SASL PLAIN authentication
// =====================================================================
Console.WriteLine("\n=== SASL PLAIN Authentication ===");
try
{
await using var client = new StreamlineClient(bootstrapServers, new StreamlineOptions
{
SecurityProtocol = SecurityProtocol.SaslPlaintext,
Sasl = new SaslOptions
{
Mechanism = SaslMechanism.Plain,
Username = "my-user",
Password = "my-password",
},
});
await using var producer = client.CreateProducer<string, string>();
var metadata = await producer.SendAsync("auth-topic", "key", "Hello with SASL PLAIN!");
Console.WriteLine($"Message sent with SASL PLAIN at offset={metadata.Offset}");
}
catch (StreamlineException ex)
{
Console.WriteLine($"SASL PLAIN example: {ex.Message}");
Console.WriteLine("(Expected if server is not configured for SASL)");
}
// =====================================================================
// Example 3: SASL SCRAM-SHA-256 with TLS (most secure)
// =====================================================================
Console.WriteLine("\n=== SASL SCRAM-SHA-256 + TLS ===");
try
{
await using var client = new StreamlineClient(bootstrapServers, new StreamlineOptions
{
SecurityProtocol = SecurityProtocol.SaslSsl,
Tls = new TlsOptions
{
CaCertificatePath = "/path/to/ca-cert.pem",
},
Sasl = new SaslOptions
{
Mechanism = SaslMechanism.ScramSha256,
Username = "my-user",
Password = "my-password",
},
});
await using var producer = client.CreateProducer<string, string>();
var metadata = await producer.SendAsync("secure-auth-topic", "key", "Hello with SCRAM + TLS!");
Console.WriteLine($"Message sent with SCRAM-SHA-256 + TLS at offset={metadata.Offset}");
}
catch (StreamlineException ex)
{
Console.WriteLine($"SCRAM + TLS example: {ex.Message}");
Console.WriteLine("(Expected if server is not configured for SASL+TLS)");
}
Console.WriteLine("\nDone! Adjust paths and credentials for your environment.");