Skip to content
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
95 changes: 94 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,18 @@ These settings mean that if many workflows start waiting at the same time, regis

## Kafka Cancellation Notifier

Handle task cancellation via Kafka events:
Handle task cancellation via Kafka events. The notifier can be registered in two ways.

**From configuration (recommended, supports SSL, mTLS and SASL):**

```csharp
.AddExecutionManager(...)
.AddKafkaCancellationNotifier(configuration.GetSection("Conductor:KafkaCancellationNotifier"))
```

Settings are validated on startup, so a misconfigured connection fails fast with a descriptive error.

**From code (plaintext connections only):**

```csharp
.AddExecutionManager(...)
Expand Down Expand Up @@ -964,6 +975,88 @@ Handle task cancellation via Kafka events:
}
```

### Settings

Optional settings that are not configured are not passed to Kafka, so the librdkafka default applies.

| Setting | Required | Default | Description |
|---|---|---|---|
| `BootstrapServers` | Yes | — | Kafka broker list |
| `TopicName` | Yes | — | Topic with Conductor task status events |
| `GroupId` | Yes | — | Consumer group id |
| `CreateTopicOnStartup` | No | `false` | Create the topic if it does not exist |
| `SecurityProtocol` | No | `Plaintext` | `Plaintext`, `Ssl`, `SaslPlaintext` or `SaslSsl` |
| `SaslMechanism` | With `SaslPlaintext`/`SaslSsl` | — | `Plain`, `ScramSha256` or `ScramSha512` |
| `SaslUsername` | With `SaslPlaintext`/`SaslSsl` | — | SASL username |
| `SaslPassword` | With `SaslPlaintext`/`SaslSsl` | — | SASL password |
| `SslCaLocation` | No | System trust store | Path to CA certificate PEM file |
| `SslCertificateLocation` | Together with `SslKeyLocation` | — | Path to client certificate PEM file (mTLS) |
| `SslKeyLocation` | Together with `SslCertificateLocation` | — | Path to client private key PEM file (mTLS) |
| `SslKeyPassword` | No | — | Password of an encrypted private key |
| `SslKeystoreLocation` | No | — | Path to PKCS#12 keystore (mTLS), alternative to certificate and key |
| `SslKeystorePassword` | No | — | Keystore password |
| `EnableSslCertificateVerification` | No | `true` | Verify the broker certificate; disable only for local development |
| `SslEndpointIdentificationAlgorithm` | No | `Https` | `None` disables broker hostname verification |

`Ssl*` settings are used only when `SecurityProtocol` is `Ssl` or `SaslSsl`. Configured file paths must exist on startup.

### Security examples

**SSL** (broker certificate signed by a private CA):
```json
"KafkaCancellationNotifier": {
"BootstrapServers": "broker:9093",
"GroupId": "my-worker",
"TopicName": "conductor.status.task",
"SecurityProtocol": "Ssl",
"SslCaLocation": "/certs/ca.pem"
}
```

**mTLS** (client certificate authentication):
```json
"KafkaCancellationNotifier": {
"BootstrapServers": "broker:9093",
"GroupId": "my-worker",
"TopicName": "conductor.status.task",
"SecurityProtocol": "Ssl",
"SslCaLocation": "/certs/ca.pem",
"SslCertificateLocation": "/certs/client.pem",
"SslKeyLocation": "/certs/client.key"
}
```

Instead of `SslCertificateLocation` and `SslKeyLocation`, a PKCS#12 keystore can be used with `SslKeystoreLocation` and `SslKeystorePassword`.

**SASL_SSL** (username and password over TLS):
```json
"KafkaCancellationNotifier": {
"BootstrapServers": "broker:9094",
"GroupId": "my-worker",
"TopicName": "conductor.status.task",
"SecurityProtocol": "SaslSsl",
"SaslMechanism": "ScramSha512",
"SaslUsername": "my-user",
"SaslPassword": "my-password",
"SslCaLocation": "/certs/ca.pem"
}
```

**SASL_PLAINTEXT** (username and password without TLS):
```json
"KafkaCancellationNotifier": {
"BootstrapServers": "broker:9092",
"GroupId": "my-worker",
"TopicName": "conductor.status.task",
"SecurityProtocol": "SaslPlaintext",
"SaslMechanism": "Plain",
"SaslUsername": "my-user",
"SaslPassword": "my-password"
}
```

Keep passwords out of `appsettings.json`, for example by using environment variables (`Conductor__KafkaCancellationNotifier__SaslPassword`) or a secret store.

## Toolkit CLI

Generate C# models from existing Conductor task/workflow definitions.
Expand Down
24 changes: 24 additions & 0 deletions SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,30 @@ public class MyController(

### Kafka Cancellation Notifier

Register from configuration (supports SSL, mTLS, SASL_SSL and SASL_PLAINTEXT; settings are validated on startup):

```csharp
.AddExecutionManager(...)
.AddKafkaCancellationNotifier(configuration.GetSection("Conductor:KafkaCancellationNotifier"))
```

```json
"KafkaCancellationNotifier": {
"BootstrapServers": "broker:9094",
"GroupId": "my-worker-group",
"TopicName": "conductor.status.task",
"SecurityProtocol": "SaslSsl",
"SaslMechanism": "ScramSha512",
"SaslUsername": "my-user",
"SaslPassword": "my-password",
"SslCaLocation": "/certs/ca.pem"
}
```

`BootstrapServers`, `GroupId` and `TopicName` are required. `SecurityProtocol` defaults to `Plaintext`. SASL protocols require `SaslMechanism` (`Plain`, `ScramSha256`, `ScramSha512`), `SaslUsername` and `SaslPassword`. For mTLS, set `SslCertificateLocation` and `SslKeyLocation` (PEM files) or `SslKeystoreLocation` (PKCS#12). Certificates are configured as file paths only. See the README for the full settings reference.

Or register from code (plaintext connections only):

```csharp
.AddExecutionManager(...)
.AddKafkaCancellationNotifier(
Expand Down
6 changes: 1 addition & 5 deletions examples/ConductorSharp.NoApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@
pipelines.AddCustomBehavior<PrepareEmailBehavior, PrepareEmailRequest, PrepareEmailResponse>();
})
.AddConductorSharpPatterns();
//.AddKafkaCancellationNotifier(
// kafkaBootstrapServers: configuration.GetValue<string>(
// "Conductor:KafkaCancellationNotifier:BootstrapServers"),
// topicName: configuration.GetValue<string>("Conductor:KafkaCancellationNotifier:TopicName"),
// groupId: configuration.GetValue<string>("Conductor:KafkaCancellationNotifier:GroupId"));
//.AddKafkaCancellationNotifier(configuration.GetSection("Conductor:KafkaCancellationNotifier"));

services.RegisterWorkerTask<GetCustomerHandler>();
services.RegisterWorkerTask<PrepareEmailHandler>();
Expand Down
8 changes: 8 additions & 0 deletions examples/ConductorSharp.NoApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
"BootstrapServers": "localhost:29092",
"GroupId": "ConductorSharp.NoApi",
"TopicName": "conductor.status.task"
// Optional security settings, see README "Kafka Cancellation Notifier" section:
//"SecurityProtocol": "SaslSsl",
//"SaslMechanism": "ScramSha512",
//"SaslUsername": "user",
//"SaslPassword": "password",
//"SslCaLocation": "/certs/ca.pem",
//"SslCertificateLocation": "/certs/client.pem",
//"SslKeyLocation": "/certs/client.key"
}
}
}
2 changes: 1 addition & 1 deletion src/ConductorSharp.Client/ConductorSharp.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Authors>Codaxy</Authors>
<Company>Codaxy</Company>
<PackageId>ConductorSharp.Client</PackageId>
<Version>4.4.1</Version>
<Version>4.5.0</Version>
<Description>Client library for Netflix Conductor, with some additional quality of life features.</Description>
<RepositoryUrl>https://github.com/codaxy/conductor-sharp</RepositoryUrl>
<PackageTags>netflix;conductor</PackageTags>
Expand Down
2 changes: 1 addition & 1 deletion src/ConductorSharp.Engine/ConductorSharp.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Authors>Codaxy</Authors>
<Company>Codaxy</Company>
<PackageId>ConductorSharp.Engine</PackageId>
<Version>4.4.1</Version>
<Version>4.5.0</Version>
<Description>Client library for Netflix Conductor, with some additional quality of life features.</Description>
<RepositoryUrl>https://github.com/codaxy/conductor-sharp</RepositoryUrl>
<PackageTags>netflix;conductor</PackageTags>
Expand Down
3 changes: 3 additions & 0 deletions src/ConductorSharp.KafkaCancellationNotifier/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("ConductorSharp.Engine.Tests")]
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>4.4.1</Version>
<Version>4.5.0</Version>
<Authors>Codaxy</Authors>
<Company>Codaxy</Company>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="2.8.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using ConductorSharp.Engine.Extensions;
using ConductorSharp.Engine.Interface;
using ConductorSharp.KafkaCancellationNotifier.Service;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace ConductorSharp.KafkaCancellationNotifier.Extensions
{
Expand Down Expand Up @@ -29,6 +32,26 @@ public static IExecutionManagerBuilder AddKafkaCancellationNotifier(
opts.CreateTopicOnStartup = createTopicOnStartup;
});

return AddNotifierServices(builder);
}

/// <summary>
/// Registers the Kafka cancellation notifier with settings bound from the given configuration section
/// (e.g. <c>Conductor:KafkaCancellationNotifier</c>), including SSL, mTLS and SASL settings.
/// Settings are validated on startup.
/// </summary>
public static IExecutionManagerBuilder AddKafkaCancellationNotifier(this IExecutionManagerBuilder builder, IConfiguration configuration)
{
ArgumentNullException.ThrowIfNull(configuration);

builder.Builder.AddOptions<KafkaOptions>().Bind(configuration).ValidateOnStart();
builder.Builder.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<KafkaOptions>, KafkaOptionsValidator>());

return AddNotifierServices(builder);
}

private static IExecutionManagerBuilder AddNotifierServices(IExecutionManagerBuilder builder)
{
builder.Builder.AddSingleton<ICancellationNotifier, Service.KafkaCancellationNotifier>();
builder.Builder.AddHostedService<KafkaConsumerBackgroundService>();

Expand Down
34 changes: 33 additions & 1 deletion src/ConductorSharp.KafkaCancellationNotifier/KafkaOptions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
namespace ConductorSharp.KafkaCancellationNotifier;
using Confluent.Kafka;

namespace ConductorSharp.KafkaCancellationNotifier;

internal class KafkaOptions
{
public string BootstrapServers { get; set; } = null!;
public string TopicName { get; set; } = null!;
public string GroupId { get; set; } = null!;
public bool CreateTopicOnStartup { get; set; }

// Security settings are optional and passed to librdkafka only when configured, so librdkafka defaults apply otherwise
public SecurityProtocol? SecurityProtocol { get; set; }

public SaslMechanism? SaslMechanism { get; set; }
public string? SaslUsername { get; set; }
public string? SaslPassword { get; set; }

public string? SslCaLocation { get; set; }
public string? SslCertificateLocation { get; set; }
public string? SslKeyLocation { get; set; }
public string? SslKeyPassword { get; set; }
public string? SslKeystoreLocation { get; set; }
public string? SslKeystorePassword { get; set; }
public bool? EnableSslCertificateVerification { get; set; }
public SslEndpointIdentificationAlgorithm? SslEndpointIdentificationAlgorithm { get; set; }

internal bool IsSaslProtocol => SecurityProtocol is Confluent.Kafka.SecurityProtocol.SaslPlaintext or Confluent.Kafka.SecurityProtocol.SaslSsl;

internal bool IsSslProtocol => SecurityProtocol is Confluent.Kafka.SecurityProtocol.Ssl or Confluent.Kafka.SecurityProtocol.SaslSsl;

internal bool HasSslSettings =>
SslCaLocation is not null
|| SslCertificateLocation is not null
|| SslKeyLocation is not null
|| SslKeyPassword is not null
|| SslKeystoreLocation is not null
|| SslKeystorePassword is not null
|| EnableSslCertificateVerification is not null
|| SslEndpointIdentificationAlgorithm is not null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Confluent.Kafka;

namespace ConductorSharp.KafkaCancellationNotifier.Service
{
internal static class KafkaClientConfigFactory
{
public static ConsumerConfig CreateConsumerConfig(KafkaOptions options)
{
var config = new ConsumerConfig { GroupId = options.GroupId, AutoOffsetReset = AutoOffsetReset.Latest };
ApplyConnectionSettings(config, options);
return config;
}

public static AdminClientConfig CreateAdminClientConfig(KafkaOptions options)
{
var config = new AdminClientConfig();
ApplyConnectionSettings(config, options);
return config;
}

private static void ApplyConnectionSettings(ClientConfig config, KafkaOptions options)
{
config.BootstrapServers = options.BootstrapServers;

// Only configured values are set, so an unsecured setup produces exactly the same config as before
if (options.SecurityProtocol is not null)
config.SecurityProtocol = options.SecurityProtocol;

if (options.SaslMechanism is not null)
config.SaslMechanism = options.SaslMechanism;
if (options.SaslUsername is not null)
config.SaslUsername = options.SaslUsername;
if (options.SaslPassword is not null)
config.SaslPassword = options.SaslPassword;

if (options.SslCaLocation is not null)
config.SslCaLocation = options.SslCaLocation;
if (options.SslCertificateLocation is not null)
config.SslCertificateLocation = options.SslCertificateLocation;
if (options.SslKeyLocation is not null)
config.SslKeyLocation = options.SslKeyLocation;
if (options.SslKeyPassword is not null)
config.SslKeyPassword = options.SslKeyPassword;
if (options.SslKeystoreLocation is not null)
config.SslKeystoreLocation = options.SslKeystoreLocation;
if (options.SslKeystorePassword is not null)
config.SslKeystorePassword = options.SslKeystorePassword;
if (options.EnableSslCertificateVerification is not null)
config.EnableSslCertificateVerification = options.EnableSslCertificateVerification;
if (options.SslEndpointIdentificationAlgorithm is not null)
config.SslEndpointIdentificationAlgorithm = options.SslEndpointIdentificationAlgorithm;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ ICancellationNotifier notifier

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (_kafkaOptions.Value.HasSslSettings && !_kafkaOptions.Value.IsSslProtocol)
_logger.LogWarning(
"SSL settings are configured but SecurityProtocol is {SecurityProtocol}, SSL settings will be ignored",
_kafkaOptions.Value.SecurityProtocol ?? SecurityProtocol.Plaintext
);

if (_kafkaOptions.Value.CreateTopicOnStartup)
await CreateTopicIfDoesNotExists();

using var consumer = new ConsumerBuilder<string, TaskStatusModel>(
new ConsumerConfig
{
BootstrapServers = _kafkaOptions.Value.BootstrapServers,
GroupId = _kafkaOptions.Value.GroupId,
AutoOffsetReset = AutoOffsetReset.Latest
}
).SetValueDeserializer(new TaskStatusModelDeserializer()).SetLogHandler(LogHandler).Build();
using var consumer = new ConsumerBuilder<string, TaskStatusModel>(KafkaClientConfigFactory.CreateConsumerConfig(_kafkaOptions.Value))
.SetValueDeserializer(new TaskStatusModelDeserializer())
.SetLogHandler(LogHandler)
.Build();

consumer.Subscribe(_kafkaOptions.Value.TopicName);

Expand Down Expand Up @@ -95,7 +97,7 @@ await Task.Run(

private async Task CreateTopicIfDoesNotExists()
{
using var admin = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = _kafkaOptions.Value.BootstrapServers }).Build();
using var admin = new AdminClientBuilder(KafkaClientConfigFactory.CreateAdminClientConfig(_kafkaOptions.Value)).Build();

try
{
Expand Down
Loading
Loading