2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.vid.encryption;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
29 import org.apache.commons.cli.CommandLine;
30 import org.apache.commons.cli.CommandLineParser;
31 import org.apache.commons.cli.DefaultParser;
32 import org.apache.commons.cli.Options;
33 import org.apache.commons.cli.ParseException;
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 * The Class EncryptedPropValue.
39 public class EncryptedPropValue {
41 /** The encrypted configuration. */
42 private EncryptedConfiguration encryptedConfiguration;
44 /** The encryption key. */
45 private String encryptionKey;
47 /** The encryption method. */
48 private String encryptionMethod;
51 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EncryptedPropValue.class);
54 * Instantiates a new encrypted prop value.
56 public EncryptedPropValue() {
57 // encryptionKey = "57ajqe{kJjjarj}G#(3)ea7";
58 encryptionKey = "aa1adm1n";
59 encryptionMethod = "AES";
60 encryptedConfiguration = new EncryptedConfiguration(encryptionKey, encryptionMethod);
64 * Gets the encrypted string.
67 * @param name the name
68 * @param deflt the deflt
69 * @return the encrypted string
70 * @throws Exception the exception
72 public String getEncryptedString(String f, String name, String deflt) throws Exception {
73 return encryptedConfiguration.getString(f, name, deflt);
77 * Generate encrypted property.
79 * @param name the name
80 * @param value the value
82 public static void generateEncryptedProperty(String name, String value) {
83 logger.debug(EELFLoggerDelegate.debugLogger, "==> generateEncryptedProperty");
84 EncryptedPropValue aaiPropValue = new EncryptedPropValue();
86 System.out.println(name + ".x=" +
87 EncryptedConfiguration.encryptToTriple(
88 aaiPropValue.encryptionMethod,
89 EncryptedConfiguration.generateSalt(),
90 aaiPropValue.encryptionKey, value));
91 } catch (Exception e) {
92 System.err.println("Cannot encrypt '" + value + "' for property '" + name + "': "+ e.toString());
100 * @param name the name
102 public static void extractProperty(String f, String name) {
103 EncryptedPropValue aaiPropValue = new EncryptedPropValue();
105 logger.debug(EELFLoggerDelegate.debugLogger, "==> extractProperty");
107 val = aaiPropValue.getEncryptedString(f, name, "");
108 System.out.println(val);
109 } catch (Exception e) {
110 System.err.println("Cannot extract '" + name + "' from '" + f + "': " + e.toString());
117 public static void usage() {
125 * @param triple the triple
128 public static String decryptTriple(String triple) {
129 EncryptedPropValue aaiPropValue = new EncryptedPropValue();
130 logger.debug(EELFLoggerDelegate.debugLogger, "==> descrptTriple");
134 //System.out.println(dragonPropValue.encryptedConfiguration.decrypt(triple, dragonPropValue.encryptionKey));
135 logger.debug(EELFLoggerDelegate.debugLogger, "calling dragonPropValue.encryptedConfiguration.decrypt()");
136 out = EncryptedConfiguration.decrypt(triple,
137 aaiPropValue.encryptionKey,
138 aaiPropValue.encryptionMethod,
139 EncryptedConfiguration.generateSalt());
140 //System.out.println("out = " + out);
141 } catch (Exception e) {
142 System.err.println("Cannot decrypt '" + triple + "': " + e.toString());
151 public static void encryptInput() {
154 Pattern p = Pattern.compile("^ENCRYPTME[.]([A-Z]*)[.]([^= \t]*)[ \t]*=[ \t]*([^ \t]*)[ \t]*$");
156 EncryptedPropValue aaiPropValue = null;
158 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
161 while ((s = in.readLine()) != null) {
162 Matcher m = p.matcher(s);
164 if (aaiPropValue == null)
165 aaiPropValue = new EncryptedPropValue();
166 String method = m.group(1);
167 String name = m.group(2);
168 String value = m.group(3);
170 System.out.println(name + ".x=" +
171 EncryptedConfiguration.encryptToTriple(method,
172 EncryptedConfiguration.generateSalt(),
173 aaiPropValue.encryptionKey, value));
174 } catch (Exception e) {
175 System.err.println("Error: Cannot encrypt '" + value + "', method '" + method + "' for property '" + name + "': " + e.toString());
178 System.out.println(s);
181 } catch (IOException e) {
182 System.err.println("Error: Cannot read from stdin: " + e.toString());
192 public static void usage(String msg) {
193 if (msg != null) System.err.println(msg);
194 System.err.println("Usage: java EncryptedPropValue -n property -f property-file");
195 System.err.println("\tExtract the named value from the given property-file (or full pathname)");
196 System.err.println("Usage: java EncryptedPropValue -n property -v value");
197 System.err.println("\tEncrypt the given property with the given name and value");
198 System.err.println("Usage: java EncryptedPropValue -E");
199 System.err.println("\tEncrypt all lines that look like ENCRYPTME.METHOD.name=value");
200 System.err.println("Usage: java EncryptedPropValue -u value");
201 System.err.println("\tDecrypt the given value, expressed as a single HEXVAL");
208 * @param args the arguments
210 public static void main(String[] args) {
211 Options options = new Options();
212 options.addOption("n", true, "name");
213 options.addOption("f", true, "property-file");
214 options.addOption("v", true, "value");
215 options.addOption("E", false, "Encrypt all lines that look like ENCRYPTME.METHOD.name=value");
216 options.addOption("u", true, "Decrypt the given value, expressed as a single HEXVAL");
217 options.addOption("h", false, "show help");
218 options.addOption("?", false, "show help");
220 String propfile = null, name = null, value = null, unencrypt = null;
221 boolean encryptStdin = false;
223 CommandLineParser parser = new DefaultParser();
224 CommandLine cmd = null;
227 cmd = parser.parse(options, args);
229 System.out.println("You picked " + cmd.toString() + "\n");
230 if (cmd.hasOption("n")) {
231 name = cmd.getOptionValue("n");
233 if (cmd.hasOption("f")) {
234 propfile = cmd.getOptionValue("f");
236 if (cmd.hasOption("u")) {
237 unencrypt = cmd.getOptionValue("u");
239 if (cmd.hasOption("E")) {
242 if (cmd.hasOption("v")) {
243 value = cmd.getOptionValue("v");
245 if (cmd.hasOption("?") || cmd.hasOption("h")) {
251 if (name != null || propfile != null || value != null) {
252 usage("cannot use -E with other options");
255 } else if (unencrypt == null) {
256 if (name == null) usage("-n is required");
257 if (propfile == null) {
258 if (value == null) usage("-v required");
260 generateEncryptedProperty(name, value);
263 extractProperty(propfile, name);
266 String out = decryptTriple(unencrypt);
267 System.out.println(out);
269 } catch (ParseException e) {
270 System.out.println("Failed to parse command line properties e="+e.toString());
271 } catch (Exception e) {
272 System.out.println("Failed to run EncryptedConfiguration main() e="+e.toString());