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