2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=========================================================
23 package org.onap.appc.artifact.handler.dbservices;
25 import java.sql.Connection;
26 import java.sql.PreparedStatement;
27 import java.sql.ResultSet;
28 import java.sql.ResultSetMetaData;
29 import java.sql.SQLException;
30 import java.util.ArrayList;
32 import javax.sql.rowset.CachedRowSet;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.ccsdk.sli.core.dblib.DbLibService;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
41 public class CtxParameterizedResolver {
43 private static String CRYPT_KEY = "QtfJMKggVk";
44 private static final EELFLogger log = EELFManager.getInstance().getLogger(CtxParameterizedResolver.class);
46 protected static String resolveCtxVars(String key, SvcLogicContext ctx, ArrayList<String> arguments) {
51 if (key.startsWith("'") && key.endsWith("'")) {
52 key = key.substring(1, key.length() - 1);
53 log.debug("Stripped outer single quotes - key is now [" + key + "]");
56 String[] keyTerms = key.split("\\s+");
58 StringBuffer sqlBuffer = new StringBuffer();
60 for (int i = 0; i < keyTerms.length; i++) {
61 sqlBuffer.append(resolveTerm(keyTerms[i], ctx, arguments));
62 sqlBuffer.append(" ");
65 return (sqlBuffer.toString());
68 private static String resolveTerm(String term, SvcLogicContext ctx, ArrayList<String> arguments) {
73 log.trace("resolveTerm: term is " + term);
75 if (term.startsWith("$") && (ctx != null)) {
76 // Resolve any index variables.
77 term = resolveCtxVariable(term.substring(1), ctx);
78 // Escape single quote
80 term = term.replaceAll("'", "''");
90 private static String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
92 if (ctxVarName.indexOf('[') == -1) {
93 // Ctx variable contains no arrays
94 if ("CRYPT_KEY".equals(ctxVarName)) {
95 // Handle crypt key as special case. If it's set as a context
96 // variable, use it. Otherwise, use
97 // configured crypt key.
98 String cryptKey = ctx.getAttribute(ctxVarName);
99 if ((cryptKey != null) && (cryptKey.length() > 0)) {
105 return (ctx.getAttribute(ctxVarName));
108 // Resolve any array references
109 StringBuffer sbuff = new StringBuffer();
110 String[] ctxVarParts = ctxVarName.split("\\[");
111 sbuff.append(ctxVarParts[0]);
112 for (int i = 1; i < ctxVarParts.length; i++) {
113 if (ctxVarParts[i].startsWith("$")) {
114 int endBracketLoc = ctxVarParts[i].indexOf("]");
115 if (endBracketLoc == -1) {
116 // Missing end bracket ... give up parsing
117 log.warn("Variable reference " + ctxVarName + " seems to be missing a ']'");
118 return (ctx.getAttribute(ctxVarName));
121 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
122 String remainder = ctxVarParts[i].substring(endBracketLoc);
125 sbuff.append(ctx.getAttribute(idxVarName));
126 sbuff.append(remainder);
129 // Index is not a variable reference
131 sbuff.append(ctxVarParts[i]);
135 return (ctx.getAttribute(sbuff.toString()));
138 protected static void saveCachedRowSetToCtx(CachedRowSet results, SvcLogicContext ctx, String prefix, DbLibService dblibSvc)
139 throws SQLException {
141 if ((prefix != null) && prefix.endsWith("[]")) {
143 String pfx = prefix.substring(0, prefix.length() - 2);
146 ResultSetMetaData rsMeta = results.getMetaData();
147 int numCols = rsMeta.getColumnCount();
149 for (int i = 0; i < numCols; i++) {
150 String colValue = null;
151 String tableName = rsMeta.getTableName(i + 1);
152 if (rsMeta.getColumnType(i + 1) == java.sql.Types.VARBINARY) {
153 colValue = decryptColumn(tableName, rsMeta.getColumnName(i + 1), results.getBytes(i + 1),
156 colValue = results.getString(i + 1);
158 log.debug("Setting " + pfx + "[" + idx + "]."
159 + rsMeta.getColumnLabel(i + 1).replaceAll("_", "-") + " = " + colValue);
160 ctx.setAttribute(pfx + "[" + idx + "]." + rsMeta.getColumnLabel(i + 1).replaceAll("_", "-"),
164 } while (results.next());
165 log.debug("Setting " + pfx + "_length = " + idx);
166 ctx.setAttribute(pfx + "_length", "" + idx);
168 ResultSetMetaData rsMeta = results.getMetaData();
169 int numCols = rsMeta.getColumnCount();
171 for (int i = 0; i < numCols; i++) {
172 String colValue = null;
173 String tableName = rsMeta.getTableName(i + 1);
174 if ("VARBINARY".equalsIgnoreCase(rsMeta.getColumnTypeName(i + 1))) {
175 colValue = decryptColumn(tableName, rsMeta.getColumnName(i + 1), results.getBytes(i + 1),
178 colValue = results.getString(i + 1);
180 if (prefix != null) {
181 log.debug("Setting " + prefix + "." + rsMeta.getColumnLabel(i + 1).replaceAll("_", "-") + " = "
183 ctx.setAttribute(prefix + "." + rsMeta.getColumnLabel(i + 1).replaceAll("_", "-"), colValue);
185 log.debug("Setting " + rsMeta.getColumnLabel(i + 1).replaceAll("_", "-") + " = " + colValue);
186 ctx.setAttribute(rsMeta.getColumnLabel(i + 1).replaceAll("_", "-"), colValue);
193 private static String decryptColumn(String tableName, String colName, byte[] colValue, DbLibService dblibSvc) {
194 String strValue = new String(colValue);
196 if (StringUtils.isAsciiPrintable(strValue)) {
198 // If printable, not encrypted
201 ResultSet results = null;
202 try (Connection conn = dblibSvc.getConnection();
203 PreparedStatement stmt = conn.prepareStatement("SELECT CAST(AES_DECRYPT(?, ?) AS CHAR(50)) FROM DUAL")) {
205 stmt.setBytes(1, colValue);
206 stmt.setString(2, CRYPT_KEY);
207 results = stmt.executeQuery();
209 if ((results != null) && results.next()) {
210 strValue = results.getString(1);
211 log.debug("Decrypted value is " + strValue);
213 log.warn("Cannot decrypt " + tableName + "." + colName);
215 } catch (Exception e) {
216 log.error("Caught exception trying to decrypt " + tableName + "." + colName, e);
218 if (results != null) {
221 } catch (SQLException se) {
222 log.error("Caught exception trying to close ResultSet",se);