Skip to content

Commit 95471ee

Browse files
committed
Ignore unknown properties on endpoints in endpoint rules.
1 parent 752d8ad commit 95471ee

File tree

7 files changed

+188
-3
lines changed

7 files changed

+188
-3
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/poet/rules2/CodeGeneratorVisitor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
import java.util.Arrays;
2222
import java.util.List;
2323
import java.util.Map;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2426
import software.amazon.awssdk.awscore.endpoints.AwsEndpointAttribute;
2527
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4AuthScheme;
2628
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4aAuthScheme;
2729
import software.amazon.awssdk.codegen.model.config.customization.KeyTypePair;
2830
import software.amazon.awssdk.endpoints.Endpoint;
2931

3032
public class CodeGeneratorVisitor extends WalkRuleExpressionVisitor {
33+
private static final Logger log = LoggerFactory.getLogger(CodeGeneratorVisitor.class);
34+
3135
private final CodeBlock.Builder builder;
3236
private final RuleRuntimeTypeMirror typeMirror;
3337
private final SymbolTable symbolTable;
@@ -293,7 +297,7 @@ public Void visitPropertiesExpression(PropertiesExpression e) {
293297
} else if (knownEndpointAttributes.containsKey(k)) {
294298
addAttributeBlock(k, v);
295299
} else {
296-
throw new RuntimeException("unknown endpoint property: " + k);
300+
log.warn("Ignoring unknown endpoint property: {}", k);
297301
}
298302
});
299303
return null;

codegen/src/test/java/software/amazon/awssdk/codegen/poet/ClientTestModels.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ public static IntermediateModel queryServiceModelsWithOverrideKnowProperties() {
159159
return new IntermediateModelBuilder(models).build();
160160
}
161161

162+
public static IntermediateModel queryServiceModelsWithUnknownEndpointProperties() {
163+
File serviceModel = new File(ClientTestModels.class.getResource("client/c2j/query/service-2.json").getFile());
164+
File waitersModel = new File(ClientTestModels.class.getResource("client/c2j/query/waiters-2.json").getFile());
165+
File endpointRuleSetModel =
166+
new File(ClientTestModels.class.getResource("client/c2j/query/endpoint-rule-set-unknown-properties.json").getFile());
167+
File endpointTestsModel =
168+
new File(ClientTestModels.class.getResource("client/c2j/query/endpoint-tests.json").getFile());
169+
170+
C2jModels models = C2jModels
171+
.builder()
172+
.serviceModel(getServiceModel(serviceModel))
173+
.waitersModel(getWaiters(waitersModel))
174+
.customizationConfig(CustomizationConfig.create())
175+
.endpointRuleSetModel(getEndpointRuleSet(endpointRuleSetModel))
176+
.endpointTestSuiteModel(getEndpointTestSuite(endpointTestsModel))
177+
.build();
178+
179+
return new IntermediateModelBuilder(models).build();
180+
}
181+
162182
public static IntermediateModel queryServiceModelsEndpointAuthParamsWithAllowList() {
163183
File serviceModel = new File(ClientTestModels.class.getResource("client/c2j/query/service-2.json").getFile());
164184
File customizationModel =

codegen/src/test/java/software/amazon/awssdk/codegen/poet/rules/EndpointProviderCompiledRulesClassSpecTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ void knowPropertiesOverride() {
3737
new EndpointProviderSpec2(ClientTestModels.queryServiceModelsWithOverrideKnowProperties());
3838
assertThat(endpointProviderSpec, generatesTo("endpoint-provider-know-prop-override-class.java"));
3939
}
40+
41+
@Test
42+
void unknownEndpointProperties() {
43+
ClassSpec endpointProviderSpec =
44+
new EndpointProviderSpec2(ClientTestModels.queryServiceModelsWithUnknownEndpointProperties());
45+
assertThat(endpointProviderSpec, generatesTo("endpoint-provider-unknown-property-class.java"));
46+
}
4047
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"version": "1.0",
3+
"parameters": {
4+
"Endpoint": {
5+
"builtIn": "SDK::Endpoint",
6+
"required": false,
7+
"documentation": "Override the endpoint used to send this request",
8+
"type": "String"
9+
}
10+
},
11+
"rules": [
12+
{
13+
"conditions": [
14+
{
15+
"fn": "isSet",
16+
"argv": [
17+
{
18+
"ref": "Endpoint"
19+
}
20+
]
21+
}
22+
],
23+
"rules": [
24+
{
25+
"conditions": [],
26+
"endpoint": {
27+
"url": {
28+
"ref": "Endpoint"
29+
},
30+
"properties": {
31+
"unknownProperty": "value"
32+
},
33+
"headers": {}
34+
},
35+
"type": "endpoint"
36+
}
37+
],
38+
"type": "tree"
39+
},
40+
{
41+
"conditions": [],
42+
"error": "Invalid Configuration: Missing Endpoint",
43+
"type": "error"
44+
}
45+
]
46+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.services.query.endpoints.internal;
17+
18+
import java.net.URI;
19+
import java.util.concurrent.CompletableFuture;
20+
import software.amazon.awssdk.annotations.Generated;
21+
import software.amazon.awssdk.annotations.SdkInternalApi;
22+
import software.amazon.awssdk.core.exception.SdkClientException;
23+
import software.amazon.awssdk.endpoints.Endpoint;
24+
import software.amazon.awssdk.services.query.endpoints.QueryEndpointParams;
25+
import software.amazon.awssdk.services.query.endpoints.QueryEndpointProvider;
26+
import software.amazon.awssdk.utils.CompletableFutureUtils;
27+
28+
@Generated("software.amazon.awssdk:codegen")
29+
@SdkInternalApi
30+
public final class DefaultQueryEndpointProvider implements QueryEndpointProvider {
31+
@Override
32+
public CompletableFuture<Endpoint> resolveEndpoint(QueryEndpointParams params) {
33+
try {
34+
RuleResult result = endpointRule0(params, new LocalState());
35+
if (result.canContinue()) {
36+
throw SdkClientException.create("Rule engine did not reach an error or endpoint result");
37+
}
38+
if (result.isError()) {
39+
String errorMsg = result.error();
40+
if (errorMsg.contains("Invalid ARN") && errorMsg.contains(":s3:::")) {
41+
errorMsg += ". Use the bucket name instead of simple bucket ARNs in GetBucketLocationRequest.";
42+
}
43+
throw SdkClientException.create(errorMsg);
44+
}
45+
return CompletableFuture.completedFuture(result.endpoint());
46+
} catch (Exception error) {
47+
return CompletableFutureUtils.failedFuture(error);
48+
}
49+
}
50+
51+
private static RuleResult endpointRule0(QueryEndpointParams params, LocalState locals) {
52+
RuleResult result = endpointRule1(params, locals);
53+
if (result.isResolved()) {
54+
return result;
55+
}
56+
return endpointRule3(params, locals);
57+
}
58+
59+
private static RuleResult endpointRule1(QueryEndpointParams params, LocalState locals) {
60+
if (params.endpoint() != null) {
61+
return endpointRule2(params, locals);
62+
}
63+
return RuleResult.carryOn();
64+
}
65+
66+
private static RuleResult endpointRule2(QueryEndpointParams params, LocalState locals) {
67+
return RuleResult.endpoint(Endpoint.builder().url(URI.create(params.endpoint())).build());
68+
}
69+
70+
private static RuleResult endpointRule3(QueryEndpointParams params, LocalState locals) {
71+
return RuleResult.error("Invalid Configuration: Missing Endpoint");
72+
}
73+
74+
@Override
75+
public boolean equals(Object rhs) {
76+
return rhs != null && getClass().equals(rhs.getClass());
77+
}
78+
79+
@Override
80+
public int hashCode() {
81+
return getClass().hashCode();
82+
}
83+
84+
private static final class LocalState {
85+
LocalState() {
86+
}
87+
88+
LocalState(LocalStateBuilder builder) {
89+
}
90+
91+
public LocalStateBuilder toBuilder() {
92+
return new LocalStateBuilder(this);
93+
}
94+
}
95+
96+
private static final class LocalStateBuilder {
97+
LocalStateBuilder() {
98+
}
99+
100+
LocalStateBuilder(LocalState locals) {
101+
}
102+
103+
LocalState build() {
104+
return new LocalState(this);
105+
}
106+
}
107+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"enableGenerateCompiledEndpointRules": true
2+
"enableGenerateCompiledEndpointRules": true,
3+
"enableEnvironmentBearerToken": true
34
}

services/bedrockruntime/src/main/resources/codegen-resources/service-2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version":"2.0",
33
"metadata":{
44
"apiVersion":"2023-09-30",
5-
"auth":["aws.auth#sigv4"],
5+
"auth":["aws.auth#sigv4", "smithy.api#httpBearerAuth"],
66
"endpointPrefix":"bedrock-runtime",
77
"protocol":"rest-json",
88
"protocolSettings":{"h2":"optional"},

0 commit comments

Comments
 (0)