|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | +using System.ComponentModel.DataAnnotations; |
| 4 | +using Azure.Core; |
| 5 | +using Azure.Identity; |
| 6 | +using Grpc.Core; |
| 7 | +using Grpc.Net.Client; |
| 8 | + |
| 9 | +namespace Microsoft.DurableTask; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Options for configuring the Durable Task Scheduler. |
| 13 | +/// </summary> |
| 14 | +public class DurableTaskSchedulerClientOptions |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Gets or sets the endpoint address of the Durable Task Scheduler resource. |
| 18 | + /// Expected to be in the format "https://{scheduler-name}.{region}.durabletask.io". |
| 19 | + /// </summary> |
| 20 | + [Required(ErrorMessage = "Endpoint address is required")] |
| 21 | + public string EndpointAddress { get; set; } = string.Empty; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets or sets the name of the task hub resource associated with the Durable Task Scheduler resource. |
| 25 | + /// </summary> |
| 26 | + [Required(ErrorMessage = "Task hub name is required")] |
| 27 | + public string TaskHubName { get; set; } = string.Empty; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets or sets the credential used to authenticate with the Durable Task Scheduler task hub resource. |
| 31 | + /// </summary> |
| 32 | + public TokenCredential? Credential { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets or sets the resource ID of the Durable Task Scheduler resource. |
| 36 | + /// The default value is https://durabletask.io. |
| 37 | + /// </summary> |
| 38 | + public string ResourceId { get; set; } = "https://durabletask.io"; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets or sets a value indicating whether to allow insecure channel credentials. |
| 42 | + /// This should only be set to true in local development/testing scenarios. |
| 43 | + /// </summary> |
| 44 | + public bool AllowInsecureCredentials { get; set; } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Creates a new instance of <see cref="DurableTaskSchedulerClientOptions"/> from a connection string. |
| 48 | + /// </summary> |
| 49 | + /// <param name="connectionString">The connection string to parse.</param> |
| 50 | + /// <returns>A new instance of <see cref="DurableTaskSchedulerClientOptions"/>.</returns> |
| 51 | + public static DurableTaskSchedulerClientOptions FromConnectionString(string connectionString) |
| 52 | + { |
| 53 | + return FromConnectionString(new DurableTaskSchedulerConnectionString(connectionString)); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Creates a gRPC channel for communicating with the Durable Task Scheduler service. |
| 58 | + /// </summary> |
| 59 | + /// <returns>A configured <see cref="GrpcChannel"/> instance that can be used to make gRPC calls.</returns> |
| 60 | + internal GrpcChannel CreateChannel() |
| 61 | + { |
| 62 | + Verify.NotNull(this.EndpointAddress, nameof(this.EndpointAddress)); |
| 63 | + Verify.NotNull(this.TaskHubName, nameof(this.TaskHubName)); |
| 64 | + string taskHubName = this.TaskHubName; |
| 65 | + string endpoint = !this.EndpointAddress.Contains("://") |
| 66 | + ? $"https://{this.EndpointAddress}" |
| 67 | + : this.EndpointAddress; |
| 68 | + AccessTokenCache? cache = |
| 69 | + this.Credential is not null |
| 70 | + ? new AccessTokenCache( |
| 71 | + this.Credential, |
| 72 | + new TokenRequestContext(new[] { $"{this.ResourceId}/.default" }), |
| 73 | + TimeSpan.FromMinutes(5)) |
| 74 | + : null; |
| 75 | + CallCredentials managedBackendCreds = CallCredentials.FromInterceptor( |
| 76 | + async (context, metadata) => |
| 77 | + { |
| 78 | + metadata.Add("taskhub", taskHubName); |
| 79 | + if (cache == null) |
| 80 | + { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + AccessToken token = await cache.GetTokenAsync(context.CancellationToken); |
| 85 | + metadata.Add("Authorization", $"Bearer {token.Token}"); |
| 86 | + }); |
| 87 | + |
| 88 | + // Production will use HTTPS, but local testing will use HTTP |
| 89 | + ChannelCredentials channelCreds = endpoint.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ? |
| 90 | + ChannelCredentials.SecureSsl : |
| 91 | + ChannelCredentials.Insecure; |
| 92 | + return GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions |
| 93 | + { |
| 94 | + Credentials = ChannelCredentials.Create(channelCreds, managedBackendCreds), |
| 95 | + UnsafeUseInsecureChannelCallCredentials = this.AllowInsecureCredentials, |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Creates a new instance of <see cref="DurableTaskSchedulerClientOptions"/> from a parsed connection string. |
| 101 | + /// </summary> |
| 102 | + /// <param name="connectionString">The connection string to parse.</param> |
| 103 | + /// <returns>A new instance of <see cref="DurableTaskSchedulerClientOptions"/>.</returns> |
| 104 | + internal static DurableTaskSchedulerClientOptions FromConnectionString( |
| 105 | + DurableTaskSchedulerConnectionString connectionString) => new() |
| 106 | + { |
| 107 | + EndpointAddress = connectionString.Endpoint, |
| 108 | + TaskHubName = connectionString.TaskHubName, |
| 109 | + Credential = GetCredentialFromConnectionString(connectionString), |
| 110 | + }; |
| 111 | + |
| 112 | + static TokenCredential? GetCredentialFromConnectionString(DurableTaskSchedulerConnectionString connectionString) |
| 113 | + { |
| 114 | + string authType = connectionString.Authentication; |
| 115 | + |
| 116 | + // Parse the supported auth types, in a case-insensitive way and without spaces |
| 117 | + switch (authType.ToLowerInvariant()) |
| 118 | + { |
| 119 | + case "defaultazure": |
| 120 | + return new DefaultAzureCredential(); |
| 121 | + case "managedidentity": |
| 122 | + return new ManagedIdentityCredential(connectionString.ClientId); |
| 123 | + case "workloadidentity": |
| 124 | + var opts = new WorkloadIdentityCredentialOptions(); |
| 125 | + if (!string.IsNullOrEmpty(connectionString.ClientId)) |
| 126 | + { |
| 127 | + opts.ClientId = connectionString.ClientId; |
| 128 | + } |
| 129 | + |
| 130 | + if (!string.IsNullOrEmpty(connectionString.TenantId)) |
| 131 | + { |
| 132 | + opts.TenantId = connectionString.TenantId; |
| 133 | + } |
| 134 | + |
| 135 | + if (connectionString.AdditionallyAllowedTenants is not null) |
| 136 | + { |
| 137 | + foreach (string tenant in connectionString.AdditionallyAllowedTenants) |
| 138 | + { |
| 139 | + opts.AdditionallyAllowedTenants.Add(tenant); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return new WorkloadIdentityCredential(opts); |
| 144 | + case "environment": |
| 145 | + return new EnvironmentCredential(); |
| 146 | + case "azurecli": |
| 147 | + return new AzureCliCredential(); |
| 148 | + case "azurepowershell": |
| 149 | + return new AzurePowerShellCredential(); |
| 150 | + case "none": |
| 151 | + return null; |
| 152 | + default: |
| 153 | + throw new ArgumentException( |
| 154 | + $"The connection string contains an unsupported authentication type '{authType}'.", |
| 155 | + nameof(connectionString)); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments