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

Fix IamPolicy code example #4229

Closed
tkhill-AWS opened this issue Jul 26, 2023 · 3 comments
Closed

Fix IamPolicy code example #4229

tkhill-AWS opened this issue Jul 26, 2023 · 3 comments
Labels
bug This issue is a bug. documentation This is a problem with documentation. p2 This is a standard priority issue

Comments

@tkhill-AWS
Copy link
Contributor

Describe the issue

The second code snippet in the Javadoc contains an error.

// 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));

    IamPolicy policy = IamPolicy.fromJson(getPolicyVersionResponse.policyVersion().document());

    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()));
}

The eighth line of the snippet:

IamPolicy policy = IamPolicy.fromJson(getPolicyVersionResponse.policyVersion().document());

does not work because getPolicyVersionResponse.policyVersion().document() returns a URL-encoded string.

I needed to change it to the following for the example to work:

            String decodedPolicy = URLDecoder.decode(getPolicyVersionResponse.policyVersion().document(), StandardCharsets.UTF_8);
            IamPolicy policy = IamPolicy.fromJson(decodedPolicy);

Links

https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/policybuilder/iam/IamPolicy.html

@tkhill-AWS tkhill-AWS added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Jul 26, 2023
@debora-ito
Copy link
Member

Hi Todd!

Thank you for reporting. We'll fix the docs.

@debora-ito debora-ito added bug This issue is a bug. p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Jul 27, 2023
@debora-ito
Copy link
Member

Fixed via #4324, will be available in the next release.

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. documentation This is a problem with documentation. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

2 participants