From ea262e6da52fd4da0733f02998f87aebaf502ddb Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 12 Jun 2019 14:32:25 -0400 Subject: Apply simple sonar fixes Note: A number of these were identified, by SonarLint, in the Test classes, which are not typically scanned by Sonar. Removed unnecessary imports. Removed unneeded "throws Xxx". Replaced lambda with method references. Replaced duplicate strings with constants. Replaced try-fail-catch with assert-j methods to eliminate sonar complaints about duplicate failure messages. Added missing @Override annotations. Use map.computeIfAbsent() where appropriate. Also fixed some minor checkstyle issues. Removed unneeded "volatile" declarations. Replaced some if-else constructs with "?:" construct, per sonar. Replaced Object.wait() with CountDownLatch.await(); according to sonar (and javadocs), Object.wait() can return due to "spurious wakeups". Fixed issue whereby CryptoUtilsTest wouldn't run in my Eclipse. Change-Id: Ib6b71ed65662cfd6209400dac57ed69279bf29ec Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn --- .../policy/common/utils/security/CryptoUtils.java | 20 +++++++++----------- .../onap/policy/common/utils/services/Registry.java | 2 +- .../utils/validation/ParameterValidationUtils.java | 20 ++++++++------------ 3 files changed, 18 insertions(+), 24 deletions(-) (limited to 'utils/src/main/java') diff --git a/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java index ebe0483f..579eed9f 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java @@ -20,16 +20,13 @@ package org.onap.policy.common.utils.security; -import com.google.common.base.Charsets; - +import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; - import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; - import org.apache.commons.lang3.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -65,7 +62,7 @@ public class CryptoUtils { /** * CryptoUtils - encryption tool constructor. - * @param secretKey + * @param secretKeySpec * AES supports 128, 192 or 256-bit long key size, it can be plain text or generated with key generator */ public CryptoUtils(SecretKeySpec secretKeySpec) { @@ -80,7 +77,7 @@ public class CryptoUtils { * Encrypt a value based on the Policy Encryption Key. * Equivalent openssl command: echo -n "123456" | openssl aes-128-cbc -e -K PrivateHexkey * -iv 16BytesIV | xxd -u -g100 - * + * *

Final result is to put in properties file is: IV + Outcome of openssl command * * @param value @@ -123,7 +120,7 @@ public class CryptoUtils { cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivspec); return "enc:" + DatatypeConverter.printBase64Binary( - ArrayUtils.addAll(iv, cipher.doFinal(value.getBytes(Charsets.UTF_8)))); + ArrayUtils.addAll(iv, cipher.doFinal(value.getBytes(StandardCharsets.UTF_8)))); } catch (Exception e) { logger.error("Could not encrypt value - exception: ", e); return value; @@ -181,7 +178,7 @@ public class CryptoUtils { cipher.init(Cipher.DECRYPT_MODE, keySpec, ivspec); byte[] decrypted = cipher.doFinal(realData); - return new String(decrypted, Charsets.UTF_8); + return new String(decrypted, StandardCharsets.UTF_8); } catch (Exception e) { logger.error("Could not decrypt value - exception: ", e); } @@ -224,6 +221,7 @@ public class CryptoUtils { return null; } } + /** * Check if string is encrypted by verify if string prefix with 'enc:'. * @@ -243,12 +241,12 @@ public class CryptoUtils { if (args.length == 3) { if ("enc".equals(args[0])) { String encryptedValue = encrypt(args[1], args[2]); - logger.info("original value: " + args[1] + " encrypted value: " + encryptedValue); + logger.info("original value: {} encrypted value: {}", args[1], encryptedValue); } else if ("dec".equals(args[0])) { String decryptedValue = decrypt(args[1], args[2]); - logger.info("original value: " + args[1] + " decrypted value: " + decryptedValue); + logger.info("original value: {} decrypted value: {}", args[1], decryptedValue); } else { - logger.info("Unknown request: " + args[0]); + logger.info("Unknown request: {}", args[0]); } } else { logger.info("Usage : CryptoUtils enc/dec password secretKey"); diff --git a/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java b/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java index 13fb3389..c3eabe8e 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java +++ b/utils/src/main/java/org/onap/policy/common/utils/services/Registry.java @@ -32,7 +32,7 @@ import org.slf4j.LoggerFactory; public class Registry { private static final Logger logger = LoggerFactory.getLogger(Registry.class); - private static volatile Registry instance = new Registry(); + private static Registry instance = new Registry(); /** * Registry map. diff --git a/utils/src/main/java/org/onap/policy/common/utils/validation/ParameterValidationUtils.java b/utils/src/main/java/org/onap/policy/common/utils/validation/ParameterValidationUtils.java index e2e462da..f15d936b 100644 --- a/utils/src/main/java/org/onap/policy/common/utils/validation/ParameterValidationUtils.java +++ b/utils/src/main/java/org/onap/policy/common/utils/validation/ParameterValidationUtils.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +28,10 @@ package org.onap.policy.common.utils.validation; */ public class ParameterValidationUtils { + private ParameterValidationUtils() { + + } + /** * Validates the given string input. * @@ -34,10 +39,7 @@ public class ParameterValidationUtils { * @return the boolean validation result */ public static boolean validateStringParameter(final String inputString) { - if (inputString == null || inputString.trim().length() == 0) { - return false; - } - return true; + return (inputString != null && !inputString.trim().isEmpty()); } /** @@ -47,10 +49,7 @@ public class ParameterValidationUtils { * @return the boolean validation result */ public static boolean validateIntParameter(final int input) { - if (input <= 0) { - return false; - } - return true; + return (input > 0); } /** @@ -60,9 +59,6 @@ public class ParameterValidationUtils { * @return the boolean validation result */ public static boolean validateLongParameter(final long input) { - if (input <= 0) { - return false; - } - return true; + return (input > 0); } } -- cgit 1.2.3-korg