Azure / azure-event-hubs-for-kafka

Azure Event Hubs for Apache Kafka Ecosystems
https://docs.microsoft.com/azure/event-hubs/event-hubs-for-kafka-ecosystem-overview
Other
221 stars 209 forks source link

.net core sample #97

Open luisedcastillo opened 4 years ago

luisedcastillo commented 4 years ago

Description

Please, is there a way to have a .net core 3.1 guidelines? Now we want to implement Manage Identity using event hubs, but we are not able to follow your explanation because we cannot create authenticate callback.

How to reproduce

Has it worked previously?

<Is this a first attempt at getting the sample application to run, or has it worked in the past?>

Checklist

IMPORTANT: We will close issues where the checklist has not been completed or where adequate information has not been provided.

Please provide the relevant information for the following items:

If this is a question on basic functionality, please verify the following:

arerlend commented 4 years ago

@serkantkaraca

serkantkaraca commented 4 years ago

Confluent Kafka .Net client doesn't support OauthBearer yet. See active issue tracking here https://github.com/confluentinc/confluent-kafka-dotnet/issues/871

arerlend commented 3 years ago

confluent-kafka-dotnet now supports OAuth - they don't have samples yet, but they recommend checking out their integration tests. We will add samples to this repository soon.

https://github.com/confluentinc/confluent-kafka-dotnet/tree/master/test/Confluent.Kafka.IntegrationTests/Tests

KunalAdu commented 3 years ago

@arerlend : Is there any update on samples for .net core

kamleshsingh4u commented 3 years ago

@arerlend : Do we have the sample for .net desktop app to connect to kafka using OAuth

serkantkaraca commented 3 years ago

@kamleshsingh4u Confluent C# library recently provided an API for OAuthBearer auth. I am planning to add a sample soon.

kamleshsingh4u commented 3 years ago

confluent-kafka-dotnet now supports OAuth - they don't have samples yet, but they recommend checking out their integration tests. We will add samples to this repository soon.

https://github.com/confluentinc/confluent-kafka-dotnet/tree/master/test/Confluent.Kafka.IntegrationTests/Tests

@arerlend Could you please provide the dotnet sample for OAuthBearer

KunalAdu commented 2 years ago

@arerlend @serkantkaraca : Is there any update on samples for OAuthBearer in .net ? @luisedcastillo : Did you manage to resolve this?

PSanetra commented 2 years ago

Maybe this helps as a starting point:

var consumerConfig = new ConsumerConfig
{
    SaslMechanism = SaslMechanism.OAuthBearer,
    SaslOauthbearerConfig = "https://my-eventhub-namespace.servicebus.windows.net/.default"
};

using var kafkaConsumer = new ConsumerBuilder<byte[], byte[]>(consumerConfig)
            .SetOAuthBearerTokenRefreshHandler(TokenRefreshHandler)
            .Build();

[...]

private void TokenRefreshHandler(IConsumer<byte[], byte[]> consumer, string config)
{
    var credentials = new DefaultAzureCredential();
    var request = new TokenRequestContext(new[] { config });

    try
    {
        var token = credentials.GetToken(request);
        consumer.OAuthBearerSetToken(token.Token, token.ExpiresOn.ToUnixTimeMilliseconds(), "NoName");
    }
    catch (Exception e)
    {
        consumer.OAuthBearerSetTokenFailure(e.Message);
    }
}
sookeke commented 1 year ago

Here's a successful implementation of SASL/OAUTH OauthTokenRefreshCallback in dotnet

https://github.com/sookeke/jps-pidp/blob/dev-merge/backend/webapi/Kafka/Consumer/KafkaConsumer.cs

inikulshin commented 1 year ago

@PSanetra maybe this is a dumb question, but what should be a principal, that is passed to OAuthBearerSetToken? Just "NoName"?

PSanetra commented 1 year ago

@inikulshin good question. I have tried to find documentation about that parameter and looked into the librdkafka source code. As far as I see this name is just used for logging purposes and maybe as an identifier for the token, but has no further impact on the authorization or authentication mechanism.