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