aboutsummaryrefslogtreecommitdiffstats
path: root/aaf/src/main/java/com/att/cadi/aaf/v2_0/AbsAAFLur.java
blob: 297ad8a9bea14ff21b0220a844c52565cc7d1944 (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
/*******************************************************************************
 * ============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.URISyntaxException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.att.aft.dme2.api.DME2Exception;
import com.att.cadi.AbsUserCache;
import com.att.cadi.Access.Level;
import com.att.cadi.CachingLur;
import com.att.cadi.Permission;
import com.att.cadi.StrLur;
import com.att.cadi.Transmutate;
import com.att.cadi.User;
import com.att.cadi.config.Config;
import com.att.cadi.aaf.AAFPermission;
import com.att.cadi.aaf.AAFTransmutate;
import com.att.inno.env.APIException;
import com.att.inno.env.util.Split;

public abstract class AbsAAFLur<PERM extends Permission> extends AbsUserCache<PERM> implements StrLur, CachingLur<PERM> {
	protected static final byte[] BLANK_PASSWORD = new byte[0];
	protected static final Transmutate<Principal> transmutate = new AAFTransmutate();
	private String[] debug = null;
	public AAFCon<?> aaf;
	private String[] supports;

	public AbsAAFLur(AAFCon<?> con) throws DME2Exception, URISyntaxException, APIException {
		super(con.access, con.cleanInterval, con.highCount, con.usageRefreshTriggerCount);
		aaf = con;
		setLur(this);
		supports = con.access.getProperty(Config.AAF_DOMAIN_SUPPORT, Config.AAF_DOMAIN_SUPPORT_DEF).split("\\s*:\\s*");
	}

	public AbsAAFLur(AAFCon<?> con, AbsUserCache<PERM> auc) throws DME2Exception, URISyntaxException, APIException {
		super(auc);
		aaf = con;
		setLur(this);
		supports = con.access.getProperty(Config.AAF_DOMAIN_SUPPORT, Config.AAF_DOMAIN_SUPPORT_DEF).split("\\s*:\\s*");
	}

	@Override
	public void setDebug(String ids) {
		this.debug = ids==null?null:Split.split(',', ids);
	}
	
	protected abstract User<PERM> loadUser(Principal bait);
	protected abstract User<PERM> loadUser(String name);
	public final boolean supports(String userName) {
		if(userName!=null) {
			for(String s : supports) {
				if(userName.endsWith(s))
					return true;
			}
		}
		return false;
	}
	
	protected abstract boolean isCorrectPermType(Permission pond);
	
	// This is where you build AAF CLient Code.  Answer the question "Is principal "bait" in the "pond"
	public boolean fish(Principal bait, Permission pond) {
		return fish(bait.getName(), pond);
	}

	public void fishAll(Principal bait, List<Permission> perms) {
		fishAll(bait.getName(),perms);
	}

	// This is where you build AAF CLient Code.  Answer the question "Is principal "bait" in the "pond"
	public boolean fish(String bait, Permission pond) {
		if(isDebug(bait)) {
			boolean rv = false;
			StringBuilder sb = new StringBuilder("Log for ");
			sb.append(bait);
			if(supports(bait)) {
				User<PERM> user = getUser(bait);
				if(user==null) {
					sb.append("\n\tUser is not in Cache");
				} else {
					if(user.noPerms())sb.append("\n\tUser has no Perms");
					if(user.permExpired()) {
						sb.append("\n\tUser's perm expired [");
						sb.append(new Date(user.permExpires()));
						sb.append(']');
					} else {
						sb.append("\n\tUser's perm expires [");
						sb.append(new Date(user.permExpires()));
						sb.append(']');
					}
				}
				if(user==null || (user.noPerms() && user.permExpired())) {
					user = loadUser(bait);
					sb.append("\n\tloadUser called");
				}
				if(user==null) {
					sb.append("\n\tUser was not Loaded");
				} else if(user.contains(pond)) {
					sb.append("\n\tUser contains ");
					sb.append(pond.getKey());
					rv = true;
				} else {
					sb.append("\n\tUser does not contain ");
					sb.append(pond.getKey());
					List<Permission> perms = new ArrayList<Permission>();
					user.copyPermsTo(perms);
					for(Permission p : perms) {
						sb.append("\n\t\t");
						sb.append(p.getKey());
					}
				}
			} else {
				sb.append("AAF Lur does not support [");
				sb.append(bait);
				sb.append("]");
			}
			aaf.access.log(Level.INFO, sb);
			return rv;
		} else {
			if(supports(bait)) {
				User<PERM> user = getUser(bait);
				if(user==null || (user.noPerms() && user.permExpired())) {
					user = loadUser(bait);
				}
				return user==null?false:user.contains(pond);
			}
			return false;
		}
	}

	public void fishAll(String bait, List<Permission> perms) {
		if(isDebug(bait)) {
			StringBuilder sb = new StringBuilder("Log for ");
			sb.append(bait);
			if(supports(bait)) {
				User<PERM> user = getUser(bait);
				if(user==null) {
					sb.append("\n\tUser is not in Cache");
				} else {
					if(user.noPerms())sb.append("\n\tUser has no Perms");
					if(user.permExpired()) {
						sb.append("\n\tUser's perm expired [");
						sb.append(new Date(user.permExpires()));
						sb.append(']');
					} else {
						sb.append("\n\tUser's perm expires [");
						sb.append(new Date(user.permExpires()));
						sb.append(']');
					}
				}
				if(user==null || (user.noPerms() && user.permExpired())) {
					user = loadUser(bait);
					sb.append("\n\tloadUser called");
				}
				if(user==null) {
					sb.append("\n\tUser was not Loaded");
				} else {
					sb.append("\n\tCopying Perms ");
					user.copyPermsTo(perms);
					for(Permission p : perms) {
						sb.append("\n\t\t");
						sb.append(p.getKey());
					}
				}
			} else {
				sb.append("AAF Lur does not support [");
				sb.append(bait);
				sb.append("]");
			}
			aaf.access.log(Level.INFO, sb);
		} else {
			if(supports(bait)) {
				User<PERM> user = getUser(bait);
				if(user==null || (user.noPerms() && user.permExpired())) user = loadUser(bait);
				if(user!=null) {
					user.copyPermsTo(perms);
				}
			}
		}
	}
	
	@Override
	public void remove(String user) {
		super.remove(user);
	}

	private boolean isDebug(String bait) {
		if(debug!=null) {
			if(debug.length==1 && "all".equals(debug[0]))return true;
			for(String s : debug) {
				if(s.equals(bait))return true;
			}
		}
		return false;
	}
	/**
	 * This special case minimizes loops, avoids multiple Set hits, and calls all the appropriate Actions found.
	 * 
	 * @param bait
	 * @param obj
	 * @param type
	 * @param instance
	 * @param actions
	 */
	public<A> void fishOneOf(String bait, A obj, String type, String instance, List<Action<A>> actions) {
		User<PERM> user = getUser(bait);
		if(user==null || (user.noPerms() && user.permExpired()))user = loadUser(bait);
//		return user==null?false:user.contains(pond);
		if(user!=null) {
			ReuseAAFPermission perm = new ReuseAAFPermission(type,instance);
			for(Action<A> action : actions) {
				perm.setAction(action.getName());
				if(user.contains(perm)) {
					if(action.exec(obj))return;
				}
			}
		}
	}
	
	public static interface Action<A> {
		public String getName();
		/**
		 *  Return false to continue, True to end now
		 * @return
		 */
		public boolean exec(A a);
	}
	
	private class ReuseAAFPermission extends AAFPermission {
		public ReuseAAFPermission(String type, String instance) {
			super(type,instance,null);
		}

		public void setAction(String s) {
			action = s;
		}
		
		/**
		 * This function understands that AAF Keys are hierarchical, :A:B:C, 
		 *  Cassandra follows a similar method, so we'll short circuit and do it more efficiently when there isn't a first hit
		 * @return
		 */
	}
}