Skip to content

Commit

Permalink
Update authToken creation to support infinite expiration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
David Jensen committed Jun 12, 2024
1 parent 56d3eab commit 13679a3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
9 changes: 8 additions & 1 deletion docs/modules/pulsar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 21 additions & 19 deletions src/Testcontainers.Pulsar/PulsarContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,44 @@ public string GetServiceAddress()
/// <summary>
/// Creates an authentication token.
/// </summary>
/// <param name="expire">The time after the authentication token expires.</param>
/// <param name="expiryTime">The time after the authentication token expires.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>A task that completes when the authentication token has been created.</returns>
/// <exception cref="ArgumentException"></exception>
public async Task<string> CreateAuthenticationTokenAsync(TimeSpan expire = default, CancellationToken ct = default)
public async Task<string> 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<string>(9)
{
"bin/pulsar",
"tokens",
"create",
"--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);

Expand Down
2 changes: 1 addition & 1 deletion tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public PulsarAuthConfiguration()

protected override async Task<IPulsarClient> 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();
Expand Down

0 comments on commit 13679a3

Please sign in to comment.