|
| 1 | +using System.Security.Cryptography.X509Certificates; |
| 2 | +using Azure.Core; |
| 3 | +using Azure.Identity; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using Microsoft.Extensions.Hosting; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using OpenTelemetry.Metrics; |
| 8 | +using OpenTelemetry.Resources; |
| 9 | +using Orleans.Connections.Security; |
| 10 | +using Orleans.Connections.Security.Entra; |
| 11 | +using Orleans.Hosting; |
| 12 | + |
| 13 | +namespace Orleans.Docs.ConnectionSecurity; |
| 14 | + |
| 15 | +internal static class ConnectionAuthenticationExamples |
| 16 | +{ |
| 17 | + public static TokenCredential CreateCredential(ConnectionSecurityOptions options) |
| 18 | + { |
| 19 | + // <ExplicitCredential> |
| 20 | + TokenCredential credential = new WorkloadIdentityCredential( |
| 21 | + new WorkloadIdentityCredentialOptions |
| 22 | + { |
| 23 | + TenantId = options.Entra.TenantId, |
| 24 | + ClientId = options.Entra.WorkloadClientId, |
| 25 | + TokenFilePath = options.Entra.FederatedTokenFile, |
| 26 | + }); |
| 27 | + // </ExplicitCredential> |
| 28 | + |
| 29 | + return credential; |
| 30 | + } |
| 31 | + |
| 32 | + public static void ConfigureSilo( |
| 33 | + ISiloBuilder siloBuilder, |
| 34 | + ConnectionSecurityOptions options, |
| 35 | + TokenCredential credential, |
| 36 | + X509Certificate2 siloCertificate) |
| 37 | + { |
| 38 | + // <AuthenticatedSiloConnections> |
| 39 | + siloBuilder.UseAuthenticatedSiloConnections( |
| 40 | + tls => |
| 41 | + { |
| 42 | + tls.LocalCertificate = siloCertificate; |
| 43 | + tls.RemoteCertificateMode = RemoteCertificateMode.RequireCertificate; |
| 44 | + tls.ClientCertificateMode = RemoteCertificateMode.RequireCertificate; |
| 45 | + tls.CheckCertificateRevocation = true; |
| 46 | + }, |
| 47 | + authentication => |
| 48 | + { |
| 49 | + ConfigureAuthentication( |
| 50 | + authentication, |
| 51 | + options, |
| 52 | + credential, |
| 53 | + options.Entra.AllowedSiloCallerClientIds, |
| 54 | + "Orleans.Silo.Connect"); |
| 55 | + }); |
| 56 | + // </AuthenticatedSiloConnections> |
| 57 | + |
| 58 | + // <AuthenticatedClientGateway> |
| 59 | + siloBuilder.UseAuthenticatedClientConnections( |
| 60 | + tls => |
| 61 | + { |
| 62 | + tls.LocalCertificate = siloCertificate; |
| 63 | + tls.RemoteCertificateMode = RemoteCertificateMode.NoCertificate; |
| 64 | + }, |
| 65 | + authentication => |
| 66 | + { |
| 67 | + ConfigureAuthentication( |
| 68 | + authentication, |
| 69 | + options, |
| 70 | + credential, |
| 71 | + options.Entra.AllowedClientCallerClientIds, |
| 72 | + "Orleans.Client.Connect"); |
| 73 | + }); |
| 74 | + // </AuthenticatedClientGateway> |
| 75 | + } |
| 76 | + |
| 77 | + public static void ConfigureClient( |
| 78 | + IClientBuilder clientBuilder, |
| 79 | + ConnectionSecurityOptions options, |
| 80 | + TokenCredential credential) |
| 81 | + { |
| 82 | + // <AuthenticatedClient> |
| 83 | + clientBuilder.UseAuthenticatedClientConnections( |
| 84 | + tls => |
| 85 | + { |
| 86 | + tls.CheckCertificateRevocation = true; |
| 87 | + }, |
| 88 | + authentication => |
| 89 | + { |
| 90 | + ConfigureAuthentication( |
| 91 | + authentication, |
| 92 | + options, |
| 93 | + credential, |
| 94 | + options.Entra.AllowedClientCallerClientIds, |
| 95 | + "Orleans.Client.Connect"); |
| 96 | + }); |
| 97 | + // </AuthenticatedClient> |
| 98 | + } |
| 99 | + |
| 100 | + public static void ConfigureDiagnostics( |
| 101 | + HostApplicationBuilder builder, |
| 102 | + bool exportToOtlp) |
| 103 | + { |
| 104 | + // <FixedDiagnostics> |
| 105 | + builder.Logging.ClearProviders(); |
| 106 | + builder.Logging.AddJsonConsole(console => |
| 107 | + { |
| 108 | + console.TimestampFormat = "O"; |
| 109 | + console.JsonWriterOptions = new() { Indented = false }; |
| 110 | + }); |
| 111 | + builder.Logging.AddFilter("Orleans.Connections.Security", LogLevel.Information); |
| 112 | + builder.Logging.AddFilter("Azure.Identity", LogLevel.Warning); |
| 113 | + |
| 114 | + builder.Services.AddOpenTelemetry() |
| 115 | + .ConfigureResource(resource => resource.AddService( |
| 116 | + serviceName: "authenticated-orleans-silo", |
| 117 | + serviceInstanceId: Environment.MachineName)) |
| 118 | + .WithMetrics(metrics => |
| 119 | + { |
| 120 | + metrics.AddMeter("Microsoft.Orleans.Connections.Security"); |
| 121 | + |
| 122 | + if (exportToOtlp) |
| 123 | + { |
| 124 | + metrics.AddOtlpExporter(); |
| 125 | + } |
| 126 | + }); |
| 127 | + // </FixedDiagnostics> |
| 128 | + } |
| 129 | + |
| 130 | + private static void ConfigureAuthentication( |
| 131 | + SiloConnectionAuthenticationBuilder authentication, |
| 132 | + ConnectionSecurityOptions options, |
| 133 | + TokenCredential credential, |
| 134 | + IEnumerable<string> allowedCallerClientIds, |
| 135 | + string requiredRole) |
| 136 | + { |
| 137 | + authentication.Mode = options.AuthenticationMode; |
| 138 | + authentication.TargetHost = options.Certificate.TargetHost; |
| 139 | + authentication.TokenExchangeTimeout = TimeSpan.FromSeconds(10); |
| 140 | + authentication.MaxTokenSize = 16 * 1024; |
| 141 | + authentication.MaxConcurrentInboundAuthentications = 256; |
| 142 | + authentication.MaxConcurrentOutboundAuthentications = 256; |
| 143 | + authentication.MaxPendingInboundAuthentications = 256; |
| 144 | + authentication.MaxPendingOutboundAuthentications = 256; |
| 145 | + authentication.MinimumRemainingTokenLifetime = TimeSpan.FromMinutes(2); |
| 146 | + |
| 147 | + authentication.UseEntra( |
| 148 | + credential, |
| 149 | + entra => |
| 150 | + { |
| 151 | + entra.Authority = options.Entra.Authority; |
| 152 | + entra.TokenScope = $"{options.Entra.Audience}/.default"; |
| 153 | + entra.ValidAudiences.Add(options.Entra.Audience); |
| 154 | + entra.ValidTenantIds.Add(options.Entra.TenantId); |
| 155 | + entra.ClusterAudienceFormat = |
| 156 | + $"api://{options.Entra.ResourceApplicationId}/{{0}}"; |
| 157 | + |
| 158 | + foreach (var clientId in allowedCallerClientIds) |
| 159 | + { |
| 160 | + entra.AllowedClientIds.Add(clientId); |
| 161 | + } |
| 162 | + |
| 163 | + entra.RequiredRoles.Add(requiredRole); |
| 164 | + }); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +internal sealed class ConnectionSecurityOptions |
| 169 | +{ |
| 170 | + public SiloConnectionAuthenticationMode AuthenticationMode { get; init; } |
| 171 | + |
| 172 | + public CertificateOptions Certificate { get; init; } = new(); |
| 173 | + |
| 174 | + public EntraOptions Entra { get; init; } = new(); |
| 175 | +} |
| 176 | + |
| 177 | +internal sealed class CertificateOptions |
| 178 | +{ |
| 179 | + public string TargetHost { get; init; } = ""; |
| 180 | +} |
| 181 | + |
| 182 | +internal sealed class EntraOptions |
| 183 | +{ |
| 184 | + public string TenantId { get; init; } = ""; |
| 185 | + |
| 186 | + public string ResourceApplicationId { get; init; } = ""; |
| 187 | + |
| 188 | + public string WorkloadClientId { get; init; } = ""; |
| 189 | + |
| 190 | + public string FederatedTokenFile { get; init; } = ""; |
| 191 | + |
| 192 | + public string Audience { get; init; } = ""; |
| 193 | + |
| 194 | + public Uri Authority { get; init; } = null!; |
| 195 | + |
| 196 | + public string[] AllowedSiloCallerClientIds { get; init; } = []; |
| 197 | + |
| 198 | + public string[] AllowedClientCallerClientIds { get; init; } = []; |
| 199 | +} |
0 commit comments