Interface IamPolicy
- All Superinterfaces:
ToCopyableBuilder<IamPolicy.Builder,
IamPolicy>
- All Known Implementing Classes:
DefaultIamPolicy
By default, all requests to use your resource coming from anyone but you are denied. Access control polices can override that by allowing different types of access to your resources, or by explicitly denying different types of access.
Each statement in an AWS access control policy takes the form: "A has permission to do B to C where D applies".
- A is the principal - the AWS account that is making a request to access or modify one of your AWS resources.
- B is the action - the way in which your AWS resource is being accessed or modified, such as sending a message to an Amazon SQS queue, or storing an object in an Amazon S3 bucket.
- C is the resource - your AWS entity that the principal wants to access, such as an Amazon SQS queue, or an object stored in Amazon S3.
- D is the set of conditions - optional constraints that specify when to allow or deny access for the principal to access your resource. Many expressive conditions are available, some specific to each service. For example you can use date conditions to allow access to your resources only after or before a specific time.
For more information, see The IAM User Guide
Usage Examples
Create a new IAM identity policy that allows a role to write items to an Amazon DynamoDB table.// IamClient requires a dependency on software.amazon.awssdk:iam
try (IamClient iam = IamClient.builder().region(Region.AWS_GLOBAL).build()) {
IamPolicy policy =
IamPolicy.builder()
.addStatement(IamStatement.builder()
.effect(IamEffect.ALLOW)
.addAction("dynamodb:PutItem")
.addResource("arn:aws:dynamodb:us-east-2:123456789012:table/books")
.build())
.build();
iam.createPolicy(r -> r.policyName("AllowWriteBookMetadata")
.policyDocument(policy.toJson()));
}
Download the policy uploaded in the previous example and create a new policy with "read" access added to it.
// IamClient requires a dependency on software.amazon.awssdk:iam
try (IamClient iam = IamClient.builder().region(Region.AWS_GLOBAL).build()) {
String policyArn = "arn:aws:iam::123456789012:policy/AllowWriteBookMetadata";
GetPolicyResponse getPolicyResponse = iam.getPolicy(r -> r.policyArn(policyArn));
String policyVersion = getPolicyResponse.defaultVersionId();
GetPolicyVersionResponse getPolicyVersionResponse =
iam.getPolicyVersion(r -> r.policyArn(policyArn).versionId(policyVersion));
String decodedPolicy = URLDecoder.decode(getPolicyVersionResponse.policyVersion().document(), StandardCharsets.UTF_8);
IamPolicy policy = IamPolicy.fromJson(decodedPolicy);
IamStatement newStatement = policy.statements().get(0).copy(s -> s.addAction("dynamodb:GetItem"));
IamPolicy newPolicy = policy.copy(p -> p.statements(Arrays.asList(newStatement)));
iam.createPolicy(r -> r.policyName("AllowReadWriteBookMetadata")
.policyDocument(newPolicy.toJson()));
}
- See Also:
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic IamPolicy.Builder
builder()
Create aIamPolicy.Builder
for anIamPolicy
.static IamPolicy
create
(Collection<IamStatement> statements) Create anIamPolicy
containing the provided statements.static IamPolicy
Create anIamPolicy
from an IAM policy in JSON form.id()
Retrieve the value set byIamPolicy.Builder.id(String)
.Retrieve the value set byIamPolicy.Builder.statements(Collection)
.toJson()
Convert this policy to the JSON format that is accepted by AWS services.toJson
(IamPolicyWriter writer) Convert this policy to the JSON format that is accepted by AWS services, using the provided writer.version()
Retrieve the value set byIamPolicy.Builder.version(String)
.Methods inherited from interface software.amazon.awssdk.utils.builder.ToCopyableBuilder
copy, toBuilder
-
Method Details
-
fromJson
Create anIamPolicy
from an IAM policy in JSON form.This will raise an exception if the provided JSON is invalid or does not appear to represent a valid policy document.
This is equivalent to
IamPolicyReader.create().read(json)
. -
create
Create anIamPolicy
containing the provided statements.At least one statement is required.
This is equivalent to
IamPolicy.builder().statements(statements).build()
-
builder
Create aIamPolicy.Builder
for anIamPolicy
. -
id
String id()Retrieve the value set byIamPolicy.Builder.id(String)
. -
version
String version()Retrieve the value set byIamPolicy.Builder.version(String)
. -
statements
List<IamStatement> statements()Retrieve the value set byIamPolicy.Builder.statements(Collection)
. -
toJson
String toJson()Convert this policy to the JSON format that is accepted by AWS services.This is equivalent to
IamPolicyWriter.create().writeToString(policy)
IamPolicy policy = IamPolicy.builder() .addStatement(IamStatement.builder() .effect(IamEffect.ALLOW) .addAction("dynamodb:PutItem") .addResource("arn:aws:dynamodb:us-east-2:123456789012:table/books") .build()) .build(); System.out.println("Policy:\n" + policy.toJson());
-
toJson
Convert this policy to the JSON format that is accepted by AWS services, using the provided writer.This is equivalent to
writer.writeToString(policy)
IamPolicyWriter prettyWriter = IamPolicyWriter.builder() .prettyPrint(true) .build(); IamPolicy policy = IamPolicy.builder() .addStatement(IamStatement.builder() .effect(IamEffect.ALLOW) .addAction("dynamodb:PutItem") .addResource("arn:aws:dynamodb:us-east-2:123456789012:table/books") .build()) .build(); System.out.println("Policy:\n" + policy.toJson(prettyWriter));
-