Skip to content

Fix generation of credentialType(bearer) for non-priority bearer operations #6201

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-8191b31.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Fix generation of credentialType(bearer) for non-priority bearer operations"
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ default String discoveredEndpoint(OperationModel opModel) {

default CodeBlock credentialType(OperationModel opModel, IntermediateModel model) {

if (AuthUtils.isOpBearerAuth(model, opModel)) {
if (AuthUtils.isOpBearerAuthPreferred(model, opModel)) {
return CodeBlock.of(".credentialType($T.TOKEN)\n", CredentialType.class);
} else {
return CodeBlock.of("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ public static boolean usesAwsAuth(IntermediateModel model) {
}

/**
* Returns {@code true} if the operation should use bearer auth.
* Returns {@code true} if and only if the operation should use bearer auth as the first preferred auth scheme.
*/
public static boolean isOpBearerAuth(IntermediateModel model, OperationModel opModel) {
if (opModel.getAuthType() == AuthType.BEARER) {
return true;
}
return isServiceBearerAuth(model) && hasNoAuthType(opModel);
public static boolean isOpBearerAuthPreferred(IntermediateModel model, OperationModel opModel) {
return opModel.getAuthType() == AuthType.BEARER // single modeled auth on operation is bearer
// auth array, first auth type is bearer
|| (opModel.getAuth() != null && !opModel.getAuth().isEmpty() && opModel.getAuth().get(0) == AuthType.BEARER)
// service is only bearer and operation doesn't override
|| (model.getMetadata().getAuthType() == AuthType.BEARER && hasNoAuthType(opModel))
// service is only bearer first and operation doesn't override
|| (model.getMetadata().getAuth() != null && !model.getMetadata().getAuth().isEmpty()
&& model.getMetadata().getAuth().get(0) == AuthType.BEARER && hasNoAuthType(opModel));
}

private static boolean isServiceBearerAuth(IntermediateModel model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,30 @@ private static Stream<Arguments> awsAuthServiceValues() {

@ParameterizedTest
@MethodSource("opValues")
public void testIfOperationIsBearerAuth(AuthType serviceAuthType, AuthType opAuthType, Boolean expectedResult) {
IntermediateModel model = modelWith(serviceAuthType);
OperationModel opModel = opModelWith(opAuthType);
assertThat(AuthUtils.isOpBearerAuth(model, opModel)).isEqualTo(expectedResult);
public void testIfOperationIsBearerAuthPreferred(AuthType serviceAuthType, List<AuthType> serviceAuth,
AuthType opAuthType, List<AuthType> opAuth,
Boolean expectedResult) {
IntermediateModel model = modelWith(serviceAuthType, serviceAuth);
OperationModel opModel = opModelWith(opAuthType, opAuth);
assertThat(AuthUtils.isOpBearerAuthPreferred(model, opModel)).isEqualTo(expectedResult);
}

private static Stream<Arguments> opValues() {
return Stream.of(Arguments.of(AuthType.BEARER, AuthType.BEARER, true),
Arguments.of(AuthType.BEARER, AuthType.S3V4, false),
Arguments.of(AuthType.BEARER, AuthType.NONE, true),
Arguments.of(AuthType.BEARER, null, true),
Arguments.of(AuthType.S3V4, AuthType.BEARER, true),
Arguments.of(AuthType.S3V4, AuthType.S3V4, false),
Arguments.of(AuthType.S3V4, AuthType.NONE, false),
Arguments.of(AuthType.S3V4, null, false));
return Stream.of(
Arguments.of(AuthType.BEARER, null, AuthType.BEARER, null, true),
Arguments.of(AuthType.BEARER, null, AuthType.S3V4, null, false),
Arguments.of(AuthType.BEARER, null, AuthType.NONE, null, true),
Arguments.of(AuthType.BEARER, null, null, null, true),
Arguments.of(AuthType.S3V4, null, AuthType.BEARER, null, true),
Arguments.of(AuthType.S3V4, null, AuthType.S3V4, null, false),
Arguments.of(AuthType.S3V4, null, AuthType.NONE, null, false),
Arguments.of(AuthType.S3V4, null, null, null, false),
Arguments.of(AuthType.S3V4, Arrays.asList(AuthType.S3V4, AuthType.BEARER), AuthType.S3V4, null, false),
Arguments.of(AuthType.S3V4, null, AuthType.S3V4, Arrays.asList(AuthType.S3V4, AuthType.BEARER), false),
Arguments.of(AuthType.S3V4, Arrays.asList(AuthType.BEARER, AuthType.S3V4), null, null, true),
Arguments.of(AuthType.S3V4, Arrays.asList(AuthType.BEARER, AuthType.S3V4), AuthType.S3V4, null, false),
Arguments.of(AuthType.S3V4, null, AuthType.S3V4, Arrays.asList(AuthType.BEARER, AuthType.S3V4), true)
);
}

private static OperationModel opModelWith(AuthType authType) {
Expand All @@ -102,6 +111,12 @@ private static OperationModel opModelWith(AuthType authType) {
return opModel;
}

private static OperationModel opModelWith(AuthType authType, List<AuthType> auth) {
OperationModel opModel = opModelWith(authType);
opModel.setAuth(auth);
return opModel;
}

private static IntermediateModel modelWith(AuthType authType) {
IntermediateModel model = new IntermediateModel();
Metadata metadata = new Metadata();
Expand Down
Loading