-/*******************************************************************************\r
- * ============LICENSE_START==================================================\r
- * * org.onap.dmaap\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-\r
-\r
-package com.att.research.datarouter.node;\r
-\r
-import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;\r
-import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS;\r
-import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;\r
-\r
-import java.security.*;\r
-import java.io.*;\r
-import java.util.*;\r
-import java.security.cert.*;\r
-import java.net.*;\r
-import java.text.*;\r
-import org.apache.commons.codec.binary.Base64;\r
-import org.apache.log4j.Logger;\r
-import org.slf4j.MDC;\r
-\r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
-import com.att.research.datarouter.node.eelf.EelfMsgs;\r
-\r
-/**\r
- * Utility functions for the data router node\r
- */\r
-public class NodeUtils {\r
- private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("com.att.research.datarouter.node.NodeUtils");\r
- private static Logger logger = Logger.getLogger("com.att.research.datarouter.node.NodeUtils");\r
- private static SimpleDateFormat logdate;\r
- static {\r
- logdate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");\r
- logdate.setTimeZone(TimeZone.getTimeZone("GMT"));\r
- }\r
- private NodeUtils() {}\r
- /**\r
- * Base64 encode a byte array\r
- * @param raw The bytes to be encoded\r
- * @return The encoded string\r
- */\r
- public static String base64Encode(byte[] raw) {\r
- return(Base64.encodeBase64String(raw));\r
- }\r
- /**\r
- * Given a user and password, generate the credentials\r
- * @param user User name\r
- * @param password User password\r
- * @return Authorization header value\r
- */\r
- public static String getAuthHdr(String user, String password) {\r
- if (user == null || password == null) {\r
- return(null);\r
- }\r
- return("Basic " + base64Encode((user + ":" + password).getBytes()));\r
- }\r
- /**\r
- * Given a node name, generate the credentials\r
- * @param node Node name\r
- */\r
- public static String getNodeAuthHdr(String node, String key) {\r
- try {\r
- MessageDigest md = MessageDigest.getInstance("SHA");\r
- md.update(key.getBytes());\r
- md.update(node.getBytes());\r
- md.update(key.getBytes());\r
- return(getAuthHdr(node, base64Encode(md.digest())));\r
- } catch (Exception e) {\r
- return(null);\r
- }\r
- }\r
- /**\r
- * Given a keystore file and its password, return the value of the CN of the first private key entry with a certificate.\r
- * @param kstype The type of keystore\r
- * @param ksfile The file name of the keystore\r
- * @param kspass The password of the keystore\r
- * @return CN of the certificate subject or null\r
- */\r
- public static String getCanonicalName(String kstype, String ksfile, String kspass) {\r
- try {\r
- KeyStore ks = KeyStore.getInstance(kstype);\r
- ks.load(new FileInputStream(ksfile), kspass.toCharArray());\r
- return(getCanonicalName(ks));\r
- } catch (Exception e) {\r
- setIpAndFqdnForEelf("getCanonicalName");\r
- eelflogger.error(EelfMsgs.MESSAGE_KEYSTORE_LOAD_ERROR, ksfile, e.toString());\r
- logger.error("NODE0401 Error loading my keystore file + " + ksfile + " " + e.toString(), e);\r
- return(null);\r
- }\r
- }\r
- /**\r
- * Given a keystore, return the value of the CN of the first private key entry with a certificate.\r
- * @param ks The KeyStore\r
- * @return CN of the certificate subject or null\r
- */\r
- public static String getCanonicalName(KeyStore ks) {\r
- try {\r
- Enumeration<String> aliases = ks.aliases();\r
- while (aliases.hasMoreElements()) {\r
- String s = aliases.nextElement();\r
- if (ks.entryInstanceOf(s, KeyStore.PrivateKeyEntry.class)) {\r
- X509Certificate c = (X509Certificate)ks.getCertificate(s);\r
- if (c != null) {\r
- String subject = c.getSubjectX500Principal().getName();\r
- String[] parts = subject.split(",");\r
- if (parts.length < 1) {\r
- return(null);\r
- }\r
- subject = parts[0].trim();\r
- if (!subject.startsWith("CN=")) {\r
- return(null);\r
-\r
- }\r
- return(subject.substring(3));\r
- }\r
- }\r
- }\r
- } catch (Exception e) {\r
- logger.error("NODE0402 Error extracting my name from my keystore file " + e.toString(), e);\r
- }\r
- return(null);\r
- }\r
- /**\r
- * Given a string representation of an IP address, get the corresponding byte array\r
- * @param ip The IP address as a string\r
- * @return The IP address as a byte array or null if the address is invalid\r
- */\r
- public static byte[] getInetAddress(String ip) {\r
- try {\r
- return(InetAddress.getByName(ip).getAddress());\r
- } catch (Exception e) {\r
- }\r
- return(null);\r
- }\r
- /**\r
- * Given a uri with parameters, split out the feed ID and file ID\r
- */\r
- public static String[] getFeedAndFileID(String uriandparams) {\r
- int end = uriandparams.length();\r
- int i = uriandparams.indexOf('#');\r
- if (i != -1 && i < end) {\r
- end = i;\r
- }\r
- i = uriandparams.indexOf('?');\r
- if (i != -1 && i < end) {\r
- end = i;\r
- }\r
- end = uriandparams.lastIndexOf('/', end);\r
- if (end < 2) {\r
- return(null);\r
- }\r
- i = uriandparams.lastIndexOf('/', end - 1);\r
- if (i == -1) {\r
- return(null);\r
- }\r
- return(new String[] { uriandparams.substring(i + 1, end - 1), uriandparams.substring(end + 1) });\r
- }\r
- /**\r
- * Escape fields that might contain vertical bar, backslash, or newline by replacing them with backslash p, backslash e and backslash n.\r
- */\r
- public static String loge(String s) {\r
- if (s == null) {\r
- return(s);\r
- }\r
- return(s.replaceAll("\\\\", "\\\\e").replaceAll("\\|", "\\\\p").replaceAll("\n", "\\\\n"));\r
- }\r
- /**\r
- * Undo what loge does.\r
- */\r
- public static String unloge(String s) {\r
- if (s == null) {\r
- return(s);\r
- }\r
- return(s.replaceAll("\\\\p", "\\|").replaceAll("\\\\n", "\n").replaceAll("\\\\e", "\\\\"));\r
- }\r
- /**\r
- * Format a logging timestamp as yyyy-mm-ddThh:mm:ss.mmmZ\r
- */\r
- public static String logts(long when) {\r
- return(logts(new Date(when)));\r
- }\r
- /**\r
- * Format a logging timestamp as yyyy-mm-ddThh:mm:ss.mmmZ\r
- */\r
- public static synchronized String logts(Date when) {\r
- return(logdate.format(when));\r
- }\r
- \r
- /* Method prints method name, server FQDN and IP Address of the machine in EELF logs\r
- * @Method - setIpAndFqdnForEelf - Rally:US664892 \r
- * @Params - method, prints method name in EELF log.\r
- */ \r
- public static void setIpAndFqdnForEelf(String method) {\r
- MDC.clear();\r
- MDC.put(MDC_SERVICE_NAME, method);\r
- try {\r
- MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());\r
- MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());\r
- } catch (Exception e) {\r
- e.printStackTrace();\r
- }\r
-\r
- }\r
- \r
-\r
-}\r