Skip to content

Throw ModelInvalidException for bearerAuth issues #6229

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

Merged
merged 3 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidationEntry> validationEntries;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading