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.CadiException;
32 import org.onap.aaf.cadi.CadiWrap;
33 import org.onap.aaf.cadi.Connector;
34 import org.onap.aaf.cadi.Locator;
35 import org.onap.aaf.cadi.LocatorException;
36 import org.onap.aaf.cadi.Lur;
37 import org.onap.aaf.cadi.PropAccess;
38 import org.onap.aaf.cadi.SecuritySetter;
39 import org.onap.aaf.cadi.Access.Level;
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.Error;
60 import aaf.v2_0.Perms;
61 import aaf.v2_0.Users;
63 public abstract class AAFCon<CLIENT> implements Connector {
64 final public Access access;
66 final public int timeout, cleanInterval, connTimeout;
67 final public int highCount, userExpires, usageRefreshTriggerCount;
68 private Map<String,Rcli<CLIENT>> clients = new ConcurrentHashMap<>();
69 final public RosettaDF<Perms> permsDF;
70 final public RosettaDF<Certs> certsDF;
71 final public RosettaDF<Users> usersDF;
72 final public RosettaDF<Error> errDF;
74 public final String app;
75 protected final String apiVersion;
76 protected SecurityInfoC<CLIENT> si;
78 private AAFLurPerm lur;
80 final public RosettaEnv env;
81 protected AAFCon(AAFCon<CLIENT> copy) {
83 apiVersion = access.getProperty(Config.AAF_API_VERSION, Config.AAF_DEFAULT_API_VERSION);
84 timeout = copy.timeout;
85 cleanInterval = copy.cleanInterval;
86 connTimeout = copy.connTimeout;
87 highCount = copy.highCount;
88 userExpires = copy.userExpires;
89 usageRefreshTriggerCount = copy.usageRefreshTriggerCount;
90 permsDF = copy.permsDF;
91 certsDF = copy.certsDF;
92 usersDF = copy.usersDF;
99 protected AAFCon(Access access, String tag, SecurityInfoC<CLIENT> si) throws CadiException{
100 apiVersion = access.getProperty(Config.AAF_API_VERSION, Config.AAF_DEFAULT_API_VERSION);
102 throw new CadiException("AAFCon cannot be constructed without a property tag or URL");
104 String str = access.getProperty(tag,null);
106 if (tag.contains("://")) { // assume a URL
109 throw new CadiException("A URL or " + tag + " property is required.");
113 RegistrationPropHolder rph = new RegistrationPropHolder(access, 0);
114 str = rph.replacements(str, null,null);
115 } catch (UnknownHostException e) {
116 throw new CadiException(e);
118 access.printf(Level.INFO, "AAFCon has URL of %s",str);
122 this.access = access;
124 if (si.defSS.getID().equals(SecurityInfoC.DEF_ID)) { // it's the Preliminary SS, try to get a better one
125 String mechid = access.getProperty(Config.AAF_APPID, null);
127 mechid=access.getProperty(Config.OAUTH_CLIENT_ID,null);
129 String encpass = access.getProperty(Config.AAF_APPPASS, null);
131 encpass = access.getProperty(Config.OAUTH_CLIENT_SECRET,null);
134 String alias = access.getProperty(Config.CADI_ALIAS, mechid);
136 access.printf(Access.Level.WARN,"%s, %s or %s required before use.", Config.CADI_ALIAS, Config.AAF_APPID, Config.OAUTH_CLIENT_ID);
139 si.defSS=x509Alias(alias);
144 si.defSS=basicAuth(mechid, encpass);
147 si.defSS=new SecuritySetter<CLIENT>() {
150 public String getID() {
155 public void setSecurity(CLIENT client) throws CadiException {
156 throw new CadiException("AAFCon has not been initialized with Credentials (SecuritySetter)");
160 public int setLastResponse(int respCode) {
169 timeout = Integer.parseInt(access.getProperty(Config.AAF_CALL_TIMEOUT, Config.AAF_CALL_TIMEOUT_DEF));
170 cleanInterval = Integer.parseInt(access.getProperty(Config.AAF_CLEAN_INTERVAL, Config.AAF_CLEAN_INTERVAL_DEF));
171 highCount = Integer.parseInt(access.getProperty(Config.AAF_HIGH_COUNT, Config.AAF_HIGH_COUNT_DEF).trim());
172 connTimeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF).trim());
173 userExpires = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim());
174 usageRefreshTriggerCount = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim())-1; // zero based
176 app=FQI.reverseDomain(si.defSS.getID());
177 //TODO Get Realm from AAF
178 realm="people.osaaf.org";
180 env = new RosettaEnv();
181 permsDF = env.newDataFactory(Perms.class);
182 usersDF = env.newDataFactory(Users.class);
183 certsDF = env.newDataFactory(Certs.class);
184 certsDF.rootMarshal(new CertsMarshal()); // Speedier Marshaling
185 errDF = env.newDataFactory(Error.class);
186 } catch (APIException e) {
187 throw new CadiException("AAFCon cannot be configured",e);
190 protected abstract URI initURI();
191 protected abstract void setInitURI(String uriString) throws CadiException;
193 public final String aafVersion() {
198 * Use this call to get the appropriate client based on configuration (HTTP, future)
199 * using default AAF API Version
203 * @throws CadiException
205 public Rcli<CLIENT> client() throws CadiException {
206 return client(apiVersion);
210 * Use this call to get the appropriate client based on configuration (HTTP, future)
214 * @throws CadiException
216 public Rcli<CLIENT> client(final String apiVersion) throws CadiException {
217 Rcli<CLIENT> client = clients.get(apiVersion);
219 client = rclient(initURI(),si.defSS);
220 client.apiVersion(apiVersion)
221 .readTimeout(connTimeout);
222 clients.put(apiVersion, client);
227 public Rcli<CLIENT> client(URI uri) throws CadiException {
228 return rclient(uri,si.defSS).readTimeout(connTimeout);
232 * Use this API when you have permission to have your call act as the end client's ID.
234 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
239 * @throws CadiException
241 public Rcli<CLIENT> clientAs(TaggedPrincipal p) throws CadiException {
242 return clientAs(apiVersion,p);
246 * Use this API when you have permission to have your call act as the end client's ID.
248 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
253 * @throws CadiException
255 public Rcli<CLIENT> clientAs(String apiVersion, TaggedPrincipal p) throws CadiException {
256 Rcli<CLIENT> cl = client(apiVersion);
257 return cl.forUser(transferSS(p));
261 public RosettaEnv env() {
266 * Return the backing AAFCon, if there is a Lur Setup that is AAF.
268 * If there is no AAFLur setup, it will return "null"
269 * @param servletRequest
272 public static final AAFCon<?> obtain(Object servletRequest) {
273 if (servletRequest instanceof CadiWrap) {
274 Lur lur = ((CadiWrap)servletRequest).getLur();
276 if (lur instanceof EpiLur) {
277 AbsAAFLur<?> aal = (AbsAAFLur<?>) ((EpiLur)lur).subLur(AbsAAFLur.class);
282 if (lur instanceof AbsAAFLur) {
283 return ((AbsAAFLur<?>)lur).aaf;
291 public abstract AAFCon<CLIENT> clone(String url) throws CadiException, LocatorException;
293 public AAFAuthn<CLIENT> newAuthn() throws APIException {
295 return new AAFAuthn<>(this);
296 } catch (Exception e) {
297 throw new APIException(e);
301 public AAFAuthn<CLIENT> newAuthn(AbsUserCache<AAFPermission> c) {
302 return new AAFAuthn<>(this, c);
305 public AAFLurPerm newLur() throws CadiException {
308 lur = new AAFLurPerm(this);
311 return new AAFLurPerm(this,lur);
313 } catch (CadiException e) {
315 } catch (Exception e) {
316 throw new CadiException(e);
320 public AAFLurPerm newLur(AbsUserCache<AAFPermission> c) throws APIException {
322 return new AAFLurPerm(this,c);
323 } catch (APIException e) {
325 } catch (Exception e) {
326 throw new APIException(e);
330 protected abstract Rcli<CLIENT> rclient(URI uri, SecuritySetter<CLIENT> ss) throws CadiException;
332 public abstract Rcli<CLIENT> rclient(Locator<URI> loc, SecuritySetter<CLIENT> ss) throws CadiException;
334 public Rcli<CLIENT> client(Locator<URI> locator) throws CadiException {
335 return rclient(locator,si.defSS);
338 public abstract<RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
340 public abstract<RET> RET bestForUser(GetSetter get, Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
342 public abstract SecuritySetter<CLIENT> basicAuth(String user, String password) throws CadiException;
344 public abstract SecuritySetter<CLIENT> transferSS(TaggedPrincipal principal) throws CadiException;
346 public abstract SecuritySetter<CLIENT> basicAuthSS(BasicPrincipal principal) throws CadiException;
348 public abstract SecuritySetter<CLIENT> tokenSS(final String client_id, final String accessToken) throws CadiException;
350 public abstract SecuritySetter<CLIENT> x509Alias(String alias) throws APIException, CadiException;
353 public String getRealm() {
359 * This interface allows the AAFCon, even though generic, to pass in correctly typed values based on the above SS commands.
363 public interface GetSetter {
364 public<CLIENT> SecuritySetter<CLIENT> get(AAFCon<CLIENT> con) throws CadiException;
367 public SecuritySetter<CLIENT> set(final SecuritySetter<CLIENT> ss) {
369 for (Rcli<CLIENT> client : clients.values()) {
370 client.setSecuritySetter(ss);
375 public SecurityInfoC<CLIENT> securityInfo() {
379 public String defID() {
381 return si.defSS.getID();
386 public void invalidate() throws CadiException {
387 for (Rcli<CLIENT> client : clients.values()) {
393 public String readableErrMsg(Future<?> f) {
394 String text = f.body();
395 if (text==null || text.length()==0) {
396 text = f.code() + ": **No Message**";
397 } else if (text.contains("%")) {
399 Error err = errDF.newData().in(TYPE.JSON).load(f.body()).asObject();
400 return Vars.convert(err.getText(),err.getVariables());
401 } catch (APIException e){
408 public static AAFCon<?> newInstance(PropAccess pa) throws CadiException, LocatorException {
409 // Potentially add plugin for other kinds of Access
410 return new AAFConHttp(pa);