Skip to content

Commit 895e0fd

Browse files
authored
Make conversion of multi-auth opt-in (#5011)
Currently, `metadata.auth` and `operation.auth` are read although full support for multi-auth is not implemented yet. This change allows reading these `auth` values to be opt-in, especially for `useSraAuth` tests. Also, allows conversions of select smithy auth trait shape IDs into `AuthType`s: - `smithy.api#httpBearerAuth => BEARER` - `smithy.api#noAuth => NONE` - `aws.auth#sigv4 => V4`
1 parent eca1665 commit 895e0fd

File tree

10 files changed

+173
-15
lines changed

10 files changed

+173
-15
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/AddMetadata.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.awssdk.codegen;
1717

1818
import java.util.Collections;
19+
import java.util.List;
1920
import java.util.Optional;
2021
import java.util.stream.Collectors;
2122
import software.amazon.awssdk.codegen.internal.Constant;
@@ -78,11 +79,7 @@ public static Metadata constructMetadata(ServiceModel serviceModel,
7879
.withSupportsH2(supportsH2(serviceMetadata))
7980
.withJsonVersion(getJsonVersion(metadata, serviceMetadata))
8081
.withAwsQueryCompatible(serviceMetadata.getAwsQueryCompatible())
81-
.withAuth(Optional.ofNullable(serviceMetadata.getAuth())
82-
.orElseGet(() -> Collections.singletonList(serviceMetadata.getSignatureVersion()))
83-
.stream()
84-
.map(AuthType::fromValue)
85-
.collect(Collectors.toList()));
82+
.withAuth(getAuthFromServiceMetadata(serviceMetadata, customizationConfig.useMultiAuth()));
8683

8784
return metadata;
8885
}
@@ -135,4 +132,20 @@ private static String getJsonVersion(Metadata metadata, ServiceMetadata serviceM
135132
return serviceMetadata.getJsonVersion();
136133
}
137134
}
135+
136+
/**
137+
* Converts service metadata into a list of AuthTypes. If useMultiAuth is enabled, then
138+
* {@code metadata.auth} will be used in the conversion if present. Otherwise, use
139+
* {@code metadata.signatureVersion}.
140+
*/
141+
private static List<AuthType> getAuthFromServiceMetadata(ServiceMetadata serviceMetadata,
142+
boolean useMultiAuth) {
143+
if (useMultiAuth) {
144+
List<String> serviceAuth = serviceMetadata.getAuth();
145+
if (serviceAuth != null) {
146+
return serviceAuth.stream().map(AuthType::fromValue).collect(Collectors.toList());
147+
}
148+
}
149+
return Collections.singletonList(AuthType.fromValue(serviceMetadata.getSignatureVersion()));
150+
}
138151
}

codegen/src/main/java/software/amazon/awssdk/codegen/AddOperations.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ final class AddOperations {
4747
private final NamingStrategy namingStrategy;
4848
private final Map<String, PaginatorDefinition> paginators;
4949
private final List<String> deprecatedShapes;
50+
private final boolean useMultiAuth;
5051

5152
AddOperations(IntermediateModelBuilder builder) {
5253
this.serviceModel = builder.getService();
5354
this.namingStrategy = builder.getNamingStrategy();
5455
this.paginators = builder.getPaginators().getPagination();
5556
this.deprecatedShapes = builder.getCustomConfig().getDeprecatedShapes();
57+
this.useMultiAuth = builder.getCustomConfig().useMultiAuth();
5658
}
5759

5860
private static boolean isAuthenticated(Operation op) {
@@ -234,13 +236,16 @@ public Map<String, OperationModel> constructOperations() {
234236
}
235237

236238
/**
237-
* Returns the list of authTypes defined for an operation. If the new auth member is defined we use it, otherwise we retrofit
238-
* the list with the value of the authType member if present or return an empty list if not.
239+
* Returns the list of authTypes defined for an operation. If useMultiAuth is enabled, then
240+
* {@code operation.auth} will be used in the conversion if present. Otherwise, use
241+
* {@code operation.authtype} if present.
239242
*/
240243
private List<AuthType> getAuthFromOperation(Operation op) {
241-
List<String> opAuth = op.getAuth();
242-
if (opAuth != null) {
243-
return opAuth.stream().map(AuthType::fromValue).collect(Collectors.toList());
244+
if (useMultiAuth) {
245+
List<String> opAuth = op.getAuth();
246+
if (opAuth != null) {
247+
return opAuth.stream().map(AuthType::fromValue).collect(Collectors.toList());
248+
}
244249
}
245250
AuthType legacyAuthType = op.getAuthtype();
246251
if (legacyAuthType != null) {

codegen/src/main/java/software/amazon/awssdk/codegen/model/config/customization/CustomizationConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ public class CustomizationConfig {
311311
*/
312312
private String rootPackageName;
313313

314+
/**
315+
* Set to true to read from c2j multi-auth values. Currently defaults to false.
316+
*
317+
* TODO(multi-auth): full multi-auth support is not implemented
318+
*/
319+
private boolean useMultiAuth;
320+
314321
private CustomizationConfig() {
315322
}
316323

@@ -828,4 +835,12 @@ public CustomizationConfig withRootPackageName(String packageName) {
828835
this.rootPackageName = packageName;
829836
return this;
830837
}
838+
839+
public void setUseMultiAuth(boolean useMultiAuth) {
840+
this.useMultiAuth = useMultiAuth;
841+
}
842+
843+
public boolean useMultiAuth() {
844+
return useMultiAuth;
845+
}
831846
}

codegen/src/main/java/software/amazon/awssdk/codegen/model/service/AuthType.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,21 @@ public String value() {
4141
}
4242

4343
public static AuthType fromValue(String value) {
44-
String normalizedValue = StringUtils.lowerCase(value);
45-
return Arrays.stream(values())
46-
.filter(authType -> authType.value.equals(normalizedValue))
47-
.findFirst()
48-
.orElseThrow(() -> new IllegalArgumentException(String.format("Unknown AuthType '%s'", normalizedValue)));
44+
switch (value) {
45+
// TODO(multi-auth): review conversion of smithy auth trait shape IDs
46+
case "smithy.api#httpBearerAuth":
47+
return BEARER;
48+
case "smithy.api#noAuth":
49+
return NONE;
50+
case "aws.auth#sigv4":
51+
return V4;
52+
default:
53+
String normalizedValue = StringUtils.lowerCase(value);
54+
return Arrays.stream(values())
55+
.filter(authType -> authType.value.equals(normalizedValue))
56+
.findFirst()
57+
.orElseThrow(() -> new IllegalArgumentException(
58+
String.format("Unknown AuthType '%s'", normalizedValue)));
59+
}
4960
}
5061
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
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+
*/
15+
16+
package software.amazon.awssdk.codegen.model.service;
17+
18+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
19+
import static org.hamcrest.CoreMatchers.equalTo;
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
22+
import org.junit.Test;
23+
24+
public class AuthTypeTest {
25+
@Test
26+
public void authTypeConvertBearer() {
27+
String smithyAuthTypeInput = "smithy.api#httpBearerAuth";
28+
String c2jAuthTypeInput = "bearer";
29+
30+
AuthType smithyAuthType = AuthType.fromValue(smithyAuthTypeInput);
31+
AuthType c2jAuthType = AuthType.fromValue(c2jAuthTypeInput);
32+
assertThat(smithyAuthType, equalTo(AuthType.BEARER));
33+
assertThat(c2jAuthType, equalTo(AuthType.BEARER));
34+
assertThat(smithyAuthType, equalTo(c2jAuthType));
35+
}
36+
37+
@Test
38+
public void authTypeConvertNone() {
39+
String smithyAuthTypeInput = "smithy.api#noAuth";
40+
String c2jAuthTypeInput = "none";
41+
42+
AuthType smithyAuthType = AuthType.fromValue(smithyAuthTypeInput);
43+
AuthType c2jAuthType = AuthType.fromValue(c2jAuthTypeInput);
44+
assertThat(smithyAuthType, equalTo(AuthType.NONE));
45+
assertThat(c2jAuthType, equalTo(AuthType.NONE));
46+
assertThat(smithyAuthType, equalTo(c2jAuthType));
47+
}
48+
49+
@Test
50+
public void authTypeConvertV4() {
51+
String smithyAuthTypeInput = "aws.auth#sigv4";
52+
String c2jAuthTypeInput = "v4";
53+
54+
AuthType smithyAuthType = AuthType.fromValue(smithyAuthTypeInput);
55+
AuthType c2jAuthType = AuthType.fromValue(c2jAuthTypeInput);
56+
assertThat(smithyAuthType, equalTo(AuthType.V4));
57+
assertThat(c2jAuthType, equalTo(AuthType.V4));
58+
assertThat(smithyAuthType, equalTo(c2jAuthType));
59+
}
60+
61+
@Test
62+
public void authTypeConvertCustom() {
63+
String c2jAuthTypeInput = "custom";
64+
65+
AuthType c2jAuthType = AuthType.fromValue(c2jAuthTypeInput);
66+
assertThat(c2jAuthType, equalTo(AuthType.CUSTOM));
67+
}
68+
69+
@Test
70+
public void authTypeConvertIam() {
71+
String c2jAuthTypeInput = "iam";
72+
73+
AuthType c2jAuthType = AuthType.fromValue(c2jAuthTypeInput);
74+
assertThat(c2jAuthType, equalTo(AuthType.IAM));
75+
}
76+
77+
@Test
78+
public void authTypeConvertV4UnsignedBody() {
79+
String c2jAuthTypeInput = "v4-unsigned-body";
80+
81+
AuthType c2jAuthType = AuthType.fromValue(c2jAuthTypeInput);
82+
assertThat(c2jAuthType, equalTo(AuthType.V4_UNSIGNED_BODY));
83+
}
84+
85+
@Test
86+
public void authTypeConvertS3() {
87+
String c2jAuthTypeInput = "s3";
88+
89+
AuthType c2jAuthType = AuthType.fromValue(c2jAuthTypeInput);
90+
assertThat(c2jAuthType, equalTo(AuthType.S3));
91+
}
92+
93+
@Test
94+
public void authTypeConvertS3v4() {
95+
String c2jAuthTypeInput = "s3v4";
96+
97+
AuthType c2jAuthType = AuthType.fromValue(c2jAuthTypeInput);
98+
assertThat(c2jAuthType, equalTo(AuthType.S3V4));
99+
}
100+
101+
@Test
102+
public void authTypeConvertUnknownAuthType() {
103+
String c2jAuthTypeInput = "unknown";
104+
105+
assertThatThrownBy(() -> AuthType.fromValue(c2jAuthTypeInput))
106+
.isInstanceOf(IllegalArgumentException.class)
107+
.hasMessage("Unknown AuthType 'unknown'");
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{
2+
"useMultiAuth": true
23
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{
2+
"useMultiAuth": true
23
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{
2+
"useMultiAuth": true
23
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{
2+
"useMultiAuth": true
23
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
{
2+
"useMultiAuth": true
23
}

0 commit comments

Comments
 (0)