920a85aec75a684e781964793fedb92ae7e4110b
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / restcore / JettyObfuscationConversionCommandLineUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.restcore;
23
24 import org.apache.commons.cli.BasicParser;
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.cli.CommandLineParser;
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.cli.ParseException;
29 import org.eclipse.jetty.util.security.Password;
30
31 /*
32  * The purpose of this class is to be a tool for
33  * manually applying jetty obfuscation/deobfuscation
34  * so that one can obfuscate the various passwords/secrets
35  * in aaiconfig.properties.
36  * 
37  * Originally, they were being encrypted by a similar
38  * command line utility, however the encryption key
39  * was being hardcoded in the src package
40  * which is a security violation.
41  * Since this ultimately just moved the problem of how
42  * to hide secrets to a different secret in a different file, 
43  * and since that encryption was really just being done to
44  * obfuscate those values in case someone needed to look at
45  * properties with others looking at their screen,
46  * we decided that jetty obfuscation would be adequate
47  * for that task as well as
48  * removing the "turtles all the way down" secret-to-hide-
49  * the-secret-to-hide-the-secret problem.
50  */
51 public class JettyObfuscationConversionCommandLineUtil {
52         
53         /**
54          * The main method.
55          *
56          * @param args the arguments
57          */
58         public static void main(String[] args){
59                 Options options = new Options();
60                 options.addOption("e", true, "obfuscate the given string");
61                 options.addOption("d", true, "deobfuscate the given string");
62                 
63                 CommandLineParser parser = new BasicParser();
64                 
65                 try {
66                         CommandLine cmd = parser.parse(options, args);
67                         String toProcess = null;
68                         
69                         if (cmd.hasOption("e")){
70                                 toProcess = cmd.getOptionValue("e");
71                                 String encoded = Password.obfuscate(toProcess);
72                                 System.out.println(encoded);
73                         } else if (cmd.hasOption("d")) {
74                                 toProcess = cmd.getOptionValue("d");
75                                 String decoded_str = Password.deobfuscate(toProcess);
76                                 System.out.println(decoded_str);
77                         } else {
78                                 usage();
79                         }
80                 } catch (ParseException e) {
81                         System.out.println("failed to parse input");
82                         System.out.println(e.toString());
83                         usage();
84                 } catch (Exception e) {
85                         System.out.println("exception:" + e.toString());
86                 }
87         }
88         
89         /**
90          * Usage.
91          */
92         private static void usage(){
93                 System.out.println("usage:");;
94                 System.out.println("-e [string] to obfuscate");
95                 System.out.println("-d [string] to deobfuscate");
96                 System.out.println("-h help");
97         }
98 }