Adding UI extensibility
[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 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
33 import org.onap.aai.sparky.logging.AaiUiMsgs;
34
35 /**
36  * The Class Encryptor.
37  */
38 public class Encryptor {
39
40   private static final Logger LOG = LoggerFactory.getInstance().getLogger(Encryptor.class);
41
42   /**
43    * Instantiates a new encryptor.
44    */
45   public Encryptor() {}
46
47   /**
48    * Encrypt value.
49    *
50    * @param value to encrypt
51    * @return the encrypted string
52    */
53   public String encryptValue(String value) {
54     String encyptedValue = "";
55     try {
56       encyptedValue = Password.obfuscate(value);
57     } catch (Exception exc) {
58       LOG.error(AaiUiMsgs.ENCRYPTION_ERROR, value, exc.toString());
59     }
60     return encyptedValue;
61   }
62
63   /**
64    * Decrypt value.
65    *
66    * @param value the value
67    * @return the string
68    */
69   public String decryptValue(String value) {
70     String decyptedValue = "";
71     try {
72       decyptedValue = Password.deobfuscate(value);
73     } catch (Exception exc) {
74       LOG.error(AaiUiMsgs.DECRYPTION_ERROR, value, exc.toString());
75     }
76
77     return decyptedValue;
78   }
79
80   /**
81    * Usage.
82    */
83   public static void usage() {
84     usage(null);
85   }
86
87   /**
88    * Usage.
89    *
90    * @param msg the msg
91    */
92   public static void usage(String msg) {
93     if (msg != null) {
94       System.err.println(msg);
95     }
96     System.err.println("Usage: java Encryptor -e value");
97     System.err.println("\tEncrypt the given value");
98     System.err.println("Usage: java Encryptor -d value");
99     System.err.println("\tDecrypt the given value");
100     System.exit(1);
101   }
102
103   /**
104    * The main method.
105    *
106    * @param args the arguments
107    */
108   public static void main(String[] args) {
109
110     Options options = new Options();
111     options.addOption("d", true, "value to decrypt");
112     options.addOption("h", false, "show help");
113     options.addOption("?", false, "show help");
114
115     String value = null;
116     boolean encrypt = false;
117     boolean decrypt = false;
118
119     CommandLineParser parser = new BasicParser();
120     CommandLine cmd = null;
121
122     try {
123       cmd = parser.parse(options, args);
124
125       if (cmd.hasOption("d")) {
126         value = cmd.getOptionValue("d");
127         decrypt = true;
128       }
129
130       if (cmd.hasOption("?") || cmd.hasOption("h")) {
131         usage();
132         System.exit(0);
133       }
134
135       if ((encrypt && decrypt) || (!encrypt && !decrypt)) {
136         usage("Must specify one (and only one) of the -e or -d options");
137       }
138
139       Encryptor encryptor = new Encryptor();
140
141       if (decrypt) {
142         String out = encryptor.decryptValue(value);
143         System.out.println(out);
144       }
145     } catch (ParseException exc) {
146       System.out.println("Failed to parse command line properties: " + exc.toString());
147     } catch (Exception exc) {
148       System.out.println("Failure: " + exc.toString());
149     }
150
151     System.exit(0);
152   }
153 }