diff options
Diffstat (limited to 'security-util-lib/src/main/java/org/onap')
5 files changed, 107 insertions, 142 deletions
diff --git a/security-util-lib/src/main/java/org/onap/sdc/security/AuthenticationCookie.java b/security-util-lib/src/main/java/org/onap/sdc/security/AuthenticationCookie.java index 9440eea..b8f3afa 100644 --- a/security-util-lib/src/main/java/org/onap/sdc/security/AuthenticationCookie.java +++ b/security-util-lib/src/main/java/org/onap/sdc/security/AuthenticationCookie.java @@ -21,9 +21,13 @@ package org.onap.sdc.security; import java.util.Set; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; +import lombok.ToString; +@EqualsAndHashCode +@ToString public class AuthenticationCookie { @Getter @Setter @@ -72,35 +76,5 @@ public class AuthenticationCookie { this.lastName = lastName; } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof AuthenticationCookie)) return false; - AuthenticationCookie that = (AuthenticationCookie) o; - - if (getMaxSessionTime() != that.getMaxSessionTime()) return false; - if (getCurrentSessionTime() != that.getCurrentSessionTime()) return false; - if (getUserID() != null ? !getUserID().equals(that.getUserID()) : that.getUserID() != null) return false; - return getRoles() != null ? getRoles().containsAll(that.getRoles()) : that.getRoles() == null; - } - - @Override - public int hashCode() { - int result = getUserID() != null ? getUserID().hashCode() : 0; - result = 31 * result + (getRoles() != null ? getRoles().hashCode() : 0); - result = 31 * result + (int) (getMaxSessionTime() ^ (getMaxSessionTime() >>> 32)); - result = 31 * result + (int) (getCurrentSessionTime() ^ (getCurrentSessionTime() >>> 32)); - return result; - } - - @Override - public String toString() { - return "AuthenticationCookie{" + - "userID='" + userID + '\'' + - ", roles=" + roles + - ", maxSessionTime=" + maxSessionTime + - ", currentSessionTime=" + currentSessionTime + - '}'; - } } diff --git a/security-util-lib/src/main/java/org/onap/sdc/security/SecurityUtil.java b/security-util-lib/src/main/java/org/onap/sdc/security/SecurityUtil.java index 98410d2..27d946f 100644 --- a/security-util-lib/src/main/java/org/onap/sdc/security/SecurityUtil.java +++ b/security-util-lib/src/main/java/org/onap/sdc/security/SecurityUtil.java @@ -20,133 +20,148 @@ package org.onap.sdc.security; -import fj.data.Either; -import org.onap.sdc.security.logging.wrappers.Logger; +import static java.nio.charset.StandardCharsets.UTF_8; -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.spec.SecretKeySpec; +import fj.data.Either; import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.util.Base64; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.SecretKeySpec; +import org.onap.sdc.security.logging.enums.EcompLoggerErrorCode; +import org.onap.sdc.security.logging.wrappers.Logger; public class SecurityUtil { - private static final Logger LOG = Logger.getLogger( SecurityUtil.class ); - private static final byte[] KEY = new byte[]{-64,5,-32 ,-117 ,-44,8,-39, 1, -9, 36,-46,-81, 62,-15,-63,-75}; + private static final Logger LOG = Logger.getLogger(SecurityUtil.class); + private static final byte[] KEY = + new byte[] {-64, 5, -32, -117, -44, 8, -39, 1, -9, 36, -46, -81, 62, -15, -63, -75}; public static final SecurityUtil INSTANCE = new SecurityUtil(); - public static final String ALGORITHM = "AES" ; - public static final String CHARSET = StandardCharsets.UTF_8.name(); + public static final String ALGORITHM = "AES"; + public static final String CHARSET = UTF_8.name(); - public static Key secKey = null ; + public static Key secKey = null; /** - * * cmd commands >$PROGRAM_NAME decrypt "$ENCRYPTED_MSG" - * >$PROGRAM_NAME encrypt "message" - **/ + * >$PROGRAM_NAME encrypt "message" + **/ - private SecurityUtil(){ super(); } + private SecurityUtil() { + } static { - try{ - secKey = generateKey( KEY, ALGORITHM ); - } - catch(Exception e){ - LOG.warn("cannot generate key for {}", ALGORITHM); + try { + secKey = generateKey(KEY, ALGORITHM); + } catch (Exception e) { + LOG.warn(EcompLoggerErrorCode.PERMISSION_ERROR,"cannot generate key for {}", ALGORITHM); } } - - public static Key generateKey(final byte[] KEY, String algorithm){ + public static Key generateKey(final byte[] KEY, String algorithm) { return new SecretKeySpec(KEY, algorithm); } //obfuscates key prefix -> ********** - public String obfuscateKey(String sensitiveData){ + public String obfuscateKey(String sensitiveData) { - if (sensitiveData != null){ - int len = sensitiveData.length(); - StringBuilder builder = new StringBuilder(sensitiveData); - for (int i=0; i<len/2; i++){ - builder.setCharAt(i, '*'); - } - return builder.toString(); + if (sensitiveData == null) { + return null; + } + int len = sensitiveData.length(); + StringBuilder builder = new StringBuilder(sensitiveData); + for (int i = 0; i < len / 2; i++) { + builder.setCharAt(i, '*'); } - return sensitiveData; + return builder.toString(); } + /** - * @param strDataToEncrypt - plain string to encrypt - * Encrypt the Data - * a. Declare / Initialize the Data. Here the data is of type String - * b. Convert the Input Text to Bytes - * c. Encrypt the bytes using doFinal method + * @param strDataToEncrypt - plain string to encrypt + * Encrypt the Data + * a. Declare / Initialize the Data. Here the data is of type String + * b. Convert the Input Text to Bytes + * c. Encrypt the bytes using doFinal method */ - public Either<String,String> encrypt(String strDataToEncrypt){ - if (strDataToEncrypt != null ){ + public Either<String, String> encrypt(String strDataToEncrypt) { + if (strDataToEncrypt != null) { try { LOG.debug("Encrypt key -> {}", secKey); - Cipher aesCipherForEncryption = Cipher.getInstance("AES"); // Must specify the mode explicitly as most JCE providers default to ECB mode!! + Cipher aesCipherForEncryption = Cipher.getInstance( + "AES"); // Must specify the mode explicitly as most JCE providers default to ECB mode!! aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secKey); byte[] byteDataToEncrypt = strDataToEncrypt.getBytes(); byte[] byteCipherText = aesCipherForEncryption.doFinal(byteDataToEncrypt); - String strCipherText = new String( Base64.getMimeEncoder().encode(byteCipherText), CHARSET ); + String strCipherText = new String(Base64.getMimeEncoder().encode(byteCipherText), CHARSET); LOG.debug("Cipher Text generated using AES is {}", strCipherText); return Either.left(strCipherText); - } catch( NoSuchAlgorithmException | UnsupportedEncodingException e){ - LOG.warn( "cannot encrypt data unknown algorithm or missing encoding for {}" ,secKey.getAlgorithm()); - } catch( InvalidKeyException e){ - LOG.warn( "invalid key recieved - > {} | {}" , Base64.getDecoder().decode( secKey.getEncoded() ), e.getMessage() ); - } catch( IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e){ - LOG.warn( "bad algorithm definition (Illegal Block Size or padding), please review you algorithm block&padding" , e.getMessage() ); + } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { + LOG.warn(EcompLoggerErrorCode.PERMISSION_ERROR, + "cannot encrypt data unknown algorithm or missing encoding for {}", secKey.getAlgorithm()); + } catch (InvalidKeyException e) { + LOG.warn(EcompLoggerErrorCode.PERMISSION_ERROR, "invalid key recieved - > {} | {}", + new String(Base64.getDecoder().decode(secKey.getEncoded())), e.getMessage()); + } catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) { + LOG.warn(EcompLoggerErrorCode.PERMISSION_ERROR, + "bad algorithm definition (Illegal Block Size or padding), please review you algorithm block&padding", + e.getMessage()); } } - return Either.right("Cannot encrypt "+strDataToEncrypt); + return Either.right("Cannot encrypt " + strDataToEncrypt); } /** * Decrypt the Data - * @param byteCipherText - should be valid bae64 input in the length of 16bytes + * + * @param byteCipherText - should be valid bae64 input in the length of 16bytes * @param isBase64Decoded - is data already base64 encoded&aligned to 16 bytes - * a. Initialize a new instance of Cipher for Decryption (normally don't reuse the same object) - * b. Decrypt the cipher bytes using doFinal method + * a. Initialize a new instance of Cipher for Decryption (normally don't reuse the same + * object) + * b. Decrypt the cipher bytes using doFinal method */ - public Either<String,String> decrypt(byte[] byteCipherText , boolean isBase64Decoded){ - if (byteCipherText != null){ + public Either<String, String> decrypt(byte[] byteCipherText, boolean isBase64Decoded) { + if (byteCipherText != null) { byte[] alignedCipherText = byteCipherText; - try{ - if (isBase64Decoded) + try { + if (isBase64Decoded) { alignedCipherText = Base64.getDecoder().decode(byteCipherText); - LOG.debug("Decrypt key -> "+secKey.getEncoded()); - Cipher aesCipherForDecryption = Cipher.getInstance("AES"); // Must specify the mode explicitly as most JCE providers default to ECB mode!! + } + LOG.debug("Decrypt key -> " + secKey.getEncoded()); + Cipher aesCipherForDecryption = Cipher.getInstance( + "AES"); // Must specify the mode explicitly as most JCE providers default to ECB mode!! aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secKey); byte[] byteDecryptedText = aesCipherForDecryption.doFinal(alignedCipherText); String strDecryptedText = new String(byteDecryptedText); - LOG.debug("Decrypted Text message is: {}" , obfuscateKey( strDecryptedText )); + LOG.debug("Decrypted Text message is: {}", obfuscateKey(strDecryptedText)); return Either.left(strDecryptedText); - } catch( NoSuchAlgorithmException e){ - LOG.warn( "cannot encrypt data unknown algorithm or missing encoding for {}" ,secKey.getAlgorithm()); - } catch( InvalidKeyException e){ - LOG.warn( "invalid key recieved - > {} | {}" , Base64.getDecoder().decode( secKey.getEncoded() ), e.getMessage() ); - } catch( IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e){ - LOG.warn( "bad algorithm definition (Illegal Block Size or padding), please review you algorithm block&padding" , e.getMessage() ); + } catch (NoSuchAlgorithmException e) { + LOG.warn(EcompLoggerErrorCode.PERMISSION_ERROR, + "cannot encrypt data unknown algorithm or missing encoding for {}", secKey.getAlgorithm()); + } catch (InvalidKeyException e) { + LOG.warn(EcompLoggerErrorCode.PERMISSION_ERROR, "invalid key recieved - > {} | {}", + new String(Base64.getDecoder().decode(secKey.getEncoded())), e.getMessage()); + } catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) { + LOG.warn(EcompLoggerErrorCode.PERMISSION_ERROR, + "bad algorithm definition (Illegal Block Size or padding), please review you algorithm block&padding", + e.getMessage()); } } return Either.right("Decrypt FAILED"); } - public Either<String,String> decrypt(String byteCipherText){ + public Either<String, String> decrypt(String byteCipherText) { try { - return decrypt(byteCipherText.getBytes(CHARSET),true); - } catch( UnsupportedEncodingException e ){ - LOG.warn( "Missing encoding for {} | {} " ,secKey.getAlgorithm() , e.getMessage()); + return decrypt(byteCipherText.getBytes(CHARSET), true); + } catch (UnsupportedEncodingException e) { + LOG.warn(EcompLoggerErrorCode.PERMISSION_ERROR, "Missing encoding for {} | {} ", secKey.getAlgorithm(), + e.getMessage()); } return Either.right("Decrypt FAILED"); } diff --git a/security-util-lib/src/main/java/org/onap/sdc/security/filters/SampleFilter.java b/security-util-lib/src/main/java/org/onap/sdc/security/filters/SampleFilter.java index 853c40d..0578c1d 100644 --- a/security-util-lib/src/main/java/org/onap/sdc/security/filters/SampleFilter.java +++ b/security-util-lib/src/main/java/org/onap/sdc/security/filters/SampleFilter.java @@ -38,8 +38,8 @@ public class SampleFilter extends SessionValidationFilter { private static class Configuration implements ISessionValidationFilterConfiguration { private String securityKey = "AGLDdG4D04BKm2IxIWEr8o=="; - private long maxSessionTimeOut = 24*60*60*1000; - private long sessionIdleTimeOut = 60*60*1000; + private long maxSessionTimeOut = 24*60*60*1_000L; + private long sessionIdleTimeOut = 60*60*1_000L; private String redirectURL = "http://portal.api.simpledemo.onap.org:8989/ECOMPPORTAL/login.htm"; private List<String> excludedUrls = new ArrayList<>(Arrays.asList("/config","/configmgr","/rest","/kibanaProxy","/healthcheck","/upload.*")); private String cookieName = "kuku"; diff --git a/security-util-lib/src/main/java/org/onap/sdc/security/logging/elements/HttpResponse.java b/security-util-lib/src/main/java/org/onap/sdc/security/logging/elements/HttpResponse.java index 57aaf3c..61f440c 100644 --- a/security-util-lib/src/main/java/org/onap/sdc/security/logging/elements/HttpResponse.java +++ b/security-util-lib/src/main/java/org/onap/sdc/security/logging/elements/HttpResponse.java @@ -20,11 +20,19 @@ package org.onap.sdc.security.logging.elements; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.ToString; import org.apache.commons.lang3.StringUtils; +@ToString +@AllArgsConstructor public class HttpResponse<T> { + @Getter private final T response; + @Getter private final int statusCode; + @Getter private final String description; public HttpResponse(T response, int statusCode) { @@ -32,37 +40,5 @@ public class HttpResponse<T> { this.statusCode = statusCode; this.description = StringUtils.EMPTY; } - - public HttpResponse(T response, int statusCode, String description) { - this.response = response; - this.statusCode = statusCode; - this.description = description; - } - - public T getResponse() { - return response; - } - public int getStatusCode() { - return statusCode; - } - - public String getDescription() { - return description; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("HttpResponse [response="); - builder.append(response); - builder.append(", statusCode="); - builder.append(statusCode); - builder.append(", description="); - builder.append(description); - builder.append("]"); - return builder.toString(); - } - - } diff --git a/security-util-lib/src/main/java/org/onap/sdc/security/logging/elements/LoggerFactory.java b/security-util-lib/src/main/java/org/onap/sdc/security/logging/elements/LoggerFactory.java index fe67de1..67645d7 100644 --- a/security-util-lib/src/main/java/org/onap/sdc/security/logging/elements/LoggerFactory.java +++ b/security-util-lib/src/main/java/org/onap/sdc/security/logging/elements/LoggerFactory.java @@ -30,19 +30,19 @@ public class LoggerFactory { @SuppressWarnings("unchecked") public static <T, V> V getLogger(Class<T> type, Logger logger) { - if (type.getName().equals(LoggerAudit.class.getName())) { + if (type.isAssignableFrom(LoggerAudit.class) ) { return (V) new LoggerAudit(new LogFieldsMdcHandler(), logger); } - if (type.getName().equals(LoggerDebug.class.getName())) { + if (type.isAssignableFrom(LoggerDebug.class)) { return (V) new LoggerDebug(new LogFieldsMdcHandler(), logger); } - if (type.getName().equals(LoggerMetric.class.getName())) { + if (type.isAssignableFrom(LoggerMetric.class)) { return (V) new LoggerMetric(new LogFieldsMdcHandler(), logger); } - if (type.getName().equals(LoggerError.class.getName())) { + if (type.isAssignableFrom(LoggerError.class)) { return (V) new LoggerError(new LogFieldsMdcHandler(), logger); } @@ -52,23 +52,23 @@ public class LoggerFactory { @SuppressWarnings("unchecked") public static <T, V> V getMdcLogger(Class<T> type, Logger logger) { - if (type.getName().equals(LoggerAudit.class.getName())) { + if (type.isAssignableFrom(LoggerAudit.class)) { return (V) new LoggerAudit(LogFieldsMdcHandler.getInstance(), logger); } - if (type.getName().equals(LoggerDebug.class.getName())) { + if (type.isAssignableFrom(LoggerDebug.class)) { return (V) new LoggerDebug(LogFieldsMdcHandler.getInstance(), logger); } - if (type.getName().equals(LoggerMetric.class.getName())) { + if (type.isAssignableFrom(LoggerMetric.class)) { return (V) new LoggerMetric(LogFieldsMdcHandler.getInstance(), logger); } - if (type.getName().equals(LoggerError.class.getName())) { + if (type.isAssignableFrom(LoggerError.class)) { return (V) new LoggerError(LogFieldsMdcHandler.getInstance(), logger); } - if (type.getName().equals(LoggerSupportability.class.getName())) { + if (type.isAssignableFrom(LoggerSupportability.class)) { return (V) new LoggerSupportability(LogFieldsMdcHandler.getInstance(), logger); } |