|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "Dapr Cryptography Client" |
| 4 | +linkTitle: "Cryptography client" |
| 5 | +weight: 510005 |
| 6 | +description: Learn how to create Dapr Crytography clients |
| 7 | +--- |
| 8 | + |
| 9 | +The Dapr Cryptography package allows you to perform encryption and decryption operations provided by the Dapr sidecar. |
| 10 | + |
| 11 | +## Lifetime management |
| 12 | +A `DaprEncryptionClient` is a version of the Dapr client that is dedicated to interacting with the Dapr Cryptography API. |
| 13 | +It can be registered alongside a `DaprClient` and other Dapr clients without issue. |
| 14 | + |
| 15 | +It maintains access to networking resources in the form of TCP sockets used to communicate with the Dapr sidecar. |
| 16 | + |
| 17 | +For best performance, create a single long-lived instance of `DaprEncryptionClient` and provide access to that shared |
| 18 | +instance throughout your application. `DaprEncryptionClient` instances are thread-safe and intended to be shared. |
| 19 | + |
| 20 | +This can be aided by utilizing the dependency injection functionality. The registration method supports registration |
| 21 | +as a singleton, a scoped instance, or as a transient (meaning it's recreated every time it's injected), but also enables |
| 22 | +registration to utilize values from an `IConfiguration` or other injected service in a way that's impractical when creating |
| 23 | +the client from scratch in each of your classes. |
| 24 | + |
| 25 | +Avoid creating a `DaprEncryptionClient` for each operation. |
| 26 | + |
| 27 | +## Configuring `DaprEncryptionClient` via `DaprEncryptionClientBuilder` |
| 28 | +A `DaprCryptographyClient` can be configured by invoking methods on the `DaprEncryptionClientBuilder` class before calling |
| 29 | +`.Build()` to create the client itself. The settings for each `DaprEncryptionClientBuilder` are separate can cannot be |
| 30 | +changed after calling `.Build()`. |
| 31 | + |
| 32 | +```cs |
| 33 | +var daprEncryptionClient = new DaprEncryptionClientBuilder() |
| 34 | + .UseDaprApiToken("abc123") //Specify the API token used to authenticate to the Dapr sidecar |
| 35 | + .Build(); |
| 36 | +``` |
| 37 | + |
| 38 | +The `DaprEncryptionClientBuilder` contains settings for: |
| 39 | +- The HTTP endpoint of the Dapr sidecar |
| 40 | +- The gRPC endpoint of the Dapr sidecar |
| 41 | +- The `JsonSerializerOptions` object used to configure JSON serialization |
| 42 | +- The `GrpcChannelOptions` object used to configure gRPC |
| 43 | +- The API token used to authenticate requests to the sidecar |
| 44 | +- The factory method used to create the `HttpClient` instance used by the SDK |
| 45 | +- The timeout used for the `HttpClient` instance when making requests to the sidecar |
| 46 | + |
| 47 | +The SDK will read the following environment variables to configure the default values: |
| 48 | + |
| 49 | +- `DAPR_HTTP_ENDPOINT`: used to find the HTTP endpoint of the Dapr sidecar, example: `https://dapr-api.mycompany.com` |
| 50 | +- `DAPR_GRPC_ENDPOINT`: used to find the gRPC endpoint of the Dapr sidecar, example: `https://dapr-grpc-api.mycompany.com` |
| 51 | +- `DAPR_HTTP_PORT`: if `DAPR_HTTP_ENDPOINT` is not set, this is used to find the HTTP local endpoint of the Dapr sidecar |
| 52 | +- `DAPR_GRPC_PORT`: if `DAPR_GRPC_ENDPOINT` is not set, this is used to find the gRPC local endpoint of the Dapr sidecar |
| 53 | +- `DAPR_API_TOKEN`: used to set the API token |
| 54 | + |
| 55 | +### Configuring gRPC channel options |
| 56 | + |
| 57 | +Dapr's use of `CancellationToken` for cancellation relies on the configuration of the gRPC channel options. If you need |
| 58 | +to configure these options yourself, make sure to enable the [ThrowOperationCanceledOnCancellation setting](https://grpc.github.io/grpc/csharp-dotnet/api/Grpc.Net.Client.GrpcChannelOptions.html#Grpc_Net_Client_GrpcChannelOptions_ThrowOperationCanceledOnCancellation). |
| 59 | + |
| 60 | +```cs |
| 61 | +var daprEncryptionClient = new DaprEncryptionClientBuilder() |
| 62 | + .UseGrpcChannelOptions(new GrpcChannelOptions { .. ThrowOperationCanceledOnCancellation = true }) |
| 63 | + .Build(); |
| 64 | +``` |
| 65 | + |
| 66 | +## Using cancellation with `DaprEncryptionClient` |
| 67 | +The APIs on `DaprEncryptionClient` perform asynchronous operations and accept an optional `CancellationToken` parameter. This |
| 68 | +follows a standard .NET practice for cancellable operations. Note that when cancellation occurs, there is no guarantee that |
| 69 | +the remote endpoint stops processing the request, only that the client has stopped waiting for completion. |
| 70 | + |
| 71 | +When an operation is cancelled, it will throw an `OperationCancelledException`. |
| 72 | + |
| 73 | +## Configuring `DaprEncryptionClient` via dependency injection |
| 74 | +Using the built-in extension methods for registering the `DaprEncryptionClient` in a dependency injection container can |
| 75 | +provide the benefit of registering the long-lived service a single time, centralize complex configuration and improve |
| 76 | +performance by ensuring similarly long-lived resources are re-purposed when possible (e.g. `HttpClient` instances). |
| 77 | + |
| 78 | +There are three overloads available to give the developer the greatest flexibility in configuring the client for their |
| 79 | +scenario. Each of these will register the `IHttpClientFactory` on your behalf if not already registered, and configure |
| 80 | +the `DaprEncryptionClientBuilder` to use it when creating the `HttpClient` instance in order to re-use the same instance as |
| 81 | +much as possible and avoid socket exhaustion and other issues. |
| 82 | + |
| 83 | +In the first approach, there's no configuration done by the developer and the `DaprEncryptionClient` is configured with the |
| 84 | +default settings. |
| 85 | + |
| 86 | +```cs |
| 87 | +var builder = WebApplication.CreateBuilder(args); |
| 88 | + |
| 89 | +builder.Services.AddDaprEncryptionClent(); //Registers the `DaprEncryptionClient` to be injected as needed |
| 90 | +var app = builder.Build(); |
| 91 | +``` |
| 92 | + |
| 93 | +Sometimes the developer will need to configure the created client using the various configuration options detailed |
| 94 | +above. This is done through an overload that passes in the `DaprEncryptionClientBuiler` and exposes methods for configuring |
| 95 | +the necessary options. |
| 96 | + |
| 97 | +```cs |
| 98 | +var builder = WebApplication.CreateBuilder(args); |
| 99 | + |
| 100 | +builder.Services.AddDaprEncryptionClient((_, daprEncrpyptionClientBuilder) => { |
| 101 | + //Set the API token |
| 102 | + daprEncryptionClientBuilder.UseDaprApiToken("abc123"); |
| 103 | + //Specify a non-standard HTTP endpoint |
| 104 | + daprEncryptionClientBuilder.UseHttpEndpoint("http://dapr.my-company.com"); |
| 105 | +}); |
| 106 | + |
| 107 | +var app = builder.Build(); |
| 108 | +``` |
| 109 | + |
| 110 | +Finally, it's possible that the developer may need to retrieve information from another service in order to populate |
| 111 | +these configuration values. That value may be provided from a `DaprClient` instance, a vendor-specific SDK or some |
| 112 | +local service, but as long as it's also registered in DI, it can be injected into this configuration operation via the |
| 113 | +last overload: |
| 114 | + |
| 115 | +```cs |
| 116 | +var builder = WebApplication.CreateBuilder(args); |
| 117 | + |
| 118 | +//Register a fictional service that retrieves secrets from somewhere |
| 119 | +builder.Services.AddSingleton<SecretService>(); |
| 120 | + |
| 121 | +builder.Services.AddDaprEncryptionClient((serviceProvider, daprEncryptionClientBuilder) => { |
| 122 | + //Retrieve an instance of the `SecretService` from the service provider |
| 123 | + var secretService = serviceProvider.GetRequiredService<SecretService>(); |
| 124 | + var daprApiToken = secretService.GetSecret("DaprApiToken").Value; |
| 125 | + |
| 126 | + //Configure the `DaprEncryptionClientBuilder` |
| 127 | + daprEncryptionClientBuilder.UseDaprApiToken(daprApiToken); |
| 128 | +}); |
| 129 | + |
| 130 | +var app = builder.Build(); |
| 131 | +``` |
0 commit comments