From 13679a36f03c720be60e0b08257a8b630e8b55b1 Mon Sep 17 00:00:00 2001 From: David Jensen Date: Wed, 12 Jun 2024 10:40:09 +0200 Subject: [PATCH] Update authToken creation to support infinite expiration This update modifies the CreateAuthenticationTokenAsync function to accept an expiry time, including an option for infinite expiration. In addition, the PulsarContainerTest and Pulsar documentation have been updated accordingly. The function still warns of a known regression issue when image tags start with "3.2" or "latest", converting the expiry time unit improperly. --- docs/modules/pulsar.md | 9 ++++- src/Testcontainers.Pulsar/PulsarContainer.cs | 40 ++++++++++--------- .../PulsarContainerTest.cs | 2 +- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/docs/modules/pulsar.md b/docs/modules/pulsar.md index 3f91b83fa..39b582a23 100644 --- a/docs/modules/pulsar.md +++ b/docs/modules/pulsar.md @@ -92,13 +92,20 @@ If you need to use token authentication, use the following builder configuration PulsarContainer _pulsarContainer = PulsarBuilder().WithTokenAuthentication().Build(); ``` -Start the container and get the token from the running instance by using: +Start the container and obtain an authentication token with a specified expiration time ```csharp var authToken = await container.CreateAuthenticationTokenAsync(TimeSpan.FromHours(1)) .ConfigureAwait(false); ``` +Alternatively, set the token to never expire + +```csharp +var authToken = await container.CreateAuthenticationTokenAsync(Timeout.InfiniteTimeSpan) + .ConfigureAwait(false); +``` + ## Enable Pulsar Functions If you need to use Pulsar Functions, use the following builder configuration to enable it: diff --git a/src/Testcontainers.Pulsar/PulsarContainer.cs b/src/Testcontainers.Pulsar/PulsarContainer.cs index 7d42e7cac..a802e02b0 100644 --- a/src/Testcontainers.Pulsar/PulsarContainer.cs +++ b/src/Testcontainers.Pulsar/PulsarContainer.cs @@ -37,30 +37,18 @@ public string GetServiceAddress() /// /// Creates an authentication token. /// - /// The time after the authentication token expires. + /// The time after the authentication token expires. /// Cancellation token. /// A task that completes when the authentication token has been created. /// - public async Task CreateAuthenticationTokenAsync(TimeSpan expire = default, CancellationToken ct = default) + public async Task CreateAuthenticationTokenAsync(TimeSpan expiryTime, CancellationToken ct = default) { - int secondsToMilliseconds; - if (_configuration.AuthenticationEnabled.HasValue && !_configuration.AuthenticationEnabled.Value) { throw new ArgumentException("Failed to create token. Authentication is not enabled."); } - - if (_configuration.Image.Tag.StartsWith("3.2") || _configuration.Image.Tag.StartsWith("latest")) - { - Logger.LogWarning("The 'apachepulsar/pulsar:3.2.?' image contains a regression. The expiry time is converted to the wrong unit of time: https://github.com/apache/pulsar/issues/22811."); - secondsToMilliseconds = 1000; - } - else - { - secondsToMilliseconds = 1; - } - - var command = new[] + + var command = new List(9) { "bin/pulsar", "tokens", @@ -68,11 +56,25 @@ public async Task CreateAuthenticationTokenAsync(TimeSpan expire = defau "--secret-key", PulsarBuilder.SecretKeyFilePath, "--subject", - PulsarBuilder.Username, - "--expiry-time", - $"{secondsToMilliseconds * expire.TotalSeconds}s", + PulsarBuilder.Username }; + if (expiryTime != Timeout.InfiniteTimeSpan) + { + int secondsToMilliseconds; + if (_configuration.Image.Tag.StartsWith("3.2") || _configuration.Image.Tag.StartsWith("latest")) + { + Logger.LogWarning("The 'apachepulsar/pulsar:3.2.?' image contains a regression. The expiry time is converted to the wrong unit of time: https://github.com/apache/pulsar/issues/22811."); + secondsToMilliseconds = 1000; + } + else + { + secondsToMilliseconds = 1; + } + command.Add("--expiry-time"); + command.Add($"{secondsToMilliseconds * expiryTime.TotalSeconds}s"); + } + var tokensResult = await ExecAsync(command, ct) .ConfigureAwait(false); diff --git a/tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs b/tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs index 27ae620e4..997ccfd59 100644 --- a/tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs +++ b/tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs @@ -79,7 +79,7 @@ public PulsarAuthConfiguration() protected override async Task CreateClientAsync(CancellationToken ct = default) { - var authToken = await _pulsarContainer.CreateAuthenticationTokenAsync(TimeSpan.FromHours(1), ct) + var authToken = await _pulsarContainer.CreateAuthenticationTokenAsync(Timeout.InfiniteTimeSpan, ct) .ConfigureAwait(false); return PulsarClient.Builder().ServiceUrl(new Uri(_pulsarContainer.GetBrokerAddress())).Authentication(new TokenAuthentication(authToken)).Build();