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