Skip to content

Commit

Permalink
Update AuthScemeParams with RegionSet for Sigv4a auth Scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
joviegas committed Dec 30, 2024
1 parent 35daaa0 commit 44348f2
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils;
import software.amazon.awssdk.http.auth.aws.scheme.AwsV4AuthScheme;
import software.amazon.awssdk.http.auth.aws.scheme.AwsV4aAuthScheme;
import software.amazon.awssdk.http.auth.aws.signer.RegionSet;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.utils.builder.CopyableBuilder;
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
Expand Down Expand Up @@ -117,7 +119,16 @@ private void addAccessorMethods(TypeSpec.Builder b) {
.addJavadoc("Returns the region. The region parameter may be used with the $S auth scheme.",
AwsV4AuthScheme.SCHEME_ID)
.build());
}

if (authSchemeSpecUtils.usesSigV4a()) {
b.addMethod(MethodSpec.methodBuilder("regionSet")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.returns(RegionSet.class)
.addJavadoc("Returns the RegionSet. The regionSet parameter may be used with the $S auth "
+ "scheme.",
AwsV4aAuthScheme.SCHEME_ID)
.build());
}

if (authSchemeSpecUtils.generateEndpointBasedParams()) {
Expand Down Expand Up @@ -162,6 +173,17 @@ private void addBuilderSetterMethods(TypeSpec.Builder b) {

}

if (authSchemeSpecUtils.usesSigV4a()) {
b.addMethod(MethodSpec.methodBuilder("regionSet")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addParameter(ParameterSpec.builder(RegionSet.class, "regionSet").build())
.returns(authSchemeSpecUtils.parametersInterfaceBuilderInterfaceName())
.addJavadoc("Set the RegionSet. The regionSet parameter may be used with the $S auth scheme.",
AwsV4aAuthScheme.SCHEME_ID)
.build());

}

if (authSchemeSpecUtils.generateEndpointBasedParams()) {
parameters().forEach((name, model) -> {
if (authSchemeSpecUtils.includeParam(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public boolean usesSigV4() {
return AuthUtils.usesAwsAuth(intermediateModel);
}

public boolean usesSigV4a() {
return AuthUtils.usesSigv4aAuth(intermediateModel);
}

public boolean useEndpointBasedAuthProvider() {
// Endpoint based auth provider is gated using the same setting that enables the use of auth scheme params. One does
// not make sense without the other so there's no much point on creating another setting if both have to be at the same
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils;
import software.amazon.awssdk.http.auth.aws.signer.RegionSet;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.utils.Validate;

Expand Down Expand Up @@ -79,6 +80,10 @@ private MethodSpec constructor() {
b.addStatement("this.region = builder.region");
}

if (authSchemeSpecUtils.usesSigV4a()) {
b.addStatement("this.regionSet = builder.regionSet");
}

if (authSchemeSpecUtils.generateEndpointBasedParams()) {
parameters().forEach((name, model) -> {
if (authSchemeSpecUtils.includeParam(name)) {
Expand Down Expand Up @@ -145,6 +150,9 @@ private void addBuilderConstructors(TypeSpec.Builder b) {
if (authSchemeSpecUtils.usesSigV4()) {
builderFromInstance.addStatement("this.region = params.region");
}
if (authSchemeSpecUtils.usesSigV4a()) {
builderFromInstance.addStatement("this.regionSet = params.regionSet");
}
if (authSchemeSpecUtils.generateEndpointBasedParams()) {
parameters().forEach((name, model) -> {
if (authSchemeSpecUtils.includeParam(name)) {
Expand Down Expand Up @@ -181,6 +189,19 @@ private void addFieldsAndAccessors(TypeSpec.Builder b) {
.build());
}

if (authSchemeSpecUtils.usesSigV4a()) {
b.addField(FieldSpec.builder(RegionSet.class, "regionSet")
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
.build());

b.addMethod(MethodSpec.methodBuilder("regionSet")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.returns(RegionSet.class)
.addStatement("return regionSet")
.build());
}

if (authSchemeSpecUtils.generateEndpointBasedParams()) {
parameters().forEach((name, model) -> {
if (authSchemeSpecUtils.includeParam(name)) {
Expand Down Expand Up @@ -227,6 +248,13 @@ private void addBuilderFieldsAndSetter(TypeSpec.Builder b) {
b.addMethod(builderSetterMethod("region", TypeName.get(Region.class)));
}

if (authSchemeSpecUtils.usesSigV4a()) {
b.addField(FieldSpec.builder(RegionSet.class, "regionSet")
.addModifiers(Modifier.PRIVATE)
.build());
b.addMethod(builderSetterMethod("regionSet", TypeName.get(RegionSet.class)));
}

if (authSchemeSpecUtils.generateEndpointBasedParams()) {
parameters().forEach((name, model) -> {
if (authSchemeSpecUtils.includeParam(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public static boolean usesBearerAuth(IntermediateModel model) {
.anyMatch(authType -> authType == AuthType.BEARER);
}

public static boolean usesSigv4aAuth(IntermediateModel model) {
if (isServiceSigv4a(model)) {
return true;
}
return model.getOperations()
.values()
.stream()
.anyMatch(operationModel -> operationModel.getAuth().stream().anyMatch(authType -> authType == AuthType.V4A));
}

public static boolean usesAwsAuth(IntermediateModel model) {
if (isServiceAwsAuthType(model)) {
return true;
Expand All @@ -60,6 +70,10 @@ private static boolean isServiceBearerAuth(IntermediateModel model) {
return model.getMetadata().getAuthType() == AuthType.BEARER;
}

private static boolean isServiceSigv4a(IntermediateModel model) {
return model.getMetadata().getAuth().stream().anyMatch(authType -> authType == AuthType.V4A);
}

private static boolean isServiceAwsAuthType(IntermediateModel model) {
AuthType authType = model.getMetadata().getAuthType();
return isAuthTypeAws(authType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ static List<TestCase> parameters() {
.classSpecProvider(ModelBasedAuthSchemeProviderSpec::new)
.caseName("ops-auth-sigv4a-value")
.outputFileSuffix("default-provider")
.build(),
TestCase.builder()
.modelProvider(ClientTestModels::opsWithSigv4a)
.classSpecProvider(AuthSchemeParamsSpec::new)
.caseName("ops-auth-sigv4a-value")
.outputFileSuffix("params")
.build(),
TestCase.builder()
.modelProvider(ClientTestModels::opsWithSigv4a)
.classSpecProvider(DefaultAuthSchemeParamsSpec::new)
.caseName("ops-auth-sigv4a-value")
.outputFileSuffix("default-params")
.build()
);
}
Expand All @@ -210,7 +222,7 @@ static class TestCase {
@Override
public String toString() {
return "TestCase{" +
"caseName='" + caseName + '\'' +
"caseName='" + caseName + "-" + outputFileSuffix + '\'' +
'}';
}

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

import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.http.auth.aws.signer.RegionSet;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.database.auth.scheme.DatabaseAuthSchemeParams;
import software.amazon.awssdk.utils.Validate;

@Generated("software.amazon.awssdk:codegen")
@SdkInternalApi
public final class DefaultDatabaseAuthSchemeParams implements DatabaseAuthSchemeParams {
private final String operation;

private final Region region;

private final RegionSet regionSet;

private DefaultDatabaseAuthSchemeParams(Builder builder) {
this.operation = Validate.paramNotNull(builder.operation, "operation");
this.region = builder.region;
this.regionSet = builder.regionSet;
}

public static DatabaseAuthSchemeParams.Builder builder() {
return new Builder();
}

@Override
public String operation() {
return operation;
}

@Override
public Region region() {
return region;
}

@Override
public RegionSet regionSet() {
return regionSet;
}

@Override
public DatabaseAuthSchemeParams.Builder toBuilder() {
return new Builder(this);
}

private static final class Builder implements DatabaseAuthSchemeParams.Builder {
private String operation;

private Region region;

private RegionSet regionSet;

Builder() {
}

Builder(DefaultDatabaseAuthSchemeParams params) {
this.operation = params.operation;
this.region = params.region;
this.regionSet = params.regionSet;
}

@Override
public Builder operation(String operation) {
this.operation = operation;
return this;
}

@Override
public Builder region(Region region) {
this.region = region;
return this;
}

@Override
public Builder regionSet(RegionSet regionSet) {
this.regionSet = regionSet;
return this;
}

@Override
public DatabaseAuthSchemeParams build() {
return new DefaultDatabaseAuthSchemeParams(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package software.amazon.awssdk.services.database.auth.scheme;

import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.http.auth.aws.signer.RegionSet;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.database.auth.scheme.internal.DefaultDatabaseAuthSchemeParams;
import software.amazon.awssdk.utils.builder.CopyableBuilder;
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;

/**
* The parameters object used to resolve the auth schemes for the Database service.
*/
@Generated("software.amazon.awssdk:codegen")
@SdkPublicApi
public interface DatabaseAuthSchemeParams extends ToCopyableBuilder<DatabaseAuthSchemeParams.Builder, DatabaseAuthSchemeParams> {
/**
* Get a new builder for creating a {@link DatabaseAuthSchemeParams}.
*/
static Builder builder() {
return DefaultDatabaseAuthSchemeParams.builder();
}

/**
* Returns the operation for which to resolve the auth scheme.
*/
String operation();

/**
* Returns the region. The region parameter may be used with the "aws.auth#sigv4" auth scheme.
*/
Region region();

/**
* Returns the RegionSet. The regionSet parameter may be used with the "aws.auth#sigv4a" auth scheme.
*/
RegionSet regionSet();

/**
* Returns a {@link Builder} to customize the parameters.
*/
Builder toBuilder();

/**
* A builder for a {@link DatabaseAuthSchemeParams}.
*/
interface Builder extends CopyableBuilder<Builder, DatabaseAuthSchemeParams> {
/**
* Set the operation for which to resolve the auth scheme.
*/
Builder operation(String operation);

/**
* Set the region. The region parameter may be used with the "aws.auth#sigv4" auth scheme.
*/
Builder region(Region region);

/**
* Set the RegionSet. The regionSet parameter may be used with the "aws.auth#sigv4a" auth scheme.
*/
Builder regionSet(RegionSet regionSet);

/**
* Returns a {@link DatabaseAuthSchemeParams} object that is created from the properties that have been set on
* the builder.
*/
DatabaseAuthSchemeParams build();
}
}

0 comments on commit 44348f2

Please sign in to comment.