summaryrefslogtreecommitdiffstats
path: root/aaf/src/main/java/com/att/cadi/aaf/v2_0/AAFCon.java
blob: a9d53f3d4a6865dea36855a960f643438c821e1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.cadi.aaf.v2_0;

import java.net.URI;
import java.security.Principal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import com.att.cadi.AbsUserCache;
import com.att.cadi.CadiException;
import com.att.cadi.CadiWrap;
import com.att.cadi.Connector;
import com.att.cadi.LocatorException;
import com.att.cadi.Lur;
import com.att.cadi.PropAccess;
import com.att.cadi.SecuritySetter;
import com.att.cadi.aaf.AAFPermission;
import com.att.cadi.aaf.marshal.CertsMarshal;
import com.att.cadi.client.AbsBasicAuth;
import com.att.cadi.client.Future;
import com.att.cadi.client.Rcli;
import com.att.cadi.client.Retryable;
import com.att.cadi.config.Config;
import com.att.cadi.config.SecurityInfoC;
import com.att.cadi.lur.EpiLur;
import com.att.cadi.principal.BasicPrincipal;
import com.att.cadi.util.Vars;
import com.att.inno.env.APIException;
import com.att.inno.env.Data.TYPE;
import com.att.inno.env.util.Split;
import com.att.rosetta.env.RosettaDF;
import com.att.rosetta.env.RosettaEnv;

import aaf.v2_0.Certs;
import aaf.v2_0.Error;
import aaf.v2_0.Perms;
import aaf.v2_0.Users;

public abstract class AAFCon<CLIENT> implements Connector {
	public static final String AAF_LATEST_VERSION = "2.0";

	final public PropAccess access;
	// Package access
	final public int timeout, cleanInterval, connTimeout;
	final public int highCount, userExpires, usageRefreshTriggerCount;
	private Map<String,Rcli<CLIENT>> clients = new ConcurrentHashMap<String,Rcli<CLIENT>>();
	final public RosettaDF<Perms> permsDF;
	final public RosettaDF<Certs> certsDF;
	final public RosettaDF<Users> usersDF;
	final public RosettaDF<Error> errDF;
	private String realm;
	public final String app;
	protected SecuritySetter<CLIENT> ss;
	protected SecurityInfoC<CLIENT> si;

	private DisableCheck disableCheck;

	private AAFLurPerm lur;

	private RosettaEnv env;
	protected abstract URI initURI();
	protected abstract void setInitURI(String uriString) throws CadiException;

	/**
	 * Use this call to get the appropriate client based on configuration (DME2, HTTP, future)
	 * 
	 * @param apiVersion
	 * @return
	 * @throws CadiException
	 */
	public Rcli<CLIENT> client(String apiVersion) throws CadiException {
		Rcli<CLIENT> client = clients.get(apiVersion);
		if(client==null) {
			client = rclient(initURI(),ss);
			client.apiVersion(apiVersion)
				  .readTimeout(connTimeout);
			clients.put(apiVersion, client);
		} 
		return client;
	}
	
	/**
	 * Use this API when you have permission to have your call act as the end client's ID.
	 * 
	 *  Your calls will get 403 errors if you do not have this permission.  it is a special setup, rarely given.
	 * 
	 * @param apiVersion
	 * @param req
	 * @return
	 * @throws CadiException
	 */
	public Rcli<CLIENT> clientAs(String apiVersion, ServletRequest req) throws CadiException {
		Rcli<CLIENT> cl = client(apiVersion);
		return cl.forUser(transferSS(((HttpServletRequest)req).getUserPrincipal()));
	}
	
	protected AAFCon(AAFCon<CLIENT> copy) {
		access = copy.access;
		timeout = copy.timeout;
		cleanInterval = copy.cleanInterval;
		connTimeout = copy.connTimeout;
		highCount = copy.highCount;
		userExpires = copy.userExpires;
		usageRefreshTriggerCount = copy.usageRefreshTriggerCount;
		permsDF = copy.permsDF;
		certsDF = copy.certsDF;
		usersDF = copy.usersDF;
		errDF = copy.errDF;
		app = copy.app;
		ss = copy.ss;
		si = copy.si;
		env = copy.env;
		disableCheck = copy.disableCheck;
		realm = copy.realm;
	}
	
	protected AAFCon(PropAccess access, String tag, SecurityInfoC<CLIENT> si) throws CadiException{
		if(tag==null) {
			throw new CadiException("AAFCon cannot be constructed with a tag=null");
		}
		try {
			this.access = access;
			this.si = si;
			this.ss = si.defSS;
			if(ss==null) {
				String mechid = access.getProperty(Config.AAF_MECHID, null);
				String encpass = access.getProperty(Config.AAF_MECHPASS, null);
				if(encpass==null) {
					String alias = access.getProperty(Config.CADI_ALIAS, mechid);
					if(alias==null) {
						throw new CadiException(Config.CADI_ALIAS + " or " + Config.AAF_MECHID + " required.");
					}
					set(si.defSS=x509Alias(alias));
				} else {
					if(mechid!=null && encpass !=null) {
						set(si.defSS=basicAuth(mechid, encpass));
					} else {
						set(si.defSS=new SecuritySetter<CLIENT>() {
							
							@Override
							public String getID() {
								return "";
							}
			
							@Override
							public void setSecurity(CLIENT client) throws CadiException {
								throw new CadiException("AAFCon has not been initialized with Credentials (SecuritySetter)");
							}

							@Override
							public int setLastResponse(int respCode) {
								return 0;
							}
						});
					}
				}
			}
			
			timeout = Integer.parseInt(access.getProperty(Config.AAF_READ_TIMEOUT, Config.AAF_READ_TIMEOUT_DEF));
			cleanInterval = Integer.parseInt(access.getProperty(Config.AAF_CLEAN_INTERVAL, Config.AAF_CLEAN_INTERVAL_DEF));
			highCount = Integer.parseInt(access.getProperty(Config.AAF_HIGH_COUNT, Config.AAF_HIGH_COUNT_DEF).trim());
			connTimeout = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF).trim());
			userExpires = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim());
			usageRefreshTriggerCount = Integer.parseInt(access.getProperty(Config.AAF_USER_EXPIRES, Config.AAF_USER_EXPIRES_DEF).trim())-1; // zero based
	
			String str = access.getProperty(tag,null);
			if(str==null) {
				throw new CadiException(tag + " property is required.");
			}
			setInitURI(str);
	
			app=reverseDomain(ss.getID());
			realm="openecomp.org";
	
			env = new RosettaEnv();
			permsDF = env.newDataFactory(Perms.class);
			usersDF = env.newDataFactory(Users.class);
			certsDF = env.newDataFactory(Certs.class);
			certsDF.rootMarshal(new CertsMarshal()); // Speedier Marshaling
			errDF = env.newDataFactory(Error.class);
		} catch (APIException e) {
			throw new CadiException("AAFCon cannot be configured",e);
		}
	}
	
	public RosettaEnv env() {
		return env;
	}
	
	/**
	 * Return the backing AAFCon, if there is a Lur Setup that is AAF.
	 * 
	 * If there is no AAFLur setup, it will return "null"
	 * @param servletRequest
	 * @return
	 */
	public static final AAFCon<?> obtain(Object servletRequest) {
		if(servletRequest instanceof CadiWrap) {
			Lur lur = ((CadiWrap)servletRequest).getLur();
			if(lur != null) {
				if(lur instanceof EpiLur) {
					AbsAAFLur<?> aal = (AbsAAFLur<?>) ((EpiLur)lur).subLur(AbsAAFLur.class);
					if(aal!=null) {
						return aal.aaf;
					}
				} else {
					if(lur instanceof AbsAAFLur) {
						return ((AbsAAFLur<?>)lur).aaf;
					}
				}
			}
		}
		return null;
	}
	
	public abstract AAFCon<CLIENT> clone(String url) throws CadiException;
	
	public AAFAuthn<CLIENT> newAuthn() throws APIException {
		try {
			return new AAFAuthn<CLIENT>(this);
		} catch (APIException e) {
			throw e;
		} catch (Exception e) {
			throw new APIException(e);
		}
	}

	public AAFAuthn<CLIENT> newAuthn(AbsUserCache<AAFPermission> c) throws APIException {
		try {
			return new AAFAuthn<CLIENT>(this,c);
		} catch (APIException e) {
			throw e;
		} catch (Exception e) {
			throw new APIException(e);
		}
	}

	public AAFLurPerm newLur() throws CadiException {
		try {
			if(lur==null) {
				return new AAFLurPerm(this);
			} else {
				return new AAFLurPerm(this,lur);
			}
		} catch (CadiException e) {
			throw e;
		} catch (Exception e) {
			throw new CadiException(e);
		}
	}
	
	public AAFLurPerm newLur(AbsUserCache<AAFPermission> c) throws APIException {
		try {
			return new AAFLurPerm(this,c);
		} catch (APIException e) {
			throw e;
		} catch (Exception e) {
			throw new APIException(e);
		}
	}

	/**
	 * Take a Fully Qualified User, and get a Namespace from it.
	 * @param user
	 * @return
	 */
	public static String reverseDomain(String user) {
		StringBuilder sb = null;
		String[] split = Split.split('.',user);
		int at;
		for(int i=split.length-1;i>=0;--i) {
			if(sb == null) {
				sb = new StringBuilder();
			} else {
				sb.append('.');
			}

			if((at = split[i].indexOf('@'))>0) {
				sb.append(split[i].subSequence(at+1, split[i].length()));
			} else {
				sb.append(split[i]);
			}
		}
		
		return sb==null?"":sb.toString();
	}

	protected abstract Rcli<CLIENT> rclient(URI uri, SecuritySetter<CLIENT> ss) throws CadiException;
	
	public abstract<RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException;


	public abstract SecuritySetter<CLIENT> basicAuth(String user, String password) throws CadiException;
	
	public abstract SecuritySetter<CLIENT> transferSS(Principal principal) throws CadiException;
	
	public abstract SecuritySetter<CLIENT> basicAuthSS(BasicPrincipal principal) throws CadiException;
	
	public abstract SecuritySetter<CLIENT> x509Alias(String alias) throws APIException, CadiException;


	public String getRealm() {
		return realm;

	}

	public SecuritySetter<CLIENT> set(final SecuritySetter<CLIENT> ss) {
		this.ss = ss;
		if(ss instanceof AbsBasicAuth) {
			disableCheck = (ss instanceof AbsBasicAuth)?
			new DisableCheck() {
				AbsBasicAuth<?> aba = (AbsBasicAuth<?>)ss;
				@Override
				public boolean isDisabled() {
					return aba.isDenied();
				}
			}:
			new DisableCheck() {
				@Override
				public boolean isDisabled() {
					return this.isDisabled();
				}
			};
		}
		for(Rcli<CLIENT> client : clients.values()) {
			client.setSecuritySetter(ss);
		}
		return ss;
	}
	
	public SecurityInfoC<CLIENT> securityInfo() {
		return si;
	}

	public String defID() {
		if(ss!=null) {
			return ss.getID();
		}
		return "unknown";
	}
	
	public void invalidate() throws CadiException {
		for(Rcli<CLIENT> client : clients.values()) {
			client.invalidate();
			clients.remove(client);
		}
	}

	public String readableErrMsg(Future<?> f) {
		String text = f.body();
		if(text==null || text.length()==0) {
			text = f.code() + ": **No Message**";
		} else if(text.contains("%")) {
			try {
				Error err = errDF.newData().in(TYPE.JSON).load(f.body()).asObject();
				return Vars.convert(err.getText(),err.getVariables());
			} catch (APIException e){
				// just return the body below
			}
		}
		return text;
	}
	
	private interface DisableCheck {
		public boolean isDisabled();
	};
	
	public boolean isDisabled() {
		return disableCheck.isDisabled();
	}
}