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.aaf.v2_0;
25 import java.net.UnknownHostException;
27 import java.util.concurrent.ConcurrentHashMap;
29 import org.onap.aaf.cadi.AbsUserCache;
30 import org.onap.aaf.cadi.Access;
31 import org.onap.aaf.cadi.Access.Level;
32 import org.onap.aaf.cadi.CadiException;
33 import org.onap.aaf.cadi.CadiWrap;
34 import org.onap.aaf.cadi.Connector;
35 import org.onap.aaf.cadi.Locator;
36 import org.onap.aaf.cadi.LocatorException;
37 import org.onap.aaf.cadi.Lur;
38 import org.onap.aaf.cadi.PropAccess;
39 import org.onap.aaf.cadi.SecuritySetter;
40 import org.onap.aaf.cadi.aaf.AAFPermission;
41 import org.onap.aaf.cadi.aaf.marshal.CertsMarshal;
42 import org.onap.aaf.cadi.client.Future;
43 import org.onap.aaf.cadi.client.Rcli;
44 import org.onap.aaf.cadi.client.Retryable;
45 import org.onap.aaf.cadi.config.Config;
46 import org.onap.aaf.cadi.config.RegistrationPropHolder;
47 import org.onap.aaf.cadi.config.SecurityInfoC;
48 import org.onap.aaf.cadi.lur.EpiLur;
49 import org.onap.aaf.cadi.principal.BasicPrincipal;
50 import org.onap.aaf.cadi.principal.TaggedPrincipal;
51 import org.onap.aaf.cadi.util.FQI;
52 import org.onap.aaf.cadi.util.Vars;
53 import org.onap.aaf.misc.env.APIException;
54 import org.onap.aaf.misc.env.Data.TYPE;
55 import org.onap.aaf.misc.rosetta.env.RosettaDF;
56 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
58 import aaf.v2_0.Certs;
59 import aaf.v2_0.CredRequest;
60 import aaf.v2_0.Error;
61 import aaf.v2_0.Perms;
62 import aaf.v2_0.Users;
64 public abstract class AAFCon<CLIENT> implements Connector {
65 final public Access access;
67 final public int timeout, cleanInterval, connTimeout;
68 final public int highCount, userExpires, usageRefreshTriggerCount;
69 private Map<String,Rcli<CLIENT>> clients = new ConcurrentHashMap<>();
70 final public RosettaDF<Perms> permsDF;
71 final public RosettaDF<Certs> certsDF;
72 final public RosettaDF<Users> usersDF;
73 final public RosettaDF<CredRequest> credReqDF;
74 final public RosettaDF<Error> errDF;
76 public final String app;
77 protected final String apiVersion;
78 protected SecurityInfoC<CLIENT> si;
80 private AAFLurPerm lur;
82 final public RosettaEnv env;
83 protected AAFCon(AAFCon<CLIENT> copy) {
85 apiVersion = access.getProperty(Config.AAF_API_VERSION, Config.AAF_DEFAULT_API_VERSION);
86 timeout = copy.timeout;
87 cleanInterval = copy.cleanInterval;
88 connTimeout = copy.connTimeout;
89 highCount = copy.highCount;
90 userExpires = copy.userExpires;
91 usageRefreshTriggerCount = copy.usageRefreshTriggerCount;
92 permsDF = copy.permsDF;
93 certsDF = copy.certsDF;
94 usersDF = copy.usersDF;
95 credReqDF = copy.credReqDF;
102 protected AAFCon(Access access, String tag, SecurityInfoC<CLIENT> si) throws CadiException{
103 apiVersion = access.getProperty(Config.AAF_API_VERSION, Config.AAF_DEFAULT_API_VERSION);
105 throw new CadiException("AAFCon cannot be constructed without a property tag or URL");
107 String str = access.getProperty(tag,null);
109 if (tag.contains("://")) { // assume a URL
112 throw new CadiException("A URL or " + tag + " property is required.");
116 RegistrationPropHolder rph = new RegistrationPropHolder(access, 0);
117 str = rph.replacements("AAFCon",str, null,null);
118 } catch (UnknownHostException e) {
119 throw new CadiException(e);
121 access.printf(Level.INFO, "AAFCon has URL of %s",str);
125 this.access = access;
127 if (si.defSS.getID().equals(SecurityInfoC.DEF_ID)) { // it's the Preliminary SS, try to get a better one
128 String mechid = access.getProperty(Config.AAF_APPID, null);
130 mechid=access.getProperty(Config.OAUTH_CLIENT_ID,null);
132 String alias = access.getProperty(Config.CADI_ALIAS, null);
134 si.defSS=x509Alias(alias);
138 String encpass = access.getProperty(Config.AAF_APPPASS, null);
140 encpass = access.getProperty(Config.OAUTH_CLIENT_SECRET,null);
145 access.printf(Access.Level.WARN,"%s, %s or %s required before use.", Config.CADI_ALIAS, Config.AAF_APPID, Config.OAUTH_CLIENT_ID);
150 si.defSS=basicAuth(mechid, encpass);
153 si.defSS=new SecuritySetter<CLIENT>() {
156 public String getID() {
161 public void setSecurity(CLIENT client) throws CadiException {
162 throw new CadiException("AAFCon has not been initialized with Credentials (SecuritySetter)");
166 public int setLastResponse(int respCode) {
176 timeout = Integer.parseInt(access.getProperty(Config.AAF_CALL_TIMEOUT, Config.AAF_CALL_TIMEOUT_DEF));
177 cleanInterval = Integer.parseInt(access.getProperty(Config.AAF_CLEAN_INTERVAL, Config.AAF_CLEAN_INTERVAL_DEF));
178 highCount = Integer.parseInt(access.getProperty(Config.AAF_HIGH_COUNT, Config.AAF_HIGH_COUNT_DEF).trim());
179 connTimeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF).trim());
180 userExpires = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim());
181 usageRefreshTriggerCount = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim())-1; // zero based
183 app=FQI.reverseDomain(si.defSS.getID());
184 realm = access.getProperty(Config.AAF_DEFAULT_REALM, Config.getDefaultRealm());
186 env = new RosettaEnv();
187 permsDF = env.newDataFactory(Perms.class);
188 usersDF = env.newDataFactory(Users.class);
189 certsDF = env.newDataFactory(Certs.class);
190 certsDF.rootMarshal(new CertsMarshal()); // Speedier Marshaling
191 credReqDF = env.newDataFactory(CredRequest.class);
192 errDF = env.newDataFactory(Error.class);
193 } catch (APIException e) {
194 throw new CadiException("AAFCon cannot be configured",e);
197 protected abstract URI initURI();
198 protected abstract void setInitURI(String uriString) throws CadiException;
200 public final String aafVersion() {
205 * Use this call to get the appropriate client based on configuration (HTTP, future)
206 * using default AAF API Version
210 * @throws CadiException
212 public Rcli<CLIENT> client() throws CadiException {
213 return client(apiVersion);
217 * Use this call to get the appropriate client based on configuration (HTTP, future)
221 * @throws CadiException
223 public Rcli<CLIENT> client(final String apiVersion) throws CadiException {
224 Rcli<CLIENT> client = clients.get(apiVersion);
226 client = rclient(initURI(),si.defSS);
227 client.apiVersion(apiVersion)
228 .readTimeout(connTimeout);
229 clients.put(apiVersion, client);
234 public Rcli<CLIENT> client(URI uri) throws CadiException {
235 return rclient(uri,si.defSS).readTimeout(connTimeout);
239 * Use this API when you have permission to have your call act as the end client's ID.
241 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
246 * @throws CadiException
248 public Rcli<CLIENT> clientAs(TaggedPrincipal p) throws CadiException {
249 return clientAs(apiVersion,p);
253 * Use this API when you have permission to have your call act as the end client's ID.
255 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
260 * @throws CadiException
262 public Rcli<CLIENT> clientAs(String apiVersion, TaggedPrincipal p) throws CadiException {
263 Rcli<CLIENT> cl = client(apiVersion);
264 return cl.forUser(transferSS(p));
268 public RosettaEnv env() {
273 * Return the backing AAFCon, if there is a Lur Setup that is AAF.
275 * If there is no AAFLur setup, it will return "null"
276 * @param servletRequest
279 public static final AAFCon<?> obtain(Object servletRequest) {
280 if (servletRequest instanceof CadiWrap) {
281 Lur lur = ((CadiWrap)servletRequest).getLur();
283 if (lur instanceof EpiLur) {
284 AbsAAFLur<?> aal = (AbsAAFLur<?>) ((EpiLur)lur).subLur(AbsAAFLur.class);
289 if (lur instanceof AbsAAFLur) {
290 return ((AbsAAFLur<?>)lur).aaf;
298 public abstract AAFCon<CLIENT> clone(String url) throws CadiException, LocatorException;
300 public AAFAuthn<CLIENT> newAuthn() throws APIException {
302 return new AAFAuthn<>(this);
303 } catch (Exception e) {
304 throw new APIException(e);
308 public AAFAuthn<CLIENT> newAuthn(AbsUserCache<AAFPermission> c) {
309 return new AAFAuthn<>(this, c);
312 public AAFLurPerm newLur() throws CadiException {
315 lur = new AAFLurPerm(this);
318 return new AAFLurPerm(this,lur);
320 } catch (CadiException e) {
322 } catch (Exception e) {
323 throw new CadiException(e);
327 public AAFLurPerm newLur(AbsUserCache<AAFPermission> c) throws APIException {
329 return new AAFLurPerm(this,c);
330 } catch (APIException e) {
332 } catch (Exception e) {
333 throw new APIException(e);
337 protected abstract Rcli<CLIENT> rclient(URI uri, SecuritySetter<CLIENT> ss) throws CadiException;
339 public abstract Rcli<CLIENT> rclient(Locator<URI> loc, SecuritySetter<CLIENT> ss) throws CadiException;
341 public Rcli<CLIENT> client(Locator<URI> locator) throws CadiException {
342 return rclient(locator,si.defSS);
345 public abstract<RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
347 public abstract<RET> RET bestForUser(GetSetter get, Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
349 public abstract SecuritySetter<CLIENT> basicAuth(String user, String password) throws CadiException;
351 public abstract SecuritySetter<CLIENT> transferSS(TaggedPrincipal principal) throws CadiException;
353 public abstract SecuritySetter<CLIENT> basicAuthSS(BasicPrincipal principal) throws CadiException;
355 public abstract SecuritySetter<CLIENT> tokenSS(final String client_id, final String accessToken) throws CadiException;
357 public abstract SecuritySetter<CLIENT> x509Alias(String alias) throws APIException, CadiException;
360 public String getRealm() {
366 * This interface allows the AAFCon, even though generic, to pass in correctly typed values based on the above SS commands.
370 public interface GetSetter {
371 public<CLIENT> SecuritySetter<CLIENT> get(AAFCon<CLIENT> con) throws CadiException;
374 public SecuritySetter<CLIENT> set(final SecuritySetter<CLIENT> ss) {
376 for (Rcli<CLIENT> client : clients.values()) {
377 client.setSecuritySetter(ss);
382 public SecurityInfoC<CLIENT> securityInfo() {
386 public String defID() {
388 return si.defSS.getID();
393 public void invalidate() throws CadiException {
394 for (Rcli<CLIENT> client : clients.values()) {
400 public String readableErrMsg(Future<?> f) {
401 String text = f.body();
402 if (text==null || text.length()==0) {
403 text = f.code() + ": **No Message**";
404 } else if (text.contains("%")) {
406 Error err = errDF.newData().in(TYPE.JSON).load(f.body()).asObject();
407 return Vars.convert(err.getText(),err.getVariables());
408 } catch (APIException e){
415 public static AAFCon<?> newInstance(PropAccess pa) throws CadiException, LocatorException {
416 // Potentially add plugin for other kinds of Access
417 return new AAFConHttp(pa);