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

Add ClientId configuration #191

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ The settings exposed here are targeted to more advanced users that want to custo
|LibkafkaDebug|debug|Both
|MetadataMaxAgeMs|metadata.max.age.ms|Both
|SocketKeepaliveEnable|socket.keepalive.enable|Both
|ClientId|client.id|Both

**NOTE:** `MetadataMaxAgeMs` default is `180000` `SocketKeepaliveEnable` default is `true` otherwise, the default value is the same as the [Configuration properties](https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md). The reason of the default settings, refer to this [issue](https://github.com/Azure/azure-functions-kafka-extension/issues/187).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Confluent.Kafka;
Expand Down Expand Up @@ -166,6 +167,8 @@ private ConsumerConfig GetConsumerConfiguration()
// start from earliest if no checkpoint has been committed
AutoOffsetReset = AutoOffsetReset.Earliest,

ClientId = this.listenerConfiguration.ClientId ?? Dns.GetHostName(),

// Secure communication/authentication
SaslMechanism = this.listenerConfiguration.SaslMechanism,
SaslUsername = this.listenerConfiguration.SaslUsername,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,12 @@ public KafkaAttribute()
/// ssl.key.password in librdkafka
/// </summary>
public string SslKeyPassword { get; set; }

/// <summary>
/// Client identifier.
/// Default: the hostname of the client machine
/// client.id in librdkafka
/// </summary>
public string ClientId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Concurrent;
using System.Net;
using System.Reflection;
using System.Text;
using Avro.Generic;
Expand Down Expand Up @@ -130,7 +131,8 @@ public ProducerConfig GetProducerConfig(KafkaProducerEntity entity)
SslCaLocation = resolvedSslCaLocation,
Debug = kafkaOptions?.LibkafkaDebug,
MetadataMaxAgeMs = kafkaOptions?.MetadataMaxAgeMs,
SocketKeepaliveEnable = kafkaOptions?.SocketKeepaliveEnable
SocketKeepaliveEnable = kafkaOptions?.SocketKeepaliveEnable,
ClientId = this.config.ResolveSecureSetting(nameResolver, entity.Attribute.ClientId) ?? Dns.GetHostName()
};

if (entity.Attribute.AuthenticationMode != BrokerAuthenticationMode.NotSet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public class KafkaListenerConfiguration
/// </summary>
public string SslKeyPassword { get; set; }

/// <summary>
/// Client identifier.
/// Default: the hostname of the client machine
/// client.id in librdkafka
/// </summary>
public string ClientId { get; set; }

internal void ApplyToConfig(ClientConfig conf)
{
if (this.SaslMechanism.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public KafkaTriggerAttribute(string brokerList, string topic)
/// </summary>
public string SslKeyPassword { get; set; }

/// <summary>
/// Client identifier.
/// Default: the hostname of the client machine
/// client.id in librdkafka
/// </summary>
public string ClientId { get; set; }

bool IsValidValueType(Type value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private KafkaListenerConfiguration CreateConsumerConfiguration(KafkaTriggerAttri
ConsumerGroup = this.config.ResolveSecureSetting(nameResolver, attribute.ConsumerGroup),
Topic = this.config.ResolveSecureSetting(nameResolver, attribute.Topic),
EventHubConnectionString = this.config.ResolveSecureSetting(nameResolver, attribute.EventHubConnectionString),
ClientId = this.config.ResolveSecureSetting(nameResolver, attribute.ClientId)
};

if (attribute.AuthenticationMode != BrokerAuthenticationMode.NotSet ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -342,7 +343,7 @@ public async Task When_Options_Are_Set_Should_Be_Set_In_Consumer_Config()

await target.StartAsync(default);

Assert.Equal(12, target.ConsumerConfig.Count());
Assert.Equal(13, target.ConsumerConfig.Count());
Assert.Equal("testBroker", target.ConsumerConfig.BootstrapServers);
Assert.Equal("group1", target.ConsumerConfig.GroupId);
Assert.Equal("password1", target.ConsumerConfig.SslKeyPassword);
Expand All @@ -355,10 +356,45 @@ public async Task When_Options_Are_Set_Should_Be_Set_In_Consumer_Config()
Assert.Equal(180000, target.ConsumerConfig.MetadataMaxAgeMs);
Assert.Equal(true, target.ConsumerConfig.SocketKeepaliveEnable);
Assert.Equal(AutoOffsetReset.Earliest, target.ConsumerConfig.AutoOffsetReset);
Assert.Equal(Dns.GetHostName(), target.ConsumerConfig.ClientId);

await target.StopAsync(default);
}

[Fact]
public async Task When_ClientId_Is_Set_Should_Be_Set_In_Consumer_Config()
{
var executor = new Mock<ITriggeredFunctionExecutor>();
var consumer = new Mock<IConsumer<Ignore, string>>();

var listenerConfig = new KafkaListenerConfiguration()
{
BrokerList = "testBroker",
Topic = "topic",
ClientId = "testclientid"
};

var kafkaOptions = new KafkaOptions();
var target = new KafkaListenerForTest<Ignore, string>(
executor.Object,
true,
kafkaOptions,
listenerConfig,
requiresKey: true,
valueDeserializer: null,
NullLogger.Instance,
functionId: "testId"
);

target.SetConsumer(consumer.Object);

await target.StartAsync(default);

Assert.Equal("testclientid", target.ConsumerConfig.ClientId);

await target.StopAsync(default);
}

[Fact]
public async Task When_Options_With_Ssal_Are_Set_Should_Be_Set_In_Consumer_Config()
{
Expand Down Expand Up @@ -392,7 +428,7 @@ public async Task When_Options_With_Ssal_Are_Set_Should_Be_Set_In_Consumer_Confi

await target.StartAsync(default);

Assert.Equal(12, target.ConsumerConfig.Count());
Assert.Equal(13, target.ConsumerConfig.Count());
Assert.Equal("testBroker", target.ConsumerConfig.BootstrapServers);
Assert.Equal("group1", target.ConsumerConfig.GroupId);
Assert.Equal(kafkaOptions.AutoCommitIntervalMs, target.ConsumerConfig.AutoCommitIntervalMs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;

using Avro.Generic;
Expand Down Expand Up @@ -304,5 +305,54 @@ public void GetProducerConfig_When_Ssl_Locations_Resolve_InAzure_Should_Contain_
Assert.Equal(sslCa.FullName, config.SslCaLocation);
Assert.Equal(sslKeyLocation.FullName, config.SslKeyLocation);
}

[Fact]
public void GetProducerConfig_When_Client_Defined_Should_SetFromConfig()
{
var attribute = new KafkaAttribute("brokers:9092", "myTopic")
{
AuthenticationMode = BrokerAuthenticationMode.Plain,
Protocol = BrokerProtocol.SaslSsl,
Username = "myuser",
Password = "secret",
ClientId = "%clientid%"
};

var entity = new KafkaProducerEntity()
{
Attribute = attribute,
ValueType = typeof(ProtoUser),
};

var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string> {
["clientid"] = "SpecifiedClientId"
}).Build();

var factory = new KafkaProducerFactory(configuration, new DefaultNameResolver(configuration), NullLoggerProvider.Instance);
var config = factory.GetProducerConfig(entity);
Assert.Equal("SpecifiedClientId", config.ClientId);
}

[Fact]
public void GetProducerConfig_When_Client_Not_Defined_Should_SetHostName()
{
var attribute = new KafkaAttribute("brokers:9092", "myTopic")
{
AuthenticationMode = BrokerAuthenticationMode.Plain,
Protocol = BrokerProtocol.SaslSsl,
Username = "myuser",
Password = "secret"
};

var entity = new KafkaProducerEntity()
{
Attribute = attribute,
ValueType = typeof(ProtoUser),
};

var factory = new KafkaProducerFactory(emptyConfiguration, new DefaultNameResolver(emptyConfiguration), NullLoggerProvider.Instance);
var config = factory.GetProducerConfig(entity);
Assert.Equal(Dns.GetHostName(), config.ClientId);
}
}
}