From dccfedbad1bf4c341cf9a0ecb0f3dea322bc3ada Mon Sep 17 00:00:00 2001 From: Dongie Agnir Date: Wed, 2 Jul 2025 15:50:53 -0700 Subject: [PATCH 1/2] Throw ModelInvalidException for bearerAuth issues Change from IllegalStateException to ModelInvalidException for cases where the enableEnvironmentBearerToken customization is enabled but the service model does not support it. This commit adds a new error ID `INVALID_CODEGEN_CUSTOMIZATION` for these cases. --- .../poet/builder/BaseClientBuilderClass.java | 20 ++++++++++++++++--- .../validation/ModelInvalidException.java | 4 ++++ .../codegen/validation/ValidationEntry.java | 4 ++++ .../codegen/validation/ValidationErrorId.java | 3 +++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java index 96d95f3650f8..e02505d10df5 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java @@ -63,6 +63,10 @@ import software.amazon.awssdk.codegen.poet.rules.EndpointParamsKnowledgeIndex; import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils; import software.amazon.awssdk.codegen.utils.AuthUtils; +import software.amazon.awssdk.codegen.validation.ModelInvalidException; +import software.amazon.awssdk.codegen.validation.ValidationEntry; +import software.amazon.awssdk.codegen.validation.ValidationErrorId; +import software.amazon.awssdk.codegen.validation.ValidationErrorSeverity; import software.amazon.awssdk.core.SdkPlugin; import software.amazon.awssdk.core.checksums.RequestChecksumCalculation; import software.amazon.awssdk.core.checksums.RequestChecksumCalculationResolver; @@ -320,11 +324,21 @@ private MethodSpec mergeServiceDefaultsMethod() { private void configureEnvironmentBearerToken(MethodSpec.Builder builder) { if (!authSchemeSpecUtils.useSraAuth()) { - throw new IllegalStateException("The enableEnvironmentBearerToken customization requires SRA Auth."); + ValidationEntry entry = ValidationEntry.create(ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION, + ValidationErrorSeverity.DANGER, + "The enableEnvironmentBearerToken customization requires" + + " the SRA Auth customization."); + + throw ModelInvalidException.fromEntry(entry); } if (!AuthUtils.usesBearerAuth(model)) { - throw new IllegalStateException("The enableEnvironmentBearerToken customization requires the service to model and " - + "support smithy.api#httpBearerAuth."); + ValidationEntry entry = + ValidationEntry.create(ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION, + ValidationErrorSeverity.DANGER, + "The enableEnvironmentBearerToken customization requires the service to model" + + " and support smithy.api#httpBearerAuth."); + + throw ModelInvalidException.fromEntry(entry); } builder.addStatement("$T tokenFromEnv = new $T().getStringValue()", diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ModelInvalidException.java b/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ModelInvalidException.java index 28f482328253..85008d1385eb 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ModelInvalidException.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ModelInvalidException.java @@ -38,6 +38,10 @@ public static Builder builder() { return new Builder(); } + public static ModelInvalidException fromEntry(ValidationEntry entry) { + return builder().validationEntries(Collections.singletonList(entry)).build(); + } + public static class Builder { private List validationEntries; diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ValidationEntry.java b/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ValidationEntry.java index 4e84bd625185..0de23f3e62f5 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ValidationEntry.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ValidationEntry.java @@ -61,6 +61,10 @@ public ValidationEntry withDetailMessage(String detailMessage) { return this; } + public static ValidationEntry create(ValidationErrorId errorId, ValidationErrorSeverity severity, String detailMessage) { + return new ValidationEntry().withErrorId(errorId).withSeverity(severity).withDetailMessage(detailMessage); + } + @Override public String toString() { return ToString.builder("ValidationEntry") diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ValidationErrorId.java b/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ValidationErrorId.java index 37c488e11fc5..2952cf444902 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ValidationErrorId.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/validation/ValidationErrorId.java @@ -22,6 +22,9 @@ public enum ValidationErrorId { ), UNKNOWN_SHAPE_MEMBER("The model references an unknown shape member."), REQUEST_URI_NOT_FOUND("The request URI does not exist."), + + INVALID_CODEGEN_CUSTOMIZATION("A customization is enabled for this service that cannot be applied for the given service " + + "model.") ; private final String description; From 2f95f079e7e85badf0b88dae2d046d091c345fe7 Mon Sep 17 00:00:00 2001 From: Dongie Agnir Date: Wed, 2 Jul 2025 16:05:34 -0700 Subject: [PATCH 2/2] Review comments --- .changes/next-release/feature-AWSSDKforJavav2-960b6f3.json | 6 ++++++ .../awssdk/codegen/poet/builder/BaseClientBuilderClass.java | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 .changes/next-release/feature-AWSSDKforJavav2-960b6f3.json diff --git a/.changes/next-release/feature-AWSSDKforJavav2-960b6f3.json b/.changes/next-release/feature-AWSSDKforJavav2-960b6f3.json new file mode 100644 index 000000000000..aaa74d616f3c --- /dev/null +++ b/.changes/next-release/feature-AWSSDKforJavav2-960b6f3.json @@ -0,0 +1,6 @@ +{ + "type": "feature", + "category": "AWS SDK for Java v2", + "contributor": "", + "description": "Add validation for invalid usages of the `enableEnvironmentBearerToken` codegen customization." +} diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java index e02505d10df5..41fd6da98ae2 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/builder/BaseClientBuilderClass.java @@ -327,7 +327,7 @@ private void configureEnvironmentBearerToken(MethodSpec.Builder builder) { ValidationEntry entry = ValidationEntry.create(ValidationErrorId.INVALID_CODEGEN_CUSTOMIZATION, ValidationErrorSeverity.DANGER, "The enableEnvironmentBearerToken customization requires" - + " the SRA Auth customization."); + + " the useSraAuth customization but it is disabled."); throw ModelInvalidException.fromEntry(entry); }