| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Runtime.ExceptionServices; |
| | | 3 | | using AsiBackbone.Core.Signing; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Signing.ManagedKey; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides managed-key signing for AsiBackbone signing abstractions through a host-owned managed-key client. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// This service signs precomputed hashes only. It never requests or handles raw private key material. |
| | | 12 | | /// </remarks> |
| | | 13 | | public sealed class ManagedKeySigningService : IGovernanceSigningService |
| | | 14 | | { |
| | | 15 | | private const string ProviderKind = "managed-key"; |
| | | 16 | | |
| | | 17 | | private readonly ManagedKeySigningOptions options; |
| | | 18 | | private readonly IManagedKeySigningClient client; |
| | | 19 | | private readonly IManagedKeyRetryJitterSource jitterSource; |
| | | 20 | | private readonly IManagedKeyRetryDelay retryDelay; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="ManagedKeySigningService" /> class. |
| | | 24 | | /// </summary> |
| | | 25 | | public ManagedKeySigningService(ManagedKeySigningOptions options, IManagedKeySigningClient client) |
| | 39 | 26 | | : this( |
| | 39 | 27 | | options, |
| | 39 | 28 | | client, |
| | 39 | 29 | | SharedRandomManagedKeyRetryJitterSource.Instance, |
| | 39 | 30 | | SystemManagedKeyRetryDelay.Instance) |
| | | 31 | | { |
| | 39 | 32 | | } |
| | | 33 | | |
| | 41 | 34 | | internal ManagedKeySigningService( |
| | 41 | 35 | | ManagedKeySigningOptions options, |
| | 41 | 36 | | IManagedKeySigningClient client, |
| | 41 | 37 | | IManagedKeyRetryJitterSource jitterSource, |
| | 41 | 38 | | IManagedKeyRetryDelay retryDelay) |
| | | 39 | | { |
| | 41 | 40 | | ArgumentNullException.ThrowIfNull(options); |
| | 41 | 41 | | ArgumentNullException.ThrowIfNull(client); |
| | 41 | 42 | | ArgumentNullException.ThrowIfNull(jitterSource); |
| | 41 | 43 | | ArgumentNullException.ThrowIfNull(retryDelay); |
| | | 44 | | |
| | 41 | 45 | | options.Validate(); |
| | 41 | 46 | | this.options = options; |
| | 41 | 47 | | this.client = client; |
| | 41 | 48 | | this.jitterSource = jitterSource; |
| | 41 | 49 | | this.retryDelay = retryDelay; |
| | 41 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public async ValueTask<SigningResult> SignAsync( |
| | | 54 | | SigningRequest request, |
| | | 55 | | CancellationToken cancellationToken = default) |
| | | 56 | | { |
| | 32 | 57 | | ArgumentNullException.ThrowIfNull(request); |
| | 32 | 58 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 59 | | |
| | 32 | 60 | | ManagedKeySigningAttemptDiagnostics diagnostics = CreateAttemptDiagnostics(); |
| | 32 | 61 | | string? validationFailure = ValidateSigningRequest(request); |
| | 32 | 62 | | if (validationFailure is not null) |
| | | 63 | | { |
| | 6 | 64 | | return HandleValidationFailure(request, validationFailure, diagnostics); |
| | | 65 | | } |
| | | 66 | | |
| | 26 | 67 | | int attempt = 0; |
| | | 68 | | |
| | | 69 | | while (true) |
| | | 70 | | { |
| | | 71 | | try |
| | | 72 | | { |
| | 34 | 73 | | ManagedKeySignResult managedResult = await client |
| | 34 | 74 | | .SignAsync(CreateManagedKeyRequest(request), cancellationToken) |
| | 34 | 75 | | .ConfigureAwait(false); |
| | | 76 | | |
| | 17 | 77 | | ManagedKeyResponseMismatch? mismatch = ValidateSigningResponse(request, managedResult); |
| | | 78 | | |
| | 17 | 79 | | return mismatch is not null |
| | 17 | 80 | | ? HandleFailure( |
| | 17 | 81 | | request, |
| | 17 | 82 | | mismatch.FailureCode, |
| | 17 | 83 | | mismatch.Message, |
| | 17 | 84 | | attempt, |
| | 17 | 85 | | diagnostics, |
| | 17 | 86 | | new ManagedKeySigningException(mismatch.FailureCode, mismatch.Message, isRetryable: false)) |
| | 17 | 87 | | : CreateSignedResult(request, managedResult, attempt, diagnostics); |
| | | 88 | | } |
| | 13 | 89 | | catch (ManagedKeySigningException exception) when (exception.IsRetryable && attempt < options.MaxRetryAttemp |
| | | 90 | | { |
| | 8 | 91 | | diagnostics.RecordRetry(exception); |
| | 8 | 92 | | attempt++; |
| | 8 | 93 | | await DelayForRetryAsync(attempt, diagnostics, cancellationToken).ConfigureAwait(false); |
| | 8 | 94 | | } |
| | 5 | 95 | | catch (ManagedKeySigningException exception) |
| | | 96 | | { |
| | 5 | 97 | | return HandleFailure(request, exception.FailureCode, exception.Message, attempt, diagnostics, exception) |
| | | 98 | | } |
| | 1 | 99 | | catch (TimeoutException exception) |
| | | 100 | | { |
| | 1 | 101 | | return HandleFailure(request, "managedkey.signing.provider-unavailable", exception.GetType().Name, attem |
| | | 102 | | } |
| | 2 | 103 | | catch (InvalidOperationException exception) |
| | | 104 | | { |
| | 2 | 105 | | return HandleFailure(request, "managedkey.signing.failed", exception.GetType().Name, attempt, diagnostic |
| | | 106 | | } |
| | 1 | 107 | | catch (NotSupportedException exception) |
| | | 108 | | { |
| | 1 | 109 | | return HandleFailure(request, "managedkey.signing.unsupported", exception.GetType().Name, attempt, diagn |
| | | 110 | | } |
| | | 111 | | } |
| | 29 | 112 | | } |
| | | 113 | | |
| | | 114 | | private SigningResult HandleValidationFailure( |
| | | 115 | | SigningRequest request, |
| | | 116 | | string failureCode, |
| | | 117 | | ManagedKeySigningAttemptDiagnostics diagnostics) |
| | | 118 | | { |
| | 6 | 119 | | diagnostics.RecordValidationFailure(); |
| | | 120 | | |
| | 6 | 121 | | return !options.ReturnUnsignedOnFailure |
| | 6 | 122 | | ? throw new ManagedKeySigningException(failureCode, failureCode) |
| | 6 | 123 | | : CreateUnsignedFailureResult(request, failureCode, failureCode, retryAttempts: 0, diagnostics); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private SigningResult HandleFailure( |
| | | 127 | | SigningRequest request, |
| | | 128 | | string failureCode, |
| | | 129 | | string failureMessage, |
| | | 130 | | int retryAttempts, |
| | | 131 | | ManagedKeySigningAttemptDiagnostics diagnostics, |
| | | 132 | | Exception exception) |
| | | 133 | | { |
| | 12 | 134 | | diagnostics.RecordFailure(exception); |
| | | 135 | | |
| | 12 | 136 | | if (!options.ReturnUnsignedOnFailure) |
| | | 137 | | { |
| | 2 | 138 | | ExceptionDispatchInfo.Capture(exception).Throw(); |
| | | 139 | | } |
| | | 140 | | |
| | 10 | 141 | | return CreateUnsignedFailureResult(request, failureCode, failureMessage, retryAttempts, diagnostics); |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | private ManagedKeySignRequest CreateManagedKeyRequest(SigningRequest request) |
| | | 145 | | { |
| | 34 | 146 | | return new ManagedKeySignRequest( |
| | 34 | 147 | | request.SigningHash, |
| | 34 | 148 | | NormalizeHashAlgorithm(request.HashAlgorithm), |
| | 34 | 149 | | NormalizeRequired(options.SignatureAlgorithm, ManagedKeySigningOptions.DefaultSignatureAlgorithm), |
| | 34 | 150 | | ResolveKeyId(request), |
| | 34 | 151 | | ResolveKeyVersion(request), |
| | 34 | 152 | | request.Purpose, |
| | 34 | 153 | | request.Metadata) |
| | 34 | 154 | | { |
| | 34 | 155 | | SignatureInput = request.SignatureInput |
| | 34 | 156 | | }; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Rejects a provider response whose key identity or signature algorithm differs from what was requested. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <remarks> |
| | | 163 | | /// The signed result copies the provider's key identifier, key version, and signature algorithm into signing metada |
| | | 164 | | /// Without this comparison a provider that signed under a different key, a different key version, or a different |
| | | 165 | | /// algorithm produced an artifact that recorded the substitution as though it had been requested, and the artifact |
| | | 166 | | /// could also carry two disagreeing algorithm descriptors because the configured value is written separately. |
| | | 167 | | /// A key version is only compared when the request or configuration pinned one, so ordinary version resolution by t |
| | | 168 | | /// provider is still accepted. |
| | | 169 | | /// </remarks> |
| | | 170 | | private ManagedKeyResponseMismatch? ValidateSigningResponse(SigningRequest request, ManagedKeySignResult managedResu |
| | | 171 | | { |
| | 17 | 172 | | string requestedKeyId = ResolveKeyId(request); |
| | | 173 | | |
| | 17 | 174 | | if (!string.IsNullOrWhiteSpace(managedResult.KeyId) |
| | 17 | 175 | | && !string.Equals(managedResult.KeyId, requestedKeyId, StringComparison.Ordinal)) |
| | | 176 | | { |
| | 1 | 177 | | return new ManagedKeyResponseMismatch( |
| | 1 | 178 | | "managedkey.signing.key-mismatch", |
| | 1 | 179 | | "The managed key provider signed with a different key identifier than the one requested."); |
| | | 180 | | } |
| | | 181 | | |
| | 16 | 182 | | string? requestedKeyVersion = ResolveKeyVersion(request); |
| | | 183 | | |
| | 16 | 184 | | if (requestedKeyVersion is not null |
| | 16 | 185 | | && !string.IsNullOrWhiteSpace(managedResult.KeyVersion) |
| | 16 | 186 | | && !string.Equals(managedResult.KeyVersion, requestedKeyVersion, StringComparison.Ordinal)) |
| | | 187 | | { |
| | 1 | 188 | | return new ManagedKeyResponseMismatch( |
| | 1 | 189 | | "managedkey.signing.key-version-mismatch", |
| | 1 | 190 | | "The managed key provider signed with a different key version than the one requested."); |
| | | 191 | | } |
| | | 192 | | |
| | 15 | 193 | | string configuredAlgorithm = NormalizeRequired(options.SignatureAlgorithm, ManagedKeySigningOptions.DefaultSigna |
| | | 194 | | |
| | 15 | 195 | | return !string.IsNullOrWhiteSpace(managedResult.SignatureAlgorithm) |
| | 15 | 196 | | && !string.Equals(managedResult.SignatureAlgorithm, configuredAlgorithm, StringComparison.Ordinal) |
| | 15 | 197 | | ? new ManagedKeyResponseMismatch( |
| | 15 | 198 | | "managedkey.signing.algorithm-mismatch", |
| | 15 | 199 | | "The managed key provider signed with a different signature algorithm than the configured one.") |
| | 15 | 200 | | : null; |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | private sealed record ManagedKeyResponseMismatch(string FailureCode, string Message); |
| | | 204 | | |
| | | 205 | | private SigningResult CreateSignedResult( |
| | | 206 | | SigningRequest request, |
| | | 207 | | ManagedKeySignResult managedResult, |
| | | 208 | | int retryAttempts, |
| | | 209 | | ManagedKeySigningAttemptDiagnostics diagnostics) |
| | | 210 | | { |
| | 14 | 211 | | string resolvedKeyVersion = managedResult.KeyVersion ?? ResolveKeyVersion(request) ?? string.Empty; |
| | 14 | 212 | | Dictionary<string, string> metadata = CreateBaseMetadata(request, retryAttempts, providerAttempts: retryAttempts |
| | 14 | 213 | | metadata["signing_status"] = "signed"; |
| | | 214 | | |
| | 14 | 215 | | if (managedResult.ProviderOperationId is not null) |
| | | 216 | | { |
| | 7 | 217 | | metadata["provider_operation_id"] = managedResult.ProviderOperationId; |
| | | 218 | | } |
| | | 219 | | |
| | 42 | 220 | | foreach (KeyValuePair<string, string> item in managedResult.Metadata) |
| | | 221 | | { |
| | 7 | 222 | | if (!IsSafeProviderMetadataKey(item.Key) || IsReservedDiagnosticMetadataKey(item.Key)) |
| | | 223 | | { |
| | | 224 | | continue; |
| | | 225 | | } |
| | | 226 | | |
| | 7 | 227 | | metadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 228 | | } |
| | | 229 | | |
| | 14 | 230 | | var signingMetadata = SigningMetadata.Create( |
| | 14 | 231 | | signingHash: request.SigningHash, |
| | 14 | 232 | | hashAlgorithm: NormalizeHashAlgorithm(request.HashAlgorithm), |
| | 14 | 233 | | signature: managedResult.Signature, |
| | 14 | 234 | | signatureAlgorithm: managedResult.SignatureAlgorithm, |
| | 14 | 235 | | keyId: managedResult.KeyId, |
| | 14 | 236 | | keyVersion: string.IsNullOrWhiteSpace(resolvedKeyVersion) ? null : resolvedKeyVersion, |
| | 14 | 237 | | provider: NormalizeRequired(options.ProviderName, ManagedKeySigningOptions.DefaultProviderName), |
| | 14 | 238 | | signedUtc: managedResult.SignedUtc, |
| | 14 | 239 | | metadata: metadata); |
| | | 240 | | |
| | 14 | 241 | | return SigningResult.FromMetadata(signingMetadata); |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | private SigningResult CreateUnsignedFailureResult( |
| | | 245 | | SigningRequest request, |
| | | 246 | | string failureCode, |
| | | 247 | | string failureMessage, |
| | | 248 | | int retryAttempts, |
| | | 249 | | ManagedKeySigningAttemptDiagnostics diagnostics) |
| | | 250 | | { |
| | 15 | 251 | | int providerAttempts = diagnostics.HasValidationFailure ? 0 : retryAttempts + 1; |
| | 15 | 252 | | Dictionary<string, string> metadata = CreateBaseMetadata(request, retryAttempts, providerAttempts, diagnostics); |
| | 15 | 253 | | metadata["signing_status"] = "failed"; |
| | 15 | 254 | | metadata["failure_code"] = failureCode; |
| | 15 | 255 | | metadata["failure_message"] = failureMessage; |
| | | 256 | | |
| | 15 | 257 | | var signingMetadata = SigningMetadata.Create( |
| | 15 | 258 | | signingHash: request.SigningHash, |
| | 15 | 259 | | hashAlgorithm: NormalizeHashAlgorithm(request.HashAlgorithm), |
| | 15 | 260 | | keyId: ResolveKeyId(request), |
| | 15 | 261 | | keyVersion: ResolveKeyVersion(request), |
| | 15 | 262 | | provider: NormalizeRequired(options.ProviderName, ManagedKeySigningOptions.DefaultProviderName), |
| | 15 | 263 | | metadata: metadata); |
| | | 264 | | |
| | 15 | 265 | | return SigningResult.FromMetadata(signingMetadata); |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | private Dictionary<string, string> CreateBaseMetadata( |
| | | 269 | | SigningRequest request, |
| | | 270 | | int retryAttempts, |
| | | 271 | | int providerAttempts, |
| | | 272 | | ManagedKeySigningAttemptDiagnostics diagnostics) |
| | | 273 | | { |
| | 29 | 274 | | Dictionary<string, string> metadata = new(StringComparer.Ordinal); |
| | | 275 | | |
| | 128 | 276 | | foreach (KeyValuePair<string, string> item in request.Metadata) |
| | | 277 | | { |
| | 35 | 278 | | if (string.IsNullOrWhiteSpace(item.Key) || IsReservedDiagnosticMetadataKey(item.Key)) |
| | | 279 | | { |
| | | 280 | | continue; |
| | | 281 | | } |
| | | 282 | | |
| | 10 | 283 | | metadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 284 | | } |
| | | 285 | | |
| | 29 | 286 | | metadata["provider_kind"] = ProviderKind; |
| | 29 | 287 | | metadata["remote_key_material"] = "true"; |
| | 29 | 288 | | metadata["raw_private_key_loaded"] = "false"; |
| | 29 | 289 | | metadata["retry_attempts"] = retryAttempts.ToString(CultureInfo.InvariantCulture); |
| | 29 | 290 | | metadata["provider_attempts"] = providerAttempts.ToString(CultureInfo.InvariantCulture); |
| | 29 | 291 | | metadata["max_retry_attempts"] = diagnostics.MaxRetryAttempts.ToString(CultureInfo.InvariantCulture); |
| | 29 | 292 | | metadata["retry_delay_milliseconds"] = diagnostics.BaseRetryDelay.TotalMilliseconds.ToString("0.###", CultureInf |
| | 29 | 293 | | metadata["max_retry_delay_milliseconds"] = diagnostics.MaxRetryDelay.TotalMilliseconds.ToString("0.###", Culture |
| | 29 | 294 | | metadata["last_retry_delay_milliseconds"] = diagnostics.LastRetryDelay.TotalMilliseconds.ToString("0.###", Cultu |
| | 29 | 295 | | metadata["total_retry_delay_milliseconds"] = diagnostics.TotalRetryDelay.TotalMilliseconds.ToString("0.###", Cul |
| | 29 | 296 | | metadata["retry_delay_count"] = diagnostics.RetryDelayCount.ToString(CultureInfo.InvariantCulture); |
| | 29 | 297 | | metadata["retry_backoff_strategy"] = ManagedKeyRetryBackoff.StrategyName; |
| | 29 | 298 | | metadata["retry_delay_configured"] = ToMetadataBoolean(diagnostics.BaseRetryDelay > TimeSpan.Zero); |
| | 29 | 299 | | metadata["retry_delay_applied"] = ToMetadataBoolean(diagnostics.RetryDelayCount > 0); |
| | 29 | 300 | | metadata["signature_algorithm"] = NormalizeRequired(options.SignatureAlgorithm, ManagedKeySigningOptions.Default |
| | | 301 | | |
| | 29 | 302 | | if (diagnostics.LastRetryFailureCode is not null) |
| | | 303 | | { |
| | 6 | 304 | | metadata["last_retry_failure_code"] = diagnostics.LastRetryFailureCode; |
| | | 305 | | } |
| | | 306 | | |
| | 29 | 307 | | if (diagnostics.LastRetryFailureExceptionType is not null) |
| | | 308 | | { |
| | 6 | 309 | | metadata["last_retry_failure_exception_type"] = diagnostics.LastRetryFailureExceptionType; |
| | | 310 | | } |
| | | 311 | | |
| | 29 | 312 | | if (diagnostics.FailureExceptionType is not null) |
| | | 313 | | { |
| | 10 | 314 | | metadata["failure_exception_type"] = diagnostics.FailureExceptionType; |
| | | 315 | | } |
| | | 316 | | |
| | 29 | 317 | | if (diagnostics.FailureRetryable is not null) |
| | | 318 | | { |
| | 10 | 319 | | metadata["failure_retryable"] = ToMetadataBoolean(diagnostics.FailureRetryable.Value); |
| | | 320 | | } |
| | | 321 | | |
| | 29 | 322 | | if (request.Purpose is not null) |
| | | 323 | | { |
| | 14 | 324 | | metadata["purpose"] = request.Purpose; |
| | | 325 | | } |
| | | 326 | | |
| | 29 | 327 | | return metadata; |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | private string? ValidateSigningRequest(SigningRequest request) |
| | | 331 | | { |
| | 32 | 332 | | if (!IsSupportedHashAlgorithm(request.HashAlgorithm)) |
| | | 333 | | { |
| | 2 | 334 | | return "managedkey.signing.hash-algorithm-unsupported"; |
| | | 335 | | } |
| | | 336 | | |
| | 30 | 337 | | string configuredKeyId = NormalizeRequired(options.KeyId, string.Empty); |
| | 30 | 338 | | if (request.KeyId is not null && !string.Equals(request.KeyId, configuredKeyId, StringComparison.Ordinal)) |
| | | 339 | | { |
| | 1 | 340 | | return "managedkey.signing.key-mismatch"; |
| | | 341 | | } |
| | | 342 | | |
| | 29 | 343 | | string? configuredKeyVersion = NormalizeOptional(options.KeyVersion); |
| | 29 | 344 | | return options.RequireKeyVersion && request.KeyVersion is null && configuredKeyVersion is null |
| | 29 | 345 | | ? "managedkey.signing.key-version-missing" |
| | 29 | 346 | | : request.KeyVersion is not null |
| | 29 | 347 | | && configuredKeyVersion is not null |
| | 29 | 348 | | && !string.Equals(request.KeyVersion, configuredKeyVersion, StringComparison.Ordinal) |
| | 29 | 349 | | ? "managedkey.signing.key-version-mismatch" |
| | 29 | 350 | | : null; |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | private string ResolveKeyId(SigningRequest request) |
| | | 354 | | { |
| | 66 | 355 | | return request.KeyId ?? NormalizeRequired(options.KeyId, string.Empty); |
| | | 356 | | } |
| | | 357 | | |
| | | 358 | | private string? ResolveKeyVersion(SigningRequest request) |
| | | 359 | | { |
| | 66 | 360 | | return request.KeyVersion ?? NormalizeOptional(options.KeyVersion); |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | private string NormalizeHashAlgorithm(string? hashAlgorithm) |
| | | 364 | | { |
| | 127 | 365 | | string normalized = string.IsNullOrWhiteSpace(hashAlgorithm) |
| | 127 | 366 | | ? options.HashAlgorithm |
| | 127 | 367 | | : hashAlgorithm.Trim(); |
| | | 368 | | |
| | 127 | 369 | | return normalized.Equals("SHA256", StringComparison.OrdinalIgnoreCase) |
| | 127 | 370 | | ? ManagedKeySigningOptions.DefaultHashAlgorithm |
| | 127 | 371 | | : normalized.Equals(ManagedKeySigningOptions.DefaultHashAlgorithm, StringComparison.OrdinalIgnoreCase) |
| | 127 | 372 | | ? ManagedKeySigningOptions.DefaultHashAlgorithm |
| | 127 | 373 | | : normalized; |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | private bool IsSupportedHashAlgorithm(string? hashAlgorithm) |
| | | 377 | | { |
| | 32 | 378 | | return NormalizeHashAlgorithm(hashAlgorithm).Equals( |
| | 32 | 379 | | NormalizeHashAlgorithm(options.HashAlgorithm), |
| | 32 | 380 | | StringComparison.OrdinalIgnoreCase); |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | private async ValueTask DelayForRetryAsync( |
| | | 384 | | int retryAttempt, |
| | | 385 | | ManagedKeySigningAttemptDiagnostics diagnostics, |
| | | 386 | | CancellationToken cancellationToken) |
| | | 387 | | { |
| | 8 | 388 | | TimeSpan delay = ManagedKeyRetryBackoff.CalculateDelay( |
| | 8 | 389 | | options.RetryDelay, |
| | 8 | 390 | | options.MaxRetryDelay, |
| | 8 | 391 | | retryAttempt, |
| | 8 | 392 | | jitterSource.NextDouble(), |
| | 8 | 393 | | diagnostics.LastRetryDelay); |
| | | 394 | | |
| | 8 | 395 | | if (delay <= TimeSpan.Zero) |
| | | 396 | | { |
| | 3 | 397 | | return; |
| | | 398 | | } |
| | | 399 | | |
| | 5 | 400 | | diagnostics.RecordRetryDelay(delay); |
| | 5 | 401 | | await retryDelay.DelayAsync(delay, cancellationToken).ConfigureAwait(false); |
| | 8 | 402 | | } |
| | | 403 | | |
| | | 404 | | private ManagedKeySigningAttemptDiagnostics CreateAttemptDiagnostics() |
| | | 405 | | { |
| | 32 | 406 | | return new ManagedKeySigningAttemptDiagnostics( |
| | 32 | 407 | | options.MaxRetryAttempts, |
| | 32 | 408 | | options.RetryDelay, |
| | 32 | 409 | | options.MaxRetryDelay); |
| | | 410 | | } |
| | | 411 | | |
| | | 412 | | private static bool IsSafeProviderMetadataKey(string key) |
| | | 413 | | { |
| | 7 | 414 | | return !string.IsNullOrWhiteSpace(key) |
| | 7 | 415 | | && !key.Contains("secret", StringComparison.OrdinalIgnoreCase) |
| | 7 | 416 | | && !key.Contains("token", StringComparison.OrdinalIgnoreCase) |
| | 7 | 417 | | && !key.Contains("credential", StringComparison.OrdinalIgnoreCase) |
| | 7 | 418 | | && !key.Contains("private", StringComparison.OrdinalIgnoreCase) |
| | 7 | 419 | | && !key.Contains("connection", StringComparison.OrdinalIgnoreCase); |
| | | 420 | | } |
| | | 421 | | |
| | | 422 | | private static bool IsReservedDiagnosticMetadataKey(string key) |
| | | 423 | | { |
| | 42 | 424 | | return ManagedKeyDiagnosticMetadataKeyClassifier.IsReserved(key); |
| | | 425 | | } |
| | | 426 | | |
| | | 427 | | private static string NormalizeRequired(string? value, string fallback) |
| | | 428 | | { |
| | 137 | 429 | | return string.IsNullOrWhiteSpace(value) ? fallback : value.Trim(); |
| | | 430 | | } |
| | | 431 | | |
| | | 432 | | private static string? NormalizeOptional(string? value) |
| | | 433 | | { |
| | 36 | 434 | | return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| | | 435 | | } |
| | | 436 | | |
| | | 437 | | private static string ToMetadataBoolean(bool value) |
| | | 438 | | { |
| | 68 | 439 | | return value ? "true" : "false"; |
| | | 440 | | } |
| | | 441 | | |
| | 32 | 442 | | private sealed class ManagedKeySigningAttemptDiagnostics( |
| | 32 | 443 | | int maxRetryAttempts, |
| | 32 | 444 | | TimeSpan baseRetryDelay, |
| | 32 | 445 | | TimeSpan maxRetryDelay) |
| | | 446 | | { |
| | | 447 | | public int MaxRetryAttempts { get; } = maxRetryAttempts; |
| | | 448 | | |
| | | 449 | | public TimeSpan BaseRetryDelay { get; } = baseRetryDelay; |
| | | 450 | | |
| | | 451 | | public TimeSpan MaxRetryDelay { get; } = maxRetryDelay; |
| | | 452 | | |
| | | 453 | | public TimeSpan LastRetryDelay { get; private set; } |
| | | 454 | | |
| | | 455 | | public TimeSpan TotalRetryDelay { get; private set; } |
| | | 456 | | |
| | | 457 | | public int RetryDelayCount { get; private set; } |
| | | 458 | | |
| | | 459 | | public bool HasValidationFailure { get; private set; } |
| | | 460 | | |
| | | 461 | | public string? LastRetryFailureCode { get; private set; } |
| | | 462 | | |
| | | 463 | | public string? LastRetryFailureExceptionType { get; private set; } |
| | | 464 | | |
| | | 465 | | public string? FailureExceptionType { get; private set; } |
| | | 466 | | |
| | | 467 | | public bool? FailureRetryable { get; private set; } |
| | | 468 | | |
| | | 469 | | public void RecordValidationFailure() |
| | | 470 | | { |
| | 6 | 471 | | HasValidationFailure = true; |
| | 6 | 472 | | } |
| | | 473 | | |
| | | 474 | | public void RecordRetry(ManagedKeySigningException exception) |
| | | 475 | | { |
| | 8 | 476 | | LastRetryFailureCode = exception.FailureCode; |
| | 8 | 477 | | LastRetryFailureExceptionType = exception.GetType().Name; |
| | 8 | 478 | | } |
| | | 479 | | |
| | | 480 | | public void RecordRetryDelay(TimeSpan delay) |
| | | 481 | | { |
| | 5 | 482 | | LastRetryDelay = delay; |
| | 5 | 483 | | TotalRetryDelay += delay; |
| | 5 | 484 | | RetryDelayCount++; |
| | 5 | 485 | | } |
| | | 486 | | |
| | | 487 | | public void RecordFailure(Exception exception) |
| | | 488 | | { |
| | 12 | 489 | | FailureExceptionType = exception.GetType().Name; |
| | 12 | 490 | | FailureRetryable = exception is ManagedKeySigningException managedKeyException && managedKeyException.IsRetr |
| | 12 | 491 | | } |
| | | 492 | | } |
| | | 493 | | } |