Azure / Azure/azure-functions-dotnet-worker

[QUERY] How to set Azure Key Vault Secret value as the Connection parameter of EventHub Output binding attribute?

Open
#869 8 comments 0 reactions 1 assignee Claimed by @kshyju View on GitHub
needs-discussion
Dominant language
C#
Stars
466
Forks
215
Avg merge
3d 10h
Merged PRs (30d)
7

Description

# Issue Transfer

This issue has been transferred from the Azure SDK for .NET repository, [#28443](https://github.com/Azure/azure-sdk-for-net/issues/28443).

### Please be aware that @ahsanhabib91 is the author of the original issue and include them for any questions or replies.

## Details

### Library name and version

Microsoft.Azure.Functions.Worker 1.6.0, Microsoft.Azure.Functions.Worker.Sdk 1.3.0

### Query/Question

I have a simple Azure Function **.NET 6 Isolated process** with **HttpTrigger** which sends data to **EventHub** through **output binding**. The Output binding attribute is like `[EventHubOutput("my-eh", Connection = "EventHubConnectionString")]`. Now, I would like to set the **EventHubConnectionString** from a **Secret** saved in **Azure Key Vault**. How to do it?

**Note:** I know [Key Vault references](https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references) is a good solution. But this doesn't work **locally** and we don't want to save **secrets** inside **local.settings.json** either.

Here is my **Program.cs:**

```csharp
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using Microsoft.Extensions.Hosting;

var kvUri = $"https://{Environment.GetEnvironmentVariable("KEY_VAULT_NAME")}.vault.azure.net";
var secretClient = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
var secret = await secretClient.GetSecretAsync("EventHubConnectionString");
// would like to use this value as EventHubConnectionString inside the Connection parameter of EventHubOutput attribute
var eventHubConnectionString = secret.Value.Value;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.Build();

host.Run();
```

Here is my **Function.cs:**

```csharp
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace FuncManagedIdentities;

public class ManagedIdentitiesFunction
{
private readonly ILogger _logger;

public ManagedIdentitiesFunction(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger();
}

[Function(nameof(ManagedIdentitiesFunction))]
public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
{
return new OutputType()
{
OutputEvent = $"{nameof(ManagedIdentitiesFunction)}: OutputEvent Message received at {DateTime.Now}",
HttpResponse = req.CreateResponse(HttpStatusCode.OK)
};
}
}

public class OutputType
{
[EventHubOutput("my-eh", Connection = "EventHubConnectionString")]// EventHubConnectionString should be set from KeyVault Secret
public string OutputEvent { get; set; }

public HttpResponseData HttpResponse { get; set; }
}
```

### Environment

.NET SDK (reflecting any global.json):
Version: 6.0.202
Commit: f8a55617d2

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19044
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.202\

Host (useful for support):
Version: 6.0.4
Commit: be98e88c76

.NET SDKs installed:
6.0.201 [C:\Program Files\dotnet\sdk]
6.0.202 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.