dapr / dotnet-sdk

Dapr SDK for .NET
Apache License 2.0
1.11k stars 337 forks source link

Unable to fetch secret as daprClient.GetSecretAsync returning error #952

Open nelson-b opened 2 years ago

nelson-b commented 2 years ago

Expected Behavior

Should fetch the secret from azure key vault using c sharp dapr client SDK

Actual Behavior

Getting below error when executing daprClient.GetSecretAsync function

Dapr.DaprException HResult=0x80131500 Message=Secret operation failed: the Dapr endpoint indicated a failure. See InnerException for details. Source=Dapr.Client StackTrace: at Dapr.Client.DaprClientGrpc.d48.MoveNext() at Program.<

$>d0.MoveNext() in C:\Users\DmonteN\source\repos\touchstone-svc-template\src\Touchstone.Api\Program.cs:line 120

This exception was originally thrown at this call stack: [External Code]

Inner Exception 1: RpcException: Status(StatusCode="Unavailable", Detail="Error connecting to subchannel.", DebugException="System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it. at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token) at System.Net.Sockets.Socket.g__WaitForConnectWithCancellation|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken) at Grpc.Net.Client.Balancer.Internal.SocketConnectivitySubchannelTransport.TryConnectAsync(CancellationToken cancellationToken)")

Steps to Reproduce the Problem

1) Install dapr, configure azure key vault yaml file. 2) run dapr side car engine 3) Verify if you are able to fetch the secret using dapr secret API in postman 4) If yes, then execute below code in c sharp

// Create Dapr Client
var daprClient = new DaprClientBuilder()
               .UseHttpEndpoint("http://127.0.0.1:50001/")
               .UseJsonSerializationOptions(default)
               .Build();

// Add the DaprClient to DI.

builder.Services.AddSingleton<DaprClient>(daprClient);
var noProxy = Environment.GetEnvironmentVariable("no_proxy");

CancellationTokenSource source = new CancellationTokenSource();
CancellationToken cancellationToken = source.Token;

//Using Dapr SDK to invoke a method
var metadata = new Dictionary<string, string> { ["version_id"] = "3" };
var secret = await daprClient.GetSecretAsync("azuresecretstore", "appcs-id-Primary", metadata, cancellationToken); //error line
Console.WriteLine($"Result: {string.Join(", ", secret)}");
halspang commented 2 years ago

@nelson-b - What ports is your dapr sidecar running on? It looks like the connection was refused talking to it.

halspang commented 2 years ago

@nelson-b - I've confirmed that I have no problem fetching a secret from Azure Keyvault with the client. Can you give some more information on how you're connecting/running Dapr?

flomader commented 2 years ago

I am having the same issue accessing a statestore (SQL Server). It only occurs when I start Dapr via dapr CLI: dapr run --app-id MyApp --components-path .\components If I run daprd it works: daprd --app-id MyApp --components-path .\components

If I use the CLI and start my application together with dapr in the same command line call then it works as well: dapr run --app-id MyApp --components-path .\components dotnet run I am on Windows.

jlemmons commented 1 year ago

I am getting this same error.

My experience is exactly like @flomader . Statestore, SQL Server, Windows and I'm getting the same results from those commands.

william-liebenberg commented 1 year ago

Im also suffering from this at the moment just trying to access redis state store from my .NET 7 apps :(

Tried multiple runtimes (v1.10 and v1.9.5) and both are behaving exactly the same way. Made sure I was also running in admin mode too.

I also tried both running my apps with http and https (by adding --app-ssl)

I tried using daprd directly as @flomader suggested but the results are the same.

Im on Windows 11.

My apps run successfully if i run them with dapr run <all the configs> -- dotnet run but this makes debugging the apps a bit harder because i have to manually attach the debugger...

This is severely hurting my local dev experience :(

Any advice?

jlemmons commented 1 year ago

We were able to get around this by using Dapr Sidekick https://github.com/man-group/dapr-sidekick-dotnet

It only required adding the Man.Dapr.Sidekick.AspNetCore nuget package and adding this line to Program.cs

builder.Services.AddDaprSidekick(builder.Configuration, p => p.Sidecar = new DaprSidecarOptions() { AppId = "MyApp", ComponentsDirectory = "./components" });