| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Net.Http; |
| | | 6 | | using System.Net.Security; |
| | | 7 | | using Grpc.Core; |
| | | 8 | | using Grpc.Core.Interceptors; |
| | | 9 | | using Grpc.Net.Client; |
| | | 10 | | |
| | | 11 | | namespace dotnet_etcd.DependencyInjection; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Options for configuring an EtcdClient instance. |
| | | 15 | | /// </summary> |
| | | 16 | | public class EtcdClientOptions |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets or sets the connection string for the etcd server. |
| | | 20 | | /// </summary> |
| | 52 | 21 | | public string ConnectionString { get; set; } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets or sets the port to connect to. Default is 2379. |
| | | 25 | | /// </summary> |
| | 49 | 26 | | public int Port { get; set; } = 2379; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets or sets the server name. Default is "my-etcd-server". |
| | | 30 | | /// </summary> |
| | 26 | 31 | | public string ServerName { get; set; } = "my-etcd-server"; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets or sets the action to configure the gRPC channel options. |
| | | 35 | | /// </summary> |
| | 6 | 36 | | public Action<GrpcChannelOptions> ConfigureChannel { get; set; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets the interceptors to apply to gRPC calls. |
| | | 40 | | /// </summary> |
| | 6 | 41 | | public Interceptor[] Interceptors { get; set; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets or sets a value indicating whether to use insecure credentials (no SSL). |
| | | 45 | | /// Setting this to true will automatically configure the channel with ChannelCredentials.Insecure. |
| | | 46 | | /// </summary> |
| | 9 | 47 | | public bool UseInsecureChannel { get; set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets or sets the authorization credentials. If set, this will be used to create the channel credentials. |
| | | 51 | | /// </summary> |
| | 1 | 52 | | public CallCredentials CallCredentials { get; set; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets or sets the action to configure SSL client authentication options. |
| | | 56 | | /// Use this to configure custom SSL certificates for self-signed certificates. |
| | | 57 | | /// </summary> |
| | 0 | 58 | | public Action<SslClientAuthenticationOptions> ConfigureSslOptions { get; set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets or sets a value indicating whether to enable retry policy. |
| | | 62 | | /// </summary> |
| | 14 | 63 | | public bool EnableRetryPolicy { get; set; } = true; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Applies the options to the channel options. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="options">The gRPC channel options to configure.</param> |
| | | 69 | | internal void ApplyTo(GrpcChannelOptions options) |
| | 5 | 70 | | { |
| | 5 | 71 | | if (UseInsecureChannel) |
| | 4 | 72 | | { |
| | 4 | 73 | | options.Credentials = ChannelCredentials.Insecure; |
| | 4 | 74 | | } |
| | 1 | 75 | | else if (CallCredentials != null) |
| | 0 | 76 | | { |
| | 0 | 77 | | ChannelCredentials channelCredentials = ChannelCredentials.Create(new SslCredentials(), CallCredentials); |
| | 0 | 78 | | options.Credentials = channelCredentials; |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | // Apply custom configuration if provided |
| | 5 | 82 | | ConfigureChannel?.Invoke(options); |
| | 5 | 83 | | } |
| | | 84 | | } |