1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package com.att.research.datarouter.node;
\r
27 import static com.att.eelf.configuration.Configuration.MDC_SERVER_FQDN;
\r
28 import static com.att.eelf.configuration.Configuration.MDC_SERVER_IP_ADDRESS;
\r
29 import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME;
\r
31 import java.security.*;
\r
34 import java.security.cert.*;
\r
37 import org.apache.commons.codec.binary.Base64;
\r
38 import org.apache.log4j.Logger;
\r
39 import org.slf4j.MDC;
\r
41 import com.att.eelf.configuration.EELFLogger;
\r
42 import com.att.eelf.configuration.EELFManager;
\r
43 import com.att.research.datarouter.node.eelf.EelfMsgs;
\r
46 * Utility functions for the data router node
\r
48 public class NodeUtils {
\r
49 private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("com.att.research.datarouter.node.NodeUtils");
\r
50 private static Logger logger = Logger.getLogger("com.att.research.datarouter.node.NodeUtils");
\r
51 private static SimpleDateFormat logdate;
\r
53 logdate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
\r
54 logdate.setTimeZone(TimeZone.getTimeZone("GMT"));
\r
56 private NodeUtils() {}
\r
58 * Base64 encode a byte array
\r
59 * @param raw The bytes to be encoded
\r
60 * @return The encoded string
\r
62 public static String base64Encode(byte[] raw) {
\r
63 return(Base64.encodeBase64String(raw));
\r
66 * Given a user and password, generate the credentials
\r
67 * @param user User name
\r
68 * @param password User password
\r
69 * @return Authorization header value
\r
71 public static String getAuthHdr(String user, String password) {
\r
72 if (user == null || password == null) {
\r
75 return("Basic " + base64Encode((user + ":" + password).getBytes()));
\r
78 * Given a node name, generate the credentials
\r
79 * @param node Node name
\r
81 public static String getNodeAuthHdr(String node, String key) {
\r
83 MessageDigest md = MessageDigest.getInstance("SHA");
\r
84 md.update(key.getBytes());
\r
85 md.update(node.getBytes());
\r
86 md.update(key.getBytes());
\r
87 return(getAuthHdr(node, base64Encode(md.digest())));
\r
88 } catch (Exception e) {
\r
93 * Given a keystore file and its password, return the value of the CN of the first private key entry with a certificate.
\r
94 * @param kstype The type of keystore
\r
95 * @param ksfile The file name of the keystore
\r
96 * @param kspass The password of the keystore
\r
97 * @return CN of the certificate subject or null
\r
99 public static String getCanonicalName(String kstype, String ksfile, String kspass) {
\r
101 KeyStore ks = KeyStore.getInstance(kstype);
\r
102 ks.load(new FileInputStream(ksfile), kspass.toCharArray());
\r
103 return(getCanonicalName(ks));
\r
104 } catch (Exception e) {
\r
105 setIpAndFqdnForEelf("getCanonicalName");
\r
106 eelflogger.error(EelfMsgs.MESSAGE_KEYSTORE_LOAD_ERROR, ksfile, e.toString());
\r
107 logger.error("NODE0401 Error loading my keystore file + " + ksfile + " " + e.toString(), e);
\r
112 * Given a keystore, return the value of the CN of the first private key entry with a certificate.
\r
113 * @param ks The KeyStore
\r
114 * @return CN of the certificate subject or null
\r
116 public static String getCanonicalName(KeyStore ks) {
\r
118 Enumeration<String> aliases = ks.aliases();
\r
119 while (aliases.hasMoreElements()) {
\r
120 String s = aliases.nextElement();
\r
121 if (ks.entryInstanceOf(s, KeyStore.PrivateKeyEntry.class)) {
\r
122 X509Certificate c = (X509Certificate)ks.getCertificate(s);
\r
124 String subject = c.getSubjectX500Principal().getName();
\r
125 String[] parts = subject.split(",");
\r
126 if (parts.length < 1) {
\r
129 subject = parts[0].trim();
\r
130 if (!subject.startsWith("CN=")) {
\r
134 return(subject.substring(3));
\r
138 } catch (Exception e) {
\r
139 logger.error("NODE0402 Error extracting my name from my keystore file " + e.toString(), e);
\r
144 * Given a string representation of an IP address, get the corresponding byte array
\r
145 * @param ip The IP address as a string
\r
146 * @return The IP address as a byte array or null if the address is invalid
\r
148 public static byte[] getInetAddress(String ip) {
\r
150 return(InetAddress.getByName(ip).getAddress());
\r
151 } catch (Exception e) {
\r
156 * Given a uri with parameters, split out the feed ID and file ID
\r
158 public static String[] getFeedAndFileID(String uriandparams) {
\r
159 int end = uriandparams.length();
\r
160 int i = uriandparams.indexOf('#');
\r
161 if (i != -1 && i < end) {
\r
164 i = uriandparams.indexOf('?');
\r
165 if (i != -1 && i < end) {
\r
168 end = uriandparams.lastIndexOf('/', end);
\r
172 i = uriandparams.lastIndexOf('/', end - 1);
\r
176 return(new String[] { uriandparams.substring(i + 1, end - 1), uriandparams.substring(end + 1) });
\r
179 * Escape fields that might contain vertical bar, backslash, or newline by replacing them with backslash p, backslash e and backslash n.
\r
181 public static String loge(String s) {
\r
185 return(s.replaceAll("\\\\", "\\\\e").replaceAll("\\|", "\\\\p").replaceAll("\n", "\\\\n"));
\r
188 * Undo what loge does.
\r
190 public static String unloge(String s) {
\r
194 return(s.replaceAll("\\\\p", "\\|").replaceAll("\\\\n", "\n").replaceAll("\\\\e", "\\\\"));
\r
197 * Format a logging timestamp as yyyy-mm-ddThh:mm:ss.mmmZ
\r
199 public static String logts(long when) {
\r
200 return(logts(new Date(when)));
\r
203 * Format a logging timestamp as yyyy-mm-ddThh:mm:ss.mmmZ
\r
205 public static synchronized String logts(Date when) {
\r
206 return(logdate.format(when));
\r
209 /* Method prints method name, server FQDN and IP Address of the machine in EELF logs
\r
210 * @Method - setIpAndFqdnForEelf - Rally:US664892
\r
211 * @Params - method, prints method name in EELF log.
\r
213 public static void setIpAndFqdnForEelf(String method) {
\r
215 MDC.put(MDC_SERVICE_NAME, method);
\r
217 MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
\r
218 MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
\r
219 } catch (Exception e) {
\r
220 e.printStackTrace();
\r