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<String,Rcli<CLIENT>>();
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 SecurityInfoC<CLIENT> si;
74 private AAFLurPerm lur;
76 final public RosettaEnv env;
77 protected abstract URI initURI();
78 protected abstract void setInitURI(String uriString) throws CadiException;
79 protected abstract SecuritySetter<CLIENT> bestSS(SecurityInfoC<CLIENT> si) throws CadiException;
82 * Use this call to get the appropriate client based on configuration (HTTP, future)
86 * @throws CadiException
88 public Rcli<CLIENT> client(String apiVersion) throws CadiException {
89 Rcli<CLIENT> client = clients.get(apiVersion);
91 client = rclient(initURI(),si.defSS);
92 client.apiVersion(apiVersion)
93 .readTimeout(connTimeout);
94 clients.put(apiVersion, client);
99 public Rcli<CLIENT> client(URI uri) throws CadiException {
100 return rclient(uri,si.defSS).readTimeout(connTimeout);
104 * Use this API when you have permission to have your call act as the end client's ID.
106 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
111 * @throws CadiException
113 public Rcli<CLIENT> clientAs(String apiVersion, TaggedPrincipal p) throws CadiException {
114 Rcli<CLIENT> cl = client(apiVersion);
115 return cl.forUser(transferSS(p));
118 protected AAFCon(AAFCon<CLIENT> copy) {
119 access = copy.access;
120 timeout = copy.timeout;
121 cleanInterval = copy.cleanInterval;
122 connTimeout = copy.connTimeout;
123 highCount = copy.highCount;
124 userExpires = copy.userExpires;
125 usageRefreshTriggerCount = copy.usageRefreshTriggerCount;
126 permsDF = copy.permsDF;
127 certsDF = copy.certsDF;
128 usersDF = copy.usersDF;
136 protected AAFCon(Access access, String tag, SecurityInfoC<CLIENT> si) throws CadiException{
138 throw new CadiException("AAFCon cannot be constructed without a property tag or URL");
140 si.defSS = bestSS(si);
141 String str = access.getProperty(tag,null);
143 if(tag.contains("://")) { // assume a URL
146 throw new CadiException("A URL or " + tag + " property is required.");
152 this.access = access;
154 if(si.defSS.getID().equals(SecurityInfoC.DEF_ID)) { // it's the Preliminary SS, try to get a better one
155 String mechid = access.getProperty(Config.AAF_APPID, null);
157 mechid=access.getProperty(Config.OAUTH_CLIENT_ID,null);
159 String encpass = access.getProperty(Config.AAF_APPPASS, null);
161 encpass = access.getProperty(Config.OAUTH_CLIENT_SECRET,null);
164 String alias = access.getProperty(Config.CADI_ALIAS, mechid);
166 access.printf(Access.Level.WARN,"%s, %s or %s required before use.", Config.CADI_ALIAS, Config.AAF_APPID, Config.OAUTH_CLIENT_ID);
169 set(si.defSS=x509Alias(alias));
172 if(mechid!=null && encpass !=null) {
173 set(si.defSS=basicAuth(mechid, encpass));
175 set(si.defSS=new SecuritySetter<CLIENT>() {
178 public String getID() {
183 public void setSecurity(CLIENT client) throws CadiException {
184 throw new CadiException("AAFCon has not been initialized with Credentials (SecuritySetter)");
188 public int setLastResponse(int respCode) {
196 timeout = Integer.parseInt(access.getProperty(Config.AAF_CALL_TIMEOUT, Config.AAF_CALL_TIMEOUT_DEF));
197 cleanInterval = Integer.parseInt(access.getProperty(Config.AAF_CLEAN_INTERVAL, Config.AAF_CLEAN_INTERVAL_DEF));
198 highCount = Integer.parseInt(access.getProperty(Config.AAF_HIGH_COUNT, Config.AAF_HIGH_COUNT_DEF).trim());
199 connTimeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF).trim());
200 userExpires = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim());
201 usageRefreshTriggerCount = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim())-1; // zero based
203 app=FQI.reverseDomain(si.defSS.getID());
204 //TODO Get Realm from AAF
205 realm="people.osaaf.org";
207 env = new RosettaEnv();
208 permsDF = env.newDataFactory(Perms.class);
209 usersDF = env.newDataFactory(Users.class);
210 certsDF = env.newDataFactory(Certs.class);
211 certsDF.rootMarshal(new CertsMarshal()); // Speedier Marshaling
212 errDF = env.newDataFactory(Error.class);
213 } catch (APIException e) {
214 throw new CadiException("AAFCon cannot be configured",e);
218 public RosettaEnv env() {
223 * Return the backing AAFCon, if there is a Lur Setup that is AAF.
225 * If there is no AAFLur setup, it will return "null"
226 * @param servletRequest
229 public static final AAFCon<?> obtain(Object servletRequest) {
230 if(servletRequest instanceof CadiWrap) {
231 Lur lur = ((CadiWrap)servletRequest).getLur();
233 if(lur instanceof EpiLur) {
234 AbsAAFLur<?> aal = (AbsAAFLur<?>) ((EpiLur)lur).subLur(AbsAAFLur.class);
239 if(lur instanceof AbsAAFLur) {
240 return ((AbsAAFLur<?>)lur).aaf;
248 public abstract AAFCon<CLIENT> clone(String url) throws CadiException, LocatorException;
250 public AAFAuthn<CLIENT> newAuthn() throws APIException {
252 return new AAFAuthn<CLIENT>(this);
253 } catch (APIException e) {
255 } catch (Exception e) {
256 throw new APIException(e);
260 public AAFAuthn<CLIENT> newAuthn(AbsUserCache<AAFPermission> c) {
261 return new AAFAuthn<CLIENT>(this,c);
264 public AAFLurPerm newLur() throws CadiException {
267 return (lur = new AAFLurPerm(this));
269 return new AAFLurPerm(this,lur);
271 } catch (CadiException e) {
273 } catch (Exception e) {
274 throw new CadiException(e);
278 public AAFLurPerm newLur(AbsUserCache<AAFPermission> c) throws APIException {
280 return new AAFLurPerm(this,c);
281 } catch (APIException e) {
283 } catch (Exception e) {
284 throw new APIException(e);
288 protected abstract Rcli<CLIENT> rclient(URI uri, SecuritySetter<CLIENT> ss) throws CadiException;
290 public abstract Rcli<CLIENT> rclient(Locator<URI> loc, SecuritySetter<CLIENT> ss) throws CadiException;
292 public Rcli<CLIENT> client(Locator<URI> locator) throws CadiException {
293 return rclient(locator,si.defSS);
296 public abstract<RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
298 public abstract<RET> RET bestForUser(GetSetter get, Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
300 public abstract SecuritySetter<CLIENT> basicAuth(String user, String password) throws CadiException;
302 public abstract SecuritySetter<CLIENT> transferSS(TaggedPrincipal principal) throws CadiException;
304 public abstract SecuritySetter<CLIENT> basicAuthSS(BasicPrincipal principal) throws CadiException;
306 public abstract SecuritySetter<CLIENT> tokenSS(final String client_id, final String accessToken) throws CadiException;
308 public abstract SecuritySetter<CLIENT> x509Alias(String alias) throws APIException, CadiException;
311 public String getRealm() {
317 * This interface allows the AAFCon, even though generic, to pass in correctly typed values based on the above SS commands.
321 public interface GetSetter {
322 public<CLIENT> SecuritySetter<CLIENT> get(AAFCon<CLIENT> con) throws CadiException;
325 public SecuritySetter<CLIENT> set(final SecuritySetter<CLIENT> ss) {
327 for(Rcli<CLIENT> client : clients.values()) {
328 client.setSecuritySetter(ss);
333 public SecurityInfoC<CLIENT> securityInfo() {
337 public String defID() {
339 return si.defSS.getID();
344 public void invalidate() throws CadiException {
345 for(Rcli<CLIENT> client : clients.values()) {
351 public String readableErrMsg(Future<?> f) {
352 String text = f.body();
353 if(text==null || text.length()==0) {
354 text = f.code() + ": **No Message**";
355 } else if(text.contains("%")) {
357 Error err = errDF.newData().in(TYPE.JSON).load(f.body()).asObject();
358 return Vars.convert(err.getText(),err.getVariables());
359 } catch (APIException e){
360 // just return the body below
366 public static AAFCon<?> newInstance(PropAccess pa) throws APIException, CadiException, LocatorException {
367 // Potentially add plugin for other kinds of Access
368 return new AAFConHttp(pa);