Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Remove PulsarContainer.CreateAuthenticationTokenAsync(TimeSpan) default arg #1195

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading