API Version backward compat
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / config / Config.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.config;
23
24 import java.io.IOException;
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.Field;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import java.net.HttpURLConnection;
30 import java.net.InetAddress;
31 import java.net.URI;
32 import java.net.UnknownHostException;
33 import java.security.NoSuchAlgorithmException;
34 import java.security.cert.CertificateException;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import org.onap.aaf.cadi.AbsUserCache;
39 import org.onap.aaf.cadi.Access;
40 import org.onap.aaf.cadi.Access.Level;
41 import org.onap.aaf.cadi.CachingLur;
42 import org.onap.aaf.cadi.CadiException;
43 import org.onap.aaf.cadi.Connector;
44 import org.onap.aaf.cadi.CredVal;
45 import org.onap.aaf.cadi.CredValDomain;
46 import org.onap.aaf.cadi.Locator;
47 import org.onap.aaf.cadi.LocatorException;
48 import org.onap.aaf.cadi.Lur;
49 import org.onap.aaf.cadi.PropAccess;
50 import org.onap.aaf.cadi.Symm;
51 import org.onap.aaf.cadi.TrustChecker;
52 import org.onap.aaf.cadi.lur.EpiLur;
53 import org.onap.aaf.cadi.lur.LocalLur;
54 import org.onap.aaf.cadi.lur.NullLur;
55 import org.onap.aaf.cadi.taf.HttpEpiTaf;
56 import org.onap.aaf.cadi.taf.HttpTaf;
57 import org.onap.aaf.cadi.taf.basic.BasicHttpTaf;
58 import org.onap.aaf.cadi.taf.cert.X509Taf;
59 import org.onap.aaf.cadi.taf.dos.DenialOfServiceTaf;
60
61 /**
62  * Create a Consistent Configuration mechanism, even when configuration styles are as vastly different as
63  * Properties vs JavaBeans vs FilterConfigs...
64  * 
65  * @author Jonathan
66  *
67  */
68 public class Config {
69
70     private static final String AAF_V2_0 = "org.onap.aaf.cadi.aaf.v2_0";
71     private static final String AAF_V2_0_AAFCON = AAF_V2_0+".AAFCon";
72     private static final String AAF_V2_0_AAF_LUR_PERM = AAF_V2_0+".AAFLurPerm";
73     private static final String OAUTH = "org.onap.auth.oauth";
74     private static final String OAUTH_TOKEN_MGR = OAUTH+".TokenMgr";
75     private static final String OAUTH_HTTP_TAF = OAUTH+".OAuth2HttpTaf";
76     private static final String OAUTH_DIRECT_TAF = OAUTH+".OAuthDirectTAF";
77     public static final String UTF_8 = "UTF-8";
78
79     // Property Names associated with configurations.
80     // As of 1.0.2, these have had the dots removed so as to be compatible with JavaBean style
81     // configurations as well as property list style.
82     public static final String HOSTNAME = "hostname";
83     public static final String CADI_PROP_FILES = "cadi_prop_files"; // Additional Properties files (separate with ;)
84     public static final String CADI_LOGLEVEL = "cadi_loglevel";
85     public static final String CADI_LOGDIR = "cadi_log_dir";
86     public static final String CADI_ETCDIR = "cadi_etc_dir";
87     public static final String CADI_LOGNAME = "cadi_logname";
88     public static final String CADI_KEYFILE = "cadi_keyfile";
89     public static final String CADI_KEYSTORE = "cadi_keystore";
90     public static final String CADI_KEYSTORE_PASSWORD = "cadi_keystore_password";
91     public static final String CADI_ALIAS = "cadi_alias";
92     public static final String CADI_LOGINPAGE_URL = "cadi_loginpage_url";
93     public static final String CADI_LATITUDE = "cadi_latitude";
94     public static final String CADI_LONGITUDE = "cadi_longitude";
95
96
97     public static final String CADI_KEY_PASSWORD = "cadi_key_password";
98     public static final String CADI_TRUSTSTORE = "cadi_truststore";
99     public static final String CADI_TRUSTSTORE_PASSWORD = "cadi_truststore_password";
100     public static final String CADI_X509_ISSUERS = "cadi_x509_issuers";
101     public static final String CADI_TRUST_MASKS="cadi_trust_masks";
102     public static final String CADI_TRUST_PERM="cadi_trust_perm"; //  IDs with this perm can utilize the "AS " user concept
103     public static final String CADI_PROTOCOLS = "cadi_protocols";
104     public static final String CADI_NOAUTHN = "cadi_noauthn";
105     public static final String CADI_LOC_LIST = "cadi_loc_list";
106     public static final String CADI_BATH_CONVERT = "cadi_bath_convert";
107     
108     public static final String CADI_USER_CHAIN_TAG = "cadi_user_chain";
109     public static final String CADI_USER_CHAIN = "USER_CHAIN";
110     
111     public static final String CADI_OAUTH2_URL="cadi_oauth2_url";
112     public static final String CADI_TOKEN_DIR = "cadi_token_dir";
113
114     public static final String HTTPS_PROTOCOLS = "https.protocols";
115     public static final String HTTPS_CIPHER_SUITES = "https.cipherSuites";
116     public static final String HTTPS_CLIENT_PROTOCOLS="jdk.tls.client.protocols";
117     public static final String HTTPS_CIPHER_SUITES_DEFAULT="TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,"
118             + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,"
119             + "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,"
120             + "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,TLS_ECDHE_RSA_WITH_RC4_128_SHA,TLS_ECDH_ECDSA_WITH_RC4_128_SHA,"
121             + "TLS_ECDH_RSA_WITH_RC4_128_SHA,TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,"
122             + "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,TLS_EMPTY_RENEGOTIATION_INFO_SCSV";
123              
124
125     public static final String LOCALHOST_ALLOW = "localhost_allow";
126     public static final String LOCALHOST_DENY = "localhost_deny";
127     
128     public static final String BASIC_REALM = "basic_realm";  // what is sent to the client 
129     public static final String BASIC_WARN = "basic_warn";  // Warning of insecure channel 
130     public static final String USERS = "local_users";
131     public static final String GROUPS = "local_groups";
132     public static final String WRITE_TO = "local_writeto"; // dump RBAC to local file in Tomcat Style (some apps use)
133     
134     public static final String OAUTH_CLIENT_ID="client_id";
135     public static final String OAUTH_CLIENT_SECRET="client_secret";
136     
137     public static final String AAF_ENV = "aaf_env";
138     public static final String AAF_ROOT_NS = "aaf_root_ns";
139     public static final String AAF_ROOT_NS_DEF = "org.osaaf.aaf";
140     public static final String AAF_ROOT_COMPANY = "aaf_root_company";
141     public static final String AAF_LOCATE_URL = "aaf_locate_url"; //URL for AAF locator
142     private static final String AAF_LOCATE_URL_TAG = "AAF_LOCATE_URL"; // Name of Above for use in Config Variables.
143     public static final String AAF_DEFAULT_API_VERSION = "2.1";
144     public static final String AAF_API_VERSION = "aaf_api_version";
145     public static final String AAF_URL = "aaf_url"; //URL for AAF... Use to trigger AAF configuration
146     public static final String AAF_URL_DEF = "https://AAF_LOCATE_URL/AAF_NS.service:" + AAF_DEFAULT_API_VERSION;
147     public static final String GUI_URL_DEF = "https://AAF_LOCATE_URL/AAF_NS.gui:" + AAF_DEFAULT_API_VERSION;
148     public static final String CM_URL_DEF = "https://AAF_LOCATE_URL/AAF_NS.cm:" + AAF_DEFAULT_API_VERSION;
149     public static final String FS_URL_DEF = "https://AAF_LOCATE_URL/AAF_NS.fs:" + AAF_DEFAULT_API_VERSION;
150     public static final String HELLO_URL_DEF = "https://AAF_LOCATE_URL/AAF_NS.hello:" + AAF_DEFAULT_API_VERSION;
151     public static final String OAUTH2_TOKEN_URL = "https://AAF_LOCATE_URL/AAF_NS.token:" + AAF_DEFAULT_API_VERSION;
152     public static final String OAUTH2_INTROSPECT_URL = "https://AAF_LOCATE_URL/AAF_NS.introspect:" + AAF_DEFAULT_API_VERSION;
153
154     public static final String AAF_REGISTER_AS = "aaf_register_as";
155     public static final String AAF_APPID = "aaf_id";
156     public static final String AAF_APPPASS = "aaf_password";
157     public static final String AAF_LUR_CLASS = "aaf_lur_class";
158     public static final String AAF_TAF_CLASS = "aaf_taf_class";
159     public static final String AAF_CONNECTOR_CLASS = "aaf_connector_class";
160     public static final String AAF_LOCATOR_CLASS = "aaf_locator_class";
161     public static final String AAF_CONN_TIMEOUT = "aaf_conn_timeout";
162     public static final String AAF_CONN_TIMEOUT_DEF = "3000";
163     public static final String AAF_CONN_IDLE_TIMEOUT = "aaf_conn_idle_timeout"; // only for Direct Jetty Access.
164     public static final String AAF_CONN_IDLE_TIMEOUT_DEF = "10000"; // only for Direct Jetty Access.
165      
166     // Default Classes: These are for Class loading to avoid direct compile links
167     public static final String AAF_TAF_CLASS_DEF = "org.onap.aaf.cadi.aaf.v2_0.AAFTaf";
168     public static final String AAF_LOCATOR_CLASS_DEF = "org.onap.aaf.cadi.aaf.v2_0.AAFLocator";
169     public static final String CADI_OLUR_CLASS_DEF = "org.onap.aaf.cadi.olur.OLur";
170     public static final String CADI_OBASIC_HTTP_TAF_DEF = "org.onap.aaf.cadi.obasic.OBasicHttpTaf";
171     public static final String CADI_AAF_CON_DEF = "org.onap.aaf.cadi.aaf.v2_0.AAFCon";
172
173     public static final String AAF_CALL_TIMEOUT = "aaf_timeout";
174     public static final String AAF_CALL_TIMEOUT_DEF = "5000";
175     public static final String AAF_USER_EXPIRES = "aaf_user_expires";
176     public static final String AAF_USER_EXPIRES_DEF = "600000"; // Default is 10 mins
177     public static final String AAF_CLEAN_INTERVAL = "aaf_clean_interval";
178     public static final String AAF_CLEAN_INTERVAL_DEF = "30000"; // Default is 30 seconds
179     public static final String AAF_REFRESH_TRIGGER_COUNT = "aaf_refresh_trigger_count";
180     public static final String AAF_REFRESH_TRIGGER_COUNT_DEF = "3"; // Default is 10 mins
181     
182     public static final String AAF_HIGH_COUNT = "aaf_high_count";
183     public static final String AAF_HIGH_COUNT_DEF = "1000"; // Default is 1000 entries
184     public static final String AAF_PERM_MAP = "aaf_perm_map";
185     public static final String AAF_COMPONENT = "aaf_component";
186     public static final String AAF_CERT_IDS = "aaf_cert_ids";
187     public static final String AAF_DEBUG_IDS = "aaf_debug_ids"; // comma delimited
188     public static final String AAF_DATA_DIR = "aaf_data_dir"; // AAF processes and Components only.
189     public static final String AAF_RELEASE = "aaf_release";
190
191     public static final String GW_URL = "gw_url";
192     public static final String CM_URL = "cm_url";
193     public static final String CM_TRUSTED_CAS = "cm_trusted_cas";
194
195     public static final String PATHFILTER_URLPATTERN = "pathfilter_urlpattern";
196     public static final String PATHFILTER_STACK = "pathfilter_stack";
197     public static final String PATHFILTER_NS = "pathfilter_ns";
198     public static final String PATHFILTER_NOT_AUTHORIZED_MSG = "pathfilter_not_authorized_msg";
199
200     // This one should go unpublic
201     public static final String AAF_DEFAULT_REALM = "aaf_default_realm";
202     private static String defaultRealm="none";
203
204     public static final String AAF_DOMAIN_SUPPORT = "aaf_domain_support";
205     public static final String AAF_DOMAIN_SUPPORT_DEF = ".com:.org";
206
207     // OAUTH2
208     public static final String AAF_OAUTH2_TOKEN_URL = "aaf_oauth2_token_url";
209     public static final String AAF_OAUTH2_INTROSPECT_URL = "aaf_oauth2_introspect_url";
210     public static final String AAF_ALT_OAUTH2_TOKEN_URL = "aaf_alt_oauth2_token_url";
211     public static final String AAF_ALT_OAUTH2_INTROSPECT_URL = "aaf_alt_oauth2_introspect_url";
212     public static final String AAF_ALT_OAUTH2_DOMAIN = "aaf_alt_oauth2_domain"; 
213     public static final String AAF_ALT_CLIENT_ID = "aaf_alt_oauth2_client_id";
214     public static final String AAF_ALT_CLIENT_SECRET = "aaf_alt_oauth2_client_secret";
215     public static final String AAF_OAUTH2_HELLO_URL = "aaf_oauth2_hello_url";
216
217     private static final String AAF_V2_0_AAF_CON_HTTP = "org.onap.aaf.cadi.aaf.v2_0.AAFConHttp";
218
219
220     public static void setDefaultRealm(Access access) {
221         try {
222             defaultRealm = logProp(access,Config.AAF_DEFAULT_REALM,
223                 logProp(access,Config.BASIC_REALM,
224                     logProp(access,HOSTNAME,InetAddress.getLocalHost().getHostName())
225                     )
226                 );
227         } catch (UnknownHostException e) {
228             access.log(Level.INIT, "Unable to determine Hostname",e);
229         }
230     }
231
232     public static HttpTaf configHttpTaf(Connector con, SecurityInfoC<HttpURLConnection> si, TrustChecker tc, CredVal up, Lur lur, Object ... additionalTafLurs) throws CadiException, LocatorException {
233         Access access = si.access;
234         /////////////////////////////////////////////////////
235         // Setup AAFCon for any following
236         /////////////////////////////////////////////////////
237         Class<?> aafConClass = loadClass(access,CADI_AAF_CON_DEF);
238         Object aafcon = null;
239         if (con!=null && aafConClass!=null && aafConClass.isAssignableFrom(con.getClass())) {
240             aafcon = con;
241         } else if (lur != null) {
242             Field f;
243             try {
244                 f = lur.getClass().getField("aaf");
245                 aafcon = f.get(lur);
246             } catch (Exception e) {
247                 access.log(Level.INIT, e);
248             }
249         }
250
251         boolean hasDirectAAF = hasDirect("DirectAAFLur",additionalTafLurs);
252         // IMPORTANT!  Don't attempt to load AAF Connector if there is no AAF URL
253         String aafURL = access.getProperty(AAF_URL,null);
254         if (!hasDirectAAF && aafcon==null && aafURL!=null) {
255             aafcon = loadAAFConnector(si, aafURL);    
256         }
257         
258         HttpTaf taf;
259         // Setup Host, in case Network reports an unusable Hostname (i.e. VTiers, VPNs, etc)
260         String hostname = logProp(access, HOSTNAME,null);
261         if (hostname==null) {
262             try {
263                 hostname = InetAddress.getLocalHost().getHostName();
264             } catch (UnknownHostException e1) {
265                 throw new CadiException("Unable to determine Hostname",e1);
266             }
267         }
268         
269         access.log(Level.INIT, "Hostname set to",hostname);
270         // Get appropriate TAFs
271         ArrayList<HttpTaf> htlist = new ArrayList<>();
272
273         /////////////////////////////////////////////////////
274         // Add a Denial of Service TAF
275         // Note: how IPs and IDs are added are up to service type.
276         // They call "DenialOfServiceTaf.denyIP(String) or denyID(String)
277         /////////////////////////////////////////////////////
278         htlist.add(new DenialOfServiceTaf(access));
279
280         /////////////////////////////////////////////////////
281         // Configure Client Cert TAF
282         /////////////////////////////////////////////////////
283         X509Taf x509TAF = null;
284         String truststore = logProp(access, CADI_TRUSTSTORE,null);
285         if (truststore!=null) {
286             String truststorePwd = access.getProperty(CADI_TRUSTSTORE_PASSWORD,null);
287             if (truststorePwd!=null) {
288                 if (truststorePwd.startsWith(Symm.ENC)) {
289                     try {
290                         access.decrypt(truststorePwd,false);
291                     } catch (IOException e) {
292                         throw new CadiException(CADI_TRUSTSTORE_PASSWORD + " cannot be decrypted",e);
293                     }
294                 }
295                 try {
296                     x509TAF=new X509Taf(access,lur);
297                     htlist.add(x509TAF);
298                     access.log(Level.INIT,"Certificate Authorization enabled");
299                 } catch (SecurityException | IllegalArgumentException e) {
300                     access.log(Level.INIT,"AAFListedCertIdentity cannot be instantiated. Certificate Authorization is now disabled",e);
301                 } catch (CertificateException e) {
302                     access.log(Level.INIT,"Certificate Authorization failed, it is disabled",e);
303                 } catch (NoSuchAlgorithmException e) {
304                     access.log(Level.INIT,"Certificate Authorization failed, wrong Security Algorithm",e);
305                 }
306             }
307         } else {
308             access.log(Level.INIT,"Certificate Authorization not enabled");
309         }
310         
311         /////////////////////////////////////////////////////
312         // Configure Basic Auth (local content)
313         /////////////////////////////////////////////////////
314         boolean hasOAuthDirectTAF = hasDirect("DirectOAuthTAF", additionalTafLurs);
315         String basicRealm = logProp(access, BASIC_REALM,null);
316         String aafCleanup = logProp(access, AAF_USER_EXPIRES,AAF_USER_EXPIRES_DEF); // Default is 10 mins
317         long userExp = Long.parseLong(aafCleanup);
318         boolean basicWarn = "TRUE".equals(access.getProperty(BASIC_WARN,"FALSE"));
319
320         if (!hasDirectAAF) {
321             HttpTaf aaftaf=null;
322             if (!hasOAuthDirectTAF) {
323                 if (basicRealm!=null) {
324                     @SuppressWarnings("unchecked")
325                     Class<HttpTaf> obasicCls = (Class<HttpTaf>)loadClass(access,CADI_OBASIC_HTTP_TAF_DEF);
326                     if (obasicCls!=null) {
327                         try {
328                             String tokenurl = logProp(access,Config.AAF_OAUTH2_TOKEN_URL, null);
329                             String introspecturl = logProp(access,Config.AAF_OAUTH2_INTROSPECT_URL, null);
330                             if (tokenurl==null || introspecturl==null) {
331                                 access.log(Level.INIT,"Both tokenurl and introspecturl are required. Oauth Authorization is disabled.");
332                             }
333                             Constructor<HttpTaf> obasicConst = obasicCls.getConstructor(PropAccess.class,String.class, String.class, String.class);
334                             htlist.add(obasicConst.newInstance(access,basicRealm,tokenurl,introspecturl));
335                             access.log(Level.INIT,"Oauth supported Basic Authorization is enabled");
336                         } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
337                             access.log(Level.INIT, e);
338                         }
339                     } else if (up!=null) {
340                         access.log(Level.INIT,"Basic Authorization is enabled using realm",basicRealm);
341                         // Allow warning about insecure channel to be turned off
342                         if (!basicWarn) {
343                             access.log(Level.INIT, "WARNING! The basicWarn property has been set to false.",
344                                 " There will be no additional warning if Basic Auth is used on an insecure channel");
345                         }
346                         BasicHttpTaf bht = new BasicHttpTaf(access, up, basicRealm, userExp, basicWarn);
347                         for (Object o : additionalTafLurs) {
348                             if (o instanceof CredValDomain) {
349                                 bht.add((CredValDomain)o);
350                             }
351                         }
352                         if (x509TAF!=null) {
353                             x509TAF.add(bht);
354                         }
355                         htlist.add(bht);
356                         access.log(Level.INIT,"Basic Authorization is enabled");
357                     }
358                 } else {
359                     access.log(Level.INIT,"Local Basic Authorization is disabled.  Enable by setting basicRealm=<appropriate realm, i.e. my.att.com>");
360                 }
361             
362                 /////////////////////////////////////////////////////
363                 // Configure AAF Driven Basic Auth
364                 /////////////////////////////////////////////////////
365                 if (aafcon==null) {
366                     access.log(Level.INIT,"AAF Connection (AAFcon) is null.  Cannot create an AAF TAF");
367                 } else if (aafURL==null) {
368                     access.log(Level.INIT,"No AAF URL in properties, Cannot create an AAF TAF");
369                 } else {// There's an AAF_URL... try to configure an AAF 
370                     String aafTafClassName = logProp(access, AAF_TAF_CLASS,AAF_TAF_CLASS_DEF);
371                     // Only 2.0 available at this time
372                     if (AAF_TAF_CLASS_DEF.equals(aafTafClassName)) { 
373                         try {
374                             Class<?> aafTafClass = loadClass(access,aafTafClassName);
375                             if (aafTafClass!=null) {
376                                 Constructor<?> cstr = aafTafClass.getConstructor(Connector.class,boolean.class,AbsUserCache.class);
377                                 if (cstr!=null) {
378                                     if (lur instanceof AbsUserCache) {
379                                         aaftaf = (HttpTaf)cstr.newInstance(aafcon,basicWarn,lur);
380                                     } else {
381                                         cstr = aafTafClass.getConstructor(Connector.class,boolean.class);
382                                         if (cstr!=null) {
383                                             aaftaf = (HttpTaf)cstr.newInstance(aafcon,basicWarn);
384                                         }
385                                     }
386                                     if (aaftaf==null) {
387                                         access.log(Level.INIT,"ERROR! AAF TAF Failed construction.  NOT Configured");
388                                     } else {
389                                         access.log(Level.INIT,"AAF TAF Configured to ",aafURL);
390                                         // Note: will add later, after all others configured
391                                     }
392                                 }
393                             } else {
394                                 access.log(Level.INIT, "There is no AAF TAF class available: %s. AAF TAF not configured.",aafTafClassName);
395                             }
396                         } catch (Exception e) {
397                             access.log(Level.INIT,"ERROR! AAF TAF Failed construction.  NOT Configured",e);
398                         }
399                     }
400                 }
401             }
402             
403             /////////////////////////////////////////////////////
404             // Configure OAuth TAF
405             /////////////////////////////////////////////////////
406             if (!hasOAuthDirectTAF) {
407                 String oauthTokenUrl = logProp(access,Config.AAF_OAUTH2_TOKEN_URL,null);
408                 Class<?> oadtClss;
409                 try {
410                     oadtClss = Class.forName(OAUTH_DIRECT_TAF);
411                 } catch (ClassNotFoundException e1) {
412                     oadtClss = null;
413                     access.log(Level.INIT, e1);
414                 }
415                 if (additionalTafLurs!=null && additionalTafLurs.length>0 && (oadtClss!=null && additionalTafLurs[0].getClass().isAssignableFrom(oadtClss))) {
416                     htlist.add((HttpTaf)additionalTafLurs[0]);
417                     String[] array= new String[additionalTafLurs.length-1];
418                     if (array.length>0) {
419                         System.arraycopy(htlist, 1, array, 0, array.length);
420                     }
421                     additionalTafLurs = array;
422                     access.log(Level.INIT,"OAuth2 Direct is enabled");
423                 } else if (oauthTokenUrl!=null) {
424                     String oauthIntrospectUrl = logProp(access,Config.AAF_OAUTH2_INTROSPECT_URL,null);
425                     @SuppressWarnings("unchecked")
426                     Class<HttpTaf> oaTCls = (Class<HttpTaf>)loadClass(access,OAUTH_HTTP_TAF);
427                     if (oaTCls!=null) {
428                         Class<?> oaTTmgrCls = loadClass(access, OAUTH_TOKEN_MGR);
429                         if (oaTTmgrCls!=null) {
430                             try {
431                                 Method oaTTmgrGI = oaTTmgrCls.getMethod("getInstance",PropAccess.class,String.class,String.class);
432                                 Object oaTTmgr = oaTTmgrGI.invoke(null /*this is static method*/,access,oauthTokenUrl,oauthIntrospectUrl);
433                                 Constructor<HttpTaf> oaTConst = oaTCls.getConstructor(Access.class,oaTTmgrCls);
434                                 htlist.add(oaTConst.newInstance(access,oaTTmgr));
435                                 access.log(Level.INIT,"OAuth2 TAF is enabled");
436                             } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
437                                 access.log(Level.INIT,"OAuth2HttpTaf cannot be instantiated. OAuth2 is disabled",e);
438                             }
439                         }
440                     }
441                 } else {
442                     access.log(Level.INIT,"OAuth TAF is not configured");
443                 }
444             }
445     
446             /////////////////////////////////////////////////////
447             // Adding BasicAuth (AAF) last, after other primary Cookie Based
448             // Needs to be before Cert... see below
449             /////////////////////////////////////////////////////
450             if (aaftaf!=null) {
451                 htlist.add(aaftaf);
452             }
453         }    
454
455         /////////////////////////////////////////////////////
456         // Any Additional Lurs passed in Constructor
457         /////////////////////////////////////////////////////
458         if (additionalTafLurs!=null) {
459             for (Object additional : additionalTafLurs) {
460                 if (additional instanceof BasicHttpTaf) {
461                     BasicHttpTaf ht = (BasicHttpTaf)additional;
462                     for (Object cv : additionalTafLurs) {
463                         if (cv instanceof CredValDomain) {
464                             ht.add((CredValDomain)cv);
465                             access.printf(Level.INIT,"%s Authentication is enabled",cv);
466                         }
467                     }
468                     htlist.add(ht);
469                 } else if (additional instanceof HttpTaf) {
470                     HttpTaf ht = (HttpTaf)additional;
471                     htlist.add(ht);
472                     access.printf(Level.INIT,"%s Authentication is enabled",additional.getClass().getSimpleName());
473                 } else if (hasOAuthDirectTAF) {
474                     Class<?> daupCls;
475                     try {
476                         daupCls = Class.forName("org.onap.aaf.auth.direct.DirectAAFUserPass");
477                     } catch (ClassNotFoundException e) {
478                         daupCls = null;
479                         access.log(Level.INIT, e);
480                     }
481                     if (daupCls != null && additional.getClass().isAssignableFrom(daupCls)) {
482                         htlist.add(new BasicHttpTaf(access, (CredVal)additional , basicRealm, userExp, basicWarn));
483                         access.printf(Level.INIT,"Direct BasicAuth Authentication is enabled",additional.getClass().getSimpleName());
484                     }
485                 }
486             }
487         }
488         
489         // Add BasicAuth, if any, to x509Taf
490         if (x509TAF!=null) {
491             for ( HttpTaf ht : htlist) {
492                 if (ht instanceof BasicHttpTaf) {
493                     x509TAF.add((BasicHttpTaf)ht);
494                 }
495             }
496         }
497         /////////////////////////////////////////////////////
498         // Create EpiTaf from configured TAFs
499         /////////////////////////////////////////////////////
500         if (htlist.size()==1) {
501             // just return the one
502             taf = htlist.get(0);
503         } else {
504             HttpTaf[] htarray = new HttpTaf[htlist.size()];
505             htlist.toArray(htarray);
506             Locator<URI> locator = loadLocator(si, logProp(access, AAF_LOCATE_URL, null));
507             
508             taf = new HttpEpiTaf(access,locator, tc, htarray); // ok to pass locator == null
509             String level = logProp(access, CADI_LOGLEVEL, null);
510             if (level!=null) {
511                 access.setLogLevel(Level.valueOf(level));
512             }
513         }
514         
515         return taf;
516     }
517     
518     public static String logProp(Access access,String tag, String def) {
519         String rv = access.getProperty(tag, def);
520         if (rv == null) {
521             access.log(Level.INIT,tag,"is not explicitly set");
522         } else {
523             access.log(Level.INIT,tag,"is set to",rv);
524         }
525         return rv;
526     }
527     
528     public static Lur configLur(SecurityInfoC<HttpURLConnection> si, Connector con, Object ... additionalTafLurs) throws CadiException {
529         Access access = si.access;
530         List<Lur> lurs = new ArrayList<>();
531         
532         /////////////////////////////////////////////////////
533         // Configure a Local Property Based RBAC/LUR
534         /////////////////////////////////////////////////////
535         try {
536             String users = access.getProperty(USERS,null);
537             String groups = access.getProperty(GROUPS,null);
538
539             if (groups!=null || users!=null) {
540                 LocalLur ll = new LocalLur(access, users, groups);  // note b64==null is ok.. just means no encryption.
541                 lurs.add(ll);
542                 
543                 String writeto = access.getProperty(WRITE_TO,null);
544                 if (writeto!=null) {
545                     String msg = UsersDump.updateUsers(writeto, ll);
546                     if (msg!=null) {
547                         access.log(Level.INIT,"ERROR! Error Updating ",writeto,"with roles and users:",msg);
548                     }
549                 }
550             }
551         } catch (IOException e) {
552             throw new CadiException(e);
553         }
554
555         /////////////////////////////////////////////////////
556         // Configure the OAuth Lur (if any)
557         /////////////////////////////////////////////////////
558         String tokenUrl = logProp(access,AAF_OAUTH2_TOKEN_URL, null);
559         String introspectUrl = logProp(access,AAF_OAUTH2_INTROSPECT_URL, null);
560         if (tokenUrl!=null && introspectUrl !=null) {
561             try {
562                 Class<?> olurCls = loadClass(access, CADI_OLUR_CLASS_DEF);
563                 if (olurCls!=null) {
564                     Constructor<?> olurCnst = olurCls.getConstructor(PropAccess.class,String.class,String.class);
565                     Lur olur = (Lur)olurCnst.newInstance(access,tokenUrl,introspectUrl);
566                     lurs.add(olur);
567                     access.log(Level.INIT, "OAuth2 LUR enabled");
568                 } else {
569                     access.log(Level.INIT,"AAF/OAuth LUR plugin is not available.");
570                 }
571             } catch (NoSuchMethodException| SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
572                 String msg = e.getMessage();
573                 if (msg==null && e.getCause()!=null) {
574                     msg = e.getCause().getMessage();
575                 }
576                 access.log(Level.INIT,"AAF/OAuth LUR is not instantiated.",msg,e);
577             } 
578         } else {
579             access.log(Level.INIT, "OAuth2 Lur disabled");
580         }
581
582         if (con!=null) { // try to reutilize connector
583             lurs.add(con.newLur());
584         } else { 
585             /////////////////////////////////////////////////////
586             // Configure the AAF Lur (if any)
587             /////////////////////////////////////////////////////
588             String aafURL = logProp(access,AAF_URL,null); // Trigger Property
589             String aafEnv = access.getProperty(AAF_ENV,null);
590             if (aafEnv == null && aafURL!=null && access instanceof PropAccess) { // set AAF_ENV from AAF_URL
591                 int ec = aafURL.indexOf("envContext=");
592                 if (ec>0) {
593                     ec += 11; // length of envContext=
594                     int slash = aafURL.indexOf('/', ec);
595                     if (slash>0) {
596                         aafEnv = aafURL.substring(ec, slash);
597                         ((PropAccess)access).setProperty(AAF_ENV, aafEnv);
598                         access.printf(Level.INIT, "Setting aafEnv to %s from aaf_url value",aafEnv);
599                     }
600                 }
601             }
602
603             // Don't configure AAF if it is using DirectAccess
604             if (!hasDirect("DirectAAFLur",additionalTafLurs)) {
605                 if (aafURL==null) {
606                     access.log(Level.INIT,"No AAF LUR properties, AAF will not be loaded");
607                 } else {// There's an AAF_URL... try to configure an AAF
608                     String aafLurClassStr = logProp(access,AAF_LUR_CLASS,AAF_V2_0_AAF_LUR_PERM);
609                     ////////////AAF Lur 2.0 /////////////
610                     if (aafLurClassStr!=null && aafLurClassStr.startsWith(AAF_V2_0)) { 
611                         try {
612                             Object aafcon = loadAAFConnector(si, aafURL);
613                             if (aafcon==null) {
614                                 access.log(Level.INIT,"AAF LUR class,",aafLurClassStr,"cannot be constructed without valid AAFCon object.");
615                             } else {
616                                 Class<?> aafAbsAAFCon = loadClass(access, AAF_V2_0_AAFCON);
617                                 if (aafAbsAAFCon!=null) {
618                                     Method mNewLur = aafAbsAAFCon.getMethod("newLur");
619                                     Object aaflur = mNewLur.invoke(aafcon);
620                 
621                                     if (aaflur==null) {
622                                         access.log(Level.INIT,"ERROR! AAF LUR Failed construction.  NOT Configured");
623                                     } else {
624                                         access.log(Level.INIT,"AAF LUR Configured to ",aafURL);
625                                         lurs.add((Lur)aaflur);
626                                         String debugIDs = logProp(access,Config.AAF_DEBUG_IDS, null);
627                                         if (debugIDs !=null && aaflur instanceof CachingLur) {
628                                             ((CachingLur<?>)aaflur).setDebug(debugIDs);
629                                         }
630                                     }
631                                 }
632                             }
633                         } catch (Exception e) {
634                             access.log(e,"AAF LUR class,",aafLurClassStr,"could not be constructed with given Constructors.");
635                         }
636                     } 
637                 }
638             }
639         }
640
641         /////////////////////////////////////////////////////
642         // Any Additional passed in Constructor
643         /////////////////////////////////////////////////////
644         if (additionalTafLurs!=null) {
645             for (Object additional : additionalTafLurs) {
646                 if (additional instanceof Lur) {
647                     lurs.add((Lur)additional);
648                     access.log(Level.INIT, additional);
649                 }
650             }
651         }
652
653         /////////////////////////////////////////////////////
654         // Return a Lur based on how many there are... 
655         /////////////////////////////////////////////////////
656         switch(lurs.size()) {
657             case 0: 
658                 access.log(Level.INIT,"WARNING! No CADI LURs configured");
659                 // Return a NULL Lur that does nothing.
660                 return new NullLur();
661             case 1:
662                 return lurs.get(0); // Only one, just return it, save processing
663             default:
664                 // Multiple Lurs, use EpiLUR to handle
665                 Lur[] la = new Lur[lurs.size()];
666                 lurs.toArray(la);
667                 return new EpiLur(la);
668         }
669     }
670     
671     private static boolean hasDirect(String simpleClassName, Object[] additionalTafLurs) {
672         if (additionalTafLurs!=null) {
673             for (Object tf : additionalTafLurs) {
674                 if (tf.getClass().getSimpleName().equals(simpleClassName)) {
675                     return true;
676                 }
677             }
678         }
679         return false;
680     }
681
682     public static Object loadAAFConnector(SecurityInfoC<HttpURLConnection> si, String aafURL) {
683         Access access = si.access;
684         Object aafcon = null;
685         Class<?> aafConClass = null;
686
687         try {
688             if (aafURL!=null) {
689                 String aafConnector = access.getProperty(AAF_CONNECTOR_CLASS, AAF_V2_0_AAF_CON_HTTP);
690                 if (AAF_V2_0_AAF_CON_HTTP.equals(aafConnector)) {
691                     aafConClass = loadClass(access, AAF_V2_0_AAF_CON_HTTP);
692                     if (aafConClass != null) {
693                         for (Constructor<?> c : aafConClass.getConstructors()) {
694                             List<Object> lo = new ArrayList<>();
695                             for (Class<?> pc : c.getParameterTypes()) {
696                                 if (pc.equals(Access.class)) {
697                                     lo.add(access);
698                                 } else if (pc.equals(Locator.class)) {
699                                     lo.add(loadLocator(si, aafURL));
700                                 }
701                             }
702                             if (c.getParameterTypes().length != lo.size()) {
703                                 continue; // back to another Constructor
704                             } else {
705                                 aafcon = c.newInstance(lo.toArray());
706                             }
707                             break;
708                         }
709                     }
710                 }
711                 if (aafcon != null) {
712                     String mechid = logProp(access, Config.AAF_APPID, null);
713                     String pass = access.getProperty(Config.AAF_APPPASS, null);
714                     if (mechid != null && pass != null) {
715                         try {
716                             Method basicAuth = aafConClass.getMethod("basicAuth", String.class, String.class);
717                             basicAuth.invoke(aafcon, mechid, pass);
718                         } catch (NoSuchMethodException nsme) {
719                             access.log(Level.NONE, nsme);
720                             // it's ok, don't use
721                         }
722                     }
723                 }
724             }
725         } catch (Exception e) {
726             access.log(e, "AAF Connector could not be constructed with given Constructors.");
727         }
728
729         return aafcon;
730     }
731
732     public static Class<?> loadClass(Access access, String className) {
733         Class<?> cls=null;
734         try {
735             cls = access.classLoader().loadClass(className);
736         } catch (ClassNotFoundException cnfe) {
737             access.log(Level.NONE, cnfe);
738             try {
739                 cls = access.getClass().getClassLoader().loadClass(className);
740             } catch (ClassNotFoundException cnfe2) {
741                 access.log(Level.NONE, cnfe2);
742                 // just return null
743             }
744         }
745         return cls;
746     }
747
748     @SuppressWarnings("unchecked")
749     public static Locator<URI> loadLocator(SecurityInfoC<HttpURLConnection> si, final String _url) throws LocatorException {
750         Access access = si.access;
751         Locator<URI> locator = null;
752         if (_url==null) {
753             access.log(Level.INIT,"No URL passed to 'loadLocator'. Disabled");
754         } else {
755             String url = _url;
756             String replacement;
757             int idxAAFLocateUrl;
758             if ((idxAAFLocateUrl=_url.indexOf(AAF_LOCATE_URL_TAG))>0 && ((replacement=access.getProperty(AAF_LOCATE_URL, null))!=null)) {
759                 StringBuilder sb = new StringBuilder(replacement);
760                 if (!replacement.endsWith("/locate")) {
761                     sb.append("/locate");
762                 } 
763                 sb.append(_url,idxAAFLocateUrl+AAF_LOCATE_URL_TAG.length(),_url.length());
764                 url = sb.toString();
765             }
766     
767             try {
768                 Class<?> lcls = loadClass(access,AAF_LOCATOR_CLASS_DEF);
769                 if (lcls==null) {
770                     throw new CadiException("Need to include aaf-cadi-aaf jar for AAFLocator");
771                 }
772                 // First check for preloaded
773                 try {
774                     Method meth = lcls.getMethod("create",String.class);
775                     locator = (Locator<URI>)meth.invoke(null,url);
776                 } catch (Exception e) {
777                     access.log(Level.INIT, e);
778                 }
779                 if (locator==null) {
780                     URI locatorURI = new URI(url);
781                     Constructor<?> cnst = lcls.getConstructor(SecurityInfoC.class,URI.class);
782                     locator = (Locator<URI>)cnst.newInstance(new Object[] {si,locatorURI});
783                     int port = locatorURI.getPort();
784                     String portS = port<0?"":(":"+locatorURI.getPort());
785                     
786                     access.log(Level.INFO, "AAFLocator enabled using " + locatorURI.getScheme() +"://"+locatorURI.getHost() + portS);
787                 } else {
788                     access.log(Level.INFO, "AAFLocator enabled using preloaded " + locator.getClass().getSimpleName());
789                 }
790             } catch (InvocationTargetException e) {
791                 if (e.getTargetException() instanceof LocatorException) {
792                     throw (LocatorException)e.getTargetException();
793                 }
794                 access.log(Level.INIT,e.getTargetException().getMessage(),"AAFLocator for",url,"could not be created.",e);
795             } catch (Exception e) {
796                 access.log(Level.INIT,"AAFLocator for",url,"could not be created.",e);
797             }
798         }
799         return locator;
800     }
801
802     // Set by CSP, or is hostname.
803     public static String getDefaultRealm() {
804         return defaultRealm;
805     }
806
807 }
808