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 //TODO Get Realm from AAF
185 realm="people.osaaf.org";
187 env = new RosettaEnv();
188 permsDF = env.newDataFactory(Perms.class);
189 usersDF = env.newDataFactory(Users.class);
190 certsDF = env.newDataFactory(Certs.class);
191 certsDF.rootMarshal(new CertsMarshal()); // Speedier Marshaling
192 credReqDF = env.newDataFactory(CredRequest.class);
193 errDF = env.newDataFactory(Error.class);
194 } catch (APIException e) {
195 throw new CadiException("AAFCon cannot be configured",e);
198 protected abstract URI initURI();
199 protected abstract void setInitURI(String uriString) throws CadiException;
201 public final String aafVersion() {
206 * Use this call to get the appropriate client based on configuration (HTTP, future)
207 * using default AAF API Version
211 * @throws CadiException
213 public Rcli<CLIENT> client() throws CadiException {
214 return client(apiVersion);
218 * Use this call to get the appropriate client based on configuration (HTTP, future)
222 * @throws CadiException
224 public Rcli<CLIENT> client(final String apiVersion) throws CadiException {
225 Rcli<CLIENT> client = clients.get(apiVersion);
227 client = rclient(initURI(),si.defSS);
228 client.apiVersion(apiVersion)
229 .readTimeout(connTimeout);
230 clients.put(apiVersion, client);
235 public Rcli<CLIENT> client(URI uri) throws CadiException {
236 return rclient(uri,si.defSS).readTimeout(connTimeout);
240 * Use this API when you have permission to have your call act as the end client's ID.
242 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
247 * @throws CadiException
249 public Rcli<CLIENT> clientAs(TaggedPrincipal p) throws CadiException {
250 return clientAs(apiVersion,p);
254 * Use this API when you have permission to have your call act as the end client's ID.
256 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
261 * @throws CadiException
263 public Rcli<CLIENT> clientAs(String apiVersion, TaggedPrincipal p) throws CadiException {
264 Rcli<CLIENT> cl = client(apiVersion);
265 return cl.forUser(transferSS(p));
269 public RosettaEnv env() {
274 * Return the backing AAFCon, if there is a Lur Setup that is AAF.
276 * If there is no AAFLur setup, it will return "null"
277 * @param servletRequest
280 public static final AAFCon<?> obtain(Object servletRequest) {
281 if (servletRequest instanceof CadiWrap) {
282 Lur lur = ((CadiWrap)servletRequest).getLur();
284 if (lur instanceof EpiLur) {
285 AbsAAFLur<?> aal = (AbsAAFLur<?>) ((EpiLur)lur).subLur(AbsAAFLur.class);
290 if (lur instanceof AbsAAFLur) {
291 return ((AbsAAFLur<?>)lur).aaf;
299 public abstract AAFCon<CLIENT> clone(String url) throws CadiException, LocatorException;
301 public AAFAuthn<CLIENT> newAuthn() throws APIException {
303 return new AAFAuthn<>(this);
304 } catch (Exception e) {
305 throw new APIException(e);
309 public AAFAuthn<CLIENT> newAuthn(AbsUserCache<AAFPermission> c) {
310 return new AAFAuthn<>(this, c);
313 public AAFLurPerm newLur() throws CadiException {
316 lur = new AAFLurPerm(this);
319 return new AAFLurPerm(this,lur);
321 } catch (CadiException e) {
323 } catch (Exception e) {
324 throw new CadiException(e);
328 public AAFLurPerm newLur(AbsUserCache<AAFPermission> c) throws APIException {
330 return new AAFLurPerm(this,c);
331 } catch (APIException e) {
333 } catch (Exception e) {
334 throw new APIException(e);
338 protected abstract Rcli<CLIENT> rclient(URI uri, SecuritySetter<CLIENT> ss) throws CadiException;
340 public abstract Rcli<CLIENT> rclient(Locator<URI> loc, SecuritySetter<CLIENT> ss) throws CadiException;
342 public Rcli<CLIENT> client(Locator<URI> locator) throws CadiException {
343 return rclient(locator,si.defSS);
346 public abstract<RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
348 public abstract<RET> RET bestForUser(GetSetter get, Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
350 public abstract SecuritySetter<CLIENT> basicAuth(String user, String password) throws CadiException;
352 public abstract SecuritySetter<CLIENT> transferSS(TaggedPrincipal principal) throws CadiException;
354 public abstract SecuritySetter<CLIENT> basicAuthSS(BasicPrincipal principal) throws CadiException;
356 public abstract SecuritySetter<CLIENT> tokenSS(final String client_id, final String accessToken) throws CadiException;
358 public abstract SecuritySetter<CLIENT> x509Alias(String alias) throws APIException, CadiException;
361 public String getRealm() {
367 * This interface allows the AAFCon, even though generic, to pass in correctly typed values based on the above SS commands.
371 public interface GetSetter {
372 public<CLIENT> SecuritySetter<CLIENT> get(AAFCon<CLIENT> con) throws CadiException;
375 public SecuritySetter<CLIENT> set(final SecuritySetter<CLIENT> ss) {
377 for (Rcli<CLIENT> client : clients.values()) {
378 client.setSecuritySetter(ss);
383 public SecurityInfoC<CLIENT> securityInfo() {
387 public String defID() {
389 return si.defSS.getID();
394 public void invalidate() throws CadiException {
395 for (Rcli<CLIENT> client : clients.values()) {
401 public String readableErrMsg(Future<?> f) {
402 String text = f.body();
403 if (text==null || text.length()==0) {
404 text = f.code() + ": **No Message**";
405 } else if (text.contains("%")) {
407 Error err = errDF.newData().in(TYPE.JSON).load(f.body()).asObject();
408 return Vars.convert(err.getText(),err.getVariables());
409 } catch (APIException e){
416 public static AAFCon<?> newInstance(PropAccess pa) throws CadiException, LocatorException {
417 // Potentially add plugin for other kinds of Access
418 return new AAFConHttp(pa);