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