Skip to content

Commit

Permalink
Rename serviceId parameter to service and describe as "service code"
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Jan 15, 2023
1 parent b007e47 commit ba2957a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,15 @@ var client = new OpenSearchClient(config);
```

### Amazon OpenSearch Serverless
Use the `"aoss"` service identifier to make requests to [Amazon OpenSearch Serverless](https://aws.amazon.com/opensearch-service/features/serverless/), otherwise all configuration options are identical to above.
Use the `"aoss"` service code to make requests to [Amazon OpenSearch Serverless](https://aws.amazon.com/opensearch-service/features/serverless/), otherwise all configuration options are identical to above.
```shell
export AWS_ACCESS_KEY_ID="..."
export AWS_SECRET_ACCESS_KEY="..."
export AWS_SESSION_TOKEN="..."
```
```csharp
var endpoint = new Uri("https://aaabbbcccddd111222333.ap-southeast-2.aoss.amazonaws.com");
var connection = new AwsSigV4HttpConnection(RegionEndpoint.APSoutheast2, serviceId: AwsSigV4HttpConnection.OpenSearchServerlessServiceId);
var connection = new AwsSigV4HttpConnection(RegionEndpoint.APSoutheast2, service: AwsSigV4HttpConnection.OpenSearchServerlessService);
var config = new ConnectionSettings(endpoint, connection);
var client = new OpenSearchClient(config);
```
Expand Down
8 changes: 4 additions & 4 deletions src/OpenSearch.Net.Auth.AwsSigV4/AwsSigV4HttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ internal class AwsSigV4HttpClientHandler : DelegatingHandler
{
private readonly AWSCredentials _credentials;
private readonly RegionEndpoint _region;
private readonly string _serviceId;
private readonly string _service;

public AwsSigV4HttpClientHandler(AWSCredentials credentials, RegionEndpoint region, string serviceId, HttpMessageHandler innerHandler)
public AwsSigV4HttpClientHandler(AWSCredentials credentials, RegionEndpoint region, string service, HttpMessageHandler innerHandler)
: base(innerHandler)
{
_credentials = credentials ?? throw new ArgumentNullException(nameof(credentials));
_region = region ?? throw new ArgumentNullException(nameof(region));
_serviceId = serviceId ?? throw new ArgumentNullException(nameof(serviceId));
_service = service ?? throw new ArgumentNullException(nameof(service));
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var credentials = await _credentials.GetCredentialsAsync().ConfigureAwait(false);

await AwsSigV4Util.SignRequest(request, credentials, _region, DateTime.UtcNow, _serviceId).ConfigureAwait(false);
await AwsSigV4Util.SignRequest(request, credentials, _region, DateTime.UtcNow, _service).ConfigureAwait(false);

return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
Expand Down
28 changes: 14 additions & 14 deletions src/OpenSearch.Net.Auth.AwsSigV4/AwsSigV4HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,67 @@ namespace OpenSearch.Net.Auth.AwsSigV4
/// </summary>
public class AwsSigV4HttpConnection : HttpConnection
{
public const string OpenSearchServiceId = "es";
public const string OpenSearchServerlessServiceId = "aoss";
public const string OpenSearchService = "es";
public const string OpenSearchServerlessService = "aoss";

private readonly AWSCredentials _credentials;
private readonly RegionEndpoint _region;
private readonly string _serviceId;
private readonly string _service;

/// <summary>
/// Construct a new connection discovering both the credentials and region from the environment.
/// </summary>
/// <param name="serviceId">The service ID to use when signing, defaults to the service ID for Amazon OpenSearch Service (<c>"es"</c>).</param>
/// <param name="service">The service code to use when signing, defaults to the service code for the Amazon OpenSearch Service (<c>"es"</c>).</param>
/// <seealso cref="AwsSigV4HttpConnection(AWSCredentials, RegionEndpoint, string)"/>
public AwsSigV4HttpConnection(string serviceId = OpenSearchServiceId) : this(null, null, serviceId) { }
public AwsSigV4HttpConnection(string service = OpenSearchService) : this(null, null, service) { }

/// <summary>
/// Construct a new connection configured with the specified credentials and using the region discovered from the environment.
/// </summary>
/// <param name="credentials">The credentials to use when signing.</param>
/// <param name="serviceId">The service ID to use when signing, defaults to the service ID for Amazon OpenSearch Service (<c>"es"</c>).</param>
/// <param name="service">The service code to use when signing, defaults to the service code for the Amazon OpenSearch Service (<c>"es"</c>).</param>
/// <seealso cref="AwsSigV4HttpConnection(AWSCredentials, RegionEndpoint, string)"/>
public AwsSigV4HttpConnection(AWSCredentials credentials, string serviceId = OpenSearchServiceId) : this(credentials, null, serviceId) { }
public AwsSigV4HttpConnection(AWSCredentials credentials, string service = OpenSearchService) : this(credentials, null, service) { }

/// <summary>
/// Construct a new connection configured with a specified region and using credentials discovered from the environment.
/// </summary>
/// <param name="region">The region to use when signing.</param>
/// <param name="serviceId">The service ID to use when signing, defaults to the service ID for Amazon OpenSearch Service (<c>"es"</c>).</param>
/// <param name="service">The service code to use when signing, defaults to the service code for the Amazon OpenSearch Service (<c>"es"</c>).</param>
/// <seealso cref="AwsSigV4HttpConnection(AWSCredentials, RegionEndpoint, string)"/>
public AwsSigV4HttpConnection(RegionEndpoint region, string serviceId = OpenSearchServiceId) : this(null, region, serviceId) { }
public AwsSigV4HttpConnection(RegionEndpoint region, string service = OpenSearchService) : this(null, region, service) { }

/// <summary>
/// Construct a new connection configured with the given credentials and region.
/// </summary>
/// <param name="credentials">The credentials to use when signing, or null to have them discovered automatically by the AWS SDK.</param>
/// <param name="region">The region to use when signing, or null to have it discovered automatically by the AWS SDK.</param>
/// <param name="serviceId">The service ID to use when signing, defaults to the service ID for Amazon OpenSearch Service (<c>"es"</c>).</param>
/// <param name="service">The service code to use when signing, defaults to the service code for the Amazon OpenSearch Service (<c>"es"</c>).</param>
/// <exception cref="ArgumentNullException">Thrown if region is null and is unable to be automatically discovered by the AWS SDK.</exception>
/// <remarks>
/// The same logic as the <a href="https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/creds-assign.html">AWS SDK for .NET</a>
/// is used to automatically discover the credentials and region to use if not provided explicitly.
/// </remarks>
public AwsSigV4HttpConnection(AWSCredentials credentials, RegionEndpoint region, string serviceId = OpenSearchServiceId)
public AwsSigV4HttpConnection(AWSCredentials credentials, RegionEndpoint region, string service = OpenSearchService)
{
_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.");
_serviceId = serviceId ?? OpenSearchServiceId;
_service = service ?? OpenSearchService;
}

#if DOTNETCORE

protected override System.Net.Http.HttpMessageHandler CreateHttpClientHandler(RequestData requestData) =>
new AwsSigV4HttpClientHandler(_credentials, _region, _serviceId, base.CreateHttpClientHandler(requestData));
new AwsSigV4HttpClientHandler(_credentials, _region, _service, base.CreateHttpClientHandler(requestData));

#else

protected override System.Net.HttpWebRequest CreateHttpWebRequest(RequestData requestData)
{
var request = base.CreateHttpWebRequest(requestData);
AwsSigV4Util.SignRequest(request, requestData, _credentials.GetCredentials(), _region, DateTime.UtcNow, _serviceId);
AwsSigV4Util.SignRequest(request, requestData, _credentials.GetCredentials(), _region, DateTime.UtcNow, _service);
return request;
}

Expand Down
8 changes: 4 additions & 4 deletions src/OpenSearch.Net.Auth.AwsSigV4/AwsSigV4Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public static async Task SignRequest(
ImmutableCredentials credentials,
RegionEndpoint region,
DateTime signingTime,
string serviceId)
string service)
{
var canonicalRequest = await CanonicalRequest.From(request, credentials, signingTime).ConfigureAwait(false);

var signature = AWS4Signer.ComputeSignature(credentials, region.SystemName, signingTime, serviceId, canonicalRequest.SignedHeaders,
var signature = AWS4Signer.ComputeSignature(credentials, region.SystemName, signingTime, service, canonicalRequest.SignedHeaders,
canonicalRequest.ToString());

request.Headers.TryAddWithoutValidation(HeaderNames.XAmzDate, canonicalRequest.XAmzDate);
Expand All @@ -45,11 +45,11 @@ public static void SignRequest(
ImmutableCredentials credentials,
RegionEndpoint region,
DateTime signingTime,
string serviceId)
string service)
{
var canonicalRequest = CanonicalRequest.From(request, requestData, credentials, signingTime);

var signature = AWS4Signer.ComputeSignature(credentials, region.SystemName, signingTime, serviceId, canonicalRequest.SignedHeaders,
var signature = AWS4Signer.ComputeSignature(credentials, region.SystemName, signingTime, service, canonicalRequest.SignedHeaders,
canonicalRequest.ToString());

request.Headers[HeaderNames.XAmzDate] = canonicalRequest.XAmzDate;
Expand Down

0 comments on commit ba2957a

Please sign in to comment.