-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathPulsarBuilder.cs
207 lines (174 loc) · 7.94 KB
/
PulsarBuilder.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
namespace Testcontainers.Pulsar;
/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class PulsarBuilder : ContainerBuilder<PulsarBuilder, PulsarContainer, PulsarConfiguration>
{
public const string PulsarImage = "apachepulsar/pulsar:3.2.3";
public const ushort PulsarBrokerDataPort = 6650;
public const ushort PulsarWebServicePort = 8080;
public const string StartupScriptFilePath = "/testcontainers.sh";
public const string SecretKeyFilePath = "/pulsar/secret.key";
public const string Username = "test-user";
private static readonly IReadOnlyDictionary<string, string> AuthenticationEnvVars = InitAuthenticationEnvVars();
/// <summary>
/// Initializes a new instance of the <see cref="PulsarBuilder" /> class.
/// </summary>
public PulsarBuilder()
: this(new PulsarConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}
/// <summary>
/// Initializes a new instance of the <see cref="PulsarBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private PulsarBuilder(PulsarConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}
/// <inheritdoc />
protected override PulsarConfiguration DockerResourceConfiguration { get; }
/// <summary>
/// Enables authentication.
/// </summary>
/// <remarks>
/// To create an authentication call <see cref="PulsarContainer.CreateAuthenticationTokenAsync" />.
/// </remarks>
/// <returns>A configured instance of <see cref="Pulsar" />.</returns>
public PulsarBuilder WithAuthentication()
{
return Merge(DockerResourceConfiguration, new PulsarConfiguration(authenticationEnabled: true))
.WithEnvironment(AuthenticationEnvVars);
}
/// <summary>
/// Enables function workers.
/// </summary>
/// <param name="functionsWorkerEnabled">Determines whether function workers is enabled or not.</param>
/// <returns>A configured instance of <see cref="Pulsar" />.</returns>
public PulsarBuilder WithFunctionsWorker(bool functionsWorkerEnabled = true)
{
return Merge(DockerResourceConfiguration, new PulsarConfiguration(functionsWorkerEnabled: functionsWorkerEnabled));
}
/// <inheritdoc />
public override PulsarContainer Build()
{
Validate();
var waitStrategy = Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil(DockerResourceConfiguration.AuthenticationEnabled.GetValueOrDefault()));
if (DockerResourceConfiguration.FunctionsWorkerEnabled.GetValueOrDefault())
{
waitStrategy = waitStrategy.UntilMessageIsLogged("Function worker service started");
}
var pulsarBuilder = WithWaitStrategy(waitStrategy);
return new PulsarContainer(pulsarBuilder.DockerResourceConfiguration);
}
/// <inheritdoc />
protected override PulsarBuilder Init()
{
return base.Init()
.WithImage(PulsarImage)
.WithPortBinding(PulsarBrokerDataPort, true)
.WithPortBinding(PulsarWebServicePort, true)
.WithFunctionsWorker(false)
.WithEntrypoint("/bin/sh", "-c")
.WithCommand("while [ ! -f " + StartupScriptFilePath + " ]; do sleep 0.1; done; " + StartupScriptFilePath)
.WithStartupCallback((container, ct) => container.CopyStartupScriptAsync(ct));
}
/// <inheritdoc />
protected override PulsarBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new PulsarConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override PulsarBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new PulsarConfiguration(resourceConfiguration));
}
/// <inheritdoc />
protected override PulsarBuilder Merge(PulsarConfiguration oldValue, PulsarConfiguration newValue)
{
return new PulsarBuilder(new PulsarConfiguration(oldValue, newValue));
}
private static IReadOnlyDictionary<string, string> InitAuthenticationEnvVars()
{
const string authenticationPlugin = "org.apache.pulsar.client.impl.auth.AuthenticationToken";
var authenticationEnvVars = new Dictionary<string, string>();
authenticationEnvVars.Add("authenticateOriginalAuthData", "false");
authenticationEnvVars.Add("authenticationEnabled", "true");
authenticationEnvVars.Add("authorizationEnabled", "true");
authenticationEnvVars.Add("authenticationProviders", "org.apache.pulsar.broker.authentication.AuthenticationProviderToken");
authenticationEnvVars.Add("brokerClientAuthenticationPlugin", authenticationPlugin);
authenticationEnvVars.Add("CLIENT_PREFIX_authPlugin", authenticationPlugin);
authenticationEnvVars.Add("PULSAR_PREFIX_authenticationRefreshCheckSeconds", "5");
authenticationEnvVars.Add("PULSAR_PREFIX_tokenSecretKey", "file://" + SecretKeyFilePath);
authenticationEnvVars.Add("superUserRoles", Username);
return new ReadOnlyDictionary<string, string>(authenticationEnvVars);
}
/// <inheritdoc cref="IWaitUntil" />
private sealed class WaitUntil : IWaitUntil
{
private readonly HttpWaitStrategy _httpWaitStrategy = new HttpWaitStrategy()
.ForPath("/admin/v2/clusters")
.ForPort(PulsarWebServicePort)
.ForResponseMessageMatching(IsClusterHealthyAsync);
private readonly bool _authenticationEnabled;
private string _authToken;
/// <summary>
/// Initializes a new instance of the <see cref="WaitUntil" /> class.
/// </summary>
/// <param name="authenticationEnabled">A value indicating whether authentication is enabled or not.</param>
public WaitUntil(bool authenticationEnabled)
{
_authenticationEnabled = authenticationEnabled;
}
/// <inheritdoc />
public Task<bool> UntilAsync(IContainer container)
{
return UntilAsync(container as PulsarContainer);
}
/// <inheritdoc cref="IWaitUntil.UntilAsync" />
private async Task<bool> UntilAsync(PulsarContainer container)
{
_ = Guard.Argument(container, nameof(container))
.NotNull();
if (_authenticationEnabled && _authToken == null)
{
try
{
_authToken = await container.CreateAuthenticationTokenAsync(TimeSpan.FromHours(1))
.ConfigureAwait(false);
_ = _httpWaitStrategy.WithHeader("Authorization", "Bearer " + _authToken.Trim());
}
catch
{
return false;
}
}
return await _httpWaitStrategy.UntilAsync(container)
.ConfigureAwait(false);
}
/// <summary>
/// Determines whether the cluster is healthy or not.
/// </summary>
/// <param name="response">The HTTP response that contains the cluster information.</param>
/// <returns>A value indicating whether the cluster is healthy or not.</returns>
private static async Task<bool> IsClusterHealthyAsync(HttpResponseMessage response)
{
var jsonString = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
try
{
var status = JsonDocument.Parse(jsonString)
.RootElement
.EnumerateArray()
.ElementAt(0)
.GetString();
return "standalone".Equals(status);
}
catch
{
return false;
}
}
}
}