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