forked from smithy-lang/smithy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validator to check consistency of resource name used for IamResou…
…rce (smithy-lang#1819) * Add validator to check consistency of resource name used for IamResource * Update arn parsing method to be private * Update smithy-aws-iam-traits/src/main/java/software/amazon/smithy/aws/iam/traits/IamResourceTraitValidator.java Co-authored-by: Michael Dowling <[email protected]> * Update smithy-aws-iam-traits/src/main/java/software/amazon/smithy/aws/iam/traits/IamResourceTraitValidator.java Co-authored-by: Michael Dowling <[email protected]> * Address PR comments * Add errorfiles test runner * Revert "Address PR comments" This reverts commit 3753ac2. * Re-add changes without accidentally popped stashed changes * Update to use orElseGet * Re-add change that was accidentally removed * Add newline at end of file --------- Co-authored-by: Michael Dowling <[email protected]>
- Loading branch information
Showing
9 changed files
with
135 additions
and
14 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...traits/src/main/java/software/amazon/smithy/aws/iam/traits/IamResourceTraitValidator.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,62 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.aws.iam.traits; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import software.amazon.smithy.aws.traits.ArnTrait; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.ResourceShape; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
import software.amazon.smithy.utils.StringUtils; | ||
|
||
/** | ||
* Ensures that any resource name defined in the {@link IamResourceTrait} is | ||
* consistent with the resource name used in any {@link ArnTrait} definition | ||
* applied to the resource. | ||
*/ | ||
@SmithyInternalApi | ||
public class IamResourceTraitValidator extends AbstractValidator { | ||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> results = new ArrayList<>(); | ||
for (ResourceShape resource : model.getResourceShapesWithTrait(IamResourceTrait.class)) { | ||
// If the resource has both the IamResourceTrait and Arn trait, | ||
// check that the resource name is consistent between the two traits | ||
if (resource.hasTrait(ArnTrait.class)) { | ||
String resourceName = resource.expectTrait(IamResourceTrait.class).getName() | ||
.orElseGet(() -> StringUtils.lowerCase(resource.getId().getName())); | ||
ArnTrait arnTrait = resource.expectTrait(ArnTrait.class); | ||
List<String> arnComponents = parseArnComponents(arnTrait.getTemplate()); | ||
if (!arnComponents.contains(resourceName)) { | ||
results.add(danger(resource, String.format( | ||
"The `@aws.iam#iamResource` trait applied to the resource " | ||
+ "defines an IAM resource name, `%s`, that does not match the `@arn` template, " | ||
+ "`%s`, of the resource.", | ||
resourceName, arnTrait.getTemplate()))); | ||
} | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
private List<String> parseArnComponents(String arnTemplate) { | ||
return Arrays.asList(arnTemplate.split("/")); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
software.amazon.smithy.aws.iam.traits.ConditionKeysValidator | ||
software.amazon.smithy.aws.iam.traits.IamResourceTraitValidator |
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
21 changes: 21 additions & 0 deletions
21
...hy-aws-iam-traits/src/test/java/software/amazon/smithy/aws/iam/traits/TestRunnerTest.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,21 @@ | ||
package software.amazon.smithy.aws.iam.traits; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import software.amazon.smithy.model.validation.testrunner.SmithyTestCase; | ||
import software.amazon.smithy.model.validation.testrunner.SmithyTestSuite; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.stream.Stream; | ||
|
||
public class TestRunnerTest { | ||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("source") | ||
public void testRunner(String filename, Callable<SmithyTestCase.Result> callable) throws Exception { | ||
callable.call(); | ||
} | ||
|
||
public static Stream<?> source() { | ||
return SmithyTestSuite.defaultParameterizedTestSource(TestRunnerTest.class); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ware/amazon/smithy/aws/iam/traits/errorfiles/condition-keys/invalid-condition-keys.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 @@ | ||
[ERROR] smithy.example#Operation: This operation scoped within the `smithy.example#MyService` service refers to an undefined condition key `foo:qux`. Expected one of the following defined condition keys: [`foo:baz`] | ConditionKeys |
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
...ftware/amazon/smithy/aws/iam/traits/errorfiles/iam-resources/invalid-iam-resources.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 @@ | ||
[DANGER] smithy.example#BadIamResourceName: The `@aws.iam#iamResource` trait applied to the resource defines an IAM resource name, `bad-iam-resourceName`, that does not match the `@arn` template, `bad-iam-resource-name/{id}`, of the resource. | IamResourceTrait | ||
[DANGER] smithy.example#IncompatibleResourceName: The `@aws.iam#iamResource` trait applied to the resource defines an IAM resource name, `IncompatibleResourceName`, that does not match the `@arn` template, `beer/{beerId}/incompatible-resource-name`, of the resource. | IamResourceTrait | ||
[DANGER] smithy.example#InvalidResource: The `@aws.iam#iamResource` trait applied to the resource defines an IAM resource name, `invalidResource`, that does not match the `@arn` template, `invalid-resource`, of the resource. | IamResourceTrait |
44 changes: 44 additions & 0 deletions
44
...ftware/amazon/smithy/aws/iam/traits/errorfiles/iam-resources/invalid-iam-resources.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,44 @@ | ||
$version: "2" | ||
namespace smithy.example | ||
|
||
use aws.api#arn | ||
|
||
@aws.api#service(sdkId: "My") | ||
@aws.iam#defineConditionKeys("foo:baz": {type: "String", documentation: "Foo baz"}) | ||
service MyService { | ||
version: "2019-02-20", | ||
resources: [ | ||
BadIamResourceName, | ||
Beer, | ||
InvalidResource | ||
] | ||
} | ||
|
||
@aws.iam#iamResource(name: "bad-iam-resourceName") | ||
@arn(template: "bad-iam-resource-name/{id}") | ||
resource BadIamResourceName { | ||
identifiers: { | ||
id: String | ||
} | ||
} | ||
|
||
@aws.iam#iamResource(name: "beer") | ||
@arn(template: "beer/{beerId}") | ||
resource Beer { | ||
identifiers: { | ||
beerId: String | ||
} | ||
resources: [IncompatibleResourceName] | ||
} | ||
|
||
@arn(template: "beer/{beerId}/incompatible-resource-name") | ||
@aws.iam#iamResource(name: "IncompatibleResourceName") | ||
resource IncompatibleResourceName { | ||
identifiers: { | ||
beerId: String | ||
} | ||
} | ||
|
||
@aws.iam#iamResource(name: "invalidResource") | ||
@arn(template: "invalid-resource") | ||
resource InvalidResource {} |
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