Skip to content
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

Add AccountId and CredentialScope BuiltIns #1993

Merged
merged 8 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 19 additions & 0 deletions docs/source-2.0/aws/rules-engine/built-ins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ Description
Type
``boolean``

.. _rules-engine-aws-built-ins-account-id:

``AWS::Auth::AccountId`` built-in
=========================
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These header formatting pieces on both of these need to match the length of the header they're for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - forgot to run make html - fixed (and fixed the duplicate account-id label).


Description
The AWS AccountId.
Type
``string``

.. _rules-engine-aws-built-ins-account-id:

``AWS::Auth::CredentialScope`` built-in
=========================

Description
The AWS Credential Scope.
Type
``string``

.. _rules-engine-aws-built-ins-s3-accelerate:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ public final class AwsBuiltIns {
.documentation("The AWS region used to dispatch the request.")
.build();

/**
* Built-in parameter representing the AccountId.
*/
public static final Parameter ACCOUNT_ID =
Parameter.builder()
.name("AccountId")
.type(ParameterType.STRING)
.builtIn("AWS::Auth::AccountId")
.documentation("The AWS AccountId used for the request.")
.build();

/**
* Built-in parameter representing the Credential Scope.
*/
public static final Parameter CREDENTIAL_SCOPE =
Parameter.builder()
.name("CredentialScope")
.type(ParameterType.STRING)
.builtIn("AWS::Auth::CredentialScope")
.documentation("The AWS Credential Scope used for the request.")
.build();

/**
* This MUST only be used by the S3 rules.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public List<Parameter> getBuiltIns() {
AwsBuiltIns.DUALSTACK,
AwsBuiltIns.FIPS,
AwsBuiltIns.REGION,
AwsBuiltIns.ACCOUNT_ID,
AwsBuiltIns.CREDENTIAL_SCOPE,
AwsBuiltIns.S3_ACCELERATE,
AwsBuiltIns.S3_DISABLE_MRAP,
AwsBuiltIns.S3_FORCE_PATH_STYLE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.Arrays;
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_APPROVAL_BUILT_INS = SetUtils.of(
AwsBuiltIns.ACCOUNT_ID.getName().toString(),
AwsBuiltIns.CREDENTIAL_SCOPE.getName().toString());

@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, "RuleSet")
alextwoods marked this conversation as resolved.
Show resolved Hide resolved
.ifPresent(events::add);
}
}
return events;
}

private Optional<ValidationEvent> validateBuiltIn(
ServiceShape serviceShape,
String builtInName,
FromSourceLocation source,
String... eventIdSuffixes
) {
if (ADDITIONAL_APPROVAL_BUILT_INS.contains(builtInName)) {
return Optional.of(danger(serviceShape, source, String.format(
kstich marked this conversation as resolved.
Show resolved Hide resolved
"The `%s` built-in used requires additional consideration of the rules that use it.",
builtInName),
String.join(".", Arrays.asList(eventIdSuffixes))));
alextwoods marked this conversation as resolved.
Show resolved Hide resolved
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
software.amazon.smithy.rulesengine.aws.validators.RuleSetAwsBuiltInValidator