Skip to content

Commit

Permalink
Remove emptyMap case that would miss additionalAuthSchemes (#4364)
Browse files Browse the repository at this point in the history
* Remove emptyMap case that would miss additionalAuthSchemes

* Add test case with service level authType=none
  • Loading branch information
gosar committed Aug 29, 2023
1 parent 2aba2a0 commit bfa24e7
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,6 @@ private MethodSpec authSchemesMethod() {
.returns(returns);

Set<Class<?>> concreteAuthSchemeClasses = authSchemeSpecUtils.allServiceConcreteAuthSchemeClasses();
if (concreteAuthSchemeClasses.isEmpty()) {
builder.addStatement("return $T.emptyMap()", Collections.class);
return builder.build();
}

builder.addStatement("$T schemes = new $T<>($L + this.additionalAuthSchemes.size())",
returns, HashMap.class, concreteAuthSchemeClasses.size());
for (Class<?> concreteAuthScheme : concreteAuthSchemeClasses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public static IntermediateModel allOperationsWithAuthDifferentValueServiceModels
return new IntermediateModelBuilder(models).build();
}

public static IntermediateModel serviceWithNoAuth() {
public static IntermediateModel operationWithNoAuth() {
File serviceModel =
new File(ClientTestModels.class.getResource("client/c2j/ops-with-no-auth/service-2.json").getFile());
File customizationModel =
Expand All @@ -229,6 +229,21 @@ public static IntermediateModel serviceWithNoAuth() {
return new IntermediateModelBuilder(models).build();
}

public static IntermediateModel serviceWithNoAuth() {
File serviceModel =
new File(ClientTestModels.class.getResource("client/c2j/service-with-no-auth/service-2.json").getFile());
File customizationModel =
new File(ClientTestModels.class.getResource("client/c2j/service-with-no-auth/customization.config")
.getFile());
C2jModels models = C2jModels
.builder()
.serviceModel(getServiceModel(serviceModel))
.customizationConfig(getCustomizationConfig(customizationModel))
.build();

return new IntermediateModelBuilder(models).build();
}

public static IntermediateModel serviceMiniS3() {
File serviceModel =
new File(ClientTestModels.class.getResource("client/c2j/mini-s3/service-2.json").getFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ static List<TestCase> parameters() {
.build(),
// Service with operations with auth none
TestCase.builder()
.modelProvider(ClientTestModels::serviceWithNoAuth)
.modelProvider(ClientTestModels::operationWithNoAuth)
.classSpecProvider(ModelBasedAuthSchemeProviderSpec::new)
.caseName("service-with-no-auth")
.caseName("ops-with-no-auth")
.outputFileSuffix("default-provider")
.build(),
// Service with signature version with the same value as S3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,23 @@ public void baseClientBuilderClass() throws Exception {
}

@Test
public void baseClientBuilderInterfaceWithBearerAuthClass() throws Exception {
public void baseClientBuilderInterfaceWithBearerAuth() throws Exception {
validateBearerAuthGeneration(BaseClientBuilderInterface::new, "test-bearer-auth-client-builder-interface.java");
}

@Test
public void baseClientBuilderInterfaceWithNoAuthClass() throws Exception {
validateNoAuthAuthGeneration(BaseClientBuilderClass::new, "test-no-auth-auth-client-builder-class.java");
public void baseClientBuilderClassWithBearerAuth() throws Exception {
validateBearerAuthGeneration(BaseClientBuilderClass::new, "test-bearer-auth-client-builder-class.java");
}

@Test
public void baseClientBuilderWithBearerAuthClass() throws Exception {
validateBearerAuthGeneration(BaseClientBuilderClass::new, "test-bearer-auth-client-builder-class.java");
public void baseClientBuilderClassWithNoAuthOperation() throws Exception {
validateNoAuthOperationAuthGeneration(BaseClientBuilderClass::new, "test-no-auth-ops-client-builder-class.java");
}

@Test
public void baseClientBuilderClassWithNoAuthService() throws Exception {
validateNoAuthServiceAuthGeneration(BaseClientBuilderClass::new, "test-no-auth-service-client-builder-class.java");
}

@Test
Expand Down Expand Up @@ -123,8 +128,13 @@ private void validateBearerAuthGeneration(Function<IntermediateModel, ClassSpec>
assertThat(generatorConstructor.apply(ClientTestModels.bearerAuthServiceModels()), generatesTo(expectedClassName));
}

private void validateNoAuthAuthGeneration(Function<IntermediateModel, ClassSpec> generatorConstructor,
String expectedClassName) {
private void validateNoAuthOperationAuthGeneration(Function<IntermediateModel, ClassSpec> generatorConstructor,
String expectedClassName) {
assertThat(generatorConstructor.apply(ClientTestModels.operationWithNoAuth()), generatesTo(expectedClassName));
}

private void validateNoAuthServiceAuthGeneration(Function<IntermediateModel, ClassSpec> generatorConstructor,
String expectedClassName) {
assertThat(generatorConstructor.apply(ClientTestModels.serviceWithNoAuth()), generatesTo(expectedClassName));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package software.amazon.awssdk.services.database;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.http.auth.spi.AuthScheme;
import software.amazon.awssdk.http.auth.spi.NoAuthAuthScheme;
import software.amazon.awssdk.services.database.auth.scheme.DatabaseAuthSchemeProvider;
import software.amazon.awssdk.services.database.auth.scheme.internal.DatabaseAuthSchemeInterceptor;
import software.amazon.awssdk.services.database.endpoints.DatabaseEndpointProvider;
import software.amazon.awssdk.services.database.endpoints.internal.DatabaseRequestSetEndpointInterceptor;
import software.amazon.awssdk.services.database.endpoints.internal.DatabaseResolveEndpointInterceptor;
import software.amazon.awssdk.utils.CollectionUtils;

/**
* Internal base class for {@link DefaultDatabaseClientBuilder} and {@link DefaultDatabaseAsyncClientBuilder}.
*/
@Generated("software.amazon.awssdk:codegen")
@SdkInternalApi
abstract class DefaultDatabaseBaseClientBuilder<B extends DatabaseBaseClientBuilder<B, C>, C> extends
AwsDefaultClientBuilder<B, C> {
private final Map<String, AuthScheme<?>> additionalAuthSchemes = new HashMap<>();

@Override
protected final String serviceEndpointPrefix() {
return "database-service-endpoint";
}

@Override
protected final String serviceName() {
return "Database";
}

@Override
protected final SdkClientConfiguration mergeServiceDefaults(SdkClientConfiguration config) {
return config.merge(c -> c.option(SdkClientOption.ENDPOINT_PROVIDER, defaultEndpointProvider())
.option(SdkClientOption.AUTH_SCHEME_PROVIDER, defaultAuthSchemeProvider())
.option(SdkClientOption.AUTH_SCHEMES, authSchemes())
.option(SdkClientOption.CRC32_FROM_COMPRESSED_DATA_ENABLED, false));
}

@Override
protected final SdkClientConfiguration finalizeServiceConfiguration(SdkClientConfiguration config) {
List<ExecutionInterceptor> endpointInterceptors = new ArrayList<>();
endpointInterceptors.add(new DatabaseAuthSchemeInterceptor());
endpointInterceptors.add(new DatabaseResolveEndpointInterceptor());
endpointInterceptors.add(new DatabaseRequestSetEndpointInterceptor());
ClasspathInterceptorChainFactory interceptorFactory = new ClasspathInterceptorChainFactory();
List<ExecutionInterceptor> interceptors = interceptorFactory
.getInterceptors("software/amazon/awssdk/services/database/execution.interceptors");
List<ExecutionInterceptor> additionalInterceptors = new ArrayList<>();
interceptors = CollectionUtils.mergeLists(endpointInterceptors, interceptors);
interceptors = CollectionUtils.mergeLists(interceptors, additionalInterceptors);
interceptors = CollectionUtils.mergeLists(interceptors, config.option(SdkClientOption.EXECUTION_INTERCEPTORS));
SdkClientConfiguration.Builder builder = config.toBuilder();
builder.option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors);
return builder.build();
}

@Override
protected final String signingName() {
return "database-service";
}

private DatabaseEndpointProvider defaultEndpointProvider() {
return DatabaseEndpointProvider.defaultProvider();
}

public B authSchemeProvider(DatabaseAuthSchemeProvider authSchemeProvider) {
clientConfiguration.option(SdkClientOption.AUTH_SCHEME_PROVIDER, authSchemeProvider);
return thisBuilder();
}

private DatabaseAuthSchemeProvider defaultAuthSchemeProvider() {
return DatabaseAuthSchemeProvider.defaultProvider();
}

@Override
public B putAuthScheme(AuthScheme<?> authScheme) {
additionalAuthSchemes.put(authScheme.schemeId(), authScheme);
return thisBuilder();
}

private Map<String, AuthScheme<?>> authSchemes() {
Map<String, AuthScheme<?>> schemes = new HashMap<>(1 + this.additionalAuthSchemes.size());
NoAuthAuthScheme noAuthAuthScheme = NoAuthAuthScheme.create();
schemes.put(noAuthAuthScheme.schemeId(), noAuthAuthScheme);
schemes.putAll(this.additionalAuthSchemes);
return Collections.unmodifiableMap(schemes);
}

protected static void validateClientOptions(SdkClientConfiguration c) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"version": "2.0",
"metadata": {
"apiVersion": "2023-06-08",
"endpointPrefix": "database-service-endpoint",
"globalEndpoint": "database-service.amazonaws.com",
"protocol": "rest-json",
"serviceAbbreviation": "Database Service",
"serviceFullName": "Some Service That Has No Auth At All",
"serviceId": "Database Service",
"signingName": "database-service",
"signatureVersion": "none",
"auth": [],
"uid": "database-service-2023-06-08",
"xmlNamespace": "https://database-service.amazonaws.com/doc/2023-06-08/"
},
"operations": {
"GetDatabaseVersion": {
"name": "GetDatabaseVersion",
"http": {
"method": "GET",
"requestUri": "/get-version/"
},
"input": {
"shape": "GetDatabaseVersionRequest"
},
"output": {
"shape": "GetDatabaseVersionResponse"
},
"documentation": "<p>Performs a get database version operation</p>"
}
},
"shapes": {
"GetDatabaseVersionRequest": {
"type": "structure",
"members": {
"StringMember": {
"shape": "String",
"documentation": "<p>A string Member</p>"
}
}
},
"GetDatabaseVersionResponse": {
"type": "structure",
"members": {
"StringMember": {
"shape": "String",
"documentation": "<p>A string Member</p>"
}
}
},
"String":{"type":"string"}
},
"documentation": "A Database Service with no auth on the service at all"
}

0 comments on commit bfa24e7

Please sign in to comment.