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