Merge "Sonar fixes related to exceptions"
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / configure / Agent.java
index a86649d..ef73ada 100644 (file)
@@ -35,20 +35,24 @@ import java.security.KeyPair;
 import java.security.KeyStore;
 import java.security.cert.X509Certificate;
 import java.util.ArrayDeque;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.Deque;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Properties;
+import java.util.TreeMap;
 
 import org.onap.aaf.cadi.CadiException;
 import org.onap.aaf.cadi.CmdLine;
 import org.onap.aaf.cadi.LocatorException;
 import org.onap.aaf.cadi.PropAccess;
 import org.onap.aaf.cadi.Symm;
+import org.onap.aaf.cadi.aaf.Defaults;
 import org.onap.aaf.cadi.aaf.client.ErrMessage;
 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
@@ -85,8 +89,8 @@ public class Agent {
        private static final String HASHES = "################################################################";
        private static final String PRINT = "print";
        private static final String FILE = "file";
-       private static final String PKCS12 = "pkcs12";
-       private static final String JKS = "jks";
+       public static final String PKCS12 = "pkcs12";
+       public static final String JKS = "jks";
        private static final String SCRIPT="script";
        
        private static final String CM_VER = "1.0";
@@ -123,7 +127,7 @@ public class Agent {
                                AAFSSO aafsso=null;
                                PropAccess access;
                                
-                               if(args.length>0 && args[0].equals("validate")) {
+                               if(args.length>1 && args[0].equals("validate") ) {
                                        int idx = args[1].indexOf('=');
                                        aafsso = null;
                                        access = new PropAccess(
@@ -325,7 +329,7 @@ public class Agent {
        private static String fqi(Deque<String> cmds) {
                if(cmds.size()<1) {
                        String alias = env.getProperty(Config.CADI_ALIAS);
-                       return alias!=null?alias:AAFSSO.cons.readLine("MechID: ");
+                       return alias!=null?alias:AAFSSO.cons.readLine("AppID: ");
                }
                return cmds.removeFirst();      
        }
@@ -350,17 +354,17 @@ public class Agent {
        }
 
        private static void createArtifact(Trans trans, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
-               String mechID = fqi(cmds);
-               String machine = machine(cmds);
+               final String mechID = fqi(cmds);
+               final String machine = machine(cmds);
 
                Artifacts artifacts = new Artifacts();
                Artifact arti = new Artifact();
                artifacts.getArtifact().add(arti);
-               arti.setMechid(mechID!=null?mechID:AAFSSO.cons.readLine("MechID: "));
+               arti.setMechid(mechID!=null?mechID:AAFSSO.cons.readLine("AppID: "));
                arti.setMachine(machine!=null?machine:AAFSSO.cons.readLine("Machine (%s): ",InetAddress.getLocalHost().getHostName()));
                arti.setCa(AAFSSO.cons.readLine("CA: (%s): ","aaf"));
                
-               String resp = AAFSSO.cons.readLine("Types [file,jks,script] (%s): ", "jks");
+               String resp = AAFSSO.cons.readLine("Types [file,pkcs12,jks,script] (%s): ", PKCS12);
                for(String s : Split.splitTrim(',', resp)) {
                        arti.getType().add(s);
                }
@@ -415,7 +419,7 @@ public class Agent {
                        if(future.get(TIMEOUT)) {
                                boolean printed = false;
                                for(Artifact a : future.value.getArtifact()) {
-                                       AAFSSO.cons.printf("MechID:          %s\n",a.getMechid()); 
+                                       AAFSSO.cons.printf("AppID:          %s\n",a.getMechid()); 
                                        AAFSSO.cons.printf("  Sponsor:       %s\n",a.getSponsor()); 
                                        AAFSSO.cons.printf("Machine:         %s\n",a.getMachine()); 
                                        AAFSSO.cons.printf("CA:              %s\n",a.getCa()); 
@@ -646,7 +650,7 @@ public class Agent {
                                // Have to wait for JDK 1.7 source...
                                //switch(artifact.getType()) {
                                if(acf.value.getArtifact()==null || acf.value.getArtifact().isEmpty()) {
-                                       AAFSSO.cons.printf("No Artifacts found for %s on %s", mechID, machine);
+                                       AAFSSO.cons.printf("No Artifacts found for %s on %s ", mechID, machine);
                                } else {
                                        String id = aafcon.defID();
                                        boolean allowed;
@@ -656,7 +660,7 @@ public class Agent {
                                                                                && aafcon.securityInfo().defSS.getClass().isAssignableFrom(HBasicAuthSS.class)));
                                                if(!allowed) {
                                                        Future<String> pf = aafcon.client(CM_VER).read("/cert/may/" + 
-                                                                       a.getNs() + ".certman|"+a.getCa()+"|showpass","*/*");
+                                                                       a.getNs()+"|certman|"+a.getCa()+"|showpass","*/*");
                                                        if(pf.get(TIMEOUT)) {
                                                                allowed = true;
                                                        } else {
@@ -754,7 +758,7 @@ public class Agent {
                                        psProps.print("# Configuration File generated on ");
                                        psProps.println(new Date().toString());
                                        psProps.println(HASHES);
-                                       for(String tag : new String[] {Config.CADI_LATITUDE,Config.CADI_LONGITUDE}) {
+                                       for(String tag : LOC_TAGS) {
                                                psProps.print(tag);
                                                psProps.print('=');
                                                psProps.println(getProperty(pa, trans, false, tag, "%s: ",tag));
@@ -788,19 +792,15 @@ public class Agent {
                                        if(!fkf.exists()) {
                                                CmdLine.main(new String[] {"keygen",fkf.toString()});
                                        }
-                                       psCredProps.print("cadi_keyfile=");
-                                       psCredProps.println(fkf.getCanonicalPath());
-                                       
-                                       psCredProps.print(Config.AAF_APPID);
-                                       psCredProps.print('=');
-                                       psCredProps.println(fqi);
-                                       
                                        Symm filesymm = Symm.obtain(fkf);
-                                       psCredProps.print(Config.AAF_APPPASS);
-                                       psCredProps.print("=enc:");
-                                       String ps = pa.decrypt(pa.getProperty(Config.AAF_APPPASS), false);
-                                       ps = filesymm.enpass(ps);
-                                       psCredProps.println(ps);
+                                       Map<String,String> normal = new TreeMap<>();
+                                       Map<String,String> creds = new TreeMap<>();
+
+                                       directedPut(pa, filesymm, normal,creds, Config.CADI_KEYFILE, fkf.getCanonicalPath());
+                                       directedPut(pa, filesymm, normal,creds, Config.AAF_APPID,fqi);
+                                       directedPut(pa, filesymm, normal,creds, Config.AAF_APPPASS,null);
+                                       directedPut(pa, filesymm, normal,creds, Config.AAF_URL, Defaults.AAF_URL);
+                                       
 
                                        String cts = pa.getProperty(Config.CADI_TRUSTSTORE);
                                        if(cts!=null) {
@@ -820,7 +820,7 @@ public class Agent {
                                                                        }
                                                                }
                                                                if(!origTruststore.exists()) {
-                                                                       throw new CadiException(cts + "does not exist");
+                                                                       throw new CadiException(cts + " does not exist");
                                                                }
                                                        }
                                                        
@@ -829,97 +829,73 @@ public class Agent {
                                                if(!newTruststore.exists()) {
                                                        Files.copy(origTruststore.toPath(), newTruststore.toPath());
                                                }
-                                               psCredProps.print(Config.CADI_TRUSTSTORE);
-                                               psCredProps.print("=");
-                                               psCredProps.println(newTruststore.getCanonicalPath());
-               
-                                               psCredProps.print(Config.CADI_TRUSTSTORE_PASSWORD);
-                                               psCredProps.print("=enc:");
-                                               ps = pa.decrypt(pa.getProperty(Config.CADI_TRUSTSTORE_PASSWORD), false);
-                                               ps = filesymm.enpass(ps);
-                                               psCredProps.println(ps);
+                                               
+                                               directedPut(pa, filesymm, normal,creds, Config.CADI_TRUSTSTORE,newTruststore.getCanonicalPath());
+                                               directedPut(pa, filesymm, normal,creds, Config.CADI_TRUSTSTORE_PASSWORD,null);
                                        }
                                        
-//                                     String cadi_x509_issuers = pa.getProperty(Config.CADI_X509_ISSUERS);
-//                                     if(cadi_x509_issuers!=null) {
-//                                             psCredProps.print(Config.CADI_X509_ISSUERS);
-//                                             psCredProps.print('=');
-//                                             psCredProps.println(cadi_x509_issuers);
-//                                     }
-
-                                       
-                                       try {
-                                               if(aafcon!=null) { // get Properties from Remote AAF
-                                                       final String locator = getProperty(pa,aafcon.env,false,Config.AAF_LOCATE_URL,"AAF Locator URL: ");
+                                       if(aafcon!=null) { // get Properties from Remote AAF
+                                               final String locator = getProperty(pa,aafcon.env,false,Config.AAF_LOCATE_URL,"AAF Locator URL: ");
 
-                                                       Future<Configuration> acf = aafcon.client(new SingleEndpointLocator(locator))
-                                                                       .read("/configure/"+fqi+"/aaf", configDF);
-                                                       if(acf.get(TIMEOUT)) {
-                                                               PrintStream pstemp;
-                                                               for(Props props : acf.value.getProps()) {
-                                                                       if(Config.CADI_X509_ISSUERS.equals(props.getTag())) {
-                                                                               pstemp=psCredProps;
-                                                                       } else {
-                                                                               pstemp = psProps;
-                                                                       }
-                                                                       pstemp.print(props.getTag());
-                                                                       pstemp.print('=');
-                                                                       pstemp.println(props.getValue());                                       
-                                                               }
-                                                               ok = true;
-                                                       } else if(acf.code()==401){
-                                                               trans.error().log("Bad Password sent to AAF");
-                                                       } else {
-                                                               trans.error().log(errMsg.toMsg(acf));
+                                               Future<Configuration> acf = aafcon.client(new SingleEndpointLocator(locator))
+                                                               .read("/configure/"+fqi+"/aaf", configDF);
+                                               if(acf.get(TIMEOUT)) {
+                                                       for(Props props : acf.value.getProps()) {
+                                                               directedPut(pa, filesymm, normal,creds, props.getTag(),props.getValue());                                       
                                                        }
+                                                       ok = true;
+                                               } else if(acf.code()==401){
+                                                       trans.error().log("Bad Password sent to AAF");
                                                } else {
-                                                       String cpf = pa.getProperty(Config.CADI_PROP_FILES);
-                                                       if(cpf!=null){
-                                                               for(String f : Split.split(File.pathSeparatorChar, cpf)) {
-                                                                       System.out.format("Reading %s\n",f);
-                                                                       FileInputStream fis = new FileInputStream(f); 
-                                                                       try {
-                                                                               Properties props = new Properties();
-                                                                               props.load(fis);
-                                                                               PrintStream pstemp;
-                                                                               String key,value;
-                                                                               for(Entry<Object, Object> prop : props.entrySet()) {
-                                                                                       key = prop.getKey().toString();
-                                                                                       if(Config.CADI_X509_ISSUERS.equals(key)) {
-                                                                                               pstemp=psCredProps;
-                                                                                               value = prop.getValue().toString();
-                                                                                       } else if(key.endsWith("_password")){
-                                                                                               if(Config.AAF_APPPASS.equals(key) || Config.CADI_TRUSTSTORE_PASSWORD.equals(key)) {
-                                                                                                       continue;
-                                                                                               }
-                                                                                               value = "enc:" + filesymm.enpass(prop.getValue().toString());
-                                                                                               pstemp = psCredProps;
-                                                                                       } else if(Config.CADI_TRUSTSTORE.equals(key)) {
-                                                                                               continue;
-                                                                                       } else {
-                                                                                               value = prop.getValue().toString();
-                                                                                               pstemp = psProps;
-                                                                                       }
-                                                                                       pstemp.print(key);
-                                                                                       pstemp.print('=');
-                                                                                       pstemp.println(value);
-                                                                               }
-                                                                       } finally {
-                                                                               fis.close();
+                                                       trans.error().log(errMsg.toMsg(acf));
+                                               }
+                                       } else {
+                                               String cpf = pa.getProperty(Config.CADI_PROP_FILES);
+                                               if(cpf!=null){
+                                                       for(String f : Split.split(File.pathSeparatorChar, cpf)) {
+                                                               System.out.format("Reading %s\n",f);
+                                                               FileInputStream fis = new FileInputStream(f); 
+                                                               try {
+                                                                       Properties props = new Properties();
+                                                                       props.load(fis);
+                                                                       for(Entry<Object, Object> prop : props.entrySet()) {
+                                                                               directedPut(pa, filesymm, normal,creds, prop.getKey().toString(),prop.getValue().toString());
                                                                        }
+                                                               } finally {
+                                                                       fis.close();
                                                                }
                                                        }
-                                                       ok = true;
                                                }
-                                       } finally {
-                                               psProps.close();
+                                               ok = true;
                                        }
                                        if(ok) {
+                                               for(Entry<String, String> es : normal.entrySet()) {
+                                                       psProps.print(es.getKey());
+                                                       psProps.print('=');
+                                                       psProps.println(es.getValue());
+                                               }
+                                               
+                                               for(Entry<String, String> es : creds.entrySet()) {
+                                                       psCredProps.print(es.getKey());
+                                                       psCredProps.print('=');
+                                                       psCredProps.println(es.getValue());
+                                               }
+                                               
                                                File newFile = new File(dir,rootFile+".props");
+                                               if(newFile.exists()) {
+                                                       File backup = new File(dir,rootFile+".props.backup");
+                                                       newFile.renameTo(backup);
+                                                       System.out.println("Backed up to " + backup.getCanonicalPath());
+                                               }
                                                fProps.renameTo(newFile);
                                                System.out.println("Created " + newFile.getCanonicalPath());
                                                fProps = newFile;
                                                
+                                               if(fSecureProps.exists()) {
+                                                       File backup = new File(dir,fSecureProps.getName()+".backup");
+                                                       fSecureProps.renameTo(backup);
+                                                       System.out.println("Backed up to " + backup.getCanonicalPath());
+                                               }
                                                fSecureTempProps.renameTo(fSecureProps);
                                                System.out.println("Created " + fSecureProps.getCanonicalPath());
                                                fProps = newFile;
@@ -937,7 +913,36 @@ public class Agent {
                        tt.done();
                }
        }
+
+       private static List<String> CRED_TAGS = Arrays.asList(new String[] {
+                       Config.CADI_KEYFILE,
+                       Config.AAF_APPID, Config.AAF_APPPASS,
+                       Config.CADI_KEYSTORE, Config.CADI_KEYSTORE_PASSWORD, Config.CADI_KEY_PASSWORD,
+                       Config.CADI_TRUSTSTORE,Config.CADI_TRUSTSTORE_PASSWORD,
+                       Config.CADI_ALIAS, Config.CADI_X509_ISSUERS
+                       });
+
+       private static List<String> LOC_TAGS = Arrays.asList(new String[] {Config.CADI_LATITUDE, Config.CADI_LONGITUDE});
        
+       private static void directedPut(final PropAccess orig, final Symm symm, final Map<String,String> main, final Map<String,String> secured, final String tag, final String value) throws IOException {
+               if(!LOC_TAGS.contains(tag)) { // Location already covered
+                       String val = value==null?orig.getProperty(tag):value;
+                       if(tag.endsWith("_password")) {
+                               if(val.length()>4) {
+                                       if(val.startsWith("enc:")) {
+                                               val = orig.decrypt(val, true);
+                                       }
+                                       val = "enc:" + symm.enpass(val);
+                               }
+                       }
+                       if(CRED_TAGS.contains(tag)) {
+                               secured.put(tag, val);
+                       } else {
+                               main.put(tag, val);
+                       }
+               }
+       }
+
        private static void validate(final PropAccess pa) throws LocatorException, CadiException, APIException {
                System.out.println("Validating Configuration...");
                final AAFCon<?> aafcon = new AAFConHttp(pa,Config.AAF_URL,new SecurityInfoC<HttpURLConnection>(pa));
@@ -1012,13 +1017,13 @@ public class Agent {
                                                        String prop;                                            
                                                        File f;
        
-                                                       if((prop=props.getProperty(Config.CADI_KEYFILE))==null ||
+                                                       if((prop=trans.getProperty(Config.CADI_KEYFILE))==null ||
                                                                !(f=new File(prop)).exists()) {
                                                                        trans.error().printf("Keyfile must exist to check Certificates for %s on %s",
                                                                                a.getMechid(), a.getMachine());
                                                        } else {
-                                                               String ksf = props.getProperty(Config.CADI_KEYSTORE);
-                                                               String ksps = props.getProperty(Config.CADI_KEYSTORE_PASSWORD);
+                                                               String ksf = trans.getProperty(Config.CADI_KEYSTORE);
+                                                               String ksps = trans.getProperty(Config.CADI_KEYSTORE_PASSWORD);
                                                                if(ksf==null || ksps == null) {
                                                                        trans.error().printf("Properties %s and %s must exist to check Certificates for %s on %s",
                                                                                        Config.CADI_KEYSTORE, Config.CADI_KEYSTORE_PASSWORD,a.getMechid(), a.getMachine());