2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.cm;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import java.security.KeyStore;
30 import java.security.cert.X509Certificate;
31 import java.util.ArrayDeque;
32 import java.util.Deque;
33 import java.util.GregorianCalendar;
34 import java.util.HashMap;
35 import java.util.Iterator;
37 import java.util.Map.Entry;
39 import org.onap.aaf.cadi.PropAccess;
40 import org.onap.aaf.cadi.Symm;
41 import org.onap.aaf.cadi.aaf.client.ErrMessage;
42 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
43 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
44 import org.onap.aaf.cadi.client.Future;
45 import org.onap.aaf.cadi.config.Config;
46 import org.onap.aaf.cadi.http.HBasicAuthSS;
47 import org.onap.aaf.cadi.sso.AAFSSO;
48 import org.onap.aaf.cadi.util.FQI;
49 import org.onap.aaf.misc.env.Env;
50 import org.onap.aaf.misc.env.TimeTaken;
51 import org.onap.aaf.misc.env.Trans;
52 import org.onap.aaf.misc.env.Data.TYPE;
53 import org.onap.aaf.misc.env.util.Chrono;
54 import org.onap.aaf.misc.env.util.Split;
55 import org.onap.aaf.misc.rosetta.env.RosettaDF;
56 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
58 import java.util.Properties;
60 import certman.v1_0.Artifacts;
61 import certman.v1_0.Artifacts.Artifact;
62 import certman.v1_0.CertInfo;
63 import certman.v1_0.CertificateRequest;
65 public class CmAgent {
66 private static final String PRINT = "print";
67 private static final String FILE = "file";
68 private static final String PKCS12 = "pkcs12";
69 private static final String JKS = "jks";
70 private static final String SCRIPT="script";
72 private static final String CM_VER = "1.0";
73 public static final int PASS_SIZE = 24;
74 private static int TIMEOUT;
76 private static RosettaDF<CertificateRequest> reqDF;
77 private static RosettaDF<CertInfo> certDF;
78 private static RosettaDF<Artifacts> artifactsDF;
79 private static ErrMessage errMsg;
80 private static Map<String,PlaceArtifact> placeArtifact;
81 private static RosettaEnv env;
83 public static void main(String[] args) {
86 AAFSSO aafsso = new AAFSSO(args);
87 if(aafsso.loginOnly()) {
88 aafsso.setLogDefault();
90 System.out.println("AAF SSO information created in ~/.aaf");
92 PropAccess access = aafsso.access();
93 env = new RosettaEnv(access.getProperties());
94 Deque<String> cmds = new ArrayDeque<String>();
95 for(String p : args) {
96 if(p.indexOf('=')<0) {
102 aafsso.setLogDefault();
103 System.out.println("Usage: java -jar <cadi-aaf-*-full.jar> cmd [<tag=value>]*");
104 System.out.println(" create <mechID> [<machine>]");
105 System.out.println(" read <mechID> [<machine>]");
106 System.out.println(" update <mechID> [<machine>]");
107 System.out.println(" delete <mechID> [<machine>]");
108 System.out.println(" copy <mechID> <machine> <newmachine>[,<newmachine>]*");
109 System.out.println(" place <mechID> [<machine>]");
110 System.out.println(" showpass <mechID> [<machine>]");
111 System.out.println(" check <mechID> [<machine>]");
112 System.out.println(" genkeypair");
116 TIMEOUT = Integer.parseInt(env.getProperty(Config.AAF_CONN_TIMEOUT, "5000"));
118 reqDF = env.newDataFactory(CertificateRequest.class);
119 artifactsDF = env.newDataFactory(Artifacts.class);
120 certDF = env.newDataFactory(CertInfo.class);
121 errMsg = new ErrMessage(env);
123 placeArtifact = new HashMap<String,PlaceArtifact>();
124 placeArtifact.put(JKS, new PlaceArtifactInKeystore(JKS));
125 placeArtifact.put(PKCS12, new PlaceArtifactInKeystore(PKCS12));
126 placeArtifact.put(FILE, new PlaceArtifactInFiles());
127 placeArtifact.put(PRINT, new PlaceArtifactOnStream(System.out));
128 placeArtifact.put(SCRIPT, new PlaceArtifactScripts());
130 Trans trans = env.newTrans();
132 if((token=access.getProperty("oauth_token"))!=null) {
133 trans.setProperty("oauth_token", token);
136 // show Std out again
137 aafsso.setLogDefault();
138 aafsso.setStdErrDefault();
140 // if CM_URL can be obtained, add to sso.props, if written
141 String cm_url = getProperty(access,env,false, Config.CM_URL,Config.CM_URL+": ");
143 aafsso.addProp(Config.CM_URL, cm_url);
147 AAFCon<?> aafcon = new AAFConHttp(access,Config.CM_URL);
149 String cmd = cmds.removeFirst();
150 if("place".equals(cmd)) {
151 placeCerts(trans,aafcon,cmds);
152 } else if("create".equals(cmd)) {
153 createArtifact(trans, aafcon,cmds);
154 } else if("read".equals(cmd)) {
155 readArtifact(trans, aafcon, cmds);
156 } else if("copy".equals(cmd)) {
157 copyArtifact(trans, aafcon, cmds);
158 } else if("update".equals(cmd)) {
159 updateArtifact(trans, aafcon, cmds);
160 } else if("delete".equals(cmd)) {
161 deleteArtifact(trans, aafcon, cmds);
162 } else if("showpass".equals(cmd)) {
163 showPass(trans,aafcon,cmds);
164 } else if("check".equals(cmd)) {
166 exitCode = check(trans,aafcon,cmds);
167 } catch (Exception e) {
172 AAFSSO.cons.printf("Unknown command \"%s\"\n", cmd);
175 StringBuilder sb = new StringBuilder();
176 trans.auditTrail(4, sb, Trans.REMOTE);
178 trans.info().log("Trans Info\n",sb);
183 } catch (Exception e) {
187 System.exit(exitCode);
191 private static String getProperty(PropAccess pa, Env env, boolean secure, String tag, String prompt, Object ... def) {
193 if((value=pa.getProperty(tag))==null) {
195 value = new String(AAFSSO.cons.readPassword(prompt, def));
197 value = AAFSSO.cons.readLine(prompt,def).trim();
200 if(value.length()>0) {
201 pa.setProperty(tag,value);
202 env.setProperty(tag,value);
203 } else if(def.length==1) {
204 value=def[0].toString();
205 pa.setProperty(tag,value);
206 env.setProperty(tag,value);
213 private static String mechID(Deque<String> cmds) {
215 String alias = env.getProperty(Config.CADI_ALIAS);
216 return alias!=null?alias:AAFSSO.cons.readLine("MechID: ");
218 return cmds.removeFirst();
221 private static String machine(Deque<String> cmds) throws UnknownHostException {
223 return cmds.removeFirst();
225 String mach = env.getProperty(Config.HOSTNAME);
226 return mach!=null?mach:InetAddress.getLocalHost().getHostName();
230 private static String[] machines(Deque<String> cmds) {
233 machines = cmds.removeFirst();
235 machines = AAFSSO.cons.readLine("Machines (sep by ','): ");
237 return Split.split(',', machines);
240 private static void createArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
241 String mechID = mechID(cmds);
242 String machine = machine(cmds);
244 Artifacts artifacts = new Artifacts();
245 Artifact arti = new Artifact();
246 artifacts.getArtifact().add(arti);
247 arti.setMechid(mechID!=null?mechID:AAFSSO.cons.readLine("MechID: "));
248 arti.setMachine(machine!=null?machine:AAFSSO.cons.readLine("Machine (%s): ",InetAddress.getLocalHost().getHostName()));
249 arti.setCa(AAFSSO.cons.readLine("CA: (%s): ","aaf"));
251 String resp = AAFSSO.cons.readLine("Types [file,jks,script] (%s): ", "jks");
252 for(String s : Split.splitTrim(',', resp)) {
253 arti.getType().add(s);
256 if(!resp.contains(SCRIPT)) {
257 arti.getType().add(SCRIPT);
260 // Note: Sponsor is set on Creation by CM
261 String configRootName = FQI.reverseDomain(arti.getMechid());
262 arti.setNs(AAFSSO.cons.readLine("Namespace (%s): ",configRootName));
263 arti.setDir(AAFSSO.cons.readLine("Directory (%s): ", System.getProperty("user.dir")));
264 arti.setOsUser(AAFSSO.cons.readLine("OS User (%s): ", System.getProperty("user.name")));
265 arti.setRenewDays(Integer.parseInt(AAFSSO.cons.readLine("Renewal Days (%s):", "30")));
266 arti.setNotification(toNotification(AAFSSO.cons.readLine("Notification (mailto owner):", "")));
268 TimeTaken tt = trans.start("Create Artifact", Env.REMOTE);
270 Future<Artifacts> future = aafcon.client(CM_VER).create("/cert/artifacts", artifactsDF, artifacts);
271 if(future.get(TIMEOUT)) {
272 trans.info().printf("Call to AAF Certman successful %s, %s",arti.getMechid(), arti.getMachine());
274 trans.error().printf("Call to AAF Certman failed, %s",
275 errMsg.toMsg(future));
282 private static String toNotification(String notification) {
283 if(notification==null) {
285 } else if(notification.length()>0) {
286 if(notification.indexOf(':')<0) {
287 notification = "mailto:" + notification;
294 private static void readArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
295 String mechID = mechID(cmds);
296 String machine = machine(cmds);
298 TimeTaken tt = trans.start("Read Artifact", Env.SUB);
300 Future<Artifacts> future = aafcon.client(CM_VER)
301 .read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF,"Authorization","Bearer " + trans.getProperty("oauth_token"));
303 if(future.get(TIMEOUT)) {
304 boolean printed = false;
305 for(Artifact a : future.value.getArtifact()) {
306 AAFSSO.cons.printf("MechID: %s\n",a.getMechid());
307 AAFSSO.cons.printf(" Sponsor: %s\n",a.getSponsor());
308 AAFSSO.cons.printf("Machine: %s\n",a.getMachine());
309 AAFSSO.cons.printf("CA: %s\n",a.getCa());
310 StringBuilder sb = new StringBuilder();
311 boolean first = true;
312 for(String t : a.getType()) {
313 if(first) {first=false;}
314 else{sb.append(',');}
317 AAFSSO.cons.printf("Types: %s\n",sb);
318 AAFSSO.cons.printf("Namespace: %s\n",a.getNs());
319 AAFSSO.cons.printf("Directory: %s\n",a.getDir());
320 AAFSSO.cons.printf("O/S User: %s\n",a.getOsUser());
321 AAFSSO.cons.printf("Renew Days: %d\n",a.getRenewDays());
322 AAFSSO.cons.printf("Notification %s\n",a.getNotification());
326 AAFSSO.cons.printf("Artifact for %s %s does not exist\n", mechID, machine);
329 trans.error().log(errMsg.toMsg(future));
336 private static void copyArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
337 String mechID = mechID(cmds);
338 String machine = machine(cmds);
339 String[] newmachs = machines(cmds);
340 if(machine==null || newmachs == null) {
341 trans.error().log("No machines listed to copy to");
343 TimeTaken tt = trans.start("Copy Artifact", Env.REMOTE);
345 Future<Artifacts> future = aafcon.client(CM_VER)
346 .read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
348 if(future.get(TIMEOUT)) {
349 boolean printed = false;
350 for(Artifact a : future.value.getArtifact()) {
351 for(String m : newmachs) {
353 Future<Artifacts> fup = aafcon.client(CM_VER).update("/cert/artifacts", artifactsDF, future.value);
354 if(fup.get(TIMEOUT)) {
355 trans.info().printf("Copy of %s %s successful to %s",mechID,machine,m);
357 trans.error().printf("Call to AAF Certman failed, %s",
365 AAFSSO.cons.printf("Artifact for %s %s does not exist", mechID, machine);
368 trans.error().log(errMsg.toMsg(future));
376 private static void updateArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
377 String mechID = mechID(cmds);
378 String machine = machine(cmds);
380 TimeTaken tt = trans.start("Update Artifact", Env.REMOTE);
382 Future<Artifacts> fread = aafcon.client(CM_VER)
383 .read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
385 if(fread.get(TIMEOUT)) {
386 Artifacts artifacts = new Artifacts();
387 for(Artifact a : fread.value.getArtifact()) {
388 Artifact arti = new Artifact();
389 artifacts.getArtifact().add(arti);
391 AAFSSO.cons.printf("For %s on %s\n", a.getMechid(),a.getMachine());
392 arti.setMechid(a.getMechid());
393 arti.setMachine(a.getMachine());
394 arti.setCa(AAFSSO.cons.readLine("CA: (%s): ",a.getCa()));
395 StringBuilder sb = new StringBuilder();
396 boolean first = true;
397 for(String t : a.getType()) {
398 if(first) {first=false;}
399 else{sb.append(',');}
403 String resp = AAFSSO.cons.readLine("Types [file,jks,pkcs12] (%s): ", sb);
404 for(String s : Split.splitTrim(',', resp)) {
405 arti.getType().add(s);
408 if(!resp.contains(SCRIPT)) {
409 arti.getType().add(SCRIPT);
412 // Note: Sponsor is set on Creation by CM
413 arti.setNs(AAFSSO.cons.readLine("Namespace (%s): ",a.getNs()));
414 arti.setDir(AAFSSO.cons.readLine("Directory (%s): ", a.getDir()));
415 arti.setOsUser(AAFSSO.cons.readLine("OS User (%s): ", a.getOsUser()));
416 arti.setRenewDays(Integer.parseInt(AAFSSO.cons.readLine("Renew Days (%s):", a.getRenewDays())));
417 arti.setNotification(toNotification(AAFSSO.cons.readLine("Notification (%s):", a.getNotification())));
420 if(artifacts.getArtifact().size()==0) {
421 AAFSSO.cons.printf("Artifact for %s %s does not exist", mechID, machine);
423 Future<Artifacts> fup = aafcon.client(CM_VER).update("/cert/artifacts", artifactsDF, artifacts);
424 if(fup.get(TIMEOUT)) {
425 trans.info().printf("Call to AAF Certman successful %s, %s",mechID,machine);
427 trans.error().printf("Call to AAF Certman failed, %s",
432 trans.error().printf("Call to AAF Certman failed, %s %s, %s",
433 errMsg.toMsg(fread),mechID,machine);
440 private static void deleteArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
441 String mechid = mechID(cmds);
442 String machine = machine(cmds);
444 TimeTaken tt = trans.start("Delete Artifact", Env.REMOTE);
446 Future<Void> future = aafcon.client(CM_VER)
447 .delete("/cert/artifacts/"+mechid+"/"+machine,"application/json" );
449 if(future.get(TIMEOUT)) {
450 trans.info().printf("Call to AAF Certman successful %s, %s",mechid,machine);
452 trans.error().printf("Call to AAF Certman failed, %s %s, %s",
453 errMsg.toMsg(future),mechid,machine);
462 private static boolean placeCerts(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
464 String mechID = mechID(cmds);
465 String machine = machine(cmds);
466 String[] fqdns = Split.split(':', machine);
475 TimeTaken tt = trans.start("Place Artifact", Env.REMOTE);
477 Future<Artifacts> acf = aafcon.client(CM_VER)
478 .read("/cert/artifacts/"+mechID+'/'+key, artifactsDF);
479 if(acf.get(TIMEOUT)) {
480 if(acf.value.getArtifact()==null || acf.value.getArtifact().isEmpty()) {
481 AAFSSO.cons.printf("===> There are no artifacts for %s on machine '%s'\n", mechID, key);
483 for(Artifact a : acf.value.getArtifact()) {
484 String osID = System.getProperty("user.name");
485 if(a.getOsUser().equals(osID)) {
486 CertificateRequest cr = new CertificateRequest();
487 cr.setMechid(a.getMechid());
488 cr.setSponsor(a.getSponsor());
489 for(int i=0;i<fqdns.length;++i) {
490 cr.getFqdns().add(fqdns[i]);
492 Future<String> f = aafcon.client(CM_VER)
493 .setQueryParams("withTrust")
494 .updateRespondString("/cert/" + a.getCa(),reqDF, cr);
496 CertInfo capi = certDF.newData().in(TYPE.JSON).load(f.body()).asObject();
497 for(String type : a.getType()) {
498 PlaceArtifact pa = placeArtifact.get(type);
500 if(rv = pa.place(trans, capi, a,machine)) {
505 // Cover for the above multiple pass possibilities with some static Data, then clear per Artifact
507 trans.error().log(errMsg.toMsg(f));
510 trans.error().log("You must be OS User \"" + a.getOsUser() +"\" to place Certificates on this box");
515 trans.error().log(errMsg.toMsg(acf));
523 private static void notifyPlaced(Artifact a, boolean rv) {
526 private static void showPass(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
527 String mechID = mechID(cmds);
528 String machine = machine(cmds);
530 TimeTaken tt = trans.start("Show Password", Env.REMOTE);
532 Future<Artifacts> acf = aafcon.client(CM_VER)
533 .read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
534 if(acf.get(TIMEOUT)) {
535 // Have to wait for JDK 1.7 source...
536 //switch(artifact.getType()) {
537 if(acf.value.getArtifact()==null || acf.value.getArtifact().isEmpty()) {
538 AAFSSO.cons.printf("No Artifacts found for %s on %s", mechID, machine);
540 String id = aafcon.defID();
542 for(Artifact a : acf.value.getArtifact()) {
543 allowed = id!=null && (id.equals(a.getSponsor()) ||
544 (id.equals(a.getMechid())
545 && aafcon.securityInfo().defSS.getClass().isAssignableFrom(HBasicAuthSS.class)));
547 Future<String> pf = aafcon.client(CM_VER).read("/cert/may/" +
548 a.getNs() + ".certman|"+a.getCa()+"|showpass","*/*");
549 if(pf.get(TIMEOUT)) {
552 trans.error().log(errMsg.toMsg(pf));
556 File dir = new File(a.getDir());
557 Properties props = new Properties();
558 FileInputStream fis = new FileInputStream(new File(dir,a.getNs()+".props"));
562 fis = new FileInputStream(new File(dir,a.getNs()+".chal"));
568 File f = new File(dir,a.getNs()+".keyfile");
570 Symm symm = Symm.obtain(f);
572 for(Iterator<Entry<Object,Object>> iter = props.entrySet().iterator(); iter.hasNext();) {
573 Entry<Object,Object> en = iter.next();
574 if(en.getValue().toString().startsWith("enc:")) {
575 System.out.printf("%s=%s\n", en.getKey(), symm.depass(en.getValue().toString()));
579 trans.error().printf("%s.keyfile must exist to read passwords for %s on %s",
580 f.getAbsolutePath(),a.getMechid(), a.getMachine());
586 trans.error().log(errMsg.toMsg(acf));
596 * Check returns Error Codes, so that Scripts can know what to do
598 * 0 - Check Complete, nothing to do
600 * 2 - Error for specific Artifact - read check.msg
601 * 10 - Certificate Updated - check.msg is email content
609 private static int check(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
611 String mechID = mechID(cmds);
612 String machine = machine(cmds);
614 TimeTaken tt = trans.start("Check Certificate", Env.REMOTE);
617 Future<Artifacts> acf = aafcon.client(CM_VER)
618 .read("/cert/artifacts/"+mechID+'/'+machine, artifactsDF);
619 if(acf.get(TIMEOUT)) {
620 // Have to wait for JDK 1.7 source...
621 //switch(artifact.getType()) {
622 if(acf.value.getArtifact()==null || acf.value.getArtifact().isEmpty()) {
623 AAFSSO.cons.printf("No Artifacts found for %s on %s", mechID, machine);
625 String id = aafcon.defID();
626 GregorianCalendar now = new GregorianCalendar();
627 for(Artifact a : acf.value.getArtifact()) {
628 if(id.equals(a.getMechid())) {
629 File dir = new File(a.getDir());
630 Properties props = new Properties();
631 FileInputStream fis = new FileInputStream(new File(dir,a.getNs()+".props"));
641 if((prop=props.getProperty(Config.CADI_KEYFILE))==null ||
642 !(f=new File(prop)).exists()) {
643 trans.error().printf("Keyfile must exist to check Certificates for %s on %s",
644 a.getMechid(), a.getMachine());
646 String ksf = props.getProperty(Config.CADI_KEYSTORE);
647 String ksps = props.getProperty(Config.CADI_KEYSTORE_PASSWORD);
648 if(ksf==null || ksps == null) {
649 trans.error().printf("Properties %s and %s must exist to check Certificates for %s on %s",
650 Config.CADI_KEYSTORE, Config.CADI_KEYSTORE_PASSWORD,a.getMechid(), a.getMachine());
652 KeyStore ks = KeyStore.getInstance("JKS");
653 Symm symm = Symm.obtain(f);
655 fis = new FileInputStream(ksf);
657 ks.load(fis,symm.depass(ksps).toCharArray());
661 X509Certificate cert = (X509Certificate)ks.getCertificate(mechID);
665 msg = String.format("X509Certificate does not exist for %s on %s in %s",
666 a.getMechid(), a.getMachine(), ksf);
667 trans.error().log(msg);
670 GregorianCalendar renew = new GregorianCalendar();
671 renew.setTime(cert.getNotAfter());
672 renew.add(GregorianCalendar.DAY_OF_MONTH,-1*a.getRenewDays());
673 if(renew.after(now)) {
674 msg = String.format("X509Certificate for %s on %s has been checked on %s. It expires on %s; it will not be renewed until %s.\n",
675 a.getMechid(), a.getMachine(),Chrono.dateOnlyStamp(now),cert.getNotAfter(),Chrono.dateOnlyStamp(renew));
676 trans.info().log(msg);
679 trans.info().printf("X509Certificate for %s on %s expiration, %s, needs Renewal.\n",
680 a.getMechid(), a.getMachine(),cert.getNotAfter());
681 cmds.offerLast(mechID);
682 cmds.offerLast(machine);
683 if(placeCerts(trans,aafcon,cmds)) {
684 msg = String.format("X509Certificate for %s on %s has been renewed. Ensure services using are refreshed.\n",
685 a.getMechid(), a.getMachine());
686 exitCode = 10; // Refreshed
688 msg = String.format("X509Certificate for %s on %s attempted renewal, but failed. Immediate Investigation is required!\n",
689 a.getMechid(), a.getMachine());
690 exitCode = 1; // Error Renewing
695 FileOutputStream fos = new FileOutputStream(a.getDir()+'/'+a.getNs()+".msg");
697 fos.write(msg.getBytes());
709 trans.error().log(errMsg.toMsg(acf));