AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2026-01-16 · Documentation low

File: code-library/latest/ug/controltower_example_controltower_EnableBaseline_section.md

Summary

Updated code examples for EnableBaseline across C#, Java, and Python SDKs. Changes include: 1) Modified return value documentation in C#; 2) Added identityCenterBaseline parameter conditional logic in C# and Python; 3) Added comprehensive Java async implementation with error handling; 4) Improved exception handling for 'already enabled' scenario; 5) Added parameter passing in Python SDK call.

Security assessment

The changes focus on general functionality improvements and error handling enhancements for baseline operations. There is no evidence of security vulnerability fixes, security incident references, or new security features being documented. The added parameter handling and error management are routine development improvements without security-specific context.

Diff

diff --git a/code-library/latest/ug/controltower_example_controltower_EnableBaseline_section.md b/code-library/latest/ug/controltower_example_controltower_EnableBaseline_section.md
index b950a505d..acdaef367 100644
--- a//code-library/latest/ug/controltower_example_controltower_EnableBaseline_section.md
+++ b//code-library/latest/ug/controltower_example_controltower_EnableBaseline_section.md
@@ -36 +36 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        /// <returns>The enabled baseline ARN or null if already enabled.</returns>
+        /// <returns>The enabled baseline ARN or null.</returns>
@@ -41 +41,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                var parameters = new List<EnabledBaselineParameter>
+                var parameters = new List<EnabledBaselineParameter>();
+                if (!string.IsNullOrEmpty(identityCenterBaseline))
@@ -42,0 +44 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+                    parameters.Add(
@@ -46,0 +49 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+                        });
@@ -48,2 +50,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                };
-    
@@ -75 +76 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            catch (ValidationException ex) when (ex.Message.Contains("already enabled"))
+            catch (ValidationException ex)
@@ -76,0 +78 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+                if (ex.Message.Contains("already enabled"))
@@ -77,0 +80,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+                else { Console.WriteLine(ex.Message); }
+                // Write the message and return null if baseline cannot be enabled.
@@ -94,0 +99,121 @@ 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). 
+    
+    
+        /**
+         * Asynchronously enables a baseline for the specified target if not already enabled.
+         *
+         * @param targetIdentifier       The ARN of the target (OU or account).
+         * @param baselineIdentifier     The baseline definition ARN to enable.
+         * @param baselineVersion        The baseline version to enable.
+         * @return A CompletableFuture containing the enabled baseline ARN, or null if already enabled.
+         */
+        public CompletableFuture<String> enableBaselineAsync(
+                String targetIdentifier,
+                String baselineIdentifier,
+                String baselineVersion
+        ) {
+            EnableBaselineRequest request = EnableBaselineRequest.builder()
+                    .baselineIdentifier(baselineIdentifier)
+                    .baselineVersion(baselineVersion)
+                    .targetIdentifier(targetIdentifier)
+                    .build();
+    
+            return getAsyncClient().enableBaseline(request)
+                    .handle((resp, exception) -> {
+                        if (exception != null) {
+                            Throwable cause = exception.getCause() != null ? exception.getCause() : exception;
+                            if (cause instanceof ControlTowerException e) {
+                                String code = e.awsErrorDetails() != null ? e.awsErrorDetails().errorCode() : "UNKNOWN";
+                                String msg = e.awsErrorDetails() != null ? e.awsErrorDetails().errorMessage() : e.getMessage();
+    
+                                if ("ValidationException".equals(code) && msg.contains("already enabled")) {
+                                    System.out.println("Baseline is already enabled for this target → fetching ARN...");
+                                    return fetchEnabledBaselineArn(targetIdentifier, baselineIdentifier)
+                                            .join(); // fetch existing ARN synchronously
+                                }
+    
+                                throw new RuntimeException("Error enabling baseline: " + code + " - " + msg, e);
+                            }
+    
+                            throw new RuntimeException("Unexpected error enabling baseline: " + cause.getMessage(), cause);
+                        }
+    
+                        return resp;
+                    })
+                    .thenCompose(result -> {
+                        if (result instanceof EnableBaselineResponse resp) {
+                            String operationId = resp.operationIdentifier();
+                            String enabledBaselineArn = resp.arn();
+                            System.out.println("Baseline enable started. ARN: " + enabledBaselineArn
+                                    + ", operation ID: " + operationId);
+    
+                            // Inline polling
+                            return CompletableFuture.supplyAsync(() -> {
+                                while (true) {
+                                    GetBaselineOperationRequest opReq = GetBaselineOperationRequest.builder()
+                                            .operationIdentifier(operationId)
+                                            .build();
+    
+                                    GetBaselineOperationResponse opResp = getAsyncClient().getBaselineOperation(opReq).join();
+                                    BaselineOperation op = opResp.baselineOperation();
+                                    BaselineOperationStatus status = op.status();
+                                    System.out.println("Operation " + operationId + " status: " + status);
+    
+                                    if (status == BaselineOperationStatus.SUCCEEDED) {
+                                        return enabledBaselineArn;
+                                    } else if (status == BaselineOperationStatus.FAILED) {
+                                        String opId = op.operationIdentifier();
+                                        String reason = op.statusMessage() != null ? op.statusMessage() : "No failure reason provided";
+                                        throw new RuntimeException("Baseline operation failed (ID: " + opId + "), status: "
+                                                + status + ", reason: " + reason);
+                                    }
+    
+                                    try {
+                                        Thread.sleep(Duration.ofSeconds(15).toMillis());
+                                    } catch (InterruptedException e) {
+                                        Thread.currentThread().interrupt();
+                                        throw new RuntimeException(e);
+                                    }
+                                }
+                            });
+                        } else if (result instanceof String existingArn) {
+                            // Already enabled branch
+                            return CompletableFuture.completedFuture(existingArn);
+                        }
+    
+                        return CompletableFuture.completedFuture(null);
+                    });
+        }
+    
+    
+        /**
+         * Fetches the ARN of an already-enabled baseline for the target asynchronously.
+         */
+        private CompletableFuture<String> fetchEnabledBaselineArn(String targetIdentifier, String baselineIdentifier) {
+            return getAsyncClient().listEnabledBaselines(ListEnabledBaselinesRequest.builder().build())
+                    .thenApply(listResp -> {
+                        for (EnabledBaselineSummary eb : listResp.enabledBaselines()) {
+                            if (baselineIdentifier.equals(eb.baselineIdentifier())
+                                    && targetIdentifier.equals(eb.targetIdentifier())) {
+                                return eb.arn();
+                            }
+                        }
+                        return null; // not yet available
+                    });
+        }
+    
+    
+
+  * For API details, see [EnableBaseline](https://docs.aws.amazon.com/goto/SdkForJavaV2/controltower-2018-05-10/EnableBaseline) in _AWS SDK for Java 2.x API Reference_. 
+
+
+
+
@@ -145,4 +270,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                response = self.controltower_client.enable_baseline(
-                    baselineIdentifier=baseline_identifier,
-                    baselineVersion=baseline_version,
-                    targetIdentifier=target_identifier,
+                # Only include parameters if identity_center_baseline is not empty
+                parameters = []
+                if identity_center_baseline:
@@ -154 +278,7 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                    ],
+                    ]
+                
+                response = self.controltower_client.enable_baseline(
+                    baselineIdentifier=baseline_identifier,
+                    baselineVersion=baseline_version,
+                    targetIdentifier=target_identifier,
+                    parameters=parameters,
@@ -170 +299,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                        return None
@@ -182 +311 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-                raise
+                return None