Enhancement to use the common CryptoUtils
[policy/engine.git] / PolicyEngineUtils / src / main / java / org / onap / policy / utils / PeCryptoUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.utils;
22
23 import java.security.GeneralSecurityException;
24 import java.util.Map;
25 import java.util.concurrent.ConcurrentHashMap;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.common.utils.security.CryptoUtils;
30
31 public class PeCryptoUtils {
32
33     private static Logger logger = FlexLogger.getLogger(PeCryptoUtils.class);
34     private static final String PROP_AES_KEY = "org.onap.policy.encryption.aes.key";
35     private static CryptoUtils cryptoUtils = null;
36     private static String secretKey = System.getenv("AES_ENCRYPTION_KEY");
37     private static final Map<String, String> decryptCache = new ConcurrentHashMap<>();
38     private static final Map<String, String> encryptCache = new ConcurrentHashMap<>();
39
40
41     private PeCryptoUtils() {}
42
43     /**
44      * Inits the aes key.
45      *
46      * @param theSecretKey the the secret key
47      */
48     public static synchronized void initAesKey(String theSecretKey) {
49         String secKey = theSecretKey;
50         if (cryptoUtils == null) {
51             if (StringUtils.isBlank(secKey)) {
52                 secKey = System.getProperty(PROP_AES_KEY);
53             }
54             if (StringUtils.isBlank(secKey)) {
55                 secKey = secretKey;
56             }
57             cryptoUtils = new CryptoUtils(secKey);
58         }
59     }
60
61     /**
62      * Encrypt a value based on the Policy Encryption Key.
63      *
64      * @param value The plain text string
65      * @return The encrypted String
66      */
67     public static String encrypt(String value) {
68
69         if (cryptoUtils == null || StringUtils.isBlank(value)) {
70             return value;
71         }
72
73         return encryptCache.computeIfAbsent(value, k -> {
74             try {
75                 return cryptoUtils.encrypt(k);
76             } catch (GeneralSecurityException e) {
77                 logger.error("Could not decrypt value - exception: ", e);
78                 return value;
79             }
80         });
81     }
82
83     /**
84      * Decrypt a value based on the Policy Encryption Key if string begin with 'enc:'.
85      *
86      * @param value The encrypted string that must be decrypted using the Policy Encryption Key
87      * @return The String decrypted if string begin with 'enc:'
88      */
89     public static String decrypt(String value) {
90         if (cryptoUtils == null || StringUtils.isBlank(value)) {
91             return value;
92         }
93         return decryptCache.computeIfAbsent(value, k -> {
94             try {
95                 return cryptoUtils.decrypt(k);
96             } catch (GeneralSecurityException e) {
97                 logger.error("Could not decrypt value - exception: ", e);
98                 return value;
99             }
100         });
101     }
102 }