|
| 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.smithy.aws.iam.traits; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import software.amazon.smithy.aws.traits.ArnTrait; |
| 22 | +import software.amazon.smithy.model.Model; |
| 23 | +import software.amazon.smithy.model.shapes.ResourceShape; |
| 24 | +import software.amazon.smithy.model.validation.AbstractValidator; |
| 25 | +import software.amazon.smithy.model.validation.ValidationEvent; |
| 26 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 27 | +import software.amazon.smithy.utils.StringUtils; |
| 28 | + |
| 29 | +/** |
| 30 | + * Ensures that any resource name defined in the {@link IamResourceTrait} is |
| 31 | + * consistent with the resource name used in any {@link ArnTrait} definition |
| 32 | + * applied to the resource. |
| 33 | + */ |
| 34 | +@SmithyInternalApi |
| 35 | +public class IamResourceTraitValidator extends AbstractValidator { |
| 36 | + @Override |
| 37 | + public List<ValidationEvent> validate(Model model) { |
| 38 | + List<ValidationEvent> results = new ArrayList<>(); |
| 39 | + for (ResourceShape resource : model.getResourceShapesWithTrait(IamResourceTrait.class)) { |
| 40 | + // If the resource has both the IamResourceTrait and Arn trait, |
| 41 | + // check that the resource name is consistent between the two traits |
| 42 | + if (resource.hasTrait(ArnTrait.class)) { |
| 43 | + String resourceName = resource.expectTrait(IamResourceTrait.class).getName() |
| 44 | + .orElseGet(() -> StringUtils.lowerCase(resource.getId().getName())); |
| 45 | + ArnTrait arnTrait = resource.expectTrait(ArnTrait.class); |
| 46 | + List<String> arnComponents = parseArnComponents(arnTrait.getTemplate()); |
| 47 | + if (!arnComponents.contains(resourceName)) { |
| 48 | + results.add(danger(resource, String.format( |
| 49 | + "The `@aws.iam#iamResource` trait applied to the resource " |
| 50 | + + "defines an IAM resource name, `%s`, that does not match the `@arn` template, " |
| 51 | + + "`%s`, of the resource.", |
| 52 | + resourceName, arnTrait.getTemplate()))); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return results; |
| 57 | + } |
| 58 | + |
| 59 | + private List<String> parseArnComponents(String arnTemplate) { |
| 60 | + return Arrays.asList(arnTemplate.split("/")); |
| 61 | + } |
| 62 | +} |
0 commit comments