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("AAFCon",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 alias = access.getProperty(Config.CADI_ALIAS, null);
131 si.defSS=x509Alias(alias);
135 String encpass = access.getProperty(Config.AAF_APPPASS, null);
137 encpass = access.getProperty(Config.OAUTH_CLIENT_SECRET,null);
142 access.printf(Access.Level.WARN,"%s, %s or %s required before use.", Config.CADI_ALIAS, Config.AAF_APPID, Config.OAUTH_CLIENT_ID);
147 si.defSS=basicAuth(mechid, encpass);
150 si.defSS=new SecuritySetter<CLIENT>() {
153 public String getID() {
158 public void setSecurity(CLIENT client) throws CadiException {
159 throw new CadiException("AAFCon has not been initialized with Credentials (SecuritySetter)");
163 public int setLastResponse(int respCode) {
173 timeout = Integer.parseInt(access.getProperty(Config.AAF_CALL_TIMEOUT, Config.AAF_CALL_TIMEOUT_DEF));
174 cleanInterval = Integer.parseInt(access.getProperty(Config.AAF_CLEAN_INTERVAL, Config.AAF_CLEAN_INTERVAL_DEF));
175 highCount = Integer.parseInt(access.getProperty(Config.AAF_HIGH_COUNT, Config.AAF_HIGH_COUNT_DEF).trim());
176 connTimeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF).trim());
177 userExpires = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim());
178 usageRefreshTriggerCount = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim())-1; // zero based
180 app=FQI.reverseDomain(si.defSS.getID());
181 //TODO Get Realm from AAF
182 realm="people.osaaf.org";
184 env = new RosettaEnv();
185 permsDF = env.newDataFactory(Perms.class);
186 usersDF = env.newDataFactory(Users.class);
187 certsDF = env.newDataFactory(Certs.class);
188 certsDF.rootMarshal(new CertsMarshal()); // Speedier Marshaling
189 errDF = env.newDataFactory(Error.class);
190 } catch (APIException e) {
191 throw new CadiException("AAFCon cannot be configured",e);
194 protected abstract URI initURI();
195 protected abstract void setInitURI(String uriString) throws CadiException;
197 public final String aafVersion() {
202 * Use this call to get the appropriate client based on configuration (HTTP, future)
203 * using default AAF API Version
207 * @throws CadiException
209 public Rcli<CLIENT> client() throws CadiException {
210 return client(apiVersion);
214 * Use this call to get the appropriate client based on configuration (HTTP, future)
218 * @throws CadiException
220 public Rcli<CLIENT> client(final String apiVersion) throws CadiException {
221 Rcli<CLIENT> client = clients.get(apiVersion);
223 client = rclient(initURI(),si.defSS);
224 client.apiVersion(apiVersion)
225 .readTimeout(connTimeout);
226 clients.put(apiVersion, client);
231 public Rcli<CLIENT> client(URI uri) throws CadiException {
232 return rclient(uri,si.defSS).readTimeout(connTimeout);
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(TaggedPrincipal p) throws CadiException {
246 return clientAs(apiVersion,p);
250 * Use this API when you have permission to have your call act as the end client's ID.
252 * Your calls will get 403 errors if you do not have this permission. it is a special setup, rarely given.
257 * @throws CadiException
259 public Rcli<CLIENT> clientAs(String apiVersion, TaggedPrincipal p) throws CadiException {
260 Rcli<CLIENT> cl = client(apiVersion);
261 return cl.forUser(transferSS(p));
265 public RosettaEnv env() {
270 * Return the backing AAFCon, if there is a Lur Setup that is AAF.
272 * If there is no AAFLur setup, it will return "null"
273 * @param servletRequest
276 public static final AAFCon<?> obtain(Object servletRequest) {
277 if (servletRequest instanceof CadiWrap) {
278 Lur lur = ((CadiWrap)servletRequest).getLur();
280 if (lur instanceof EpiLur) {
281 AbsAAFLur<?> aal = (AbsAAFLur<?>) ((EpiLur)lur).subLur(AbsAAFLur.class);
286 if (lur instanceof AbsAAFLur) {
287 return ((AbsAAFLur<?>)lur).aaf;
295 public abstract AAFCon<CLIENT> clone(String url) throws CadiException, LocatorException;
297 public AAFAuthn<CLIENT> newAuthn() throws APIException {
299 return new AAFAuthn<>(this);
300 } catch (Exception e) {
301 throw new APIException(e);
305 public AAFAuthn<CLIENT> newAuthn(AbsUserCache<AAFPermission> c) {
306 return new AAFAuthn<>(this, c);
309 public AAFLurPerm newLur() throws CadiException {
312 lur = new AAFLurPerm(this);
315 return new AAFLurPerm(this,lur);
317 } catch (CadiException e) {
319 } catch (Exception e) {
320 throw new CadiException(e);
324 public AAFLurPerm newLur(AbsUserCache<AAFPermission> c) throws APIException {
326 return new AAFLurPerm(this,c);
327 } catch (APIException e) {
329 } catch (Exception e) {
330 throw new APIException(e);
334 protected abstract Rcli<CLIENT> rclient(URI uri, SecuritySetter<CLIENT> ss) throws CadiException;
336 public abstract Rcli<CLIENT> rclient(Locator<URI> loc, SecuritySetter<CLIENT> ss) throws CadiException;
338 public Rcli<CLIENT> client(Locator<URI> locator) throws CadiException {
339 return rclient(locator,si.defSS);
342 public abstract<RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
344 public abstract<RET> RET bestForUser(GetSetter get, Retryable<RET> retryable) throws LocatorException, CadiException, APIException;
346 public abstract SecuritySetter<CLIENT> basicAuth(String user, String password) throws CadiException;
348 public abstract SecuritySetter<CLIENT> transferSS(TaggedPrincipal principal) throws CadiException;
350 public abstract SecuritySetter<CLIENT> basicAuthSS(BasicPrincipal principal) throws CadiException;
352 public abstract SecuritySetter<CLIENT> tokenSS(final String client_id, final String accessToken) throws CadiException;
354 public abstract SecuritySetter<CLIENT> x509Alias(String alias) throws APIException, CadiException;
357 public String getRealm() {
363 * This interface allows the AAFCon, even though generic, to pass in correctly typed values based on the above SS commands.
367 public interface GetSetter {
368 public<CLIENT> SecuritySetter<CLIENT> get(AAFCon<CLIENT> con) throws CadiException;
371 public SecuritySetter<CLIENT> set(final SecuritySetter<CLIENT> ss) {
373 for (Rcli<CLIENT> client : clients.values()) {
374 client.setSecuritySetter(ss);
379 public SecurityInfoC<CLIENT> securityInfo() {
383 public String defID() {
385 return si.defSS.getID();
390 public void invalidate() throws CadiException {
391 for (Rcli<CLIENT> client : clients.values()) {
397 public String readableErrMsg(Future<?> f) {
398 String text = f.body();
399 if (text==null || text.length()==0) {
400 text = f.code() + ": **No Message**";
401 } else if (text.contains("%")) {
403 Error err = errDF.newData().in(TYPE.JSON).load(f.body()).asObject();
404 return Vars.convert(err.getText(),err.getVariables());
405 } catch (APIException e){
412 public static AAFCon<?> newInstance(PropAccess pa) throws CadiException, LocatorException {
413 // Potentially add plugin for other kinds of Access
414 return new AAFConHttp(pa);