Fix testing results for Authn
[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                 // Add BasicAuth, if any, to x509Taf
478                 if(x509TAF!=null) {
479                         for( HttpTaf ht : htlist) {
480                                 if(ht instanceof BasicHttpTaf) {
481                                         x509TAF.add((BasicHttpTaf)ht);
482                                 }
483                         }
484                 }
485                 /////////////////////////////////////////////////////
486                 // Create EpiTaf from configured TAFs
487                 /////////////////////////////////////////////////////
488                 if(htlist.size()==1) {
489                         // just return the one
490                         taf = htlist.get(0);
491                 } else {
492                         HttpTaf[] htarray = new HttpTaf[htlist.size()];
493                         htlist.toArray(htarray);
494                         Locator<URI> locator = loadLocator(si, logProp(access, AAF_LOCATE_URL, null));
495                         
496                         taf = new HttpEpiTaf(access,locator, tc, htarray); // ok to pass locator == null
497                         String level = logProp(access, CADI_LOGLEVEL, null);
498                         if(level!=null) {
499                                 access.setLogLevel(Level.valueOf(level));
500                         }
501                 }
502                 
503                 return taf;
504         }
505         
506         public static String logProp(Access access,String tag, String def) {
507                 String rv = access.getProperty(tag, def);
508                 if(rv == null) {
509                         access.log(Level.INIT,tag,"is not explicitly set");
510                 } else {
511                         access.log(Level.INIT,tag,"is set to",rv);
512                 }
513                 return rv;
514         }
515         
516         public static Lur configLur(SecurityInfoC<HttpURLConnection> si, Connector con, Object ... additionalTafLurs) throws CadiException {
517                 Access access = si.access;
518                 List<Lur> lurs = new ArrayList<>();
519                 
520                 /////////////////////////////////////////////////////
521                 // Configure a Local Property Based RBAC/LUR
522                 /////////////////////////////////////////////////////
523                 try {
524                         String users = access.getProperty(USERS,null);
525                         String groups = access.getProperty(GROUPS,null);
526
527                         if(groups!=null || users!=null) {
528                                 LocalLur ll;
529                                 lurs.add(ll = new LocalLur(access, users, groups)); // note b64==null is ok.. just means no encryption.
530                                 
531                                 String writeto = access.getProperty(WRITE_TO,null);
532                                 if(writeto!=null) {
533                                         String msg = UsersDump.updateUsers(writeto, ll);
534                                         if(msg!=null) access.log(Level.INIT,"ERROR! Error Updating ",writeto,"with roles and users:",msg);
535                                 }
536                         }
537                 } catch (IOException e) {
538                         throw new CadiException(e);
539                 }
540
541                 /////////////////////////////////////////////////////
542                 // Configure the OAuth Lur (if any)
543                 /////////////////////////////////////////////////////
544                 String token_url = logProp(access,AAF_OAUTH2_TOKEN_URL, null);
545                 String introspect_url = logProp(access,AAF_OAUTH2_INTROSPECT_URL, null);
546                 if(token_url!=null && introspect_url !=null) {
547                         try {
548                                 Class<?> olurCls = loadClass(access, CADI_OLUR_CLASS_DEF);
549                                 if(olurCls!=null) {
550                                         Constructor<?> olurCnst = olurCls.getConstructor(PropAccess.class,String.class,String.class);
551                                         Lur olur = (Lur)olurCnst.newInstance(access,token_url,introspect_url);
552                                         lurs.add(olur);
553                                         access.log(Level.INIT, "OAuth2 LUR enabled");
554                                 } else {
555                                         access.log(Level.INIT,"AAF/OAuth LUR plugin is not available.");
556                                 }
557                         } catch (NoSuchMethodException| SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
558                                 String msg = e.getMessage();
559                                 if(msg==null && e.getCause()!=null) {
560                                         msg = e.getCause().getMessage();
561                                 }
562                                 access.log(Level.INIT,"AAF/OAuth LUR is not instantiated.",msg);
563                         } 
564                 } else {
565                         access.log(Level.INIT, "OAuth2 Lur disabled");
566                 }
567
568                 if(con!=null) { // try to reutilize connector
569                         lurs.add(con.newLur());
570                 } else { 
571                         /////////////////////////////////////////////////////
572                         // Configure the AAF Lur (if any)
573                         /////////////////////////////////////////////////////
574                         String aafURL = logProp(access,AAF_URL,null); // Trigger Property
575                         String aaf_env = access.getProperty(AAF_ENV,null);
576                         if(aaf_env == null && aafURL!=null && access instanceof PropAccess) { // set AAF_ENV from AAF_URL
577                                 int ec = aafURL.indexOf("envContext=");
578                                 if(ec>0) {
579                                         ec += 11; // length of envContext=
580                                         int slash = aafURL.indexOf('/', ec);
581                                         if(slash>0) {
582                                                 aaf_env = aafURL.substring(ec, slash);
583                                                 ((PropAccess)access).setProperty(AAF_ENV, aaf_env);
584                                                 access.printf(Level.INIT, "Setting aaf_env to %s from aaf_url value",aaf_env);
585                                         }
586                                 }
587                         }
588
589                         // Don't configure AAF if it is using DirectAccess
590                         if(!hasDirect("DirectAAFLur",additionalTafLurs)) {
591                                 if(aafURL==null) {
592                                         access.log(Level.INIT,"No AAF LUR properties, AAF will not be loaded");
593                                 } else {// There's an AAF_URL... try to configure an AAF
594                                         String aafLurClassStr = logProp(access,AAF_LUR_CLASS,AAF_V2_0_AAF_LUR_PERM);
595                                         ////////////AAF Lur 2.0 /////////////
596                                         if(aafLurClassStr!=null && aafLurClassStr.startsWith(AAF_V2_0)) { 
597                                                 try {
598                                                         Object aafcon = loadAAFConnector(si, aafURL);
599                                                         if(aafcon==null) {
600                                                                 access.log(Level.INIT,"AAF LUR class,",aafLurClassStr,"cannot be constructed without valid AAFCon object.");
601                                                         } else {
602                                                                 Class<?> aafAbsAAFCon = loadClass(access, AAF_V2_0_AAFCON);
603                                                                 if(aafAbsAAFCon!=null) {
604                                                                         Method mNewLur = aafAbsAAFCon.getMethod("newLur");
605                                                                         Object aaflur = mNewLur.invoke(aafcon);
606                                 
607                                                                         if(aaflur==null) {
608                                                                                 access.log(Level.INIT,"ERROR! AAF LUR Failed construction.  NOT Configured");
609                                                                         } else {
610                                                                                 access.log(Level.INIT,"AAF LUR Configured to ",aafURL);
611                                                                                 lurs.add((Lur)aaflur);
612                                                                                 String debugIDs = logProp(access,Config.AAF_DEBUG_IDS, null);
613                                                                                 if(debugIDs !=null && aaflur instanceof CachingLur) {
614                                                                                         ((CachingLur<?>)aaflur).setDebug(debugIDs);
615                                                                                 }
616                                                                         }
617                                                                 }
618                                                         }
619                                                 } catch (Exception e) {
620                                                         access.log(e,"AAF LUR class,",aafLurClassStr,"could not be constructed with given Constructors.");
621                                                 }
622                                         } 
623                                 }
624                         }
625                 }
626
627                 /////////////////////////////////////////////////////
628                 // Any Additional passed in Constructor
629                 /////////////////////////////////////////////////////
630                 if(additionalTafLurs!=null) {
631                         for(Object additional : additionalTafLurs) {
632                                 if(additional instanceof Lur) {
633                                         lurs.add((Lur)additional);
634                                         access.log(Level.INIT, additional);
635                                 }
636                         }
637                 }
638
639                 /////////////////////////////////////////////////////
640                 // Return a Lur based on how many there are... 
641                 /////////////////////////////////////////////////////
642                 switch(lurs.size()) {
643                         case 0: 
644                                 access.log(Level.INIT,"WARNING! No CADI LURs configured");
645                                 // Return a NULL Lur that does nothing.
646                                 return new NullLur();
647                         case 1:
648                                 return lurs.get(0); // Only one, just return it, save processing
649                         default:
650                                 // Multiple Lurs, use EpiLUR to handle
651                                 Lur[] la = new Lur[lurs.size()];
652                                 lurs.toArray(la);
653                                 return new EpiLur(la);
654                 }
655         }
656         
657         private static boolean hasDirect(String simpleClassName, Object[] additionalTafLurs) {
658                 if(additionalTafLurs!=null) {
659                         for(Object tf : additionalTafLurs) {
660                                 if(tf.getClass().getSimpleName().equals(simpleClassName)) {
661                                         return true;
662                                 }
663                         }
664                 }
665                 return false;
666         }
667
668         private static final String AAF_V2_0_AAF_CON_HTTP = "org.onap.aaf.cadi.aaf.v2_0.AAFConHttp";
669
670         public static Object loadAAFConnector(SecurityInfoC<HttpURLConnection> si, String aafURL) {
671                 Access access = si.access;
672                 Object aafcon = null;
673                 Class<?> aafConClass = null;
674
675                 try {
676                         if (aafURL!=null) {
677                                 String aafConnector = access.getProperty(AAF_CONNECTOR_CLASS, AAF_V2_0_AAF_CON_HTTP);
678                                 if (AAF_V2_0_AAF_CON_HTTP.equals(aafConnector)) {
679                                         aafConClass = loadClass(access, AAF_V2_0_AAF_CON_HTTP);
680                                         if (aafConClass != null) {
681                                                 for (Constructor<?> c : aafConClass.getConstructors()) {
682                                                         List<Object> lo = new ArrayList<>();
683                                                         for (Class<?> pc : c.getParameterTypes()) {
684                                                                 if (pc.equals(Access.class)) {
685                                                                         lo.add(access);
686                                                                 } else if (pc.equals(Locator.class)) {
687                                                                         lo.add(loadLocator(si, aafURL));
688                                                                 } else {
689                                                                         continue;
690                                                                 }
691                                                         }
692                                                         if (c.getParameterTypes().length != lo.size()) {
693                                                                 continue; // back to another Constructor
694                                                         } else {
695                                                                 aafcon = c.newInstance(lo.toArray());
696                                                         }
697                                                         break;
698                                                 }
699                                         }
700                                 }
701                                 if (aafcon != null) {
702                                         String mechid = logProp(access, Config.AAF_APPID, null);
703                                         String pass = access.getProperty(Config.AAF_APPPASS, null);
704                                         if (mechid != null && pass != null) {
705                                                 try {
706                                                         Method basicAuth = aafConClass.getMethod("basicAuth", String.class, String.class);
707                                                         basicAuth.invoke(aafcon, mechid, pass);
708                                                 } catch (NoSuchMethodException nsme) {
709                                                         // it's ok, don't use
710                                                 }
711                                         }
712                                 }
713                         }
714                 } catch (Exception e) {
715                         access.log(e, "AAF Connector could not be constructed with given Constructors.");
716                 }
717
718                 return aafcon;
719         }
720
721         public static Class<?> loadClass(Access access, String className) {
722                 Class<?> cls=null;
723                 try {
724                         cls = access.classLoader().loadClass(className);
725                 } catch (ClassNotFoundException cnfe) {
726                         try {
727                                 cls = access.getClass().getClassLoader().loadClass(className);
728                         } catch (ClassNotFoundException cnfe2) {
729                                 // just return null
730                         }
731                 }
732                 return cls;
733         }
734
735
736         @SuppressWarnings("unchecked")
737         public static Locator<URI> loadLocator(SecurityInfoC<HttpURLConnection> si, final String _url) throws LocatorException {
738                 Access access = si.access;
739                 Locator<URI> locator = null;
740                 if(_url==null) {
741                         access.log(Level.INIT,"No URL passed to 'loadLocator'. Disabled");
742                 } else {
743                         String url = _url, replacement;
744                         int idxAAF_LOCATE_URL;
745                         if((idxAAF_LOCATE_URL=_url.indexOf(AAF_LOCATE_URL_TAG))>0 && ((replacement=access.getProperty(AAF_LOCATE_URL, null))!=null)) {
746                                 StringBuilder sb = new StringBuilder(replacement);
747                                 if(!replacement.endsWith("/locate")) {
748                                         sb.append("/locate");
749                                 } 
750                                 sb.append(_url,idxAAF_LOCATE_URL+AAF_LOCATE_URL_TAG.length(),_url.length());
751                                 url = sb.toString();
752                         }
753         
754                         try {
755                                 Class<?> lcls = loadClass(access,AAF_LOCATOR_CLASS_DEF);
756                                 if(lcls==null) {
757                                         throw new CadiException("Need to include aaf-cadi-aaf jar for AAFLocator");
758                                 }
759                                 // First check for preloaded
760                                 try {
761                                         Method meth = lcls.getMethod("create",String.class);
762                                         locator = (Locator<URI>)meth.invoke(null,url);
763                                 } catch (Exception e) {
764                                         locator = null;
765                                 }
766                                 if(locator==null) {
767                                         URI locatorURI = new URI(url);
768                                         Constructor<?> cnst = lcls.getConstructor(new Class[] {SecurityInfoC.class,URI.class});
769                                         locator = (Locator<URI>)cnst.newInstance(new Object[] {si,locatorURI});
770                                         int port = locatorURI.getPort();
771                                         String portS = port<0?"":(":"+locatorURI.getPort());
772                                         
773                                         access.log(Level.INFO, "AAFLocator enabled using " + locatorURI.getScheme() +"://"+locatorURI.getHost() + portS);
774                                 } else {
775                                         access.log(Level.INFO, "AAFLocator enabled using preloaded " + locator.getClass().getSimpleName());
776                                 }
777                         } catch (InvocationTargetException e) {
778                                 if(e.getTargetException() instanceof LocatorException) {
779                                         throw (LocatorException)e.getTargetException();
780                                 }
781                                 access.log(Level.INIT,e.getTargetException().getMessage(),"AAFLocator for",url,"could not be created.",e);
782                         } catch (Exception e) {
783                                 access.log(Level.INIT,"AAFLocator for",url,"could not be created.",e);
784                         }
785                 }
786                 return locator;
787         }
788
789         // Set by CSP, or is hostname.
790         public static String getDefaultRealm() {
791                 return defaultRealm;
792         }
793
794 }
795