Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for "derived" execution attributes. #4384

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,13 @@
<Method name="fromFile"/>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
</Match>

<!-- Intentional for backwards-compatibility -->
<Match>
<Or>
<Class name="software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute"/>
<Class name="software.amazon.awssdk.auth.signer.S3SignerExecutionAttribute"/>
</Or>
<Bug pattern="NP_BOOLEAN_RETURN_NULL"/>
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ private MethodSpec generateBeforeExecution() {
listOf(AuthSchemeOption.class))
.addStatement("$T selectedAuthScheme = selectAuthScheme(authOptions, executionAttributes)",
wildcardSelectedAuthScheme())
.addStatement("executionAttributes.putAttribute($T.SELECTED_AUTH_SCHEME, selectedAuthScheme)",
SdkInternalExecutionAttribute.class);
.addStatement("$T.putSelectedAuthScheme(executionAttributes, selectedAuthScheme)",
endpointRulesSpecUtils.rulesRuntimeClassName("AuthSchemeUtils"));
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.concurrent.CompletionException;
import javax.lang.model.element.Modifier;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute;
import software.amazon.awssdk.awscore.AwsExecutionAttribute;
import software.amazon.awssdk.awscore.endpoints.AwsEndpointAttribute;
import software.amazon.awssdk.awscore.endpoints.authscheme.EndpointAuthScheme;
Expand Down Expand Up @@ -67,8 +66,6 @@
import software.amazon.awssdk.http.auth.aws.AwsV4aHttpSigner;
import software.amazon.awssdk.http.auth.spi.AuthSchemeOption;
import software.amazon.awssdk.identity.spi.Identity;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.RegionScope;
import software.amazon.awssdk.utils.AttributeMap;
import software.amazon.awssdk.utils.HostnameValidator;
import software.amazon.awssdk.utils.StringUtils;
Expand Down Expand Up @@ -109,7 +106,6 @@ public TypeSpec poetSpec() {
addStaticContextParamMethods(b);

b.addMethod(authSchemeWithEndpointSignerPropertiesMethod());
b.addMethod(copySignerPropertiesToAttributesMethod());

if (hasClientContextParams()) {
b.addMethod(setClientContextParamsMethod());
Expand Down Expand Up @@ -171,12 +167,6 @@ private MethodSpec modifyRequestMethod() {
SdkInternalExecutionAttribute.class);
b.endControlFlow();


// Backwards-compatibility with old signers.
b.beginControlFlow("if (selectedAuthScheme != null)");
b.addStatement("copySignerPropertiesToAttributes(selectedAuthScheme.authSchemeOption(), executionAttributes)");
b.endControlFlow();

b.addStatement("executionAttributes.putAttribute(SdkInternalExecutionAttribute.RESOLVED_ENDPOINT, endpoint)");
b.addStatement("return result");
b.endControlFlow();
Expand Down Expand Up @@ -620,73 +610,4 @@ private static CodeBlock copyV4aEndpointSignerPropertiesToAuth() {
code.endControlFlow();
return code.build();
}

private MethodSpec copySignerPropertiesToAttributesMethod() {
MethodSpec.Builder method =
MethodSpec.methodBuilder("copySignerPropertiesToAttributes")
.addModifiers(Modifier.PRIVATE)
.addParameter(AuthSchemeOption.class, "authOption")
.addParameter(ExecutionAttributes.class, "executionAttributes");

if (dependsOnHttpAuthAws) {
method.addCode(copyV4SignerPropertiesToAttributes());
method.addCode(copyV4aSignerPropertiesToAttributes());
}

return method.build();
}

private static CodeBlock copyV4SignerPropertiesToAttributes() {
CodeBlock.Builder code = CodeBlock.builder();
code.beginControlFlow("if (authOption.schemeId().equals($T.SCHEME_ID))", AwsV4AuthScheme.class);

code.beginControlFlow("if (authOption.signerProperty($T.DOUBLE_URL_ENCODE) != null)", AwsV4HttpSigner.class);
code.addStatement("executionAttributes.putAttribute($T.SIGNER_DOUBLE_URL_ENCODE, authOption.signerProperty($T"
+ ".DOUBLE_URL_ENCODE))",
AwsSignerExecutionAttribute.class, AwsV4HttpSigner.class);
code.endControlFlow();

code.beginControlFlow("if (authOption.signerProperty($T.SERVICE_SIGNING_NAME) != null)", AwsV4HttpSigner.class);
code.addStatement("executionAttributes.putAttribute($T.SERVICE_SIGNING_NAME, authOption.signerProperty($T"
+ ".SERVICE_SIGNING_NAME))",
AwsSignerExecutionAttribute.class, AwsV4HttpSigner.class);
code.endControlFlow();

code.beginControlFlow("if (authOption.signerProperty($T.REGION_NAME) != null)", AwsV4HttpSigner.class);
code.addStatement("executionAttributes.putAttribute($T.SIGNING_REGION, $T.of(authOption.signerProperty($T"
+ ".REGION_NAME)))",
AwsSignerExecutionAttribute.class, Region.class, AwsV4HttpSigner.class);
code.endControlFlow();

code.addStatement("return");
code.endControlFlow();
return code.build();
}

private static CodeBlock copyV4aSignerPropertiesToAttributes() {
CodeBlock.Builder code = CodeBlock.builder();
code.beginControlFlow("if (authOption.schemeId().equals($T.SCHEME_ID))", AwsV4aAuthScheme.class);

code.beginControlFlow("if (authOption.signerProperty($T.DOUBLE_URL_ENCODE) != null)", AwsV4aHttpSigner.class);
code.addStatement("executionAttributes.putAttribute($T.SIGNER_DOUBLE_URL_ENCODE, authOption.signerProperty($T"
+ ".DOUBLE_URL_ENCODE))",
AwsSignerExecutionAttribute.class, AwsV4aHttpSigner.class);
code.endControlFlow();

code.beginControlFlow("if (authOption.signerProperty($T.SERVICE_SIGNING_NAME) != null)", AwsV4aHttpSigner.class);
code.addStatement("executionAttributes.putAttribute($T.SERVICE_SIGNING_NAME, authOption.signerProperty($T"
+ ".SERVICE_SIGNING_NAME))",
AwsSignerExecutionAttribute.class, AwsV4aHttpSigner.class);
code.endControlFlow();

code.beginControlFlow("if (authOption.signerProperty($T.REGION_NAME) != null)", AwsV4aHttpSigner.class);
code.addStatement("executionAttributes.putAttribute($T.SIGNING_REGION_SCOPE, $T.create(authOption.signerProperty($T"
+ ".REGION_NAME)))",
AwsSignerExecutionAttribute.class, RegionScope.class, AwsV4aHttpSigner.class);
code.endControlFlow();

code.addStatement("return");
code.endControlFlow();
return code.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import software.amazon.awssdk.annotations.SdkProtectedApi;
import software.amazon.awssdk.awscore.endpoints.authscheme.EndpointAuthScheme;
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4AuthScheme;
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4aAuthScheme;
import software.amazon.awssdk.core.SelectedAuthScheme;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.http.auth.spi.AuthSchemeOption;
import software.amazon.awssdk.identity.spi.Identity;
import software.amazon.awssdk.utils.Logger;

@SdkProtectedApi
Expand Down Expand Up @@ -50,56 +55,71 @@ public final class AuthSchemeUtils {

String authSchemeName = scheme.get(Identifier.of("name")).expectString();
switch (authSchemeName) {
case SIGV4A_NAME: {
SigV4aAuthScheme.Builder schemeBuilder = SigV4aAuthScheme.builder();
case SIGV4A_NAME: {
SigV4aAuthScheme.Builder schemeBuilder = SigV4aAuthScheme.builder();

Value signingName = scheme.get(Identifier.of("signingName"));
if (signingName != null) {
schemeBuilder.signingName(signingName.expectString());
}

Value signingRegionSet = scheme.get(Identifier.of("signingRegionSet"));
if (signingRegionSet != null) {
Value.Array signingRegionSetArray = signingRegionSet.expectArray();
for (int j = 0; j < signingRegionSetArray.size(); ++j) {
schemeBuilder.addSigningRegion(signingRegionSetArray.get(j).expectString());
}
}
Value signingName = scheme.get(Identifier.of("signingName"));
if (signingName != null) {
schemeBuilder.signingName(signingName.expectString());
}

Value disableDoubleEncoding = scheme.get(Identifier.of("disableDoubleEncoding"));
if (disableDoubleEncoding != null) {
schemeBuilder.disableDoubleEncoding(disableDoubleEncoding.expectBool());
Value signingRegionSet = scheme.get(Identifier.of("signingRegionSet"));
if (signingRegionSet != null) {
Value.Array signingRegionSetArray = signingRegionSet.expectArray();
for (int j = 0; j < signingRegionSetArray.size(); ++j) {
schemeBuilder.addSigningRegion(signingRegionSetArray.get(j).expectString());
}
}

authSchemes.add(schemeBuilder.build());
Value disableDoubleEncoding = scheme.get(Identifier.of("disableDoubleEncoding"));
if (disableDoubleEncoding != null) {
schemeBuilder.disableDoubleEncoding(disableDoubleEncoding.expectBool());
}
break;
case SIGV4_NAME: {
SigV4AuthScheme.Builder schemeBuilder = SigV4AuthScheme.builder();

Value signingName = scheme.get(Identifier.of("signingName"));
if (signingName != null) {
schemeBuilder.signingName(signingName.expectString());
}
authSchemes.add(schemeBuilder.build());
}
break;
case SIGV4_NAME: {
SigV4AuthScheme.Builder schemeBuilder = SigV4AuthScheme.builder();

Value signingRegion = scheme.get(Identifier.of("signingRegion"));
if (signingRegion != null) {
schemeBuilder.signingRegion(signingRegion.expectString());
}
Value signingName = scheme.get(Identifier.of("signingName"));
if (signingName != null) {
schemeBuilder.signingName(signingName.expectString());
}

Value disableDoubleEncoding = scheme.get(Identifier.of("disableDoubleEncoding"));
if (disableDoubleEncoding != null) {
schemeBuilder.disableDoubleEncoding(disableDoubleEncoding.expectBool());
}
Value signingRegion = scheme.get(Identifier.of("signingRegion"));
if (signingRegion != null) {
schemeBuilder.signingRegion(signingRegion.expectString());
}

authSchemes.add(schemeBuilder.build());
Value disableDoubleEncoding = scheme.get(Identifier.of("disableDoubleEncoding"));
if (disableDoubleEncoding != null) {
schemeBuilder.disableDoubleEncoding(disableDoubleEncoding.expectBool());
}

authSchemes.add(schemeBuilder.build());
}
break;
default:
LOG.debug(() -> "Ignoring unknown auth scheme: " + authSchemeName);
break;
default:
LOG.debug(() -> "Ignoring unknown auth scheme: " + authSchemeName);
break;
}
}
return authSchemes;
}

public static <T extends Identity> void putSelectedAuthScheme(ExecutionAttributes attributes,
SelectedAuthScheme<T> selectedAuthScheme) {
SelectedAuthScheme<?> existingAuthScheme = attributes.getAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME);
if (existingAuthScheme != null) {
AuthSchemeOption.Builder selectedOption = selectedAuthScheme.authSchemeOption().toBuilder();
existingAuthScheme.authSchemeOption().forEachIdentityProperty(selectedOption::putIdentityPropertyIfAbsent);
existingAuthScheme.authSchemeOption().forEachSignerProperty(selectedOption::putSignerPropertyIfAbsent);
selectedAuthScheme = new SelectedAuthScheme<>(selectedAuthScheme.identity(),
selectedAuthScheme.signer(),
selectedOption.build());
}

attributes.putAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME, selectedAuthScheme);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.services.query.auth.scheme.internal;

import java.time.Duration;
Expand Down Expand Up @@ -48,6 +33,7 @@
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.query.auth.scheme.QueryAuthSchemeParams;
import software.amazon.awssdk.services.query.auth.scheme.QueryAuthSchemeProvider;
import software.amazon.awssdk.services.query.endpoints.internal.AuthSchemeUtils;
import software.amazon.awssdk.utils.Logger;
import software.amazon.awssdk.utils.Validate;

Expand All @@ -60,41 +46,41 @@ public final class QueryAuthSchemeInterceptor implements ExecutionInterceptor {
public void beforeExecution(Context.BeforeExecution context, ExecutionAttributes executionAttributes) {
List<AuthSchemeOption> authOptions = resolveAuthOptions(context, executionAttributes);
SelectedAuthScheme<? extends Identity> selectedAuthScheme = selectAuthScheme(authOptions, executionAttributes);
executionAttributes.putAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME, selectedAuthScheme);
AuthSchemeUtils.putSelectedAuthScheme(executionAttributes, selectedAuthScheme);
}

private List<AuthSchemeOption> resolveAuthOptions(Context.BeforeExecution context, ExecutionAttributes executionAttributes) {
QueryAuthSchemeProvider authSchemeProvider = Validate.isInstanceOf(QueryAuthSchemeProvider.class,
executionAttributes.getAttribute(SdkInternalExecutionAttribute.AUTH_SCHEME_RESOLVER),
"Expected an instance of QueryAuthSchemeProvider");
executionAttributes.getAttribute(SdkInternalExecutionAttribute.AUTH_SCHEME_RESOLVER),
"Expected an instance of QueryAuthSchemeProvider");
QueryAuthSchemeParams params = authSchemeParams(context.request(), executionAttributes);
return authSchemeProvider.resolveAuthScheme(params);
}

private SelectedAuthScheme<? extends Identity> selectAuthScheme(List<AuthSchemeOption> authOptions,
ExecutionAttributes executionAttributes) {
ExecutionAttributes executionAttributes) {
MetricCollector metricCollector = executionAttributes.getAttribute(SdkExecutionAttribute.API_CALL_METRIC_COLLECTOR);
Map<String, AuthScheme<?>> authSchemes = executionAttributes.getAttribute(SdkInternalExecutionAttribute.AUTH_SCHEMES);
IdentityProviderConfiguration identityResolvers = executionAttributes
.getAttribute(SdkInternalExecutionAttribute.IDENTITY_PROVIDER_CONFIGURATION);
.getAttribute(SdkInternalExecutionAttribute.IDENTITY_PROVIDER_CONFIGURATION);
List<Supplier<String>> discardedReasons = new ArrayList<>();
for (AuthSchemeOption authOption : authOptions) {
AuthScheme<?> authScheme = authSchemes.get(authOption.schemeId());
SelectedAuthScheme<? extends Identity> selectedAuthScheme = trySelectAuthScheme(authOption, authScheme,
identityResolvers, discardedReasons, metricCollector);
identityResolvers, discardedReasons, metricCollector);
if (selectedAuthScheme != null) {
if (!discardedReasons.isEmpty()) {
LOG.debug(() -> String.format("%s auth will be used, discarded: '%s'", authOption.schemeId(),
discardedReasons.stream().map(Supplier::get).collect(Collectors.joining(", "))));
discardedReasons.stream().map(Supplier::get).collect(Collectors.joining(", "))));
}
return selectedAuthScheme;
}
}
throw SdkException
.builder()
.message(
"Failed to determine how to authenticate the user: "
+ discardedReasons.stream().map(Supplier::get).collect(Collectors.joining(", "))).build();
.builder()
.message(
"Failed to determine how to authenticate the user: "
+ discardedReasons.stream().map(Supplier::get).collect(Collectors.joining(", "))).build();
}

private QueryAuthSchemeParams authSchemeParams(SdkRequest request, ExecutionAttributes executionAttributes) {
Expand All @@ -103,17 +89,17 @@ private QueryAuthSchemeParams authSchemeParams(SdkRequest request, ExecutionAttr
return QueryAuthSchemeParams.builder().operation(operation).region(region).build();
}

private <T extends Identity> SelectedAuthScheme<T> trySelectAuthScheme(
AuthSchemeOption authOption, AuthScheme<T> authScheme,
IdentityProviderConfiguration identityProviders, List<Supplier<String>> discardedReasons,
MetricCollector metricCollector) {
private <T extends Identity> SelectedAuthScheme<T> trySelectAuthScheme(AuthSchemeOption authOption, AuthScheme<T> authScheme,
IdentityProviderConfiguration identityProviders, List<Supplier<String>> discardedReasons,
MetricCollector metricCollector) {
if (authScheme == null) {
discardedReasons.add(() -> String.format("'%s' is not enabled for this request.", authOption.schemeId()));
return null;
}
IdentityProvider<T> identityProvider = authScheme.identityProvider(identityProviders);
if (identityProvider == null) {
discardedReasons.add(() -> String.format("'%s' does not have an identity provider configured.", authOption.schemeId()));
discardedReasons
.add(() -> String.format("'%s' does not have an identity provider configured.", authOption.schemeId()));
return null;
}
ResolveIdentityRequest.Builder identityRequestBuilder = ResolveIdentityRequest.builder();
Expand Down
Loading
Loading