Skip to content

Commit e5a45f5

Browse files
committedMar 6, 2025
Updated EndpointDiscoveryHandler to use correct sync/async versions for getting credentials.
1 parent 685cb86 commit e5a45f5

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed
 

‎sdk/src/Core/Amazon.Runtime/Pipeline/Handlers/EndpointDiscoveryHandler.cs

+40-37
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/*
2-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License").
5-
* You may not use this file except in compliance with the License.
6-
* A copy of the License is located at
7-
*
8-
* http://aws.amazon.com/apache2.0
9-
*
10-
* or in the "license" file accompanying this file. This file is distributed
11-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12-
* express or implied. See the License for the specific language governing
13-
* permissions and limitations under the License.
14-
*/
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
1515

1616
using System;
1717
using System.Collections.Generic;
@@ -38,18 +38,20 @@ public override void InvokeSync(IExecutionContext executionContext)
3838
{
3939
var requestContext = executionContext.RequestContext;
4040
var regionalEndpoint = requestContext.Request.Endpoint;
41-
PreInvoke(executionContext);
41+
var immutableCredentials = (requestContext.Identity as AWSCredentials)?.GetCredentials();
4242

43+
PreInvoke(executionContext, immutableCredentials);
44+
4345
try
4446
{
4547
base.InvokeSync(executionContext);
4648
return;
4749
}
4850
catch (Exception exception)
49-
{
51+
{
5052
if (IsInvalidEndpointException(exception))
5153
{
52-
EvictCacheKeyForRequest(requestContext, regionalEndpoint);
54+
EvictCacheKeyForRequest(requestContext, regionalEndpoint, immutableCredentials);
5355
}
5456

5557
throw;
@@ -69,22 +71,24 @@ public override async System.Threading.Tasks.Task<T> InvokeAsync<T>(IExecutionCo
6971
{
7072
var requestContext = executionContext.RequestContext;
7173
var regionalEndpoint = requestContext.Request.Endpoint;
72-
PreInvoke(executionContext);
74+
var immutableCredentials = await ((requestContext.Identity as AWSCredentials)?.GetCredentialsAsync()).ConfigureAwait(false);
7375

76+
PreInvoke(executionContext, immutableCredentials);
77+
7478
try
7579
{
76-
return await base.InvokeAsync<T>(executionContext).ConfigureAwait(false);
80+
return await base.InvokeAsync<T>(executionContext).ConfigureAwait(false);
7781
}
7882
catch (Exception exception)
7983
{
8084
var capturedException = System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(exception);
8185
if (IsInvalidEndpointException(capturedException.SourceException))
8286
{
83-
EvictCacheKeyForRequest(requestContext, regionalEndpoint);
87+
EvictCacheKeyForRequest(requestContext, regionalEndpoint, immutableCredentials);
8488
}
8589

8690
capturedException.Throw();
87-
}
91+
}
8892

8993
throw new AmazonClientException("Neither a response was returned nor an exception was thrown in the Runtime EndpointDiscoveryResolver.");
9094
}
@@ -94,22 +98,22 @@ public override async System.Threading.Tasks.Task<T> InvokeAsync<T>(IExecutionCo
9498
/// Resolves the endpoint to be used for the current request
9599
/// before invoking the next handler.
96100
/// </summary>
97-
/// <param name="executionContext">The execution context, it contains the
98-
/// request and response context.</param>
99-
protected static void PreInvoke(IExecutionContext executionContext)
100-
{
101-
DiscoverEndpoints(executionContext.RequestContext, false);
101+
/// <param name="executionContext">The execution context, it contains the request and response context.</param>
102+
/// <param name="credentials">The AWS credentials for the account making the service call.</param>
103+
protected static void PreInvoke(IExecutionContext executionContext, ImmutableCredentials credentials)
104+
{
105+
DiscoverEndpoints(executionContext.RequestContext, false, credentials);
102106
}
103107

104-
public static void EvictCacheKeyForRequest(IRequestContext requestContext, Uri regionalEndpoint)
108+
public static void EvictCacheKeyForRequest(IRequestContext requestContext, Uri regionalEndpoint, ImmutableCredentials credentials)
105109
{
106-
DiscoverEndpoints(requestContext, true);
110+
DiscoverEndpoints(requestContext, true, credentials);
107111
requestContext.Request.Endpoint = regionalEndpoint;
108112
}
109113

110-
public static void DiscoverEndpoints(IRequestContext requestContext, bool evictCacheKey)
114+
public static void DiscoverEndpoints(IRequestContext requestContext, bool evictCacheKey, ImmutableCredentials credentials)
111115
{
112-
var discoveryEndpoints = ProcessEndpointDiscovery(requestContext, evictCacheKey, requestContext.Request.Endpoint);
116+
var discoveryEndpoints = ProcessEndpointDiscovery(requestContext, evictCacheKey, requestContext.Request.Endpoint, credentials);
113117
if (discoveryEndpoints != null)
114118
{
115119
foreach (var endpoint in discoveryEndpoints)
@@ -118,7 +122,7 @@ public static void DiscoverEndpoints(IRequestContext requestContext, bool evictC
118122
//and we couldn't get an endpoint back during an asynchronous discovery
119123
//attempt. The null address endpoint will be evicted after 60 seconds but will
120124
//prevent multiple server requests during this time.
121-
if (endpoint.Address == null)
125+
if(endpoint.Address == null)
122126
{
123127
continue;
124128
}
@@ -130,12 +134,11 @@ public static void DiscoverEndpoints(IRequestContext requestContext, bool evictC
130134
}
131135
}
132136

133-
private static IEnumerable<DiscoveryEndpointBase> ProcessEndpointDiscovery(IRequestContext requestContext, bool evictCacheKey, Uri evictUri)
134-
{
137+
private static IEnumerable<DiscoveryEndpointBase> ProcessEndpointDiscovery(IRequestContext requestContext, bool evictCacheKey, Uri evictUri, ImmutableCredentials credentials)
138+
{
135139
var options = requestContext.Options;
136-
var immutableCredentials = (requestContext.Identity as AWSCredentials)?.GetCredentials();
137140

138-
if (options.EndpointDiscoveryMarshaller != null && options.EndpointOperation != null && immutableCredentials != null)
141+
if (options.EndpointDiscoveryMarshaller != null && options.EndpointOperation != null && credentials != null)
139142
{
140143
//Endpoint discovery is supported by this operation and we have an endpoint operation available to use
141144
var endpointDiscoveryData = options.EndpointDiscoveryMarshaller.Marshall(requestContext.OriginalRequest);
@@ -144,11 +147,11 @@ private static IEnumerable<DiscoveryEndpointBase> ProcessEndpointDiscovery(IRequ
144147
{
145148
operationName = AWSSDKUtils.ExtractOperationName(requestContext.RequestName);
146149
}
147-
return options.EndpointOperation(new EndpointOperationContext(immutableCredentials.AccessKey, operationName, endpointDiscoveryData, evictCacheKey, evictUri));
150+
return options.EndpointOperation(new EndpointOperationContext(credentials.AccessKey, operationName, endpointDiscoveryData, evictCacheKey, evictUri));
148151
}
149152

150153
return null;
151-
}
154+
}
152155

153156
private static bool IsInvalidEndpointException(Exception exception)
154157
{
@@ -161,4 +164,4 @@ private static bool IsInvalidEndpointException(Exception exception)
161164
return false;
162165
}
163166
}
164-
}
167+
}

0 commit comments

Comments
 (0)
Failed to load comments.