From 8f5f3aaa17d76d3a9ce01e3c079918df55135b9b Mon Sep 17 00:00:00 2001 From: Jessie Zhang Date: Wed, 24 Jul 2024 13:36:21 -0700 Subject: [PATCH] Fix SpotBug issues --- aws-organizations-account/pom.xml | 3 +++ .../organizations/account/BaseHandlerStd.java | 4 ++-- .../organizations/account/CallbackContext.java | 15 ++++++++++++++- aws-organizations-organization/pom.xml | 3 +++ .../organization/BaseHandlerStd.java | 4 ++-- .../organization/CallbackContext.java | 13 ++++++++++++- aws-organizations-organizationalunit/pom.xml | 3 +++ .../organizationalunit/BaseHandlerStd.java | 4 ++-- .../organizationalunit/CallbackContext.java | 14 +++++++++++++- aws-organizations-policy/pom.xml | 3 +++ .../organizations/policy/BaseHandlerStd.java | 4 ++-- .../organizations/policy/CallbackContext.java | 15 ++++++++++++++- aws-organizations-resourcepolicy/pom.xml | 3 +++ .../resourcepolicy/BaseHandlerStd.java | 4 ++-- .../resourcepolicy/CallbackContext.java | 14 +++++++++++++- 15 files changed, 91 insertions(+), 15 deletions(-) diff --git a/aws-organizations-account/pom.xml b/aws-organizations-account/pom.xml index a84e0ec..46b01a2 100644 --- a/aws-organizations-account/pom.xml +++ b/aws-organizations-account/pom.xml @@ -175,6 +175,9 @@ org.apache.maven.plugins maven-surefire-plugin 3.0.0-M3 + + --add-opens java.base/java.util=ALL-UNNAMED + org.jacoco diff --git a/aws-organizations-account/src/main/java/software/amazon/organizations/account/BaseHandlerStd.java b/aws-organizations-account/src/main/java/software/amazon/organizations/account/BaseHandlerStd.java index 2f6923c..785be63 100644 --- a/aws-organizations-account/src/main/java/software/amazon/organizations/account/BaseHandlerStd.java +++ b/aws-organizations-account/src/main/java/software/amazon/organizations/account/BaseHandlerStd.java @@ -43,6 +43,7 @@ public abstract class BaseHandlerStd extends BaseHandler { protected static final String ACCOUNT_CREATION_STATUS_SUCCEEDED = "SUCCEEDED"; protected static final String ACCOUNT_CREATION_STATUS_FAILED = "FAILED"; // ExponentialBackoffJitter Constants + private static final Random RANDOM = new Random(); protected static final double RANDOMIZATION_FACTOR = 0.5; protected static final double RANDOMIZATION_FACTOR_FOR_DESCRIBE_CREATE_ACCOUNT_STATUS = 0.2; protected static final int BASE_DELAY = 15; // in second @@ -162,9 +163,8 @@ public ProgressEvent handleErrorTranslation( } public final int computeDelayBeforeNextRetry(int retryAttempt, int baseDelay, double randomizationFactor) { - Random random = new Random(); int exponentialBackoff = (int) Math.pow(2, retryAttempt) * baseDelay; - int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * randomizationFactor)); + int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * randomizationFactor)); return exponentialBackoff + jitter; } diff --git a/aws-organizations-account/src/main/java/software/amazon/organizations/account/CallbackContext.java b/aws-organizations-account/src/main/java/software/amazon/organizations/account/CallbackContext.java index 8e7d3a1..a5f8444 100644 --- a/aws-organizations-account/src/main/java/software/amazon/organizations/account/CallbackContext.java +++ b/aws-organizations-account/src/main/java/software/amazon/organizations/account/CallbackContext.java @@ -8,17 +8,30 @@ @lombok.Getter @lombok.Setter @lombok.ToString -@lombok.EqualsAndHashCode(callSuper = true) +@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap") public class CallbackContext extends StdCallbackContext { private Map actionToRetryAttemptMap = new HashMap<>(); + + // Manually implement the setter with a defensive copy + public void setActionToRetryAttemptMap(Map actionToRetryAttemptMap) { + this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap); + } + + // Manually implement the getter with a defensive copy + public Map getActionToRetryAttemptMap() { + return new HashMap<>(actionToRetryAttemptMap); + } + public int getCurrentRetryAttempt(final AccountConstants.Action actionName, final AccountConstants.Handler handlerName) { String key = actionName.toString() + handlerName.toString(); return this.actionToRetryAttemptMap.getOrDefault(key, 0); } + public void setCurrentRetryAttempt(final AccountConstants.Action actionName, final AccountConstants.Handler handlerName) { String key = actionName.toString() + handlerName.toString(); this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName)+1); } + // used in CREATE handler private boolean isAccountCreated = false; private String createAccountRequestId; diff --git a/aws-organizations-organization/pom.xml b/aws-organizations-organization/pom.xml index 2aaf0e3..23a19c3 100644 --- a/aws-organizations-organization/pom.xml +++ b/aws-organizations-organization/pom.xml @@ -174,6 +174,9 @@ org.apache.maven.plugins maven-surefire-plugin 3.0.0-M3 + + --add-opens java.base/java.util=ALL-UNNAMED + org.jacoco diff --git a/aws-organizations-organization/src/main/java/software/amazon/organizations/organization/BaseHandlerStd.java b/aws-organizations-organization/src/main/java/software/amazon/organizations/organization/BaseHandlerStd.java index 43210bd..bae44ed 100644 --- a/aws-organizations-organization/src/main/java/software/amazon/organizations/organization/BaseHandlerStd.java +++ b/aws-organizations-organization/src/main/java/software/amazon/organizations/organization/BaseHandlerStd.java @@ -26,6 +26,7 @@ import java.util.Random; public abstract class BaseHandlerStd extends BaseHandler { + private static final Random RANDOM = new Random(); private static final double RANDOMIZATION_FACTOR = 0.5; private static final int BASE_DELAY = 15; //in seconds private static final int MAX_RETRY_ATTEMPT_FOR_RETRIABLE_EXCEPTION = 2; @@ -125,9 +126,8 @@ && isRetriableException(e)) { } public final int computeDelayBeforeNextRetry(int retryAttempt) { - Random random = new Random(); int exponentialBackoff = (int) Math.pow(2, retryAttempt) * BASE_DELAY; - int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR)); + int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR)); return exponentialBackoff + jitter; } diff --git a/aws-organizations-organization/src/main/java/software/amazon/organizations/organization/CallbackContext.java b/aws-organizations-organization/src/main/java/software/amazon/organizations/organization/CallbackContext.java index 6d6ea8d..86da7bd 100644 --- a/aws-organizations-organization/src/main/java/software/amazon/organizations/organization/CallbackContext.java +++ b/aws-organizations-organization/src/main/java/software/amazon/organizations/organization/CallbackContext.java @@ -8,10 +8,20 @@ @lombok.Getter @lombok.Setter @lombok.ToString -@lombok.EqualsAndHashCode(callSuper = true) +@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap") public class CallbackContext extends StdCallbackContext { private Map actionToRetryAttemptMap = new HashMap<>(); + // Manually implement the setter with a defensive copy + public void setActionToRetryAttemptMap(Map actionToRetryAttemptMap) { + this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap); + } + + // Manually implement the getter with a defensive copy + public Map getActionToRetryAttemptMap() { + return new HashMap<>(actionToRetryAttemptMap); + } + // Used to set Propagation Delay in the CreateHandler call chain. public boolean propagationDelay = false; // used in CREATE handler re-invoking @@ -21,6 +31,7 @@ public int getCurrentRetryAttempt(final OrganizationConstants.Action actionName, String key = actionName.toString() + handlerName.toString(); return this.actionToRetryAttemptMap.getOrDefault(key, 0); } + public void setCurrentRetryAttempt(final OrganizationConstants.Action actionName, final OrganizationConstants.Handler handlerName) { String key = actionName.toString() + handlerName.toString(); this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName) + 1); diff --git a/aws-organizations-organizationalunit/pom.xml b/aws-organizations-organizationalunit/pom.xml index 388f06f..b11f863 100644 --- a/aws-organizations-organizationalunit/pom.xml +++ b/aws-organizations-organizationalunit/pom.xml @@ -175,6 +175,9 @@ org.apache.maven.plugins maven-surefire-plugin 3.0.0-M3 + + --add-opens java.base/java.util=ALL-UNNAMED + org.jacoco diff --git a/aws-organizations-organizationalunit/src/main/java/software/amazon/organizations/organizationalunit/BaseHandlerStd.java b/aws-organizations-organizationalunit/src/main/java/software/amazon/organizations/organizationalunit/BaseHandlerStd.java index 4309e25..b375758 100644 --- a/aws-organizations-organizationalunit/src/main/java/software/amazon/organizations/organizationalunit/BaseHandlerStd.java +++ b/aws-organizations-organizationalunit/src/main/java/software/amazon/organizations/organizationalunit/BaseHandlerStd.java @@ -32,6 +32,7 @@ public abstract class BaseHandlerStd extends BaseHandler { // ExponentialBackoffJitter Constants + private static final Random RANDOM = new Random(); private static final double RANDOMIZATION_FACTOR = 0.5; private static final int BASE_DELAY = 15; // in seconds private static final int MAX_RETRY_ATTEMPT_FOR_RETRIABLE_EXCEPTION = 2; @@ -122,9 +123,8 @@ && isRetriableException(e)) { } public final int computeDelayBeforeNextRetry(int retryAttempt) { - Random random = new Random(); int exponentialBackoff = (int) Math.pow(2, retryAttempt) * BASE_DELAY; - int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR)); + int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR)); return exponentialBackoff + jitter; } diff --git a/aws-organizations-organizationalunit/src/main/java/software/amazon/organizations/organizationalunit/CallbackContext.java b/aws-organizations-organizationalunit/src/main/java/software/amazon/organizations/organizationalunit/CallbackContext.java index 61fca23..ed32882 100644 --- a/aws-organizations-organizationalunit/src/main/java/software/amazon/organizations/organizationalunit/CallbackContext.java +++ b/aws-organizations-organizationalunit/src/main/java/software/amazon/organizations/organizationalunit/CallbackContext.java @@ -8,13 +8,25 @@ @lombok.Getter @lombok.Setter @lombok.ToString -@lombok.EqualsAndHashCode(callSuper = true) +@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap") public class CallbackContext extends StdCallbackContext { private Map actionToRetryAttemptMap = new HashMap<>(); + + // Manually implement the setter with a defensive copy + public void setActionToRetryAttemptMap(Map actionToRetryAttemptMap) { + this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap); + } + + // Manually implement the getter with a defensive copy + public Map getActionToRetryAttemptMap() { + return new HashMap<>(actionToRetryAttemptMap); + } + public int getCurrentRetryAttempt(final Constants.Action actionName, final Constants.Handler handlerName) { String key = actionName.toString() + handlerName.toString(); return this.actionToRetryAttemptMap.getOrDefault(key, 0); } + public void setCurrentRetryAttempt(final Constants.Action actionName, final Constants.Handler handlerName) { String key = actionName.toString() + handlerName.toString(); this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName)+1); diff --git a/aws-organizations-policy/pom.xml b/aws-organizations-policy/pom.xml index c8ad925..d9a7046 100644 --- a/aws-organizations-policy/pom.xml +++ b/aws-organizations-policy/pom.xml @@ -182,6 +182,9 @@ org.apache.maven.plugins maven-surefire-plugin 3.0.0-M3 + + --add-opens java.base/java.util=ALL-UNNAMED + org.jacoco diff --git a/aws-organizations-policy/src/main/java/software/amazon/organizations/policy/BaseHandlerStd.java b/aws-organizations-policy/src/main/java/software/amazon/organizations/policy/BaseHandlerStd.java index 8a11b3f..252fb45 100644 --- a/aws-organizations-policy/src/main/java/software/amazon/organizations/policy/BaseHandlerStd.java +++ b/aws-organizations-policy/src/main/java/software/amazon/organizations/policy/BaseHandlerStd.java @@ -36,6 +36,7 @@ public abstract class BaseHandlerStd extends BaseHandler { // ExponentialBackoffJitter Constants + private static final Random RANDOM = new Random(); private static final double RANDOMIZATION_FACTOR = 0.5; private static final int BASE_DELAY = 15; // in seconds private static final int MAX_RETRY_ATTEMPT_FOR_RETRIABLE_EXCEPTION = 2; @@ -119,9 +120,8 @@ && isRetriableException(e)) { } public final int computeDelayBeforeNextRetry(int retryAttempt) { - Random random = new Random(); int exponentialBackoff = (int) Math.pow(2, retryAttempt) * BASE_DELAY; - int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR)); + int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR)); return exponentialBackoff + jitter; } diff --git a/aws-organizations-policy/src/main/java/software/amazon/organizations/policy/CallbackContext.java b/aws-organizations-policy/src/main/java/software/amazon/organizations/policy/CallbackContext.java index 49b3c3f..e6d228e 100644 --- a/aws-organizations-policy/src/main/java/software/amazon/organizations/policy/CallbackContext.java +++ b/aws-organizations-policy/src/main/java/software/amazon/organizations/policy/CallbackContext.java @@ -8,17 +8,30 @@ @lombok.Getter @lombok.Setter @lombok.ToString -@lombok.EqualsAndHashCode(callSuper = true) +@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap") public class CallbackContext extends StdCallbackContext { private Map actionToRetryAttemptMap = new HashMap<>(); + + // Manually implement the setter with a defensive copy + public void setActionToRetryAttemptMap(Map actionToRetryAttemptMap) { + this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap); + } + + // Manually implement the getter with a defensive copy + public Map getActionToRetryAttemptMap() { + return new HashMap<>(actionToRetryAttemptMap); + } + public int getCurrentRetryAttempt(final PolicyConstants.Action actionName, final PolicyConstants.Handler handlerName) { String key = actionName.toString() + handlerName.toString(); return this.actionToRetryAttemptMap.getOrDefault(key, 0); } + public void setCurrentRetryAttempt(final PolicyConstants.Action actionName, final PolicyConstants.Handler handlerName) { String key = actionName.toString() + handlerName.toString(); this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName)+1); } + // used in CREATE handler re-invoking private boolean isPolicyCreated = false; // used in DELETE handler re-invoking diff --git a/aws-organizations-resourcepolicy/pom.xml b/aws-organizations-resourcepolicy/pom.xml index 63dcf8e..2321d0c 100644 --- a/aws-organizations-resourcepolicy/pom.xml +++ b/aws-organizations-resourcepolicy/pom.xml @@ -175,6 +175,9 @@ org.apache.maven.plugins maven-surefire-plugin 3.0.0-M3 + + --add-opens java.base/java.util=ALL-UNNAMED + org.jacoco diff --git a/aws-organizations-resourcepolicy/src/main/java/software/amazon/organizations/resourcepolicy/BaseHandlerStd.java b/aws-organizations-resourcepolicy/src/main/java/software/amazon/organizations/resourcepolicy/BaseHandlerStd.java index 8039e8c..d765d70 100644 --- a/aws-organizations-resourcepolicy/src/main/java/software/amazon/organizations/resourcepolicy/BaseHandlerStd.java +++ b/aws-organizations-resourcepolicy/src/main/java/software/amazon/organizations/resourcepolicy/BaseHandlerStd.java @@ -27,6 +27,7 @@ public abstract class BaseHandlerStd extends BaseHandler { // ExponentialBackoffJitter Constants + private static final Random RANDOM = new Random(); private static final double RANDOMIZATION_FACTOR = 0.5; private static final int BASE_DELAY = 15; // in seconds private static final int MAX_RETRY_ATTEMPT_FOR_RETRIABLE_EXCEPTION = 2; @@ -114,9 +115,8 @@ && isRetriableException(e)) { } public final int computeDelayBeforeNextRetry(int retryAttempt) { - Random random = new Random(); int exponentialBackoff = (int) Math.pow(2, retryAttempt) * BASE_DELAY; - int jitter = random.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR)); + int jitter = RANDOM.nextInt((int) Math.ceil(exponentialBackoff * RANDOMIZATION_FACTOR)); return exponentialBackoff + jitter; } diff --git a/aws-organizations-resourcepolicy/src/main/java/software/amazon/organizations/resourcepolicy/CallbackContext.java b/aws-organizations-resourcepolicy/src/main/java/software/amazon/organizations/resourcepolicy/CallbackContext.java index 5737ddf..2958815 100644 --- a/aws-organizations-resourcepolicy/src/main/java/software/amazon/organizations/resourcepolicy/CallbackContext.java +++ b/aws-organizations-resourcepolicy/src/main/java/software/amazon/organizations/resourcepolicy/CallbackContext.java @@ -8,13 +8,25 @@ @lombok.Getter @lombok.Setter @lombok.ToString -@lombok.EqualsAndHashCode(callSuper = true) +@lombok.EqualsAndHashCode(callSuper = true, exclude = "actionToRetryAttemptMap") public class CallbackContext extends StdCallbackContext { private Map actionToRetryAttemptMap = new HashMap<>(); + + // Manually implement the setter with a defensive copy + public void setActionToRetryAttemptMap(Map actionToRetryAttemptMap) { + this.actionToRetryAttemptMap = new HashMap<>(actionToRetryAttemptMap); + } + + // Manually implement the getter with a defensive copy + public Map getActionToRetryAttemptMap() { + return new HashMap<>(actionToRetryAttemptMap); + } + public int getCurrentRetryAttempt(final ResourcePolicyConstants.Action actionName, final ResourcePolicyConstants.Handler handlerName) { String key = actionName.toString() + handlerName.toString(); return this.actionToRetryAttemptMap.getOrDefault(key, 0); } + public void setCurrentRetryAttempt(final ResourcePolicyConstants.Action actionName, final ResourcePolicyConstants.Handler handlerName) { String key = actionName.toString() + handlerName.toString(); this.actionToRetryAttemptMap.put(key, getCurrentRetryAttempt(actionName, handlerName)+1);