X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-node%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fnode%2FNodeUtils.java;h=d4fc7dbe604835e4a81faa9446c7da9059fad4a0;hb=cf55456bbe6ce4da723f9dfa64b573f02908ab24;hp=2c013ca5729d3f8f5baa036aa8b8c76a0db21e78;hpb=ce73ff52ce9aafb07d1aa4c28405328d83c816b9;p=dmaap%2Fdatarouter.git diff --git a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeUtils.java b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeUtils.java index 2c013ca5..d4fc7dbe 100644 --- a/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeUtils.java +++ b/datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeUtils.java @@ -24,47 +24,49 @@ package org.onap.dmaap.datarouter.node; +import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID; import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN; import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS; import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.InetAddress; import java.security.KeyStore; +import java.security.KeyStoreException; import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; import java.util.TimeZone; +import java.util.UUID; +import java.util.zip.GZIPInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.apache.commons.codec.binary.Base64; -import org.apache.log4j.Logger; +import org.apache.commons.lang3.StringUtils; import org.onap.dmaap.datarouter.node.eelf.EelfMsgs; import org.slf4j.MDC; /** - * Utility functions for the data router node + * Utility functions for the data router node. */ public class NodeUtils { private static EELFLogger eelfLogger = EELFManager.getInstance() - .getLogger("org.onap.dmaap.datarouter.node.NodeUtils"); - private static Logger nodeUtilsLogger = Logger.getLogger("org.onap.dmaap.datarouter.node.NodeUtils"); - private static SimpleDateFormat logDate; - - static { - logDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); - logDate.setTimeZone(TimeZone.getTimeZone("GMT")); - } + .getLogger(NodeUtils.class); private NodeUtils() { } /** - * Base64 encode a byte array + * Base64 encode a byte array. * * @param raw The bytes to be encoded * @return The encoded string @@ -74,7 +76,7 @@ public class NodeUtils { } /** - * Given a user and password, generate the credentials + * Given a user and password, generate the credentials. * * @param user User name * @param password User password @@ -88,7 +90,7 @@ public class NodeUtils { } /** - * Given a node name, generate the credentials + * Given a node name, generate the credentials. * * @param node Node name */ @@ -100,8 +102,9 @@ public class NodeUtils { md.update(key.getBytes()); return (getAuthHdr(node, base64Encode(md.digest()))); } catch (Exception exception) { - nodeUtilsLogger - .error("Exception in generating Credentials for given node name:= " + exception.toString(), exception); + eelfLogger + .error("Exception in generating Credentials for given node name:= " + exception.toString(), + exception); return (null); } } @@ -119,17 +122,12 @@ public class NodeUtils { KeyStore ks; try { ks = KeyStore.getInstance(kstype); - try (FileInputStream fileInputStream = new FileInputStream(ksfile)) { - ks.load(fileInputStream, kspass.toCharArray()); - } catch (IOException ioException) { - nodeUtilsLogger.error("IOException occurred while opening FileInputStream: " + ioException.getMessage(), - ioException); + if (loadKeyStore(ksfile, kspass, ks)) { return (null); } } catch (Exception e) { setIpAndFqdnForEelf("getCanonicalName"); - eelfLogger.error(EelfMsgs.MESSAGE_KEYSTORE_LOAD_ERROR, ksfile, e.toString()); - nodeUtilsLogger.error("NODE0401 Error loading my keystore file + " + ksfile + " " + e.toString(), e); + eelfLogger.error(EelfMsgs.MESSAGE_KEYSTORE_LOAD_ERROR, e, ksfile); return (null); } return (getCanonicalName(ks)); @@ -145,32 +143,19 @@ public class NodeUtils { try { Enumeration aliases = ks.aliases(); while (aliases.hasMoreElements()) { - String s = aliases.nextElement(); - if (ks.entryInstanceOf(s, KeyStore.PrivateKeyEntry.class)) { - X509Certificate c = (X509Certificate) ks.getCertificate(s); - if (c != null) { - String subject = c.getSubjectX500Principal().getName(); - String[] parts = subject.split(","); - if (parts.length < 1) { - return (null); - } - subject = parts[5].trim(); - if (!subject.startsWith("CN=")) { - return (null); - - } - return (subject.substring(3)); - } + String name = getNameFromSubject(ks, aliases); + if (name != null) { + return name; } } } catch (Exception e) { - nodeUtilsLogger.error("NODE0402 Error extracting my name from my keystore file " + e.toString(), e); + eelfLogger.error("NODE0402 Error extracting my name from my keystore file " + e.toString(), e); } return (null); } /** - * Given a string representation of an IP address, get the corresponding byte array + * Given a string representation of an IP address, get the corresponding byte array. * * @param ip The IP address as a string * @return The IP address as a byte array or null if the address is invalid @@ -179,55 +164,56 @@ public class NodeUtils { try { return (InetAddress.getByName(ip).getAddress()); } catch (Exception exception) { - nodeUtilsLogger - .error("Exception in generating byte array for given IP address := " + exception.toString(), exception); + eelfLogger + .error("Exception in generating byte array for given IP address := " + exception.toString(), + exception); } return (null); } /** - * Given a uri with parameters, split out the feed ID and file ID + * Given a uri with parameters, split out the feed ID and file ID. */ public static String[] getFeedAndFileID(String uriandparams) { int end = uriandparams.length(); - int i = uriandparams.indexOf('#'); - if (i != -1 && i < end) { - end = i; + int index = uriandparams.indexOf('#'); + if (index != -1 && index < end) { + end = index; } - i = uriandparams.indexOf('?'); - if (i != -1 && i < end) { - end = i; + index = uriandparams.indexOf('?'); + if (index != -1 && index < end) { + end = index; } end = uriandparams.lastIndexOf('/', end); if (end < 2) { return (null); } - i = uriandparams.lastIndexOf('/', end - 1); - if (i == -1) { + index = uriandparams.lastIndexOf('/', end - 1); + if (index == -1) { return (null); } - return (new String[]{uriandparams.substring(i + 1, end), uriandparams.substring(end + 1)}); + return (new String[]{uriandparams.substring(index + 1, end), uriandparams.substring(end + 1)}); } /** * Escape fields that might contain vertical bar, backslash, or newline by replacing them with backslash p, * backslash e and backslash n. */ - public static String loge(String s) { - if (s == null) { - return (s); + public static String loge(String string) { + if (string == null) { + return (string); } - return (s.replaceAll("\\\\", "\\\\e").replaceAll("\\|", "\\\\p").replaceAll("\n", "\\\\n")); + return (string.replaceAll("\\\\", "\\\\e").replaceAll("\\|", "\\\\p").replaceAll("\n", "\\\\n")); } /** * Undo what loge does. */ - public static String unloge(String s) { - if (s == null) { - return (s); + public static String unloge(String string) { + if (string == null) { + return (string); } - return (s.replaceAll("\\\\p", "\\|").replaceAll("\\\\n", "\n").replaceAll("\\\\e", "\\\\")); + return (string.replaceAll("\\\\p", "\\|").replaceAll("\\\\n", "\n").replaceAll("\\\\e", "\\\\")); } /** @@ -241,12 +227,14 @@ public class NodeUtils { * Format a logging timestamp as yyyy-mm-ddThh:mm:ss.mmmZ */ public static synchronized String logts(Date when) { + SimpleDateFormat logDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + logDate.setTimeZone(TimeZone.getTimeZone("GMT")); return (logDate.format(when)); } - /* Method prints method name, server FQDN and IP Address of the machine in EELF logs - * @Method - setIpAndFqdnForEelf - Rally:US664892 - * @Params - method, prints method name in EELF log. + /** Method prints method name, server FQDN and IP Address of the machine in EELF logs. + * + * @param method Prints method name in EELF log. */ public static void setIpAndFqdnForEelf(String method) { MDC.clear(); @@ -255,11 +243,89 @@ public class NodeUtils { MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName()); MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); } catch (Exception exception) { - nodeUtilsLogger - .error("Exception in generating byte array for given IP address := " + exception.toString(), exception); + eelfLogger + .error("Exception in generating byte array for given IP address := " + exception.toString(), + exception); + } + + } + + /** Method sets RequestIs and InvocationId for se in EELF logs. + * + * @param req Request used to get RequestId and InvocationId. + */ + public static void setRequestIdAndInvocationId(HttpServletRequest req) { + String reqId = req.getHeader("X-ONAP-RequestID"); + if (StringUtils.isBlank(reqId)) { + reqId = UUID.randomUUID().toString(); + } + MDC.put(MDC_KEY_REQUEST_ID, reqId); + String invId = req.getHeader("X-InvocationID"); + if (StringUtils.isBlank(invId)) { + invId = UUID.randomUUID().toString(); + } + MDC.put("InvocationId", invId); + } + + /** + * Sends error as response with error code input. + */ + public static void sendResponseError(HttpServletResponse response, int errorCode, EELFLogger intlogger) { + try { + response.sendError(errorCode); + } catch (IOException ioe) { + intlogger.error("IOException", ioe); + } + } + + /** + * Method to check to see if file is of type gzip. + * + * @param file The name of the file to be checked + * @return True if the file is of type gzip + */ + public static boolean isFiletypeGzip(File file) { + try (FileInputStream fileInputStream = new FileInputStream(file); + GZIPInputStream gzip = new GZIPInputStream(fileInputStream)) { + + return true; + } catch (IOException e) { + eelfLogger.error("NODE0403 " + file.toString() + " Not in gzip(gz) format: " + e.toString() + e); + return false; } + } + + private static boolean loadKeyStore(String ksfile, String kspass, KeyStore ks) + throws NoSuchAlgorithmException, CertificateException { + try (FileInputStream fileInputStream = new FileInputStream(ksfile)) { + ks.load(fileInputStream, kspass.toCharArray()); + } catch (IOException ioException) { + eelfLogger.error("IOException occurred while opening FileInputStream: " + ioException.getMessage(), + ioException); + return true; + } + return false; } + private static String getNameFromSubject(KeyStore ks, Enumeration aliases) throws KeyStoreException { + String alias = aliases.nextElement(); + if (ks.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) { + X509Certificate cert = (X509Certificate) ks.getCertificate(alias); + if (cert != null) { + String subject = cert.getSubjectX500Principal().getName(); + String[] parts = subject.split(","); + if (parts.length < 1) { + return null; + } + subject = parts[5].trim(); + if (!subject.startsWith("CN=")) { + return null; + } + return subject.substring(3); + } + } + return null; + } }