-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AccountId and CredentialScope AWS BuiltIns
* Add AccountId and CredentialScope BuiltIns * Fix checkstyle, make docs more vague * Add AwsBuiltinValidator * Cleanup from PR + add test cases * Change eventIdSuffix for AWS Builtin validator * PR feedback * Remove extra prefix from eventId * Formatting --------- Co-authored-by: Kevin Stich <kevin@kstich.com>
- Loading branch information
1 parent
428c8a1
commit c297774
Showing
7 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...in/java/software/amazon/smithy/rulesengine/aws/validators/RuleSetAwsBuiltInValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rulesengine.aws.validators; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import software.amazon.smithy.model.FromSourceLocation; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.rulesengine.aws.language.functions.AwsBuiltIns; | ||
import software.amazon.smithy.rulesengine.language.EndpointRuleSet; | ||
import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameter; | ||
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; | ||
import software.amazon.smithy.utils.SetUtils; | ||
|
||
|
||
/** | ||
* Validator that AWS built-ins used in RuleSet parameters are supported. | ||
*/ | ||
public class RuleSetAwsBuiltInValidator extends AbstractValidator { | ||
private static final Set<String> ADDITIONAL_CONSIDERATION_BUILT_INS = SetUtils.of( | ||
AwsBuiltIns.ACCOUNT_ID.getBuiltIn().get(), | ||
AwsBuiltIns.CREDENTIAL_SCOPE.getBuiltIn().get()); | ||
private static final String ADDITIONAL_CONSIDERATION_MESSAGE = "The `%s` built-in used requires additional " | ||
+ "consideration of the rules that use it."; | ||
|
||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
for (ServiceShape serviceShape : model.getServiceShapesWithTrait(EndpointRuleSetTrait.class)) { | ||
events.addAll(validateRuleSetAwsBuiltIns(serviceShape, serviceShape.expectTrait(EndpointRuleSetTrait.class) | ||
.getEndpointRuleSet())); | ||
} | ||
return events; | ||
} | ||
|
||
private List<ValidationEvent> validateRuleSetAwsBuiltIns(ServiceShape serviceShape, EndpointRuleSet ruleSet) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
for (Parameter parameter : ruleSet.getParameters()) { | ||
if (parameter.isBuiltIn()) { | ||
validateBuiltIn(serviceShape, parameter.getBuiltIn().get(), parameter).ifPresent(events::add); | ||
} | ||
} | ||
return events; | ||
} | ||
|
||
private Optional<ValidationEvent> validateBuiltIn( | ||
ServiceShape serviceShape, | ||
String builtInName, | ||
FromSourceLocation source | ||
) { | ||
if (ADDITIONAL_CONSIDERATION_BUILT_INS.contains(builtInName)) { | ||
return Optional.of(danger( | ||
serviceShape, source, | ||
String.format(ADDITIONAL_CONSIDERATION_MESSAGE, builtInName), | ||
builtInName)); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ts/src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
software.amazon.smithy.rulesengine.aws.validators.RuleSetAwsBuiltInValidator |
3 changes: 3 additions & 0 deletions
3
...ngine/aws/language/functions/errorfiles/invalid/additional-consideration-built-ins.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[WARNING] example#FizzBuzz: This shape applies a trait that is unstable: smithy.rules#endpointRuleSet | UnstableTrait | ||
[DANGER] example#FizzBuzz: The `AWS::Auth::AccountId` built-in used requires additional consideration of the rules that use it. | RuleSetAwsBuiltIn.AWS::Auth::AccountId | ||
[DANGER] example#FizzBuzz: The `AWS::Auth::CredentialScope` built-in used requires additional consideration of the rules that use it. | RuleSetAwsBuiltIn.AWS::Auth::CredentialScope |
39 changes: 39 additions & 0 deletions
39
...ngine/aws/language/functions/errorfiles/invalid/additional-consideration-built-ins.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
$version: "1.0" | ||
|
||
namespace example | ||
|
||
use smithy.rules#endpointRuleSet | ||
|
||
@endpointRuleSet({ | ||
"version": "1.3", | ||
"parameters": { | ||
"Region": { | ||
"required": true, | ||
"builtIn": "AWS::Region", | ||
"type": "String", | ||
"documentation": "docs" | ||
}, | ||
"AccountId": { | ||
"builtIn": "AWS::Auth::AccountId", | ||
"type": "String", | ||
"documentation": "docs" | ||
}, | ||
"CredentialScope": { | ||
"builtIn": "AWS::Auth::CredentialScope", | ||
"type": "String", | ||
"documentation": "docs" | ||
} | ||
}, | ||
"rules": [ | ||
{ | ||
"conditions": [], | ||
"documentation": "base rule", | ||
"endpoint": { | ||
"url": "https://{Region}.fizzbuzz.amazonaws.com", | ||
"headers": {} | ||
}, | ||
"type": "endpoint" | ||
} | ||
] | ||
}) | ||
service FizzBuzz {} |