-
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 the aws.iam#supportedPrincipalTypes trait
This commit introduces a trait that indicates which IAM principal types can use a service or operation. This is useful for users of a service when writing IAM policies.
- Loading branch information
Showing
6 changed files
with
190 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
60 changes: 60 additions & 0 deletions
60
...its/src/main/java/software/amazon/smithy/aws/iam/traits/SupportedPrincipalTypesTrait.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,60 @@ | ||
/* | ||
* Copyright 2021 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.List; | ||
import software.amazon.smithy.model.FromSourceLocation; | ||
import software.amazon.smithy.model.SourceLocation; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.traits.StringListTrait; | ||
import software.amazon.smithy.utils.ToSmithyBuilder; | ||
|
||
public final class SupportedPrincipalTypesTrait extends StringListTrait | ||
implements ToSmithyBuilder<SupportedPrincipalTypesTrait> { | ||
public static final ShapeId ID = ShapeId.from("aws.iam#supportedPrincipalTypes"); | ||
|
||
public SupportedPrincipalTypesTrait(List<String> principals, FromSourceLocation sourceLocation) { | ||
super(ID, principals, sourceLocation); | ||
} | ||
|
||
public SupportedPrincipalTypesTrait(List<String> principals) { | ||
this(principals, SourceLocation.NONE); | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static final class Provider extends StringListTrait.Provider<SupportedPrincipalTypesTrait> { | ||
public Provider() { | ||
super(ID, SupportedPrincipalTypesTrait::new); | ||
} | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return builder().sourceLocation(getSourceLocation()).values(getValues()); | ||
} | ||
|
||
public static final class Builder extends StringListTrait.Builder<SupportedPrincipalTypesTrait, Builder> { | ||
private Builder() {} | ||
|
||
@Override | ||
public SupportedPrincipalTypesTrait build() { | ||
return new SupportedPrincipalTypesTrait(getValues(), getSourceLocation()); | ||
} | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
...src/test/java/software/amazon/smithy/aws/iam/traits/SupportedPrincipalTypesTraitTest.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,47 @@ | ||
/* | ||
* Copyright 2021 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 static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
|
||
public class SupportedPrincipalTypesTraitTest { | ||
@Test | ||
public void loadsFromModel() { | ||
Model result = Model.assembler() | ||
.discoverModels(getClass().getClassLoader()) | ||
.addImport(getClass().getResource("supported-principal-types.smithy")) | ||
.assemble() | ||
.unwrap(); | ||
|
||
Shape myService = result.expectShape(ShapeId.from("smithy.example#MyService")); | ||
Shape myOperation = result.expectShape(ShapeId.from("smithy.example#MyOperation")); | ||
|
||
assertTrue(myService.hasTrait(SupportedPrincipalTypesTrait.class)); | ||
assertThat(myService.expectTrait(SupportedPrincipalTypesTrait.class).getValues(), containsInAnyOrder( | ||
"IAMUser", "IAMRole")); | ||
|
||
assertTrue(myOperation.hasTrait(SupportedPrincipalTypesTrait.class)); | ||
assertThat(myOperation.expectTrait(SupportedPrincipalTypesTrait.class).getValues(), containsInAnyOrder( | ||
"Root", "FederatedUser")); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...src/test/resources/software/amazon/smithy/aws/iam/traits/supported-principal-types.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,9 @@ | ||
$version: "1.0" | ||
|
||
namespace smithy.example | ||
|
||
@aws.iam#supportedPrincipalTypes(["IAMUser", "IAMRole"]) | ||
operation MyService {} | ||
|
||
@aws.iam#supportedPrincipalTypes(["Root", "FederatedUser"]) | ||
operation MyOperation {} |