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 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;
80 protected abstract SecuritySetter<CLIENT> bestSS(SecurityInfoC<CLIENT> si) throws CadiException;
84 * Use this call to get the appropriate client based on configuration (HTTP, future)
88 * @throws CadiException
90 public Rcli<CLIENT> client(String apiVersion) throws CadiException {
91 Rcli<CLIENT> client = clients.get(apiVersion);
93 client = rclient(initURI(),si.defSS);
94 client.apiVersion(apiVersion)
95 .readTimeout(connTimeout);
96 clients.put(apiVersion, client);
101 public Rcli<CLIENT> client(URI uri) throws CadiException {
102 return rclient(uri,si.defSS).readTimeout(connTimeout);
106 * Use this API when you have permission to have your call act as the end client's ID.
108 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
113 * @throws CadiException
115 public Rcli<CLIENT> clientAs(String apiVersion, TaggedPrincipal p) throws CadiException {
116 Rcli<CLIENT> cl = client(apiVersion);
117 return cl.forUser(transferSS(p));
120 protected AAFCon(AAFCon<CLIENT> copy) {
121 access = copy.access;
122 timeout = copy.timeout;
123 cleanInterval = copy.cleanInterval;
124 connTimeout = copy.connTimeout;
125 highCount = copy.highCount;
126 userExpires = copy.userExpires;
127 usageRefreshTriggerCount = copy.usageRefreshTriggerCount;
128 permsDF = copy.permsDF;
129 certsDF = copy.certsDF;
130 usersDF = copy.usersDF;
138 protected AAFCon(Access access, String tag, SecurityInfoC<CLIENT> si) throws CadiException{
140 throw new CadiException("AAFCon cannot be constructed without a property tag or URL");
142 String str = access.getProperty(tag,null);
144 if(tag.contains("://")) { // assume a URL
147 throw new CadiException("A URL or " + tag + " property is required.");
153 this.access = access;
155 if(si.defSS.getID().equals(SecurityInfoC.DEF_ID)) { // it's the Preliminary SS, try to get a better one
156 String mechid = access.getProperty(Config.AAF_APPID, null);
158 mechid=access.getProperty(Config.OAUTH_CLIENT_ID,null);
160 String encpass = access.getProperty(Config.AAF_APPPASS, null);
162 encpass = access.getProperty(Config.OAUTH_CLIENT_SECRET,null);
165 String alias = access.getProperty(Config.CADI_ALIAS, mechid);
167 access.printf(Access.Level.WARN,"%s, %s or %s required before use.", Config.CADI_ALIAS, Config.AAF_APPID, Config.OAUTH_CLIENT_ID);
170 si.defSS=x509Alias(alias);
175 si.defSS=basicAuth(mechid, encpass);
178 si.defSS=new SecuritySetter<CLIENT>() {
181 public String getID() {
186 public void setSecurity(CLIENT client) throws CadiException {
187 throw new CadiException("AAFCon has not been initialized with Credentials (SecuritySetter)");
191 public int setLastResponse(int respCode) {
200 timeout = Integer.parseInt(access.getProperty(Config.AAF_CALL_TIMEOUT, Config.AAF_CALL_TIMEOUT_DEF));
201 cleanInterval = Integer.parseInt(access.getProperty(Config.AAF_CLEAN_INTERVAL, Config.AAF_CLEAN_INTERVAL_DEF));
202 highCount = Integer.parseInt(access.getProperty(Config.AAF_HIGH_COUNT, Config.AAF_HIGH_COUNT_DEF).trim());
203 connTimeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF).trim());
204 userExpires = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim());
205 usageRefreshTriggerCount = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim())-1; // zero based
207 app=FQI.reverseDomain(si.defSS.getID());
208 //TODO Get Realm from AAF
209 realm="people.osaaf.org";
211 env = new RosettaEnv();
212 permsDF = env.newDataFactory(Perms.class);
213 usersDF = env.newDataFactory(Users.class);
214 certsDF = env.newDataFactory(Certs.class);
215 certsDF.rootMarshal(new CertsMarshal()); // Speedier Marshaling
216 errDF = env.newDataFactory(Error.class);
217 } catch (APIException e) {
218 throw new CadiException("AAFCon cannot be configured",e);
222 public RosettaEnv env() {
227 * Return the backing AAFCon, if there is a Lur Setup that is AAF.
229 * If there is no AAFLur setup, it will return "null"
230 * @param servletRequest
233 public static final AAFCon<?> obtain(Object servletRequest) {
234 if(servletRequest instanceof CadiWrap) {
235 Lur lur = ((CadiWrap)servletRequest).getLur();
237 if(lur instanceof EpiLur) {
238 AbsAAFLur<?> aal = (AbsAAFLur<?>) ((EpiLur)lur).subLur(AbsAAFLur.class);
243 if(lur instanceof AbsAAFLur) {
244 return ((AbsAAFLur<?>)lur).aaf;
252 public abstract AAFCon<CLIENT> clone(String url) throws CadiException, LocatorException;
254 public AAFAuthn<CLIENT> newAuthn() throws APIException {
256 return new AAFAuthn<>(this);
257 } catch (Exception e) {
258 throw new APIException(e);
262 public AAFAuthn<CLIENT> newAuthn(AbsUserCache<AAFPermission> c) {
263 return new AAFAuthn<>(this, c);
266 public AAFLurPerm newLur() throws CadiException {
269 lur = new AAFLurPerm(this);
272 return new AAFLurPerm(this,lur);
274 } catch (CadiException e) {
276 } catch (Exception e) {
277 throw new CadiException(e);
281 public AAFLurPerm newLur(AbsUserCache<AAFPermission> c) throws APIException {
283 return new AAFLurPerm(this,c);
284 } catch (APIException e) {
286 } catch (Exception e) {
287 throw new APIException(e);
291 protected abstract Rcli<CLIENT> rclient(URI uri, SecuritySetter<CLIENT> ss) throws CadiException;
293 public abstract Rcli<CLIENT> rclient(Locator<URI> loc, SecuritySetter<CLIENT> ss) throws CadiException;
295 public Rcli<CLIENT> client(Locator<URI> locator) throws CadiException {
296 return rclient(locator,si.defSS);
299 public abstract<RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
301 public abstract<RET> RET bestForUser(GetSetter get, Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
303 public abstract SecuritySetter<CLIENT> basicAuth(String user, String password) throws CadiException;
305 public abstract SecuritySetter<CLIENT> transferSS(TaggedPrincipal principal) throws CadiException;
307 public abstract SecuritySetter<CLIENT> basicAuthSS(BasicPrincipal principal) throws CadiException;
309 public abstract SecuritySetter<CLIENT> tokenSS(final String client_id, final String accessToken) throws CadiException;
311 public abstract SecuritySetter<CLIENT> x509Alias(String alias) throws APIException, CadiException;
314 public String getRealm() {
320 * This interface allows the AAFCon, even though generic, to pass in correctly typed values based on the above SS commands.
324 public interface GetSetter {
325 public<CLIENT> SecuritySetter<CLIENT> get(AAFCon<CLIENT> con) throws CadiException;
328 public SecuritySetter<CLIENT> set(final SecuritySetter<CLIENT> ss) {
330 for(Rcli<CLIENT> client : clients.values()) {
331 client.setSecuritySetter(ss);
336 public SecurityInfoC<CLIENT> securityInfo() {
340 public String defID() {
342 return si.defSS.getID();
347 public void invalidate() throws CadiException {
348 for(Rcli<CLIENT> client : clients.values()) {
354 public String readableErrMsg(Future<?> f) {
355 String text = f.body();
356 if(text==null || text.length()==0) {
357 text = f.code() + ": **No Message**";
358 } else if(text.contains("%")) {
360 Error err = errDF.newData().in(TYPE.JSON).load(f.body()).asObject();
361 return Vars.convert(err.getText(),err.getVariables());
362 } catch (APIException e){
369 public static AAFCon<?> newInstance(PropAccess pa) throws CadiException, LocatorException {
370 // Potentially add plugin for other kinds of Access
371 return new AAFConHttp(pa);