Path: redb-route/redb.Route.Sqs/AwsClientSupport.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Sqs/AwsClientSupport.cs ---- using System.Text; using System.Text.Json; using Amazon; using Amazon.Runtime; namespace redb.Route.Sqs; /// /// Shared AWS client plumbing for the SQS and SNS endpoints: credential resolution and the common /// settings (region vs explicit service URL, timeouts, retries, proxy). /// Mirrors redb.Route.S3 so behaviour is identical across the AWS connectors. /// internal static class AwsClientSupport { /// /// The region and service URL a built client talks to, for the "client created" log: read from the client's own /// configuration, so a connection factory's values show, not the endpoint's defaults. /// internal static (string Region, string ServiceUrl) Target(IClientConfig config) => ( config.RegionEndpoint?.SystemName ?? (string.IsNullOrEmpty(config.AuthenticationRegion) ? "(default)" : config.AuthenticationRegion), string.IsNullOrEmpty(config.ServiceURL) ? "(default)" : redb.Route.Abstractions.EndpointUri.Sanitize(config.ServiceURL)); /// /// Resolves AWS credentials with the same precedence as the S3 connector: /// default provider chain → named profile → static session token → static access/secret keys. /// public static AWSCredentials ResolveCredentials(AwsEndpointOptions o) { if (o.UseDefaultCredentialsProvider) // SDK v4: the non-deprecated default-chain resolver (env -> profile -> IMDS etc.) return Amazon.Runtime.Credentials.DefaultAWSCredentialsIdentityResolver.GetCredentials(); if (!string.IsNullOrEmpty(o.ProfileName)) { var chain = new Amazon.Runtime.CredentialManagement.CredentialProfileStoreChain(); if (chain.TryGetAWSCredentials(o.ProfileName, out var profileCreds)) return profileCreds; throw new InvalidOperationException($"AWS profile '{o.ProfileName}' not found."); } if (!string.IsNullOrEmpty(o.SessionToken)) return new SessionAWSCredentials(o.AccessKey, o.SecretKey, o.SessionToken); return new BasicAWSCredentials(o.AccessKey, o.SecretKey); } /// /// Applies region / explicit service URL, timeout, retry and proxy settings onto an AWS service /// config. ServiceUrl and RegionEndpoint are mutually exclusive — an explicit URL /// (LocalStack / ElasticMQ) wins and the region is kept only for request signing. /// public static void ApplyCommonConfig(ClientConfig config, AwsEndpointOptions o) { config.Timeout = TimeSpan.FromMilliseconds(o.ConnectionTimeout); config.MaxErrorRetry = o.RetryCount; if (!string.IsNullOrEmpty(o.ServiceUrl)) { config.ServiceURL = o.ServiceUrl; config.AuthenticationRegion = o.Region; } else { config.RegionEndpoint = RegionEndpoint.GetBySystemName(o.Region); } if (!string.IsNullOrEmpty(o.ProxyHost) && o.ProxyPort > 0) { config.ProxyHost = o.ProxyHost; config.ProxyPort = o.ProxyPort; } } /// /// Maps an exchange header to the message-attribute name a producer should send, or null to /// skip it. User headers pass through unchanged; a received SQS attribute (re-exposed under /// redbSqs.attr.) is forwarded with the prefix stripped (so sqs→sqs/sns bridges preserve /// attributes); framework headers (redbSqs.* / redbSns.*) are dropped. /// /// The most message attributes SQS and SNS accept on one message. public const int MaxMessageAttributes = 10; /// /// Fails the send when the message carries more attributes than SQS/SNS accept, naming how many came from the /// exchange headers and how many from the trace context, instead of letting the service refuse it with a bare count. /// Nothing is dropped to make it fit: which attribute to lose is not the connector's call. /// /// Attributes on the message, trace context included. /// Attributes mapped from the exchange headers. /// The producer, for the message. public static void EnsureAttributeLimit(int total, int fromHeaders, string producer) { if (total <= MaxMessageAttributes) return; throw new InvalidOperationException( $"'{producer}' would send {total} message attributes, at most {MaxMessageAttributes} are accepted: " + $"{fromHeaders} from the exchange headers and {total - fromHeaders} for the trace context " + "(traceparent, tracestate, baggage). Nothing was sent. Remove the headers the next hop does not need " + "before this step (.RemoveHeaders(...))."); } public static string? MapHeaderToAttributeName(string key) { if (key.StartsWith(SqsHeaders.MessageAttributePrefix, StringComparison.Ordinal)) return key[SqsHeaders.MessageAttributePrefix.Length..]; if (SqsHeaders.IsSqsHeader(key) || SnsHeaders.IsSnsHeader(key)) return null; return key; } /// /// Coerces an exchange body to the text payload SQS/SNS carry: strings pass through, byte[] /// is decoded as UTF-8, everything else is JSON-serialized. /// public static string ResolveTextBody(object? body) => body switch { null => "", string s => s, byte[] bytes => Encoding.UTF8.GetString(bytes), _ => JsonSerializer.Serialize(body), }; }