Skip to content

Commit

Permalink
Merge pull request #31 from taneltinits/feature/token-supplier-from-di
Browse files Browse the repository at this point in the history
feat: add possibility to use token supplier from DI, instead of only …
  • Loading branch information
VonDerBeck authored May 24, 2024
2 parents c3065b6 + e4513c4 commit f7fba56
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,20 @@ private static IServiceCollection AddZeebeJobHandlers(this IServiceCollection se

private static IServiceCollection AddZeebeClient(this IServiceCollection services)
{
return services
return services
.AddZeebeBuilders()
.AddScoped(sp => {
var options = sp.GetRequiredService<IOptions<ZeebeClientAcceleratorOptions>>();
var builder = sp.GetRequiredService<IZeebeClientBuilder>();
var tokenSupplier = sp.GetService<IAccessTokenSupplier>();
var loggerFactory = sp.GetService<ILoggerFactory>();
StateBuilderExtensions.Configure(sp.GetRequiredService<IZeebeVariablesSerializer>());

if(loggerFactory != null)
builder = builder.UseLoggerFactory(loggerFactory);

return builder
.Build(options.Value.Client);
.Build(options.Value.Client, tokenSupplier);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Zeebe.Client.Accelerator.Extensions
{
public static class ZeebeClientBuilderExtensions
{
public static IZeebeClient Build(this IZeebeClientBuilder builder, ClientOptions options)
public static IZeebeClient Build(this IZeebeClientBuilder builder, ClientOptions options, IAccessTokenSupplier tokenSupplier = null)
{
return builder
.BuildTransportBuilder(options)
.BuildFinalStep(options)
.BuildFinalStep(options, tokenSupplier)
.BuildClient(options);
}

Expand All @@ -23,7 +23,7 @@ private static IZeebeClientTransportBuilder BuildTransportBuilder(this IZeebeCli
return builder.UseGatewayAddress(options.GatewayAddress);
}

private static IZeebeClientFinalBuildStep BuildFinalStep(this IZeebeClientTransportBuilder builder, ClientOptions options)
private static IZeebeClientFinalBuildStep BuildFinalStep(this IZeebeClientTransportBuilder builder, ClientOptions options, IAccessTokenSupplier tokenSupplier = null)
{
if (options.Cloud == null &&
(Environment.GetEnvironmentVariable("ZEEBE_CLIENT_ID") != null || Environment.GetEnvironmentVariable("ZEEBE_CLIENT_SECRET") != null))
Expand All @@ -41,20 +41,26 @@ private static IZeebeClientFinalBuildStep BuildFinalStep(this IZeebeClientTransp
return builder.UsePlainText();

IZeebeSecureClientBuilder clientBuilder = null;

if (options.Cloud != null)
{
clientBuilder = builder.UseTransportEncryption();
var tokenSupplier = CamundaCloudTokenProvider.Builder().UseAuthServer(options.Cloud.AuthorizationServerUrl)
.UseClientId(options.Cloud.ClientId).UseClientSecret(options.Cloud.ClientSecret).UseAudience(options.Cloud.TokenAudience)
.Build();

if (tokenSupplier == null)
{
// The CamundaCloudTokenProvider maintains a local token cache, using a local file as a means to persist the token...
tokenSupplier = CamundaCloudTokenProvider.Builder().UseAuthServer(options.Cloud.AuthorizationServerUrl)
.UseClientId(options.Cloud.ClientId).UseClientSecret(options.Cloud.ClientSecret).UseAudience(options.Cloud.TokenAudience)
.Build();
}

clientBuilder.UseAccessTokenSupplier(tokenSupplier);
// The CamundaCloudTokenProvider maintains a local token cache, using a local file as a means to persist the token...
// -> try to get token early in order to prevent errors when writing credential file in parallel upon startup
try { tokenSupplier.GetAccessTokenForRequestAsync().Wait(); } catch (Exception) { /* NOOP */ }

return clientBuilder;
}

if (!String.IsNullOrEmpty(options.TransportEncryption.RootCertificatePath))
clientBuilder = builder.UseTransportEncryption(options.TransportEncryption.RootCertificatePath);
else
Expand Down

0 comments on commit f7fba56

Please sign in to comment.