Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / util / Encryptor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.util;
24
25 import org.apache.commons.cli.BasicParser;
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.CommandLineParser;
28 import org.apache.commons.cli.Options;
29 import org.apache.commons.cli.ParseException;
30 import org.eclipse.jetty.util.security.Password;
31
32 /**
33  * The Class Encryptor.
34  */
35 public class Encryptor {
36
37   /**
38    * Instantiates a new encryptor.
39    */
40   public Encryptor() {  
41   }
42   
43   /**
44    * Decrypt value.
45    *
46    * @param value the value
47    * @return the string
48    */
49   public String decryptValue(String value) {
50     String decyptedValue = "";
51     
52     try {
53       decyptedValue = Password.deobfuscate(value);
54     } catch (Exception exc) {
55       System.err.println("Cannot decrypt '" + value + "': " + exc.toString());
56     }
57
58     return decyptedValue;
59   }
60
61   /**
62    * Usage.
63    */
64   public static void usage() {
65     usage(null);
66   }
67
68   /**
69    * Usage.
70    *
71    * @param msg the msg
72    */
73   public static void usage(String msg) {
74     if (msg != null) {
75       System.err.println(msg);
76     }
77     System.err.println("Usage: java Encryptor -e value");
78     System.err.println("\tEncrypt the given value");
79     System.err.println("Usage: java Encryptor -d value");
80     System.err.println("\tDecrypt the given value");
81     System.exit(1);
82   }
83
84   /**
85    * The main method.
86    *
87    * @param args the arguments
88    */
89   public static void main(String[] args) {
90
91     Options options = new Options();
92     options.addOption("d", true, "value to decrypt");
93     options.addOption("h", false, "show help");
94     options.addOption("?", false, "show help");
95
96     String value = null;
97     boolean encrypt = false;
98     boolean decrypt = false;
99
100     CommandLineParser parser = new BasicParser();
101     CommandLine cmd = null;
102
103     try {
104       cmd = parser.parse(options, args);
105
106       if (cmd.hasOption("d")) {
107         value = cmd.getOptionValue("d");
108         decrypt = true;
109       }
110
111       if (cmd.hasOption("?") || cmd.hasOption("h")) {
112         usage();
113         System.exit(0);
114       }
115
116       if ((encrypt && decrypt) || (!encrypt && !decrypt)) {
117         usage("Must specify one (and only one) of the -e or -d options");
118       }
119
120       Encryptor encryptor = new Encryptor();
121
122       if (decrypt) {
123         String out = encryptor.decryptValue(value);
124         System.out.println(out);
125       }
126     } catch (ParseException exc) {
127       System.out.println("Failed to parse command line properties: " + exc.toString());
128     } catch (Exception exc) {
129       System.out.println("Failure: " + exc.toString());
130     }
131
132     System.exit(0);
133   }
134 }