AWS code-library documentation change
Summary
Added Java SDK 2.x code example demonstrating how to list AWS Control Tower baselines, including error handling for AccessDeniedException
Security assessment
The change adds a routine code example for Control Tower API usage. While it includes AccessDeniedException handling, this is standard permission error handling and doesn't indicate a specific security vulnerability or weakness being addressed. No security features or vulnerabilities are documented.
Diff
diff --git a/code-library/latest/ug/controltower_example_controltower_Hello_section.md b/code-library/latest/ug/controltower_example_controltower_Hello_section.md index 3e64abd3d..068b090d7 100644 --- a//code-library/latest/ug/controltower_example_controltower_Hello_section.md +++ b//code-library/latest/ug/controltower_example_controltower_Hello_section.md @@ -98,0 +99,66 @@ There's more on GitHub. Find the complete example and learn how to set up and ru +Java + + +**SDK for Java 2.x** + + +###### Note + +There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/controltower#code-examples). + + + public class HelloControlTower { + + public static void main(String[] args) { + try { + ControlTowerClient controlTowerClient = ControlTowerClient.builder() + .build() ; + helloControlTower(controlTowerClient); + } catch (ControlTowerException e) { + System.out.println("Control Tower error occurred: " + e.awsErrorDetails().errorMessage()); + } + } + + /** + * Use the AWS SDK for Java (v2) to create an AWS Control Tower client + * and list all available baselines. + * This example uses the default settings specified in your shared credentials + * and config files. + * + * @param controlTowerClient A ControlTowerClient object. This object wraps + * the low-level AWS Control Tower service API. + */ + public static void helloControlTower(ControlTowerClient controlTowerClient) { + System.out.println("Hello, AWS Control Tower! Let's list available baselines:\n"); + + ListBaselinesIterable paginator = controlTowerClient.listBaselinesPaginator( + ListBaselinesRequest.builder().build()); + List<String> baselineNames = new ArrayList<>(); + + try { + paginator.stream() + .flatMap(response -> response.baselines().stream()) + .forEach(baseline -> baselineNames.add(baseline.name())); + + System.out.println(baselineNames.size() + " baseline(s) retrieved."); + for (String baselineName : baselineNames) { + System.out.println("\t" + baselineName); + } + + } catch (ControlTowerException e) { + if ("AccessDeniedException".equals(e.awsErrorDetails().errorCode())) { + System.out.println("Access denied. Please ensure you have the necessary permissions."); + } else { + System.out.println("An error occurred: " + e.getMessage()); + } + } + } + } + + + + * For API details, see [ListBaselines](https://docs.aws.amazon.com/goto/SdkForJavaV2/controltower-2018-05-10/ListBaselines) in _AWS SDK for Java 2.x API Reference_. + + + +