/* SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ using System; using Amazon; using Amazon.Runtime; namespace OpenSearch.Net.Auth.AwsSigV4 { /// /// An implementation that performs AWS SigV4 request signing, for performing authentication with Amazon Managed OpenSearch. /// public class AwsSigV4HttpConnection : HttpConnection { public const string OpenSearchService = "es"; public const string OpenSearchServerlessService = "aoss"; private readonly AWSCredentials _credentials; private readonly RegionEndpoint _region; private readonly string _service; private readonly IDateTimeProvider _dateTimeProvider; /// /// Construct a new connection discovering both the credentials and region from the environment. /// /// The service code to use when signing, defaults to the service code for the Amazon OpenSearch Service ("es"). /// The date time proved to use, safe to pass null to use the default /// public AwsSigV4HttpConnection(string service = OpenSearchService, IDateTimeProvider dateTimeProvider = null) : this(null, null, service, dateTimeProvider) { } /// /// Construct a new connection configured with the specified credentials and using the region discovered from the environment. /// /// The credentials to use when signing. /// The service code to use when signing, defaults to the service code for the Amazon OpenSearch Service ("es"). /// The date time proved to use, safe to pass null to use the default /// public AwsSigV4HttpConnection(AWSCredentials credentials, string service = OpenSearchService, IDateTimeProvider dateTimeProvider = null) : this(credentials, null, service, dateTimeProvider) { } /// /// Construct a new connection configured with a specified region and using credentials discovered from the environment. /// /// The region to use when signing. /// The service code to use when signing, defaults to the service code for the Amazon OpenSearch Service ("es"). /// The date time proved to use, safe to pass null to use the default /// public AwsSigV4HttpConnection(RegionEndpoint region, string service = OpenSearchService, IDateTimeProvider dateTimeProvider = null) : this(null, region, service, dateTimeProvider) { } /// /// Construct a new connection configured with the given credentials and region. /// /// The credentials to use when signing, or null to have them discovered automatically by the AWS SDK. /// The region to use when signing, or null to have it discovered automatically by the AWS SDK. /// The service code to use when signing, defaults to the service code for the Amazon OpenSearch Service ("es"). /// The date time proved to use, safe to pass null to use the default /// Thrown if region is null and is unable to be automatically discovered by the AWS SDK. /// /// The same logic as the AWS SDK for .NET /// is used to automatically discover the credentials and region to use if not provided explicitly. /// public AwsSigV4HttpConnection(AWSCredentials credentials, RegionEndpoint region, string service = OpenSearchService, IDateTimeProvider dateTimeProvider = null) { _credentials = credentials ?? FallbackCredentialsFactory.GetCredentials(); // FallbackCredentialsFactory throws in case of not finding credentials. _region = region ?? FallbackRegionFactory.GetRegionEndpoint() // FallbackRegionFactory can return null. ?? throw new ArgumentNullException(nameof(region), "A RegionEndpoint was not provided and was unable to be determined from the environment."); _service = service ?? OpenSearchService; _dateTimeProvider = dateTimeProvider ?? DateTimeProvider.Default; } protected virtual System.Net.Http.HttpMessageHandler InnerCreateHttpClientHandler(RequestData requestData) => base.CreateHttpClientHandler(requestData); protected override System.Net.Http.HttpMessageHandler CreateHttpClientHandler(RequestData requestData) => new AwsSigV4HttpClientHandler(_credentials, _region, _service, _dateTimeProvider, InnerCreateHttpClientHandler(requestData)); } }