Adjust Agent for none K8s
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / CmdLine.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
22 package org.onap.aaf.cadi;
23
24 import java.io.BufferedReader;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.security.NoSuchAlgorithmException;
32
33 import org.onap.aaf.cadi.util.Chmod;
34 import org.onap.aaf.cadi.util.JsonOutputStream;
35
36
37
38 /**
39  * A Class to run on command line to determine suitability of environment for certain TAFs.
40  *  * 
41  * @author Jonathan
42  *
43  */
44 public class CmdLine {
45
46     private static boolean systemExit = true;
47     /**
48      * @param args
49      */
50     public static void main(String[] args) {
51         if (args.length>0) {
52             if ("digest".equalsIgnoreCase(args[0]) && (args.length>2 || (args.length>1 && System.console()!=null))) {
53                 String keyfile;
54                 String password;
55                 if (args.length>2) {
56                     password = args[1];
57                     keyfile = args[2];
58                     if ("-i".equals(password)) {
59                         int c;
60                         StringBuilder sb = new StringBuilder();
61                         try {
62                             while ((c=System.in.read())>=0) {
63                                 sb.append((char)c);
64                             }
65                         } catch (IOException e) {
66                             e.printStackTrace();
67                         }
68                         password = sb.toString();
69                     }
70                 } else {
71                     keyfile = args[1];
72                     password = new String(System.console().readPassword("Type here (keystrokes hidden): "));
73                 }
74
75                 try {
76                     Symm symm;
77                     FileInputStream fis = new FileInputStream(keyfile);
78                     try {
79                         symm = Symm.obtain(fis);
80                     } finally {
81                         fis.close();
82                     }
83                     symm.enpass(password, System.out);
84                     System.out.println();
85                     System.out.flush();
86                     return;
87                     /*  testing code... don't want it exposed
88                     System.out.println(" ******** Testing *********");
89                     for (int i=0;i<100000;++i) {
90                         System.out.println(args[1]);
91                         ByteArrayOutputStream baos = new ByteArrayOutputStream();
92                         b64.enpass(args[1], baos);
93                         String pass; 
94                         System.out.println(pass=new String(baos.toByteArray()));
95                         ByteArrayOutputStream reconstituted = new ByteArrayOutputStream();
96                         b64.depass(pass, reconstituted);
97                         String r = reconstituted.toString();
98                         System.out.println(r);
99                         if (!r.equals(args[1])) {
100                             System.err.println("!!!!! STOP - ERROR !!!!!");
101                             return;
102                         }
103                         System.out.println();
104                     }
105                     System.out.flush();
106                     */
107                      
108                 } catch (IOException e) {
109                     System.err.println("Cannot digest password");
110                     System.err.println("   \""+ e.getMessage() + '"');
111                 }
112 // DO NOT LEAVE THIS METHOD Compiled IN CODE... Do not want looking at passwords on disk too easy
113 // Jonathan.  Oh, well, Deployment services need this behavior.  I will put this code in, but leave it undocumented. 
114 // One still needs access to the keyfile to read.
115 // July 2016 - thought of a tool "CMPass" to regurgitate from properties, but only if allowed.
116             } else if (("regurgitate".equalsIgnoreCase(args[0]) || "undigest".equalsIgnoreCase(args[0]))
117                                 && args.length>2) {
118                 try {
119                     Symm symm;
120                     FileInputStream fis = new FileInputStream(args[2]);
121                     try {
122                         symm = Symm.obtain(fis);
123                     } finally {
124                         fis.close();
125                     }
126                     boolean isFile = false;
127                     if ("-i".equals(args[1]) || (isFile="-f".equals(args[1]))) {
128                         BufferedReader br;
129                         if (isFile) {
130                             if (args.length<4) {
131                                 System.err.println("Filename in 4th position");
132                                 return;
133                             }
134                             br = new BufferedReader(new FileReader(args[3]));
135                         } else {
136                             br = new BufferedReader(new InputStreamReader(System.in));
137                         }
138                         try {
139                             String line;
140                             boolean cont = false;
141                             StringBuffer sb = new StringBuffer();
142                             JsonOutputStream jw = new JsonOutputStream(System.out);
143                             while ((line=br.readLine())!=null) {
144                                 if (cont) {
145                                     int end;
146                                     if ((end=line.indexOf('"'))>=0) {
147                                         sb.append(line,0,end);
148                                         cont=false;
149                                     } else {
150                                         sb.append(line);
151                                     }
152                                 } else {
153                                     int idx;
154                                     if ((idx = line.indexOf(' '))>=0 
155                                             && (idx = line.indexOf(' ',++idx))>0
156                                             && (idx = line.indexOf('=',++idx))>0
157                                             ) {
158                                         System.out.println(line.substring(0, idx-5));
159                                         int start = idx+2;
160                                         int end;
161                                         if ((end=line.indexOf('"',start))<0) {
162                                             end = line.length();
163                                             cont = true;
164                                         }
165                                         sb.append(line,start,end);
166                                     }
167                                 }
168                                 if (sb.length()>0) {
169                                     symm.depass(sb.toString(),jw);
170                                     if (!cont) {
171                                         System.out.println();
172                                     }
173                                 }
174                                 System.out.flush();
175                                 sb.setLength(0);
176                                 if (!cont) {
177                                     jw.resetIndent();
178                                 }
179                             }
180                         } finally {
181                             if (isFile) {
182                                 br.close();
183                             }
184                         }
185                     } else {
186                         symm.depass(args[1], System.out);
187                     }
188                     System.out.println();
189                     System.out.flush();
190                     return;
191                 } catch (IOException e) {
192                     System.err.println("Cannot undigest password");
193                     System.err.println("   \""+ e.getMessage() + '"');
194                 }
195             } else if ("encode64".equalsIgnoreCase(args[0]) && args.length>1) {
196                 try {
197                     Symm.base64.encode(args[1], System.out);
198                     System.out.println();
199                     System.out.flush();
200                     return;
201                 } catch (IOException e) {
202                     System.err.println("Cannot encode Base64 with " + args[1]);
203                     System.err.println("   \""+ e.getMessage() + '"');
204                 }
205             } else if ("decode64".equalsIgnoreCase(args[0]) && args.length>1) {
206                 try {
207                     Symm.base64.decode(args[1], System.out);
208                     System.out.println();
209                     System.out.flush();
210                     return;
211                 } catch (IOException e) {
212                     System.err.println("Cannot decode Base64 text from " + args[1]);
213                     System.err.println("   \""+ e.getMessage() + '"');
214                 }
215             } else if ("encode64url".equalsIgnoreCase(args[0]) && args.length>1) {
216                 try {
217                     Symm.base64url.encode(args[1], System.out);
218                     System.out.println();
219                     System.out.flush();
220                     return;
221                 } catch (IOException e) {
222                     System.err.println("Cannot encode Base64url with " + args[1]);
223                     System.err.println("   \""+ e.getMessage() + '"');
224                 }
225             } else if ("decode64url".equalsIgnoreCase(args[0]) && args.length>1) {
226                 try {
227                     Symm.base64url.decode(args[1], System.out);
228                     System.out.println();
229                     System.out.flush();
230                     return;
231                 } catch (IOException e) {
232                     System.err.println("Cannot decode Base64url text from " + args[1]);
233                     System.err.println("   \""+ e.getMessage() + '"');
234                 }
235             } else if ("md5".equalsIgnoreCase(args[0]) && args.length>1) {
236                 try {
237                     System.out.println(Hash.hashMD5asStringHex(args[1]));
238                     System.out.flush();
239                 } catch (NoSuchAlgorithmException e) {
240                     System.err.println("Cannot hash MD5 from " + args[1]);
241                     System.err.println("   \""+ e.getMessage() + '"');
242                 }
243                 return;
244             } else if ("sha256".equalsIgnoreCase(args[0]) && args.length>1) {
245                 try {
246                     if (args.length>2) {
247                         int max = args.length>7?7:args.length;
248                         for (int i=2;i<max;++i) {
249                             int salt = Integer.parseInt(args[i]);
250                             System.out.println(Hash.hashSHA256asStringHex(args[1],salt));
251                         }
252                     } else { 
253                         System.out.println(Hash.hashSHA256asStringHex(args[1]));
254                     }
255                 } catch (NoSuchAlgorithmException e) {
256                     System.err.println("Cannot hash SHA256 text from " + args[1]);
257                     System.err.println("   \""+ e.getMessage() + '"');
258                 }
259                 System.out.flush();
260                 return;
261             } else if ("keygen".equalsIgnoreCase(args[0])) {
262                 try {
263                     if (args.length>1) {
264                         File f = new File(args[1]);
265                         FileOutputStream fos = new FileOutputStream(f);
266                         try {
267                             fos.write(Symm.keygen());
268                             fos.flush();
269                         } finally {
270                             fos.close();
271                             Chmod.to400.chmod(f);
272                         }
273                     } else {
274                         // create a Symmetric Key out of same characters found in base64
275                         System.out.write(Symm.keygen());
276                         System.out.flush();
277                     }
278                     return;
279                 } catch (IOException e) {
280                     System.err.println("Cannot create a key " + args[0]);
281                     System.err.println("   \""+ e.getMessage() + '"');
282                 }
283             
284             } else if ("passgen".equalsIgnoreCase(args[0])) {
285                 int numDigits;
286                 if (args.length <= 1) {
287                     numDigits = 24;
288                 } else {
289                     numDigits = Integer.parseInt(args[1]); 
290                     if (numDigits<8)numDigits = 8;
291                 }
292                 String pass;
293                 boolean noLower,noUpper,noDigits,noSpecial,repeatingChars,missingChars;
294                 do {
295                     pass = Symm.randomGen(numDigits);
296                     missingChars=noLower=noUpper=noDigits=noSpecial=true;
297                     repeatingChars=false;
298                     int c=-1,last;
299                     for (int i=0;i<numDigits;++i) {
300                         last = c;
301                         c = pass.charAt(i);
302                         if (c==last) {
303                             repeatingChars=true;
304                             break;
305                         }
306                         if (noLower) {
307                             noLower=!(c>=0x61 && c<=0x7A);
308                         } 
309                         if (noUpper) {
310                             noUpper=!(c>=0x41 && c<=0x5A);
311                         } 
312                         if (noDigits) {
313                             noDigits=!(c>=0x30 && c<=0x39);
314                         } 
315                         if (noSpecial) {
316                             noSpecial = "+!@#$%^&*(){}[]?:;,.".indexOf(c)<0;
317                         } 
318                         
319                         missingChars = (noLower || noUpper || noDigits || noSpecial);
320                     }
321                 } while (missingChars || repeatingChars);
322                 System.out.println(pass.substring(0,numDigits));
323             } else if ("urlgen".equalsIgnoreCase(args[0])) {
324                 int numDigits;
325                 if (args.length <= 1) {
326                     numDigits = 24;
327                 } else {
328                     numDigits = Integer.parseInt(args[1]); 
329                 }
330                 System.out.println(Symm.randomGen(Symm.base64url.codeset, numDigits).substring(0,numDigits));
331             }
332         } else {
333             System.out.println("Usage: java -jar <this jar> ...");
334             System.out.println("  keygen [<keyfile>]                     (Generates Key on file, or Std Out)");
335             System.out.println("  digest [<passwd>|-i|] <keyfile>        (Encrypts Password with \"keyfile\"");
336             System.out.println("                                          if passwd = -i, will read StdIn");
337             System.out.println("                                          if passwd is blank, will ask securely)");
338             System.out.println("  undigest <enc:...> <keyfile>           (Decrypts Encoded with \"keyfile\")");
339             System.out.println("  passgen <digits>                       (Generate Password of given size)");
340             System.out.println("  urlgen <digits>                        (Generate URL field of given size)");
341             System.out.println("  encode64 <your text>                   (Encodes to Base64)");
342             System.out.println("  decode64 <base64 encoded text>         (Decodes from Base64)");
343             System.out.println("  encode64url <your text>                (Encodes to Base64 URL charset)");
344             System.out.println("  decode64url <base64url encoded text>   (Decodes from Base64 URL charset)");
345             System.out.println("  sha256 <text> <salts(s)>               (Digest String into SHA256 Hash)");
346             System.out.println("  md5 <text>                             (Digest String into MD5 Hash)");
347         }
348         if (systemExit) {
349             System.exit(1);
350         }
351     }
352     
353     public static void setSystemExit(boolean shouldExit) {
354         systemExit = shouldExit;
355     }
356     
357 }