[VID-3] Setting docker image tag
[vid.git] / vid / src / main / java / org / openecomp / vid / encryption / EncryptedPropValue.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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.openecomp.vid.encryption;
22
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;
28
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;
35
36 /**
37  * The Class EncryptedPropValue.
38  */
39 public class EncryptedPropValue {
40
41         /** The encrypted configuration. */
42         private EncryptedConfiguration encryptedConfiguration;
43         
44         /** The encryption key. */
45         private String encryptionKey;
46         
47         /** The encryption method. */
48         private String encryptionMethod;
49
50         /** The logger. */
51         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EncryptedPropValue.class);
52         
53         /**
54          * Instantiates a new encrypted prop value.
55          */
56         public EncryptedPropValue() {
57                     // encryptionKey = "57ajqe{kJjjarj}G#(3)ea7";
58                     encryptionKey = "aa1adm1n";
59                     encryptionMethod = "AES";
60                     encryptedConfiguration = new EncryptedConfiguration(encryptionKey, encryptionMethod);
61                   }
62
63         /**
64          * Gets the encrypted string.
65          *
66          * @param f the f
67          * @param name the name
68          * @param deflt the deflt
69          * @return the encrypted string
70          * @throws Exception the exception
71          */
72         public String getEncryptedString(String f, String name, String deflt) throws Exception {
73                 return encryptedConfiguration.getString(f, name, deflt);
74         }
75
76         /**
77          * Generate encrypted property.
78          *
79          * @param name the name
80          * @param value the value
81          */
82         public static void generateEncryptedProperty(String name, String value) {
83                 logger.debug(EELFLoggerDelegate.debugLogger, "==> generateEncryptedProperty");
84                 EncryptedPropValue aaiPropValue = new EncryptedPropValue();
85             try {
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());
93             }
94         }
95
96         /**
97          * Extract property.
98          *
99          * @param f the f
100          * @param name the name
101          */
102         public static void extractProperty(String f, String name) {
103                 EncryptedPropValue aaiPropValue = new EncryptedPropValue();
104                 String val = "";
105                 logger.debug(EELFLoggerDelegate.debugLogger, "==> extractProperty");
106                 try {
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());
111                 }
112         }
113
114         /**
115          * Usage.
116          */
117         public static void usage() {
118                 usage(null);
119         }
120
121
122         /**
123          * Decrypt triple.
124          *
125          * @param triple the triple
126          * @return the string
127          */
128         public static String decryptTriple(String triple) {
129                 EncryptedPropValue aaiPropValue = new EncryptedPropValue();
130                 logger.debug(EELFLoggerDelegate.debugLogger, "==> descrptTriple");
131
132                 String out = "";
133                 try {
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());
143                 }
144                     
145                 return out;
146         }
147
148         /**
149          * Encrypt input.
150          */
151         public static void encryptInput() {
152                 String s;
153
154                 Pattern p = Pattern.compile("^ENCRYPTME[.]([A-Z]*)[.]([^= \t]*)[ \t]*=[ \t]*([^ \t]*)[ \t]*$");
155
156                 EncryptedPropValue aaiPropValue = null;
157
158                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
159                     
160                 try {
161                         while ((s = in.readLine()) != null) {
162                         Matcher m = p.matcher(s);
163                         if (m.matches()) {
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);
169                           try {
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());
176                           } // end of try
177                         } else {
178                           System.out.println(s);
179                         }
180                       } // end of while
181                     } catch (IOException e) {
182                       System.err.println("Error: Cannot read from stdin: " + e.toString());
183                     }
184
185                   } 
186
187                   /**
188                  * Usage.
189                  *
190                  * @param msg the msg
191                  */
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");
202                     System.exit(1);
203                   }
204
205         /**
206          * The main method.
207          *
208          * @param args the arguments
209          */
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");
219             
220             String propfile = null, name = null, value = null, unencrypt = null;
221             boolean encryptStdin = false;
222
223             CommandLineParser parser = new DefaultParser();
224             CommandLine cmd = null;
225             
226             try {
227                 cmd = parser.parse(options, args);
228                 
229                 System.out.println("You picked " + cmd.toString() + "\n");
230                 if (cmd.hasOption("n")) {
231                         name = cmd.getOptionValue("n");
232                 }
233                 if (cmd.hasOption("f")) {
234                         propfile = cmd.getOptionValue("f");
235                 }
236                 if (cmd.hasOption("u")) {
237                         unencrypt = cmd.getOptionValue("u");
238                 }
239                 if (cmd.hasOption("E")) {
240                         encryptStdin = true;
241                 }
242                 if (cmd.hasOption("v")) {
243                         value = cmd.getOptionValue("v");
244                 }
245                 if (cmd.hasOption("?") || cmd.hasOption("h")) {
246                         usage();
247                         System.exit(0);
248                 }
249
250                     if (encryptStdin) {
251                       if (name != null || propfile != null || value != null) {
252                         usage("cannot use -E with other options");
253                       }
254                       encryptInput();
255                     } else if (unencrypt == null) {
256                         if (name == null) usage("-n is required");
257                         if (propfile == null) {
258                           if (value == null) usage("-v required"); 
259                           if (value != null) {
260                              generateEncryptedProperty(name, value);
261                           }
262                         } else {
263                           extractProperty(propfile, name);
264                         } 
265                     } else {
266                       String out = decryptTriple(unencrypt);
267                       System.out.println(out);
268                     }
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());
273             }
274             
275         System.exit(0);
276         
277         }
278
279 }