-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityUsage.swift
More file actions
107 lines (87 loc) · 3.24 KB
/
Copy pathSecurityUsage.swift
File metadata and controls
107 lines (87 loc) · 3.24 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
102
103
104
105
106
107
/**
Security example for Streamline Swift SDK.
Prerequisites:
1. Start a Streamline server with auth enabled
2. Set environment variables as shown below
Run with:
SASL_USERNAME=admin SASL_PASSWORD=admin-secret swift run SecurityUsage
SECURITY_MODE=tls CA_PATH=certs/ca.pem swift run SecurityUsage
*/
import Foundation
import StreamlineSDK
@main
struct SecurityUsage {
static func main() async throws {
print("Streamline Security Examples")
print(String(repeating: "=", count: 40))
print()
let mode = ProcessInfo.processInfo.environment["SECURITY_MODE"] ?? "sasl_plain"
switch mode {
case "scram":
try await scramExample()
case "tls":
try await tlsExample()
default:
try await saslPlainExample()
}
print("Done!")
}
static func saslPlainExample() async throws {
print("SASL/PLAIN Authentication")
print(String(repeating: "-", count: 40))
let config = StreamlineConfiguration(
url: URL(string: "ws://localhost:9092")!,
authToken: nil,
sasl: SaslConfig(
mechanism: .plain,
username: envOr("SASL_USERNAME", fallback: "admin"),
password: envOr("SASL_PASSWORD", fallback: "admin-secret")
)
)
let client = StreamlineClient(configuration: config)
client.connect()
print(" Connected with SASL/PLAIN")
try client.produce(topic: "secure-topic", stringValue: "authenticated message")
print(" Produced message to secure-topic")
client.disconnect()
print(" Disconnected.\n")
}
static func scramExample() async throws {
print("SASL/SCRAM-SHA-256 Authentication")
print(String(repeating: "-", count: 40))
let config = StreamlineConfiguration(
url: URL(string: "ws://localhost:9092")!,
sasl: SaslConfig(
mechanism: .scramSha256,
username: envOr("SASL_USERNAME", fallback: "admin"),
password: envOr("SASL_PASSWORD", fallback: "admin-secret")
)
)
let client = StreamlineClient(configuration: config)
client.connect()
print(" Connected with SCRAM-SHA-256")
client.disconnect()
print(" Disconnected.\n")
}
static func tlsExample() async throws {
print("TLS Encrypted Connection")
print(String(repeating: "-", count: 40))
let config = StreamlineConfiguration(
url: URL(string: "wss://localhost:9093")!,
tls: TlsConfig(
enabled: true,
caCertificatePath: envOr("CA_PATH", fallback: "certs/ca.pem"),
clientCertificatePath: ProcessInfo.processInfo.environment["CLIENT_CERT_PATH"],
clientKeyPath: ProcessInfo.processInfo.environment["CLIENT_KEY_PATH"]
)
)
let client = StreamlineClient(configuration: config)
client.connect()
print(" Connected with TLS")
client.disconnect()
print(" Disconnected.\n")
}
static func envOr(_ key: String, fallback: String) -> String {
ProcessInfo.processInfo.environment[key] ?? fallback
}
}