aboutsummaryrefslogtreecommitdiffstats
path: root/policy-utils/src/main/java/org/onap/policy/drools/utils/logging/LoggerUtil.java
blob: 9c6dad793945cf73667dc433f9c7e49351631d5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2017-2019,2021 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.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.utils.logging;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Loger Utils.
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class LoggerUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerUtil.class);

    /**
     * Logback configuration file system property.
     */
    public static final String LOGBACK_CONFIGURATION_FILE_SYSTEM_PROPERTY = "logback.configurationFile";

    /**
     * Logback default configuration file location.
     */
    public static final String LOGBACK_CONFIGURATION_FILE_DEFAULT = "config/logback.xml";

}
ight .s { color: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 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.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.cadi.cm;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Collection;
import java.util.List;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

import com.att.cadi.Symm;
import com.att.inno.env.Env;
import com.att.inno.env.TimeTaken;
import com.att.inno.env.Trans;

public class Factory {
	private static final String PRIVATE_KEY_HEADER = "PRIVATE KEY";
	public static final String KEY_ALGO = "RSA";
	public static final String SIG_ALGO = "SHA256withRSA";

	public  static final int KEY_LENGTH = 2048;
	private static final KeyPairGenerator keygen;
	private static final KeyFactory keyFactory;
	private static final CertificateFactory certificateFactory;
	private static final SecureRandom random;
	
	
	private static final Symm base64 = Symm.base64.copy(64);

	static {
			random = new SecureRandom();
			KeyPairGenerator tempKeygen;
			try {
				tempKeygen = KeyPairGenerator.getInstance(KEY_ALGO);//,"BC");
				tempKeygen.initialize(KEY_LENGTH, random);
			} catch (NoSuchAlgorithmException e) {
				tempKeygen = null;
				e.printStackTrace(System.err);
			}
			keygen = tempKeygen;

			KeyFactory tempKeyFactory;
			try {
				tempKeyFactory=KeyFactory.getInstance(KEY_ALGO);//,"BC"
			} catch (NoSuchAlgorithmException e) {
				tempKeyFactory = null;
				e.printStackTrace(System.err);
			};
			keyFactory = tempKeyFactory;
			 
			CertificateFactory tempCertificateFactory;
			try {
				tempCertificateFactory = CertificateFactory.getInstance("X.509");
			} catch (CertificateException e) {
				tempCertificateFactory = null;
				e.printStackTrace(System.err);
			}
			certificateFactory = tempCertificateFactory;

		 
	}


	public static KeyPair generateKeyPair(Trans trans) {
		TimeTaken tt;
		if(trans!=null) {
			tt = trans.start("Generate KeyPair", Env.SUB);
		} else {
			tt = null;
		}
		try {
			return keygen.generateKeyPair();
		} finally {
			if(tt!=null) {
				tt.done();
			}
		}
	}  

	private static final String LINE_END = "-----\n";

	protected static String textBuilder(String kind, byte[] bytes) throws IOException {
		StringBuilder sb = new StringBuilder();
		sb.append("-----BEGIN ");
		sb.append(kind);
		sb.append(LINE_END);

		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		base64.encode(bais, baos);
		sb.append(new String(baos.toByteArray()));
		
		if(sb.charAt(sb.length()-1)!='\n') {
			sb.append('\n');
		}
		sb.append("-----END ");
		sb.append(kind);
		sb.append(LINE_END);
		return sb.toString();
	}
	
	public static PrivateKey toPrivateKey(Trans trans, String pk) throws IOException, CertException {
		byte[] bytes = decode(new StringReader(pk));
		return toPrivateKey(trans, bytes);
	}
	
	public static PrivateKey toPrivateKey(Trans trans, byte[] bytes) throws IOException, CertException {
		TimeTaken tt=trans.start("Reconstitute Private Key", Env.SUB);
		try {
			return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(bytes));
		} catch (InvalidKeySpecException e) {
			throw new CertException("Translating Private Key from PKCS8 KeySpec",e);
		} finally {
			tt.done();
		}
	}
	
	public static PrivateKey toPrivateKey(Trans trans, File file) throws IOException, CertException {
		TimeTaken tt = trans.start("Decode Private Key File", Env.SUB);
		try {
			return toPrivateKey(trans,decode(file));
		}finally {
			tt.done();
		}
	}

	public static String toString(Trans trans, PrivateKey pk) throws IOException {
//		PKCS8EncodedKeySpec pemContents = new PKCS8EncodedKeySpec(pk.getEncoded());
		trans.debug().log("Private Key to String");
		return textBuilder(PRIVATE_KEY_HEADER,pk.getEncoded());
	}

	public static PublicKey toPublicKey(Trans trans, String pk) throws IOException {
		TimeTaken tt = trans.start("Reconstitute Public Key", Env.SUB);
		try {
			ByteArrayInputStream bais = new ByteArrayInputStream(pk.getBytes());
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			Symm.base64noSplit.decode(bais, baos);

			return keyFactory.generatePublic(new X509EncodedKeySpec(baos.toByteArray()));
		} catch (InvalidKeySpecException e) {
			trans.error().log(e,"Translating Public Key from X509 KeySpec");
			return null;
		} finally {
			tt.done();
		}
	}
	
	public static String toString(Trans trans, PublicKey pk) throws IOException {
		trans.debug().log("Public Key to String");
		return textBuilder("PUBLIC KEY",pk.getEncoded());
	}

	public static Collection<? extends Certificate> toX509Certificate(String x509) throws CertificateException {
		return toX509Certificate(x509.getBytes());
	}
	
	public static Collection<? extends Certificate> toX509Certificate(List<String> x509s) throws CertificateException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			for(String x509 : x509s) {
				baos.write(x509.getBytes());
			}
		} catch (IOException e) {
			throw new CertificateException(e);
		}
		return toX509Certificate(new ByteArrayInputStream(baos.toByteArray()));
	}

	public static Collection<? extends Certificate> toX509Certificate(byte[] x509) throws CertificateException {
		return certificateFactory.generateCertificates(new ByteArrayInputStream(x509));
	}

	public static Collection<? extends Certificate> toX509Certificate(Trans trans, File file) throws CertificateException, FileNotFoundException {
		FileInputStream fis = new FileInputStream(file);
		try {
			return toX509Certificate(fis);
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				throw new CertificateException(e);
			}
		}
	}

	public static Collection<? extends Certificate> toX509Certificate(InputStream is) throws CertificateException {
		return certificateFactory.generateCertificates(is);
	}

	public static String toString(Trans trans, Certificate cert) throws IOException, CertException {
		if(trans.debug().isLoggable()) {
			StringBuilder sb = new StringBuilder("Certificate to String");
			if(cert instanceof X509Certificate) {
				sb.append(" - ");
				sb.append(((X509Certificate)cert).getSubjectDN());
			}
			trans.debug().log(sb);
		}
		try {
			if(cert==null) {
				throw new CertException("Certificate not built");
			}
			return textBuilder("CERTIFICATE",cert.getEncoded());
		} catch (CertificateEncodingException e) {
			throw new CertException(e);
		}
	}

	public static Cipher pkCipher() throws NoSuchAlgorithmException, NoSuchPaddingException {
		return Cipher.getInstance(KEY_ALGO); 
	}

	public static Cipher pkCipher(Key key, boolean encrypt) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
		Cipher cipher = Cipher.getInstance(KEY_ALGO);
		cipher.init(encrypt?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE,key);
		return cipher;
	}

	public static byte[] strip(Reader rdr) throws IOException {
		BufferedReader br = new BufferedReader(rdr);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String line;
		while((line=br.readLine())!=null) {
			if(line.length()>0 &&
			   !line.startsWith("-----") &&
			   line.indexOf(':')<0) {  // Header elements
				baos.write(line.getBytes());
			}
		}
		return baos.toByteArray();
	}
	
	public static class StripperInputStream extends InputStream {
		private Reader created;
		private BufferedReader br;
		private int idx;
		private String line;

		public StripperInputStream(Reader rdr) {
			if(rdr instanceof BufferedReader) {
				br = (BufferedReader)rdr;
			} else {
				br = new BufferedReader(rdr);
			}
			created = null;
		}
		
		public StripperInputStream(File file) throws FileNotFoundException {
			this(new FileReader(file));
			created = br;
		}

		public StripperInputStream(InputStream is) throws FileNotFoundException {
			this(new InputStreamReader(is));
			created = br;
		}

		@Override
		public int read() throws IOException {
			if(line==null || idx>=line.length()) {
				while((line=br.readLine())!=null) {
					if(line.length()>0 &&
					   !line.startsWith("-----") &&
					   line.indexOf(':')<0) {  // Header elements
						break;
					}
				}

				if(line==null) {
					return -1;
				}
				idx = 0;
			}
			return line.charAt(idx++);
		}

		/* (non-Javadoc)
		 * @see java.io.InputStream#close()
		 */
		@Override
		public void close() throws IOException {
			if(created!=null) {
				created.close();
			}
		}
	}

	public static class Base64InputStream extends InputStream {
		private InputStream created;
		private InputStream is;
		private byte trio[];
		private byte duo[];
		private int idx;

		
		public Base64InputStream(File file) throws FileNotFoundException {
			this(new FileInputStream(file));
			created = is;
		}

		public Base64InputStream(InputStream is) throws FileNotFoundException {
			this.is = is;
			trio = new byte[3];
			idx = 4;
		}

		@Override
		public int read() throws IOException {
			if(duo==null || idx>=duo.length) {
				int read = is.read(trio);
				if(read==-1) {
					return -1;
				}
				duo = Symm.base64.decode(trio);
				if(duo==null || duo.length==0) {
					return -1;
				}
				idx=0;
			}
			
			return duo[idx++];
		}

		/* (non-Javadoc)
		 * @see java.io.InputStream#close()
		 */
		@Override
		public void close() throws IOException {
			if(created!=null) {
				created.close();
			}
		}
	}

	public static byte[] decode(byte[] bytes) throws IOException {
		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		Symm.base64.decode(bais, baos);
		return baos.toByteArray();
	}
	
	public static byte[] decode(File f) throws IOException {
		FileReader fr = new FileReader(f);
		try {
			return Factory.decode(fr);
		} finally {
			fr.close();
		}

	}
	public static byte[] decode(Reader rdr) throws IOException {
		return decode(strip(rdr));
	}


	public static byte[] binary(File file) throws IOException {
		DataInputStream dis = new DataInputStream(new FileInputStream(file));
		try {
			byte[] bytes = new byte[(int)file.length()];
			dis.readFully(bytes);
			return bytes;
		} finally {
			dis.close();
		}
	}


	public static byte[] sign(Trans trans, byte[] bytes, PrivateKey pk) throws IOException, InvalidKeyException, SignatureException, NoSuchAlgorithmException {
		TimeTaken tt = trans.start("Sign Data", Env.SUB);
		try {
			Signature sig = Signature.getInstance(SIG_ALGO);
			sig.initSign(pk, random);
			sig.update(bytes);
			return sig.sign();
		} finally {
			tt.done();
		}
	}

	public static String toSignatureString(byte[] signed) throws IOException {
		return textBuilder("SIGNATURE", signed);
	}

	public static boolean verify(Trans trans, byte[] bytes, byte[] signature, PublicKey pk) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
		TimeTaken tt = trans.start("Verify Data", Env.SUB);
		try {
			Signature sig = Signature.getInstance(SIG_ALGO);
			sig.initVerify(pk);
			sig.update(bytes);
			return sig.verify(signature);
		} finally {
			tt.done();
		}	
	}
}