import java.util.Map.Entry;
import java.util.Properties;
+import org.onap.aaf.cadi.Access;
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;
import org.onap.aaf.cadi.client.Rcli;
import org.onap.aaf.cadi.client.Retryable;
import org.onap.aaf.cadi.config.Config;
+import org.onap.aaf.cadi.config.RegistrationPropHolder;
import org.onap.aaf.cadi.config.SecurityInfoC;
import org.onap.aaf.cadi.http.HBasicAuthSS;
import org.onap.aaf.cadi.locator.SingleEndpointLocator;
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
+ });
+ // Note: This is set by loadURLs. Use that function as singleton, not directly.
+ private static Map<String,String> aaf_urls = null;
public static void main(String[] args) {
env = new RosettaEnv(access.getProperties());
Deque<String> cmds = new ArrayDeque<String>();
for (String p : args) {
+ int eq;
if ("-noexit".equalsIgnoreCase(p)) {
doExit = false;
- } else if (p.indexOf('=') < 0) {
+ } else if ((eq=p.indexOf('=')) < 0) {
cmds.add(p);
+ } else {
+ access.setProperty(p.substring(0,eq), p.substring(eq+1));
}
}
aafsso.setLogDefault();
aafsso.setStdErrDefault();
- // if CM_URL can be obtained, add to sso.props, if written
- String cm_url = getProperty(access,env,false, Config.CM_URL,Config.CM_URL+": ");
- if (cm_url!=null) {
- aafsso.addProp(Config.CM_URL, cm_url);
- }
+ Map<String, String> aaf_urls = loadURLs(access);
+ aafsso.addProp(Config.AAF_URL_CM, aaf_urls.get(Config.AAF_URL_CM));
aafsso.writeFiles();
}
keypairGen(trans, access, cmds);
break;
case "config":
- if (access.getProperty(Config.CADI_PROP_FILES)!=null) {
- // Get Properties from initialization Prop Files
- config(trans,access,null,cmds);
- } else {
- // Get Properties from existing AAF Instance
- config(trans,access,aafcon(access),cmds);
- }
+ config(trans,access,args,cmds);
break;
case "validate":
validate(access);
}
}
- private static synchronized AAFCon<?> aafcon(PropAccess access) throws APIException, CadiException, LocatorException {
+ public synchronized static Map<String,String> loadURLs(Access access) throws UnknownHostException, CadiException {
+ if(aaf_urls==null) {
+ Map<String,String> rv = new HashMap<>();
+ RegistrationPropHolder rph = new RegistrationPropHolder(access, 0);
+ String dot_le = access.getProperty(Config.AAF_LOCATOR_CONTAINER,null);
+ dot_le=dot_le==null?"":'.'+dot_le;
+ String version = access.getProperty(Config.AAF_API_VERSION,Config.AAF_DEFAULT_API_VERSION);
+ for(String u : new String[] {"aaf","locate","oauth","cm","gui","fs","hello","token","introspect"}) {
+ String proto = "fs".equals(u)?"http://":"https://";
+ String lhost;
+ if("locate".equals(u)) {
+ lhost=rph.default_fqdn;
+ } else {
+ lhost=Config.AAF_LOCATE_URL_TAG;
+ }
+ String value = rph.replacements("Agent:loadURLs",
+ proto + lhost + "/AAF_NS." + ("aaf".equals(u)?"service":u) + ':' + version,
+ null,dot_le);
+ switch(u) {
+ case "aaf": rv.put(Config.AAF_URL, value); break;
+ case "locate": rv.put(Config.AAF_LOCATE_URL, value); break;
+ case "token": rv.put(Config.AAF_OAUTH2_TOKEN_URL, value); break;
+ case "introspect": rv.put(Config.AAF_OAUTH2_INTROSPECT_URL, value); break;
+ case "cm": rv.put(Config.AAF_URL_CM, value); break;
+ case "gui": rv.put(Config.AAF_URL_GUI, value); break;
+ case "fs": rv.put(Config.AAF_URL_FS, value); break;
+ case "hello": rv.put(Config.AAF_URL_HELLO, value); break;
+ default:
+ rv.put("aaf_url_" + u, value);
+ }
+ };
+ aaf_urls = rv;
+ }
+ return aaf_urls;
+ }
+
+ public static void fillMissing(PropAccess access, Map<String, String> map) {
+ for(Entry<String, String> es : map.entrySet()) {
+ if(access.getProperty(es.getKey())==null) {
+ access.setProperty(es.getKey(),es.getValue());
+ }
+ }
+ }
+
+ private static synchronized AAFCon<?> aafcon(Access access) throws APIException, CadiException, LocatorException {
if (aafcon==null) {
- aafcon = new AAFConHttp(access,Config.CM_URL);
+ aafcon = new AAFConHttp(access,Config.AAF_URL_CM);
}
return aafcon;
}
System.out.printf("Wrote %s\n", f.getCanonicalFile());
}
- private static void config(Trans trans, PropAccess pa, AAFCon<?> aafcon, Deque<String> cmds) throws Exception {
-
+ private static void config(Trans trans, PropAccess propAccess, String[] args, Deque<String> cmds) throws Exception {
TimeTaken tt = trans.start("Get Configuration", Env.REMOTE);
try {
final String fqi = fqi(cmds);
Artifact arti = new Artifact();
- arti.setDir(pa.getProperty(Config.CADI_ETCDIR, "."));
+ arti.setDir(propAccess.getProperty(Config.CADI_ETCDIR, "."));
arti.setNs(FQI.reverseDomain(fqi));
-
PropHolder loc = PropHolder.get(arti, "location.props");
PropHolder cred = PropHolder.get(arti,"cred.props");
PropHolder app= PropHolder.get(arti,"props");
+ for(String c : args) {
+ int idx = c.indexOf('=');
+ if(idx>0) {
+ app.add(c.substring(0,idx), c.substring(idx+1));
+ }
+ }
app.add(Config.CADI_PROP_FILES, loc.getPath()+':'+cred.getPath());
for (String tag : LOC_TAGS) {
- loc.add(tag, getProperty(pa, trans, false, tag, "%s: ",tag));
+ loc.add(tag, getProperty(propAccess, trans, false, tag, "%s: ",tag));
}
cred.add(Config.CADI_KEYFILE, cred.getKeyPath());
- cred.addEnc(Config.AAF_APPPASS, pa, null);
+ final String ssoAppID = propAccess.getProperty(Config.AAF_APPID);
+ if(fqi!=null && fqi.equals(ssoAppID)) {
+ cred.addEnc(Config.AAF_APPPASS, propAccess, null);
+ // only Ask for Password when starting scratch
+ } else if(propAccess.getProperty(Config.CADI_PROP_FILES)==null) {
+ char[] pwd = AAFSSO.cons.readPassword("Password for %s: ", fqi);
+ if(pwd.length>0) {
+ cred.addEnc(Config.AAF_APPPASS, new String(pwd));
+ }
+ }
+
+ // load all properties that are already setup.
+ Map<String, String> aaf_urls = loadURLs(propAccess);
+ for(Entry<String, String> es : aaf_urls.entrySet()) {
+ app.add(es.getKey(), es.getValue());
+ }
+
+ app.add(Config.AAF_LOCATE_URL, propAccess, null);
+ app.add(Config.AAF_ENV,propAccess, "DEV");
+ String release = propAccess.getProperty(Config.AAF_RELEASE);
+ if(release!=null) {
+ app.add(Config.AAF_RELEASE, release);
+ }
+ for(Entry<Object, Object> aaf_loc_prop : propAccess.getProperties().entrySet()) {
+ String key = aaf_loc_prop.getKey().toString();
+ if(key.startsWith("aaf_locator")) {
+ app.add(key, aaf_loc_prop.getValue().toString());
+ }
+ }
- app.add(Config.AAF_LOCATE_URL, pa, null);
app.add(Config.AAF_APPID, fqi);
- app.add(Config.AAF_URL, pa, Defaults.AAF_URL);
- String cts = pa.getProperty(Config.CADI_TRUSTSTORE);
+ String cts = propAccess.getProperty(Config.CADI_TRUSTSTORE);
+ System.out.println("Passed in Truststore is " + cts);
if (cts!=null) {
File origTruststore = new File(cts);
- if (!origTruststore.exists()) {
- // Try same directory as cadi_prop_files
- String cpf = pa.getProperty(Config.CADI_PROP_FILES);
- if (cpf!=null) {
- for (String f : Split.split(File.pathSeparatorChar, cpf)) {
- File fcpf = new File(f);
- if (fcpf.exists()) {
- int lastSep = cts.lastIndexOf(File.pathSeparator);
- origTruststore = new File(fcpf.getParentFile(),lastSep>=0?cts.substring(lastSep):cts);
- if (origTruststore.exists()) {
- break;
- }
- }
- }
- if (!origTruststore.exists()) {
- throw new CadiException(cts + " does not exist");
- }
- }
-
- }
File newTruststore = new File(app.getDir(),origTruststore.getName());
- if (!newTruststore.exists()) {
- Files.copy(origTruststore.toPath(), newTruststore.toPath());
+ if(!newTruststore.exists()) {
+ if (!origTruststore.exists()) {
+ // Try same directory as cadi_prop_files
+ String cpf = propAccess.getProperty(Config.CADI_PROP_FILES);
+ if (cpf!=null) {
+ for (String f : Split.split(File.pathSeparatorChar, cpf)) {
+ File fcpf = new File(f);
+ if (fcpf.exists()) {
+ int lastSep = cts.lastIndexOf(File.pathSeparator);
+ origTruststore = new File(fcpf.getParentFile(),lastSep>=0?cts.substring(lastSep):cts);
+ if (origTruststore.exists()) {
+ break;
+ }
+ }
+ }
+ if (!origTruststore.exists()) {
+ throw new CadiException(cts + " does not exist");
+ }
+ }
+
+ }
+ if (!newTruststore.exists() && origTruststore.exists()) {
+ Files.copy(origTruststore.toPath(), newTruststore.toPath());
+ }
}
-
- cred.add(Config.CADI_TRUSTSTORE, pa, newTruststore.getCanonicalPath());
- cred.add(Config.CADI_TRUSTSTORE_PASSWORD, pa, "changeit" /* Java default */);
+
+ System.out.println("New Truststore is " + newTruststore);
+ cred.add(Config.CADI_TRUSTSTORE, newTruststore.getCanonicalPath());
+ cred.add(Config.CADI_TRUSTSTORE_PASSWORD, "changeit" /* Java default */);
- 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)) {
- for (Props props : acf.value.getProps()) {
- PropHolder ph = CRED_TAGS.contains(props.getTag())?cred:app;
- if(props.getTag().endsWith("_password")) {
- ph.addEnc(props.getTag(), props.getValue());
- } else {
- ph.add(props.getTag(), props.getValue());
- }
+ String cpf = propAccess.getProperty(Config.CADI_PROP_FILES);
+ if (cpf!=null){
+ String[] propFiles = Split.splitTrim(File.pathSeparatorChar, cpf);
+ for (int pfi = propFiles.length-1;pfi>=0;--pfi) {
+ String f = propFiles[pfi];
+ 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()) {
+ boolean lower = true;
+ String key = prop.getKey().toString();
+ if(LOC_TAGS.contains(key)) {
+ break;
+ }
+ for(int i=0;lower && i<key.length();++i) {
+ if(Character.isUpperCase(key.charAt(i))) {
+ lower = false;
+ }
+ }
+ if(lower) {
+ PropHolder ph = CRED_TAGS.contains(key)?cred:app;
+ if(key.endsWith("_password")) {
+ ph.addEnc(key, prop.getValue().toString());
+ } else {
+ ph.add(key, prop.getValue().toString());
+ }
+ }
+ }
+ } finally {
+ fis.close();
}
- } else if (acf.code()==401){
- trans.error().log("Bad Password sent to AAF");
- } else {
- 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()) {
- boolean lower = true;
- String key = prop.getKey().toString();
- for(int i=0;lower && i<key.length();++i) {
- if(Character.isUpperCase(key.charAt(i))) {
- lower = false;
- }
- }
- if(lower) {
- PropHolder ph = CRED_TAGS.contains(key)?cred:app;
- if(key.endsWith("_password")) {
- ph.addEnc(key, prop.getValue().toString());
- } else {
- ph.add(key, prop.getValue().toString());
- }
- }
- }
- } finally {
- fis.close();
+ aafcon = aafcon(propAccess);
+ if (aafcon!=null) { // get Properties from Remote AAF
+ final String locator = getProperty(propAccess,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)) {
+ for (Props props : acf.value.getProps()) {
+ PropHolder ph = CRED_TAGS.contains(props.getTag())?cred:app;
+ if(props.getTag().endsWith("_password")) {
+ ph.addEnc(props.getTag(), props.getValue());
+ } else {
+ ph.add(props.getTag(), props.getValue());
+ }
}
+ } else if (acf.code()==401){
+ trans.error().log("Bad Password sent to AAF");
+ } else if (acf.code()==404){
+ trans.error().log("This version of AAF does not support remote Properties");
+ } else {
+ trans.error().log(errMsg.toMsg(acf));
}
}
}
}
- private static List<String> LOC_TAGS = Arrays.asList(new String[] {Config.CADI_LATITUDE, Config.CADI_LONGITUDE});
-
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));