+ /////////////////////////////////////////////////////
+ // Setup AAFCon for any following
+ /////////////////////////////////////////////////////
+ Class<?> aafConClass = loadClass(access,CADI_AAF_CON_DEF);
+ Object aafcon = null;
+ if (con!=null && aafConClass!=null && aafConClass.isAssignableFrom(con.getClass())) {
+ aafcon = con;
+ } else if (lur != null) {
+ Field f;
+ try {
+ f = lur.getClass().getField("aaf");
+ aafcon = f.get(lur);
+ } catch (Exception e) {
+ access.log(Level.INIT, e);
+ }
+ }
+
+ boolean hasDirectAAF = hasDirect("DirectAAFLur",additionalTafLurs);
+ // IMPORTANT! Don't attempt to load AAF Connector if there is no AAF URL
+ String aafURL = logProp(rph, AAF_URL,null);
+ if (!hasDirectAAF && aafcon==null && aafURL!=null) {
+ aafcon = loadAAFConnector(si, aafURL);
+ }
+
+ HttpTaf taf;
+ // Setup Host, in case Network reports an unusable Hostname (i.e. VTiers, VPNs, etc)
+ String hostname = logProp(access, HOSTNAME,null);
+ if (hostname==null) {
+ try {
+ hostname = InetAddress.getLocalHost().getHostName();
+ } catch (UnknownHostException e1) {
+ throw new CadiException("Unable to determine Hostname",e1);
+ }
+ }
+
+ access.log(Level.INIT, "Hostname set to",hostname);
+ // Get appropriate TAFs
+ ArrayList<Priori<HttpTaf>> htlist = new ArrayList<>();
+
+ /////////////////////////////////////////////////////
+ // Add a Denial of Service TAF
+ // Note: how IPs and IDs are added are up to service type.
+ // They call "DenialOfServiceTaf.denyIP(String) or denyID(String)
+ /////////////////////////////////////////////////////
+ htlist.add(new Priori<HttpTaf>(new DenialOfServiceTaf(access),0));
+
+ /////////////////////////////////////////////////////
+ // Configure Client Cert TAF
+ /////////////////////////////////////////////////////
+ X509Taf x509TAF = null;
+ String truststore = logProp(access, CADI_TRUSTSTORE,null);
+ if (truststore!=null) {
+ String truststorePwd = access.getProperty(CADI_TRUSTSTORE_PASSWORD,null);
+ if (truststorePwd!=null) {
+ if (truststorePwd.startsWith(Symm.ENC)) {
+ try {
+ access.decrypt(truststorePwd,false);
+ } catch (IOException e) {
+ throw new CadiException(CADI_TRUSTSTORE_PASSWORD + " cannot be decrypted",e);
+ }
+ }
+ try {
+ x509TAF=new X509Taf(access,lur);
+ htlist.add(new Priori<HttpTaf>(x509TAF,10));
+ access.log(Level.INIT,"Certificate Authorization enabled");
+ } catch (SecurityException | IllegalArgumentException e) {
+ access.log(Level.INIT,"AAFListedCertIdentity cannot be instantiated. Certificate Authorization is now disabled",e);
+ } catch (CertificateException e) {
+ access.log(Level.INIT,"Certificate Authorization failed, it is disabled",e);
+ } catch (NoSuchAlgorithmException e) {
+ access.log(Level.INIT,"Certificate Authorization failed, wrong Security Algorithm",e);
+ }
+ }
+ } else {
+ access.log(Level.INIT,"Certificate Authorization not enabled");
+ }
+
+ /////////////////////////////////////////////////////
+ // Configure Basic Auth (local content)
+ /////////////////////////////////////////////////////
+ boolean hasOAuthDirectTAF = hasDirect("DirectOAuthTAF", additionalTafLurs);
+ String basicRealm = logProp(access, BASIC_REALM,null);
+ String aafCleanup = logProp(access, AAF_USER_EXPIRES,AAF_USER_EXPIRES_DEF); // Default is 10 mins
+ long userExp = Long.parseLong(aafCleanup);
+ boolean basicWarn = "TRUE".equals(access.getProperty(BASIC_WARN,"FALSE"));
+
+ if (!hasDirectAAF) {
+ HttpTaf aaftaf=null;
+ if (!hasOAuthDirectTAF) {
+ if (basicRealm!=null) {
+ @SuppressWarnings("unchecked")
+ Class<HttpTaf> obasicCls = (Class<HttpTaf>)loadClass(access,CADI_OBASIC_HTTP_TAF_DEF);
+ if (obasicCls!=null) {
+ try {
+ String tokenurl = logProp(rph,Config.AAF_OAUTH2_TOKEN_URL, null);
+ String introspecturl = logProp(rph,Config.AAF_OAUTH2_INTROSPECT_URL, null);
+ if (tokenurl==null || introspecturl==null) {
+ access.log(Level.INIT,"Both tokenurl and introspecturl are required. Oauth Authorization is disabled.");
+ }
+ Constructor<HttpTaf> obasicConst = obasicCls.getConstructor(PropAccess.class,String.class, String.class, String.class);
+ htlist.add(new Priori<HttpTaf>(obasicConst.newInstance(access,basicRealm,tokenurl,introspecturl),20));
+ access.log(Level.INIT,"Oauth supported Basic Authorization is enabled");
+ } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+ access.log(Level.INIT, e);
+ }
+ } else if (up!=null) {
+ access.log(Level.INIT,"Basic Authorization is enabled using realm",basicRealm);
+ // Allow warning about insecure channel to be turned off
+ if (!basicWarn) {
+ access.log(Level.INIT, "WARNING! The basicWarn property has been set to false.",
+ " There will be no additional warning if Basic Auth is used on an insecure channel");
+ }
+ BasicHttpTaf bht = new BasicHttpTaf(access, up, basicRealm, userExp, basicWarn);
+ for (Object o : additionalTafLurs) {
+ if (o instanceof CredValDomain) {
+ bht.add((CredValDomain)o);
+ }
+ }
+ if (x509TAF!=null) {
+ x509TAF.add(bht);
+ }
+ htlist.add(new Priori<HttpTaf>(bht,20));
+ access.log(Level.INIT,"Basic Authorization is enabled");
+ }
+ } else {
+ access.log(Level.INIT,"Local Basic Authorization is disabled. Enable by setting basicRealm=<appropriate realm, i.e. my.att.com>");
+ }
+
+ /////////////////////////////////////////////////////
+ // Configure AAF Driven Basic Auth
+ /////////////////////////////////////////////////////
+ if (aafcon==null) {
+ access.log(Level.INIT,"AAF Connection (AAFcon) is null. Cannot create an AAF TAF");
+ } else if (aafURL==null) {
+ access.log(Level.INIT,"No AAF URL in properties, Cannot create an AAF TAF");
+ } else {// There's an AAF_URL... try to configure an AAF
+ String aafTafClassName = logProp(access, AAF_TAF_CLASS,AAF_TAF_CLASS_DEF);
+ // Only 2.0 available at this time
+ if (AAF_TAF_CLASS_DEF.equals(aafTafClassName)) {
+ try {
+ Class<?> aafTafClass = loadClass(access,aafTafClassName);
+ if (aafTafClass!=null) {
+ Constructor<?> cstr = aafTafClass.getConstructor(Connector.class,boolean.class,AbsUserCache.class);
+ if (cstr!=null) {
+ if (lur instanceof AbsUserCache) {
+ aaftaf = (HttpTaf)cstr.newInstance(aafcon,basicWarn,lur);
+ } else {
+ cstr = aafTafClass.getConstructor(Connector.class,boolean.class);
+ if (cstr!=null) {
+ aaftaf = (HttpTaf)cstr.newInstance(aafcon,basicWarn);
+ }
+ }
+ if (aaftaf==null) {
+ access.log(Level.INIT,"ERROR! AAF TAF Failed construction. NOT Configured");
+ } else {
+ access.log(Level.INIT,"AAF TAF Configured to ",aafURL);
+ // Note: will add later, after all others configured
+ }
+ }
+ } else {
+ access.log(Level.INIT, "There is no AAF TAF class available: %s. AAF TAF not configured.",aafTafClassName);
+ }
+ } catch (Exception e) {
+ access.log(Level.INIT,"ERROR! AAF TAF Failed construction. NOT Configured",e);
+ }
+ }
+ }
+ }
+
+ /////////////////////////////////////////////////////
+ // Configure OAuth TAF
+ /////////////////////////////////////////////////////
+ if (!hasOAuthDirectTAF) {
+ String oauthTokenUrl = logProp(rph,Config.AAF_OAUTH2_TOKEN_URL,null);
+ Class<?> oadtClss;
+ try {
+ oadtClss = Class.forName(OAUTH_DIRECT_TAF);
+ } catch (ClassNotFoundException e1) {
+ oadtClss = null;
+ access.log(Level.DEBUG, e1);
+ }
+ if (additionalTafLurs!=null && additionalTafLurs.length>0 && (oadtClss!=null && additionalTafLurs[0].getClass().isAssignableFrom(oadtClss))) {
+ htlist.add(new Priori<HttpTaf>((HttpTaf)additionalTafLurs[0],30));
+ String[] array= new String[additionalTafLurs.length-1];
+ if (array.length>0) {
+ System.arraycopy(htlist, 1, array, 0, array.length);
+ }
+ additionalTafLurs = array;
+ access.log(Level.INIT,"OAuth2 Direct is enabled");
+ } else if (oauthTokenUrl!=null) {
+ String oauthIntrospectUrl = logProp(rph,Config.AAF_OAUTH2_INTROSPECT_URL,null);
+ @SuppressWarnings("unchecked")
+ Class<HttpTaf> oaTCls = (Class<HttpTaf>)loadClass(access,OAUTH_HTTP_TAF);
+ if (oaTCls!=null) {
+ Class<?> oaTTmgrCls = loadClass(access, OAUTH_TOKEN_MGR);
+ if (oaTTmgrCls!=null) {
+ try {
+ Method oaTTmgrGI = oaTTmgrCls.getMethod("getInstance",PropAccess.class,String.class,String.class);
+ Object oaTTmgr = oaTTmgrGI.invoke(null /*this is static method*/,access,oauthTokenUrl,oauthIntrospectUrl);
+ Constructor<HttpTaf> oaTConst = oaTCls.getConstructor(Access.class,oaTTmgrCls);
+ htlist.add(new Priori<HttpTaf>(oaTConst.newInstance(access,oaTTmgr),30));
+ access.log(Level.INIT,"OAuth2 TAF is enabled");
+ } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
+ access.log(Level.INIT,"OAuth2HttpTaf cannot be instantiated. OAuth2 is disabled",e);
+ }
+ }
+ }
+ } else {
+ access.log(Level.INIT,"OAuth TAF is not configured");
+ }
+ }
+
+ /////////////////////////////////////////////////////
+ // Adding BasicAuth (AAF) last, after other primary Cookie Based
+ // Needs to be before Cert... see below
+ /////////////////////////////////////////////////////
+ if (aaftaf!=null) {
+ htlist.add(new Priori<HttpTaf>(aaftaf,40));
+ }
+ }
+
+ /////////////////////////////////////////////////////
+ // Any Additional Tafs passed in Constructor
+ /////////////////////////////////////////////////////
+ if (additionalTafLurs!=null) {
+ int i=0;
+ for (Object additional : additionalTafLurs) {
+ if (additional instanceof BasicHttpTaf) {
+ BasicHttpTaf ht = (BasicHttpTaf)additional;
+ for (Object cv : additionalTafLurs) {
+ if (cv instanceof CredValDomain) {
+ ht.add((CredValDomain)cv);
+ access.printf(Level.INIT,"%s Authentication is enabled",cv);
+ }
+ }
+ htlist.add(new Priori<HttpTaf>(ht,50+i++));
+ } else if (additional instanceof HttpTaf) {
+ HttpTaf ht = (HttpTaf)additional;
+ htlist.add(new Priori<HttpTaf>(ht,50+i++));
+ access.printf(Level.INIT,"%s Authentication is enabled",additional.getClass().getSimpleName());
+ } else if (hasOAuthDirectTAF) {
+ Class<?> daupCls;
+ try {
+ daupCls = Class.forName("org.onap.aaf.auth.direct.DirectAAFUserPass");
+ } catch (ClassNotFoundException e) {
+ daupCls = null;
+ access.log(Level.INIT, e);
+ }
+ if (daupCls != null && additional.getClass().isAssignableFrom(daupCls)) {
+ htlist.add(new Priori<HttpTaf>(new BasicHttpTaf(access, (CredVal)additional , basicRealm, userExp, basicWarn),50+i++));
+ access.printf(Level.INIT,"Direct BasicAuth Authentication is enabled",additional.getClass().getSimpleName());
+ }
+ }
+ }
+ }
+
+ // Add BasicAuth, if any, to x509Taf
+ if (x509TAF!=null) {
+ for ( Priori<HttpTaf> ht : htlist) {
+ if (ht.t instanceof BasicHttpTaf) {
+ x509TAF.add((BasicHttpTaf)ht.t);
+ }
+ }
+ }
+
+ /////////////////////////////////////////////////////
+ // Additional TAFs by Plugin
+ /////////////////////////////////////////////////////
+ Priori.add(access, CADI_ADD_TAFS, htlist);
+
+ /////////////////////////////////////////////////////
+ // Create EpiTaf from configured TAFs
+ /////////////////////////////////////////////////////
+ if (htlist.size()==1) {
+ // just return the one
+ taf = htlist.get(0).t;
+ } else {
+ Collections.sort(htlist);
+ HttpTaf[] htarray = new HttpTaf[htlist.size()];
+ int i=-1;
+ StringBuilder sb = new StringBuilder("Tafs processed in this order:\n");
+ for(Priori<HttpTaf> pht : htlist) {
+ htarray[++i] = pht.t;
+ sb.append(" ");
+ sb.append(pht.t.getClass().getName());
+ sb.append('(');
+ sb.append(pht.priority);
+ sb.append(")\n");
+ }
+ access.log(Level.INIT, sb);
+
+ Locator<URI> locator = loadLocator(si, aafURL);
+
+ taf = new HttpEpiTaf(access,locator, tc, htarray); // ok to pass locator == null
+ String level = logProp(access, CADI_LOGLEVEL, null);
+ if (level!=null) {
+ access.setLogLevel(Level.valueOf(level));
+ }
+ }
+
+ return taf;
+ }
+
+ public static String logProp(RegistrationPropHolder rph, String tag, String def) {
+ String rv = rph.access().getProperty(tag, def);
+ if (rv == null) {
+ rph.access().log(Level.INIT,tag,"is not explicitly set");
+ } else {
+ rv = rph.replacements("Config.logProp",rv, null, null);
+ rph.access().log(Level.INIT,tag,"is set to",rv);
+ }
+ return rv;
+
+ }
+
+ public static String logProp(Access access,String tag, String def) {
+ String rv = access.getProperty(tag, def);
+ if (rv == null) {
+ access.log(Level.INIT,tag,"is not explicitly set");
+ } else {
+ access.log(Level.INIT,tag,"is set to",rv);
+ }
+ return rv;
+ }
+
+ public static Lur configLur(SecurityInfoC<HttpURLConnection> si, Connector con, Object ... additionalTafLurs) throws CadiException {
+ Access access = si.access;
+ RegistrationPropHolder rph;
+ try {
+ rph = new RegistrationPropHolder(access, 0);
+ } catch (UnknownHostException e2) {
+ throw new CadiException(e2);