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;
26 import java.util.concurrent.ConcurrentHashMap;
28 import org.onap.aaf.cadi.AbsUserCache;
29 import org.onap.aaf.cadi.Access;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.CadiWrap;
32 import org.onap.aaf.cadi.Connector;
33 import org.onap.aaf.cadi.Locator;
34 import org.onap.aaf.cadi.LocatorException;
35 import org.onap.aaf.cadi.Lur;
36 import org.onap.aaf.cadi.PropAccess;
37 import org.onap.aaf.cadi.SecuritySetter;
38 import org.onap.aaf.cadi.aaf.AAFPermission;
39 import org.onap.aaf.cadi.aaf.marshal.CertsMarshal;
40 import org.onap.aaf.cadi.client.Future;
41 import org.onap.aaf.cadi.client.Rcli;
42 import org.onap.aaf.cadi.client.Retryable;
43 import org.onap.aaf.cadi.config.Config;
44 import org.onap.aaf.cadi.config.SecurityInfoC;
45 import org.onap.aaf.cadi.lur.EpiLur;
46 import org.onap.aaf.cadi.principal.BasicPrincipal;
47 import org.onap.aaf.cadi.principal.TaggedPrincipal;
48 import org.onap.aaf.cadi.util.FQI;
49 import org.onap.aaf.cadi.util.Vars;
50 import org.onap.aaf.misc.env.APIException;
51 import org.onap.aaf.misc.env.Data.TYPE;
52 import org.onap.aaf.misc.rosetta.env.RosettaDF;
53 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
55 import aaf.v2_0.Certs;
56 import aaf.v2_0.Error;
57 import aaf.v2_0.Perms;
58 import aaf.v2_0.Users;
60 public abstract class AAFCon<CLIENT> implements Connector {
61 final public Access access;
63 final public int timeout, cleanInterval, connTimeout;
64 final public int highCount, userExpires, usageRefreshTriggerCount;
65 private Map<String,Rcli<CLIENT>> clients = new ConcurrentHashMap<>();
66 final public RosettaDF<Perms> permsDF;
67 final public RosettaDF<Certs> certsDF;
68 final public RosettaDF<Users> usersDF;
69 final public RosettaDF<Error> errDF;
71 public final String app;
72 protected final String apiVersion;
73 protected SecurityInfoC<CLIENT> si;
75 private AAFLurPerm lur;
77 final public RosettaEnv env;
78 protected AAFCon(AAFCon<CLIENT> copy) {
80 apiVersion = access.getProperty(Config.AAF_API_VERSION, Config.AAF_DEFAULT_API_VERSION);
81 timeout = copy.timeout;
82 cleanInterval = copy.cleanInterval;
83 connTimeout = copy.connTimeout;
84 highCount = copy.highCount;
85 userExpires = copy.userExpires;
86 usageRefreshTriggerCount = copy.usageRefreshTriggerCount;
87 permsDF = copy.permsDF;
88 certsDF = copy.certsDF;
89 usersDF = copy.usersDF;
96 protected AAFCon(Access access, String tag, SecurityInfoC<CLIENT> si) throws CadiException{
97 apiVersion = access.getProperty(Config.AAF_API_VERSION, Config.AAF_DEFAULT_API_VERSION);
99 throw new CadiException("AAFCon cannot be constructed without a property tag or URL");
101 String str = access.getProperty(tag,null);
103 if (tag.contains("://")) { // assume a URL
106 throw new CadiException("A URL or " + tag + " property is required.");
112 this.access = access;
114 if (si.defSS.getID().equals(SecurityInfoC.DEF_ID)) { // it's the Preliminary SS, try to get a better one
115 String mechid = access.getProperty(Config.AAF_APPID, null);
117 mechid=access.getProperty(Config.OAUTH_CLIENT_ID,null);
119 String encpass = access.getProperty(Config.AAF_APPPASS, null);
121 encpass = access.getProperty(Config.OAUTH_CLIENT_SECRET,null);
124 String alias = access.getProperty(Config.CADI_ALIAS, mechid);
126 access.printf(Access.Level.WARN,"%s, %s or %s required before use.", Config.CADI_ALIAS, Config.AAF_APPID, Config.OAUTH_CLIENT_ID);
129 si.defSS=x509Alias(alias);
134 si.defSS=basicAuth(mechid, encpass);
137 si.defSS=new SecuritySetter<CLIENT>() {
140 public String getID() {
145 public void setSecurity(CLIENT client) throws CadiException {
146 throw new CadiException("AAFCon has not been initialized with Credentials (SecuritySetter)");
150 public int setLastResponse(int respCode) {
159 timeout = Integer.parseInt(access.getProperty(Config.AAF_CALL_TIMEOUT, Config.AAF_CALL_TIMEOUT_DEF));
160 cleanInterval = Integer.parseInt(access.getProperty(Config.AAF_CLEAN_INTERVAL, Config.AAF_CLEAN_INTERVAL_DEF));
161 highCount = Integer.parseInt(access.getProperty(Config.AAF_HIGH_COUNT, Config.AAF_HIGH_COUNT_DEF).trim());
162 connTimeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF).trim());
163 userExpires = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim());
164 usageRefreshTriggerCount = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim())-1; // zero based
166 app=FQI.reverseDomain(si.defSS.getID());
167 //TODO Get Realm from AAF
168 realm="people.osaaf.org";
170 env = new RosettaEnv();
171 permsDF = env.newDataFactory(Perms.class);
172 usersDF = env.newDataFactory(Users.class);
173 certsDF = env.newDataFactory(Certs.class);
174 certsDF.rootMarshal(new CertsMarshal()); // Speedier Marshaling
175 errDF = env.newDataFactory(Error.class);
176 } catch (APIException e) {
177 throw new CadiException("AAFCon cannot be configured",e);
180 protected abstract URI initURI();
181 protected abstract void setInitURI(String uriString) throws CadiException;
183 public final String aafVersion() {
188 * Use this call to get the appropriate client based on configuration (HTTP, future)
189 * using default AAF API Version
193 * @throws CadiException
195 public Rcli<CLIENT> client() throws CadiException {
196 return client(apiVersion);
200 * Use this call to get the appropriate client based on configuration (HTTP, future)
204 * @throws CadiException
206 public Rcli<CLIENT> client(final String apiVersion) throws CadiException {
207 Rcli<CLIENT> client = clients.get(apiVersion);
209 client = rclient(initURI(),si.defSS);
210 client.apiVersion(apiVersion)
211 .readTimeout(connTimeout);
212 clients.put(apiVersion, client);
217 public Rcli<CLIENT> client(URI uri) throws CadiException {
218 return rclient(uri,si.defSS).readTimeout(connTimeout);
222 * Use this API when you have permission to have your call act as the end client's ID.
224 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
229 * @throws CadiException
231 public Rcli<CLIENT> clientAs(TaggedPrincipal p) throws CadiException {
232 return clientAs(apiVersion,p);
236 * Use this API when you have permission to have your call act as the end client's ID.
238 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
243 * @throws CadiException
245 public Rcli<CLIENT> clientAs(String apiVersion, TaggedPrincipal p) throws CadiException {
246 Rcli<CLIENT> cl = client(apiVersion);
247 return cl.forUser(transferSS(p));
251 public RosettaEnv env() {
256 * Return the backing AAFCon, if there is a Lur Setup that is AAF.
258 * If there is no AAFLur setup, it will return "null"
259 * @param servletRequest
262 public static final AAFCon<?> obtain(Object servletRequest) {
263 if (servletRequest instanceof CadiWrap) {
264 Lur lur = ((CadiWrap)servletRequest).getLur();
266 if (lur instanceof EpiLur) {
267 AbsAAFLur<?> aal = (AbsAAFLur<?>) ((EpiLur)lur).subLur(AbsAAFLur.class);
272 if (lur instanceof AbsAAFLur) {
273 return ((AbsAAFLur<?>)lur).aaf;
281 public abstract AAFCon<CLIENT> clone(String url) throws CadiException, LocatorException;
283 public AAFAuthn<CLIENT> newAuthn() throws APIException {
285 return new AAFAuthn<>(this);
286 } catch (Exception e) {
287 throw new APIException(e);
291 public AAFAuthn<CLIENT> newAuthn(AbsUserCache<AAFPermission> c) {
292 return new AAFAuthn<>(this, c);
295 public AAFLurPerm newLur() throws CadiException {
298 lur = new AAFLurPerm(this);
301 return new AAFLurPerm(this,lur);
303 } catch (CadiException e) {
305 } catch (Exception e) {
306 throw new CadiException(e);
310 public AAFLurPerm newLur(AbsUserCache<AAFPermission> c) throws APIException {
312 return new AAFLurPerm(this,c);
313 } catch (APIException e) {
315 } catch (Exception e) {
316 throw new APIException(e);
320 protected abstract Rcli<CLIENT> rclient(URI uri, SecuritySetter<CLIENT> ss) throws CadiException;
322 public abstract Rcli<CLIENT> rclient(Locator<URI> loc, SecuritySetter<CLIENT> ss) throws CadiException;
324 public Rcli<CLIENT> client(Locator<URI> locator) throws CadiException {
325 return rclient(locator,si.defSS);
328 public abstract<RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
330 public abstract<RET> RET bestForUser(GetSetter get, Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
332 public abstract SecuritySetter<CLIENT> basicAuth(String user, String password) throws CadiException;
334 public abstract SecuritySetter<CLIENT> transferSS(TaggedPrincipal principal) throws CadiException;
336 public abstract SecuritySetter<CLIENT> basicAuthSS(BasicPrincipal principal) throws CadiException;
338 public abstract SecuritySetter<CLIENT> tokenSS(final String client_id, final String accessToken) throws CadiException;
340 public abstract SecuritySetter<CLIENT> x509Alias(String alias) throws APIException, CadiException;
343 public String getRealm() {
349 * This interface allows the AAFCon, even though generic, to pass in correctly typed values based on the above SS commands.
353 public interface GetSetter {
354 public<CLIENT> SecuritySetter<CLIENT> get(AAFCon<CLIENT> con) throws CadiException;
357 public SecuritySetter<CLIENT> set(final SecuritySetter<CLIENT> ss) {
359 for (Rcli<CLIENT> client : clients.values()) {
360 client.setSecuritySetter(ss);
365 public SecurityInfoC<CLIENT> securityInfo() {
369 public String defID() {
371 return si.defSS.getID();
376 public void invalidate() throws CadiException {
377 for (Rcli<CLIENT> client : clients.values()) {
383 public String readableErrMsg(Future<?> f) {
384 String text = f.body();
385 if (text==null || text.length()==0) {
386 text = f.code() + ": **No Message**";
387 } else if (text.contains("%")) {
389 Error err = errDF.newData().in(TYPE.JSON).load(f.body()).asObject();
390 return Vars.convert(err.getText(),err.getVariables());
391 } catch (APIException e){
398 public static AAFCon<?> newInstance(PropAccess pa) throws CadiException, LocatorException {
399 // Potentially add plugin for other kinds of Access
400 return new AAFConHttp(pa);