2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.config;
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;
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.Collections;
37 import java.util.List;
39 import org.onap.aaf.cadi.AbsUserCache;
40 import org.onap.aaf.cadi.Access;
41 import org.onap.aaf.cadi.Access.Level;
42 import org.onap.aaf.cadi.CachingLur;
43 import org.onap.aaf.cadi.CadiException;
44 import org.onap.aaf.cadi.Connector;
45 import org.onap.aaf.cadi.CredVal;
46 import org.onap.aaf.cadi.CredValDomain;
47 import org.onap.aaf.cadi.Locator;
48 import org.onap.aaf.cadi.LocatorException;
49 import org.onap.aaf.cadi.Lur;
50 import org.onap.aaf.cadi.PropAccess;
51 import org.onap.aaf.cadi.Symm;
52 import org.onap.aaf.cadi.TrustChecker;
53 import org.onap.aaf.cadi.lur.EpiLur;
54 import org.onap.aaf.cadi.lur.LocalLur;
55 import org.onap.aaf.cadi.lur.NullLur;
56 import org.onap.aaf.cadi.taf.HttpEpiTaf;
57 import org.onap.aaf.cadi.taf.HttpTaf;
58 import org.onap.aaf.cadi.taf.basic.BasicHttpTaf;
59 import org.onap.aaf.cadi.taf.cert.X509Taf;
60 import org.onap.aaf.cadi.taf.dos.DenialOfServiceTaf;
61 import org.onap.aaf.cadi.util.FixURIinfo;
62 import org.onap.aaf.cadi.util.Split;
65 * Create a Consistent Configuration mechanism, even when configuration styles are as vastly different as
66 * Properties vs JavaBeans vs FilterConfigs...
73 private static final String AAF_V2_0 = "org.onap.aaf.cadi.aaf.v2_0";
74 private static final String AAF_V2_0_AAFCON = AAF_V2_0+".AAFCon";
75 private static final String AAF_V2_0_AAF_LUR_PERM = AAF_V2_0+".AAFLurPerm";
76 private static final String OAUTH = "org.onap.auth.oauth";
77 private static final String OAUTH_TOKEN_MGR = OAUTH+".TokenMgr";
78 private static final String OAUTH_HTTP_TAF = OAUTH+".OAuth2HttpTaf";
79 private static final String OAUTH_DIRECT_TAF = OAUTH+".OAuthDirectTAF";
80 public static final String UTF_8 = "UTF-8";
82 // Property Names associated with configurations.
83 // As of 1.0.2, these have had the dots removed so as to be compatible with JavaBean style
84 // configurations as well as property list style.
85 public static final String HOSTNAME = "hostname";
86 public static final String CADI_PROP_FILES = "cadi_prop_files"; // Additional Properties files (separate with ;)
87 public static final String CADI_LOGLEVEL = "cadi_loglevel";
88 public static final String CADI_LOGDIR = "cadi_log_dir";
89 public static final String CADI_ETCDIR = "cadi_etc_dir";
90 public static final String CADI_LOGNAME = "cadi_logname";
91 // public static final String CADI_LOGFMT="cad_logging_format";
92 // public static final String CADI_LOGFMT_UTC="UTC";
93 // public static final String CADI_LOGFMT_ISO8601="ISO-8601";
94 public static final String CADI_KEYFILE = "cadi_keyfile";
95 public static final String CADI_KEYSTORE = "cadi_keystore";
96 public static final String CADI_KEYSTORE_PASSWORD = "cadi_keystore_password";
97 public static final String CADI_ALIAS = "cadi_alias";
98 public static final String CADI_CLIENT_ALIAS = "cadi_client_alias";
99 public static final String CADI_LOGINPAGE_URL = "cadi_loginpage_url";
100 public static final String CADI_LATITUDE = "cadi_latitude";
101 public static final String CADI_LONGITUDE = "cadi_longitude";
104 public static final String CADI_KEY_PASSWORD = "cadi_key_password";
105 public static final String CADI_TRUSTSTORE = "cadi_truststore";
106 public static final String CADI_TRUSTSTORE_PASSWORD = "cadi_truststore_password";
107 public static final String CADI_X509_ISSUERS = "cadi_x509_issuers";
108 public static final String CADI_TRUST_MASKS="cadi_trust_masks";
109 public static final String CADI_TRUST_PERM="cadi_trust_perm"; // IDs with this perm can utilize the "AS " user concept
110 public static final String CADI_PROTOCOLS = "cadi_protocols";
111 public static final String CADI_NOAUTHN = "cadi_noauthn";
112 public static final String CADI_LOC_LIST = "cadi_loc_list";
115 public static final String CADI_BATH_CONVERT = "cadi_bath_convert";
116 public static final String CADI_API_ENFORCEMENT = "cadi_api_enforcement";
117 public static final String CADI_ADD_TAFS = "cadi_add_tafs";
118 public static final String CADI_ADD_LURS = "cadi_add_lurs";
120 public static final String CADI_USER_CHAIN_TAG = "cadi_user_chain";
121 public static final String CADI_USER_CHAIN = "USER_CHAIN";
123 public static final String CADI_OAUTH2_URL="cadi_oauth2_url";
124 public static final String CADI_TOKEN_DIR = "cadi_token_dir";
126 public static final String HTTPS_PROTOCOLS = "https.protocols";
127 public static final String HTTPS_CLIENT_PROTOCOLS="jdk.tls.client.protocols";
128 public static final String HTTPS_PROTOCOLS_DEFAULT = "TLSv1.1,TLSv1.2";
129 public static final String HTTPS_CIPHER_SUITES = "https.cipherSuites";
130 public static final String HTTPS_CIPHER_SUITES_DEFAULT="TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,"
131 + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,"
132 + "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,"
133 + "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,TLS_ECDHE_RSA_WITH_RC4_128_SHA,TLS_ECDH_ECDSA_WITH_RC4_128_SHA,"
134 + "TLS_ECDH_RSA_WITH_RC4_128_SHA,TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,"
135 + "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,TLS_EMPTY_RENEGOTIATION_INFO_SCSV";
138 public static final String LOCALHOST_ALLOW = "localhost_allow";
139 public static final String LOCALHOST_DENY = "localhost_deny";
141 public static final String BASIC_REALM = "basic_realm"; // what is sent to the client
142 public static final String BASIC_WARN = "basic_warn"; // Warning of insecure channel
143 public static final String USERS = "local_users";
144 public static final String GROUPS = "local_groups";
145 public static final String WRITE_TO = "local_writeto"; // dump RBAC to local file in Tomcat Style (some apps use)
147 public static final String OAUTH_CLIENT_ID="client_id";
148 public static final String OAUTH_CLIENT_SECRET="client_secret";
150 public static final String AAF_ENV = "aaf_env";
151 public static final String AAF_ROOT_NS = "aaf_root_ns";
152 public static final String AAF_ROOT_NS_DEF = "org.osaaf.aaf";
153 public static final String AAF_ROOT_COMPANY = "aaf_root_company";
154 public static final String AAF_LOCATE_URL = "aaf_locate_url"; //URL for AAF locator
155 public static final String AAF_LOCATE_URL_TAG = "AAF_LOCATE_URL"; // Name of Above for use in Config Variables.
156 public static final String AAF_DEFAULT_API_VERSION = "2.1";
157 public static final String AAF_API_VERSION = "aaf_api_version";
158 public static final String AAF_URL = "aaf_url"; //URL for AAF... Use to trigger AAF configuration
159 public static final String AAF_LOCATOR_CLASS = "aaf_locator_class";
160 // AAF Locator Entries are ADDITIONAL entries, which also gives the Property ability
161 // to set these entries manually
162 // example: adding a K8S name like "oom"
163 // this will allow Registrations to pick up
164 // locator_ns.oom for onap's "OOM" based k8s entries, etc.
165 public static final String AAF_LOCATOR_CONTAINER="aaf_locator_container";
166 // An ID for another Container, to be used to avoid picking up the wrong internal info
167 // for another container.
168 public static final String AAF_LOCATOR_CONTAINER_ID = "aaf_locator_container_id";
169 public static final String AAF_LOCATOR_CONTAINER_NS = "aaf_locator_container_ns";
170 public static final String AAF_LOCATOR_VERSION = "aaf_locator_version";
171 public static final String AAF_LOCATOR_PROTOCOL = "aaf_locator_protocol";
172 public static final String AAF_LOCATOR_SUBPROTOCOL = "aaf_locator_subprotocol";
173 public static final String AAF_LOCATOR_APP_NS = "aaf_locator_app_ns";
174 public static final String AAF_LOCATOR_ENTRIES = "aaf_locator_entries";
175 public static final String AAF_LOCATOR_FQDN = "aaf_locator_fqdn";
176 public static final String AAF_LOCATOR_NAME = "aaf_locator_name";
177 public static final String AAF_LOCATOR_PUBLIC_PORT = "aaf_locator_public_port";
178 public static final String AAF_LOCATOR_PUBLIC_FQDN = "aaf_locator_public_fqdn";
179 public static final String AAF_LOCATOR_PUBLIC_NAME = "aaf_locator_public_name";
181 // AAF Service will write to the Audit Log if a past due AAF stored Password
182 // is being used within # of days specified.
183 public static final String AAF_CRED_WARN_DAYS="aaf_cred_warn_days";
184 public static final String AAF_CRED_WARN_DAYS_DFT="7";
186 public static final String AAF_APPID = "aaf_id";
187 public static final String AAF_APPPASS = "aaf_password";
188 public static final String AAF_LUR_CLASS = "aaf_lur_class";
189 public static final String AAF_TAF_CLASS = "aaf_taf_class";
190 public static final String AAF_CONNECTOR_CLASS = "aaf_connector_class";
191 public static final String AAF_CONN_TIMEOUT = "aaf_conn_timeout";
192 public static final String AAF_CONN_TIMEOUT_DEF = "3000";
193 public static final String AAF_CONN_IDLE_TIMEOUT = "aaf_conn_idle_timeout"; // only for Direct Jetty Access.
194 public static final String AAF_CONN_IDLE_TIMEOUT_DEF = "10000"; // only for Direct Jetty Access.
196 // Default Classes: These are for Class loading to avoid direct compile links
197 public static final String AAF_TAF_CLASS_DEF = "org.onap.aaf.cadi.aaf.v2_0.AAFTaf";
198 public static final String AAF_LOCATOR_CLASS_DEF = "org.onap.aaf.cadi.aaf.v2_0.AAFLocator";
199 public static final String CADI_OLUR_CLASS_DEF = "org.onap.aaf.cadi.olur.OLur";
200 public static final String CADI_OBASIC_HTTP_TAF_DEF = "org.onap.aaf.cadi.obasic.OBasicHttpTaf";
201 public static final String CADI_AAF_CON_DEF = "org.onap.aaf.cadi.aaf.v2_0.AAFCon";
203 public static final String AAF_CALL_TIMEOUT = "aaf_timeout";
204 public static final String AAF_CALL_TIMEOUT_DEF = "5000";
205 public static final String AAF_USER_EXPIRES = "aaf_user_expires";
206 public static final String AAF_USER_EXPIRES_DEF = "600000"; // Default is 10 mins
207 public static final String AAF_CLEAN_INTERVAL = "aaf_clean_interval";
208 public static final String AAF_CLEAN_INTERVAL_DEF = "30000"; // Default is 30 seconds
209 public static final String AAF_REFRESH_TRIGGER_COUNT = "aaf_refresh_trigger_count";
210 public static final String AAF_REFRESH_TRIGGER_COUNT_DEF = "3"; // Default is 10 mins
212 public static final String AAF_HIGH_COUNT = "aaf_high_count";
213 public static final String AAF_HIGH_COUNT_DEF = "1000"; // Default is 1000 entries
214 public static final String AAF_PERM_MAP = "aaf_perm_map";
215 // public static final String AAF_COMPONENT = "aaf_component";
216 public static final String AAF_CERT_IDS = "aaf_cert_ids";
217 public static final String AAF_DEBUG_IDS = "aaf_debug_ids"; // comma delimited
218 public static final String AAF_DATA_DIR = "aaf_data_dir"; // AAF processes and Components only.
219 public static final String AAF_RELEASE = "aaf_release";
221 public static final String AAF_URL_GUI="aaf_url_gui";
222 public static final String AAF_URL_FS="aaf_url_fs";
223 public static final String AAF_URL_CM = "aaf_url_cm";
224 public static final String AAF_URL_HELLO = "aaf_url_hello";
225 public static final String CM_TRUSTED_CAS = "cm_trusted_cas";
227 public static final String PATHFILTER_URLPATTERN = "pathfilter_urlpattern";
228 public static final String PATHFILTER_STACK = "pathfilter_stack";
229 public static final String PATHFILTER_NS = "pathfilter_ns";
230 public static final String PATHFILTER_NOT_AUTHORIZED_MSG = "pathfilter_not_authorized_msg";
232 // This one should go unpublic
233 public static final String AAF_DEFAULT_REALM = "aaf_default_realm";
234 private static String defaultRealm="none";
236 public static final String AAF_DOMAIN_SUPPORT = "aaf_domain_support";
237 public static final String AAF_DOMAIN_SUPPORT_DEF = ".com:.org";
240 public static final String AAF_OAUTH2_TOKEN_URL = "aaf_oauth2_token_url";
241 public static final String AAF_OAUTH2_INTROSPECT_URL = "aaf_oauth2_introspect_url";
242 public static final String AAF_ALT_OAUTH2_TOKEN_URL = "aaf_alt_oauth2_token_url";
243 public static final String AAF_ALT_OAUTH2_INTROSPECT_URL = "aaf_alt_oauth2_introspect_url";
244 public static final String AAF_ALT_OAUTH2_DOMAIN = "aaf_alt_oauth2_domain";
245 public static final String AAF_ALT_CLIENT_ID = "aaf_alt_oauth2_client_id";
246 public static final String AAF_ALT_CLIENT_SECRET = "aaf_alt_oauth2_client_secret";
247 public static final String AAF_OAUTH2_HELLO_URL = "aaf_oauth2_hello_url";
249 private static final String AAF_V2_0_AAF_CON_HTTP = "org.onap.aaf.cadi.aaf.v2_0.AAFConHttp";
252 public static void setDefaultRealm(Access access) {
254 defaultRealm = logProp(access,Config.AAF_DEFAULT_REALM,
255 logProp(access,Config.BASIC_REALM,
256 logProp(access,HOSTNAME,InetAddress.getLocalHost().getHostName())
259 } catch (UnknownHostException e) {
260 access.log(Level.INIT, "Unable to determine Hostname",e);
264 public static HttpTaf configHttpTaf(Connector con, SecurityInfoC<HttpURLConnection> si, TrustChecker tc, CredVal up, Lur lur, Object ... additionalTafLurs) throws CadiException, LocatorException {
265 Access access = si.access;
266 RegistrationPropHolder rph;
268 rph = new RegistrationPropHolder(access, 0);
269 } catch (UnknownHostException e2) {
270 throw new CadiException(e2);
272 /////////////////////////////////////////////////////
273 // Setup AAFCon for any following
274 /////////////////////////////////////////////////////
275 Class<?> aafConClass = loadClass(access,CADI_AAF_CON_DEF);
276 Object aafcon = null;
277 if (con!=null && aafConClass!=null && aafConClass.isAssignableFrom(con.getClass())) {
279 } else if (lur != null) {
282 f = lur.getClass().getField("aaf");
284 } catch (Exception e) {
285 access.log(Level.INIT, e);
289 boolean hasDirectAAF = hasDirect("DirectAAFLur",additionalTafLurs);
290 // IMPORTANT! Don't attempt to load AAF Connector if there is no AAF URL
291 String aafURL = logProp(rph, AAF_URL,null);
292 if (!hasDirectAAF && aafcon==null && aafURL!=null) {
293 aafcon = loadAAFConnector(si, aafURL);
297 // Setup Host, in case Network reports an unusable Hostname (i.e. VTiers, VPNs, etc)
298 String hostname = logProp(access, HOSTNAME,null);
299 if (hostname==null) {
301 hostname = InetAddress.getLocalHost().getHostName();
302 } catch (UnknownHostException e1) {
303 throw new CadiException("Unable to determine Hostname",e1);
307 access.log(Level.INIT, "Hostname set to",hostname);
308 // Get appropriate TAFs
309 ArrayList<Priori<HttpTaf>> htlist = new ArrayList<>();
311 /////////////////////////////////////////////////////
312 // Add a Denial of Service TAF
313 // Note: how IPs and IDs are added are up to service type.
314 // They call "DenialOfServiceTaf.denyIP(String) or denyID(String)
315 /////////////////////////////////////////////////////
316 htlist.add(new Priori<HttpTaf>(new DenialOfServiceTaf(access),0));
318 /////////////////////////////////////////////////////
319 // Configure Client Cert TAF
320 /////////////////////////////////////////////////////
321 X509Taf x509TAF = null;
322 String truststore = logProp(access, CADI_TRUSTSTORE,null);
323 if (truststore!=null) {
324 String truststorePwd = access.getProperty(CADI_TRUSTSTORE_PASSWORD,null);
325 if (truststorePwd!=null) {
326 if (truststorePwd.startsWith(Symm.ENC)) {
328 access.decrypt(truststorePwd,false);
329 } catch (IOException e) {
330 throw new CadiException(CADI_TRUSTSTORE_PASSWORD + " cannot be decrypted",e);
334 x509TAF=new X509Taf(access,lur);
335 htlist.add(new Priori<HttpTaf>(x509TAF,10));
336 access.log(Level.INIT,"Certificate Authorization enabled");
337 } catch (SecurityException | IllegalArgumentException e) {
338 access.log(Level.INIT,"AAFListedCertIdentity cannot be instantiated. Certificate Authorization is now disabled",e);
339 } catch (CertificateException e) {
340 access.log(Level.INIT,"Certificate Authorization failed, it is disabled",e);
341 } catch (NoSuchAlgorithmException e) {
342 access.log(Level.INIT,"Certificate Authorization failed, wrong Security Algorithm",e);
346 access.log(Level.INIT,"Certificate Authorization not enabled");
349 /////////////////////////////////////////////////////
350 // Configure Basic Auth (local content)
351 /////////////////////////////////////////////////////
352 boolean hasOAuthDirectTAF = hasDirect("DirectOAuthTAF", additionalTafLurs);
353 String basicRealm = logProp(access, BASIC_REALM,null);
354 String aafCleanup = logProp(access, AAF_USER_EXPIRES,AAF_USER_EXPIRES_DEF); // Default is 10 mins
355 long userExp = Long.parseLong(aafCleanup);
356 boolean basicWarn = "TRUE".equals(access.getProperty(BASIC_WARN,"FALSE"));
360 if (!hasOAuthDirectTAF) {
361 if (basicRealm!=null) {
362 @SuppressWarnings("unchecked")
363 Class<HttpTaf> obasicCls = (Class<HttpTaf>)loadClass(access,CADI_OBASIC_HTTP_TAF_DEF);
364 if (obasicCls!=null) {
366 String tokenurl = logProp(rph,Config.AAF_OAUTH2_TOKEN_URL, null);
367 String introspecturl = logProp(rph,Config.AAF_OAUTH2_INTROSPECT_URL, null);
368 if (tokenurl==null || introspecturl==null) {
369 access.log(Level.INIT,"Both tokenurl and introspecturl are required. Oauth Authorization is disabled.");
371 Constructor<HttpTaf> obasicConst = obasicCls.getConstructor(PropAccess.class,String.class, String.class, String.class);
372 htlist.add(new Priori<HttpTaf>(obasicConst.newInstance(access,basicRealm,tokenurl,introspecturl),20));
373 access.log(Level.INIT,"Oauth supported Basic Authorization is enabled");
374 } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
375 access.log(Level.INIT, e);
377 } else if (up!=null) {
378 access.log(Level.INIT,"Basic Authorization is enabled using realm",basicRealm);
379 // Allow warning about insecure channel to be turned off
381 access.log(Level.INIT, "WARNING! The basicWarn property has been set to false.",
382 " There will be no additional warning if Basic Auth is used on an insecure channel");
384 BasicHttpTaf bht = new BasicHttpTaf(access, up, basicRealm, userExp, basicWarn);
385 for (Object o : additionalTafLurs) {
386 if (o instanceof CredValDomain) {
387 bht.add((CredValDomain)o);
393 htlist.add(new Priori<HttpTaf>(bht,20));
394 access.log(Level.INIT,"Basic Authorization is enabled");
397 access.log(Level.INIT,"Local Basic Authorization is disabled. Enable by setting basicRealm=<appropriate realm, i.e. my.att.com>");
400 /////////////////////////////////////////////////////
401 // Configure AAF Driven Basic Auth
402 /////////////////////////////////////////////////////
404 access.log(Level.INIT,"AAF Connection (AAFcon) is null. Cannot create an AAF TAF");
405 } else if (aafURL==null) {
406 access.log(Level.INIT,"No AAF URL in properties, Cannot create an AAF TAF");
407 } else {// There's an AAF_URL... try to configure an AAF
408 String aafTafClassName = logProp(access, AAF_TAF_CLASS,AAF_TAF_CLASS_DEF);
409 // Only 2.0 available at this time
410 if (AAF_TAF_CLASS_DEF.equals(aafTafClassName)) {
412 Class<?> aafTafClass = loadClass(access,aafTafClassName);
413 if (aafTafClass!=null) {
414 Constructor<?> cstr = aafTafClass.getConstructor(Connector.class,boolean.class,AbsUserCache.class);
416 if (lur instanceof AbsUserCache) {
417 aaftaf = (HttpTaf)cstr.newInstance(aafcon,basicWarn,lur);
419 cstr = aafTafClass.getConstructor(Connector.class,boolean.class);
421 aaftaf = (HttpTaf)cstr.newInstance(aafcon,basicWarn);
425 access.log(Level.INIT,"ERROR! AAF TAF Failed construction. NOT Configured");
427 access.log(Level.INIT,"AAF TAF Configured to ",aafURL);
428 // Note: will add later, after all others configured
432 access.log(Level.INIT, "There is no AAF TAF class available: %s. AAF TAF not configured.",aafTafClassName);
434 } catch (Exception e) {
435 access.log(Level.INIT,"ERROR! AAF TAF Failed construction. NOT Configured",e);
441 /////////////////////////////////////////////////////
442 // Configure OAuth TAF
443 /////////////////////////////////////////////////////
444 if (!hasOAuthDirectTAF) {
445 String oauthTokenUrl = logProp(rph,Config.AAF_OAUTH2_TOKEN_URL,null);
448 oadtClss = Class.forName(OAUTH_DIRECT_TAF);
449 } catch (ClassNotFoundException e1) {
451 access.log(Level.DEBUG, e1);
453 if (additionalTafLurs!=null && additionalTafLurs.length>0 && (oadtClss!=null && additionalTafLurs[0].getClass().isAssignableFrom(oadtClss))) {
454 htlist.add(new Priori<HttpTaf>((HttpTaf)additionalTafLurs[0],30));
455 String[] array= new String[additionalTafLurs.length-1];
456 if (array.length>0) {
457 System.arraycopy(htlist, 1, array, 0, array.length);
459 additionalTafLurs = array;
460 access.log(Level.INIT,"OAuth2 Direct is enabled");
461 } else if (oauthTokenUrl!=null) {
462 String oauthIntrospectUrl = logProp(rph,Config.AAF_OAUTH2_INTROSPECT_URL,null);
463 @SuppressWarnings("unchecked")
464 Class<HttpTaf> oaTCls = (Class<HttpTaf>)loadClass(access,OAUTH_HTTP_TAF);
466 Class<?> oaTTmgrCls = loadClass(access, OAUTH_TOKEN_MGR);
467 if (oaTTmgrCls!=null) {
469 Method oaTTmgrGI = oaTTmgrCls.getMethod("getInstance",PropAccess.class,String.class,String.class);
470 Object oaTTmgr = oaTTmgrGI.invoke(null /*this is static method*/,access,oauthTokenUrl,oauthIntrospectUrl);
471 Constructor<HttpTaf> oaTConst = oaTCls.getConstructor(Access.class,oaTTmgrCls);
472 htlist.add(new Priori<HttpTaf>(oaTConst.newInstance(access,oaTTmgr),30));
473 access.log(Level.INIT,"OAuth2 TAF is enabled");
474 } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
475 access.log(Level.INIT,"OAuth2HttpTaf cannot be instantiated. OAuth2 is disabled",e);
480 access.log(Level.INIT,"OAuth TAF is not configured");
484 /////////////////////////////////////////////////////
485 // Adding BasicAuth (AAF) last, after other primary Cookie Based
486 // Needs to be before Cert... see below
487 /////////////////////////////////////////////////////
489 htlist.add(new Priori<HttpTaf>(aaftaf,40));
493 /////////////////////////////////////////////////////
494 // Any Additional Tafs passed in Constructor
495 /////////////////////////////////////////////////////
496 if (additionalTafLurs!=null) {
498 for (Object additional : additionalTafLurs) {
499 if (additional instanceof BasicHttpTaf) {
500 BasicHttpTaf ht = (BasicHttpTaf)additional;
501 for (Object cv : additionalTafLurs) {
502 if (cv instanceof CredValDomain) {
503 ht.add((CredValDomain)cv);
504 access.printf(Level.INIT,"%s Authentication is enabled",cv);
507 htlist.add(new Priori<HttpTaf>(ht,50+i++));
508 } else if (additional instanceof HttpTaf) {
509 HttpTaf ht = (HttpTaf)additional;
510 htlist.add(new Priori<HttpTaf>(ht,50+i++));
511 access.printf(Level.INIT,"%s Authentication is enabled",additional.getClass().getSimpleName());
512 } else if (hasOAuthDirectTAF) {
515 daupCls = Class.forName("org.onap.aaf.auth.direct.DirectAAFUserPass");
516 } catch (ClassNotFoundException e) {
518 access.log(Level.INIT, e);
520 if (daupCls != null && additional.getClass().isAssignableFrom(daupCls)) {
521 htlist.add(new Priori<HttpTaf>(new BasicHttpTaf(access, (CredVal)additional , basicRealm, userExp, basicWarn),50+i++));
522 access.printf(Level.INIT,"Direct BasicAuth Authentication is enabled",additional.getClass().getSimpleName());
528 // Add BasicAuth, if any, to x509Taf
530 for ( Priori<HttpTaf> ht : htlist) {
531 if (ht.t instanceof BasicHttpTaf) {
532 x509TAF.add((BasicHttpTaf)ht.t);
537 /////////////////////////////////////////////////////
538 // Additional TAFs by Plugin
539 /////////////////////////////////////////////////////
540 Priori.add(access, CADI_ADD_TAFS, htlist);
542 /////////////////////////////////////////////////////
543 // Create EpiTaf from configured TAFs
544 /////////////////////////////////////////////////////
545 if (htlist.size()==1) {
546 // just return the one
547 taf = htlist.get(0).t;
549 Collections.sort(htlist);
550 HttpTaf[] htarray = new HttpTaf[htlist.size()];
552 StringBuilder sb = new StringBuilder("Tafs processed in this order:\n");
553 for(Priori<HttpTaf> pht : htlist) {
554 htarray[++i] = pht.t;
556 sb.append(pht.t.getClass().getName());
558 sb.append(pht.priority);
561 access.log(Level.INIT, sb);
563 Locator<URI> locator = loadLocator(si, logProp(rph, AAF_LOCATE_URL, null));
565 taf = new HttpEpiTaf(access,locator, tc, htarray); // ok to pass locator == null
566 String level = logProp(access, CADI_LOGLEVEL, null);
568 access.setLogLevel(Level.valueOf(level));
575 public static String logProp(RegistrationPropHolder rph, String tag, String def) {
576 String rv = rph.access().getProperty(tag, def);
578 rph.access().log(Level.INIT,tag,"is not explicitly set");
580 rv = rph.replacements("Config.logProp",rv, null, null);
581 rph.access().log(Level.INIT,tag,"is set to",rv);
587 public static String logProp(Access access,String tag, String def) {
588 String rv = access.getProperty(tag, def);
590 access.log(Level.INIT,tag,"is not explicitly set");
592 access.log(Level.INIT,tag,"is set to",rv);
597 public static Lur configLur(SecurityInfoC<HttpURLConnection> si, Connector con, Object ... additionalTafLurs) throws CadiException {
598 Access access = si.access;
599 RegistrationPropHolder rph;
601 rph = new RegistrationPropHolder(access, 0);
602 } catch (UnknownHostException e2) {
603 throw new CadiException(e2);
606 List<Priori<Lur>> lurs = new ArrayList<>();
608 /////////////////////////////////////////////////////
609 // Configure a Local Property Based RBAC/LUR
610 /////////////////////////////////////////////////////
612 String users = access.getProperty(USERS,null);
613 String groups = access.getProperty(GROUPS,null);
615 if (groups!=null || users!=null) {
616 LocalLur ll = new LocalLur(access, users, groups); // note b64==null is ok.. just means no encryption.
617 lurs.add(new Priori<Lur>(ll,10));
619 String writeto = access.getProperty(WRITE_TO,null);
621 String msg = UsersDump.updateUsers(writeto, ll);
623 access.log(Level.INIT,"ERROR! Error Updating ",writeto,"with roles and users:",msg);
627 } catch (IOException e) {
628 throw new CadiException(e);
631 /////////////////////////////////////////////////////
632 // Configure the OAuth Lur (if any)
633 /////////////////////////////////////////////////////
634 String tokenUrl = logProp(rph,AAF_OAUTH2_TOKEN_URL, null);
635 String introspectUrl = logProp(rph,AAF_OAUTH2_INTROSPECT_URL, null);
636 if (tokenUrl!=null && introspectUrl !=null) {
638 Class<?> olurCls = loadClass(access, CADI_OLUR_CLASS_DEF);
640 Constructor<?> olurCnst = olurCls.getConstructor(PropAccess.class,String.class,String.class);
641 Lur olur = (Lur)olurCnst.newInstance(access,tokenUrl,introspectUrl);
642 lurs.add(new Priori<Lur>(olur,20));
643 access.log(Level.INIT, "OAuth2 LUR enabled");
645 access.log(Level.INIT,"AAF/OAuth LUR plugin is not available.");
647 } catch (NoSuchMethodException| SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
648 String msg = e.getMessage();
649 if (msg==null && e.getCause()!=null) {
650 msg = e.getCause().getMessage();
652 access.log(Level.INIT,"AAF/OAuth LUR is not instantiated.",msg,e);
655 access.log(Level.INIT, "OAuth2 Lur disabled");
658 if (con!=null) { // try to reutilize connector
659 lurs.add(new Priori<Lur>(con.newLur(),30));
661 /////////////////////////////////////////////////////
662 // Configure the AAF Lur (if any)
663 /////////////////////////////////////////////////////
664 String aafURL = logProp(rph,AAF_URL,null); // Trigger Property
665 String aafEnv = access.getProperty(AAF_ENV,null);
666 if (aafEnv == null && aafURL!=null && access instanceof PropAccess) { // set AAF_ENV from AAF_URL
667 int ec = aafURL.indexOf("envContext=");
669 ec += 11; // length of envContext=
670 int slash = aafURL.indexOf('/', ec);
672 aafEnv = aafURL.substring(ec, slash);
673 ((PropAccess)access).setProperty(AAF_ENV, aafEnv);
674 access.printf(Level.INIT, "Setting aafEnv to %s from aaf_url value",aafEnv);
679 // Don't configure AAF if it is using DirectAccess
680 if (!hasDirect("DirectAAFLur",additionalTafLurs)) {
682 access.log(Level.INIT,"No AAF LUR properties, AAF will not be loaded");
683 } else {// There's an AAF_URL... try to configure an AAF
684 String aafLurClassStr = logProp(access,AAF_LUR_CLASS,AAF_V2_0_AAF_LUR_PERM);
685 ////////////AAF Lur 2.0 /////////////
686 if (aafLurClassStr!=null && aafLurClassStr.startsWith(AAF_V2_0)) {
688 Object aafcon = loadAAFConnector(si, aafURL);
690 access.log(Level.INIT,"AAF LUR class,",aafLurClassStr,"cannot be constructed without valid AAFCon object.");
692 Class<?> aafAbsAAFCon = loadClass(access, AAF_V2_0_AAFCON);
693 if (aafAbsAAFCon!=null) {
694 Method mNewLur = aafAbsAAFCon.getMethod("newLur");
695 Object aaflur = mNewLur.invoke(aafcon);
698 access.log(Level.INIT,"ERROR! AAF LUR Failed construction. NOT Configured");
700 access.log(Level.INIT,"AAF LUR Configured to ",aafURL);
701 lurs.add(new Priori<Lur>((Lur)aaflur,40));
702 String debugIDs = logProp(access,Config.AAF_DEBUG_IDS, null);
703 if (debugIDs !=null && aaflur instanceof CachingLur) {
704 ((CachingLur<?>)aaflur).setDebug(debugIDs);
709 } catch (Exception e) {
710 access.log(e,"AAF LUR class,",aafLurClassStr,"could not be constructed with given Constructors.");
717 /////////////////////////////////////////////////////
718 // Any Additional passed in Constructor
719 /////////////////////////////////////////////////////
720 if (additionalTafLurs!=null) {
722 for (Object additional : additionalTafLurs) {
723 if (additional instanceof Lur) {
724 lurs.add(new Priori<Lur>((Lur)additional,50+i++));
725 access.log(Level.INIT, additional);
730 /////////////////////////////////////////////////////
731 // Additional LURs by Plugin
732 /////////////////////////////////////////////////////
733 Priori.add(access, CADI_ADD_LURS, lurs);
735 /////////////////////////////////////////////////////
736 // Return a Lur based on how many there are...
737 /////////////////////////////////////////////////////
738 switch(lurs.size()) {
740 access.log(Level.INIT,"WARNING! No CADI LURs configured");
741 // Return a NULL Lur that does nothing.
742 return new NullLur();
744 return lurs.get(0).t; // Only one, just return it, save processing
746 // Multiple Lurs, use EpiLUR to handle
747 Collections.sort(lurs);
748 Lur[] la = new Lur[lurs.size()];
750 StringBuilder sb = new StringBuilder("Lurs processed in this order:\n");
751 for(Priori<Lur> pht : lurs) {
754 sb.append(pht.t.getClass().getName());
756 sb.append(pht.priority);
759 access.log(Level.INIT, sb);
760 return new EpiLur(la);
764 private static boolean hasDirect(String simpleClassName, Object[] additionalTafLurs) {
765 if (additionalTafLurs!=null) {
766 for (Object tf : additionalTafLurs) {
767 if (tf.getClass().getSimpleName().equals(simpleClassName)) {
775 public static Object loadAAFConnector(SecurityInfoC<HttpURLConnection> si, String aafURL) {
776 Access access = si.access;
777 Object aafcon = null;
778 Class<?> aafConClass = null;
782 String aafConnector = access.getProperty(AAF_CONNECTOR_CLASS, AAF_V2_0_AAF_CON_HTTP);
783 if (AAF_V2_0_AAF_CON_HTTP.equals(aafConnector)) {
784 aafConClass = loadClass(access, AAF_V2_0_AAF_CON_HTTP);
785 if (aafConClass != null) {
786 for (Constructor<?> c : aafConClass.getConstructors()) {
787 List<Object> lo = new ArrayList<>();
788 for (Class<?> pc : c.getParameterTypes()) {
789 if (pc.equals(Access.class)) {
791 } else if (pc.equals(Locator.class)) {
792 lo.add(loadLocator(si, aafURL));
795 if (c.getParameterTypes().length != lo.size()) {
796 continue; // back to another Constructor
798 aafcon = c.newInstance(lo.toArray());
804 if (aafcon != null) {
805 String mechid = logProp(access, Config.AAF_APPID, null);
806 String pass = access.getProperty(Config.AAF_APPPASS, null);
807 if (mechid != null && pass != null) {
809 Method basicAuth = aafConClass.getMethod("basicAuth", String.class, String.class);
810 basicAuth.invoke(aafcon, mechid, pass);
811 } catch (NoSuchMethodException nsme) {
812 access.log(Level.NONE, nsme);
813 // it's ok, don't use
818 } catch (Exception e) {
819 access.log(e, "AAF Connector could not be constructed with given Constructors.");
825 public static Class<?> loadClass(Access access, String className) {
828 cls = access.classLoader().loadClass(className);
829 } catch (ClassNotFoundException cnfe) {
830 access.log(Level.NONE, cnfe);
832 cls = access.getClass().getClassLoader().loadClass(className);
833 } catch (ClassNotFoundException cnfe2) {
834 access.log(Level.NONE, cnfe2);
841 @SuppressWarnings("unchecked")
842 public static Locator<URI> loadLocator(SecurityInfoC<HttpURLConnection> si, final String _url) throws LocatorException {
843 Access access = si.access;
844 Locator<URI> locator = null;
846 access.log(Level.INIT,"No URL passed to 'loadLocator'. Disabled");
848 // String url = _url.replace("/AAF_NS.", "/%C%CID%AAF_NS.");
849 // String root_ns = access.getProperty(Config.AAF_ROOT_NS, null);
851 RegistrationPropHolder rph;
853 rph = new RegistrationPropHolder(access, 0);
854 url = rph.replacements("Config.loadLocator",_url, null, null);
855 access.printf(Level.INFO, "loadLocator URL is %s",url);
856 } catch (UnknownHostException | CadiException e1) {
857 throw new LocatorException(e1);
861 Class<?> lcls = loadClass(access,AAF_LOCATOR_CLASS_DEF);
863 throw new CadiException("Need to include aaf-cadi-aaf jar for AAFLocator");
865 // First check for preloaded
867 Method meth = lcls.getMethod("create",String.class);
868 locator = (Locator<URI>)meth.invoke(null,url);
869 } catch (Exception e) {
870 access.log(Level.DEBUG, "(Not fatal) Cannot load by create(String)", e);
873 URI locatorURI = new URI(url);
874 FixURIinfo fui = new FixURIinfo(locatorURI);
875 Constructor<?> cnst = lcls.getConstructor(SecurityInfoC.class,URI.class);
876 locator = (Locator<URI>)cnst.newInstance(new Object[] {si,locatorURI});
877 int port = fui.getPort();
878 String portS = port<0?"":(":"+port);
880 access.log(Level.INFO, "AAFLocator enabled using " + locatorURI.getScheme() +"://"+fui.getHost() + portS);
882 access.log(Level.INFO, "AAFLocator enabled using preloaded " + locator.getClass().getSimpleName());
884 } catch (InvocationTargetException e) {
885 if (e.getTargetException() instanceof LocatorException) {
886 throw (LocatorException)e.getTargetException();
888 access.log(Level.INIT,e.getTargetException().getMessage(),"AAFLocator for",url,"could not be created.",e);
889 } catch (Exception e) {
890 access.log(Level.INIT,"AAFLocator for",url,"could not be created.",e);
896 // Set by CSP, or is hostname.
897 public static String getDefaultRealm() {
901 private static class Priori<T> implements Comparable<Priori<T>> {
903 public final int priority;
905 public Priori(final T t, final int priority) {
907 this.priority = priority;
911 public int compareTo(Priori<T> o) {
912 if(priority==o.priority) {
914 } else if(priority<o.priority) {
920 public static<T> void add(Access access, final String tag, List<Priori<T>> list) {
921 String plugins = access.getProperty(tag, null);
923 for(String tafs : Split.splitTrim(';', plugins)) {
924 String[] pluginArray = Split.splitTrim(',', tafs);
927 switch(pluginArray.length) {
934 clssn = pluginArray[0];
936 priority = Integer.parseInt(pluginArray[1]);
937 } catch (NumberFormatException nfe) {
938 access.printf(Level.ERROR, "%s format is <classname>,priority[;...]\n",CADI_ADD_TAFS);
943 Class<?> cls = loadClass(access, clssn);
946 @SuppressWarnings("unchecked")
947 Constructor<T> cnst = (Constructor<T>)cls.getConstructor(Access.class);
949 list.add(new Priori<T>(cnst.newInstance(access),priority));
950 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
951 access.printf(Level.ERROR, "%s cannot be constructed with Access.\n",clssn);
953 } catch (NoSuchMethodException | SecurityException e) {
954 access.printf(Level.ERROR, "%s needs a Constructor taking Access as sole param.\n",clssn);