Repository navigation
Expand file tree
/
Copy pathSecurityEngineAccessUtils.java
More file actions
366 lines (336 loc) · 13.4 KB
/
Copy pathSecurityEngineAccessUtils.java
File metadata and controls
366 lines (336 loc) · 13.4 KB
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
/*******************************************************************************
* Copyright 2015 Defense Health Agency (DHA)
*
* If your use of this software does not include any GPLv2 components:
* 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.
* ----------------------------------------------------------------------------
* If your use of this software includes any GPLv2 components:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*******************************************************************************/
package prerna.auth.utils;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import prerna.auth.AccessPermissionEnum;
import prerna.auth.User;
import prerna.auth.access.EngineGrantee;
import prerna.auth.access.UserAccess;
import prerna.util.SystemEngineRegistry;
/**
* What the engine proxy asks the security database before it lets a call to an
* engine or project method through: the signed in user's permission, whether
* they are an admin, whether an engine is public, and the permission an engine
* gives an app for its custom reactors or another engine for its own code.
*
* <p>
* The proxy asks on every call, so the answers are kept for a short time. They
* are read again as soon as a permission is saved on this server
* ({@link SecurityModelUsageLimitUtils#version()}), and no later than
* {@link #KEPT_FOR} after a change saved on another server of a cluster.
*/
public final class SecurityEngineAccessUtils {
private static final Logger classLogger = LogManager.getLogger(SecurityEngineAccessUtils.class);
private static final Duration KEPT_FOR = Duration.ofSeconds(30);
private static final Cache<User, UserAnswers> USER_ANSWERS = Caffeine.newBuilder().weakKeys()
.expireAfterWrite(KEPT_FOR).build();
private static final Cache<String, Boolean> LISTED = Caffeine.newBuilder().expireAfterWrite(KEPT_FOR)
.maximumSize(10_000).build();
private static final Cache<String, Boolean> PUBLIC = Caffeine.newBuilder().expireAfterWrite(KEPT_FOR)
.maximumSize(10_000).build();
private static final Cache<String, Boolean> DISCOVERABLE = Caffeine.newBuilder().expireAfterWrite(KEPT_FOR)
.maximumSize(10_000).build();
private static final Object RELOAD_LOCK = new Object();
private static volatile long answersVersion = Long.MIN_VALUE;
private static volatile GranteePermissions granteePermissions;
private SecurityEngineAccessUtils() {
}
/**
* Whether this JVM holds the security database. The server always does. The
* Java worker does not: an engine it holds is a stand-in that sends each call
* back to the server, which checks the call there.
*
* @return whether permissions can be read here
*/
public static boolean isSecurityDatabaseLoaded() {
return SystemEngineRegistry.isSecurityDbLoaded();
}
/**
* The permission the user holds on an engine or project now: the highest level
* among the grants in effect, given to them directly or to a team they are in,
* or read-only on a public one.
*
* @param user the signed in user
* @param id the engine or project id
* @param project true when the id is a project's
* @return the level, or null when the user has no access
*/
public static AccessPermissionEnum getUserPermission(User user, String id, boolean project) {
UserAccess access = getUserAccess(user, id, project);
return access == null ? null : access.level();
}
/**
* The access the user holds on an engine or project now, as
* {@link #getUserPermission} reads it, and where it comes from: their own
* grant, a team's grant, or the engine or project being public.
*
* @param user the signed in user
* @param id the engine or project id
* @param project true when the id is a project's
* @return the access, or null when the user has no access
*/
public static UserAccess getUserAccess(User user, String id, boolean project) {
if (user == null || id == null) {
return null;
}
return answersOf(user).levels.computeIfAbsent(key(id, project),
key -> Optional.ofNullable(project ? SecurityProjectUtils.getCurrentUserProjectAccess(user, id)
: SecurityEngineUtils.getCurrentUserEngineAccess(user, id)))
.orElse(null);
}
/**
* Whether the platform's own code may read an engine or project for a user who
* holds no permission on it: a discoverable one, whose details anyone can look
* at before asking for access, and a project one of whose insights the user can
* open. It never counts for a project's custom reactors.
*
* @param user the signed in user
* @param id the engine or project id
* @param project true when the id is a project's
* @return whether the platform may read it for the user
*/
public static boolean isReadableThroughPlatform(User user, String id, boolean project) {
if (user == null || id == null) {
return false;
}
return answersOf(user).platformReads.computeIfAbsent(key(id, project), key -> {
if (!project) {
return SecurityEngineUtils.engineIsDiscoverable(id);
}
try {
return SecurityProjectUtils.projectIsDiscoverable(id)
|| CurrentPermissionLevel.canOpenAnInsightOf(user, id);
} catch (IllegalArgumentException e) {
return false;
}
});
}
/**
* Whether the user is an admin.
*
* @param user the signed in user
* @return true for an admin
*/
public static boolean isAdmin(User user) {
if (user == null) {
return false;
}
UserAnswers answers = answersOf(user);
Boolean admin = answers.admin;
if (admin == null) {
admin = SecurityAdminUtils.storedAsAdmin(user);
answers.admin = admin;
}
return admin;
}
/**
* Whether the security database lists an engine or project. One that is being
* created is not listed yet, and one that is being deleted is no longer.
*
* @param id the engine or project id
* @param project true when the id is a project's
* @return whether it is listed
*/
public static boolean isListed(String id, boolean project) {
if (id == null) {
return false;
}
refreshIfChanged();
return LISTED.get(key(id, project),
key -> project ? SecurityProjectUtils.projectExists(id) : SecurityEngineUtils.engineExists(id));
}
/**
* Whether an engine is public, so everyone may read it.
*
* @param engineId the engine
* @return whether the engine is public
*/
public static boolean isPublic(String engineId) {
if (engineId == null) {
return false;
}
refreshIfChanged();
return PUBLIC.get(engineId, SecurityEngineUtils::engineIsGlobal);
}
/**
* Whether an engine or project is discoverable, which tells a read the platform
* made for a user with no permission on it apart from a project read through
* one of its insights.
*
* @param id the engine or project id
* @param project true when the id is a project's
* @return whether it is discoverable
*/
public static boolean isDiscoverable(String id, boolean project) {
if (id == null) {
return false;
}
refreshIfChanged();
return DISCOVERABLE.get(key(id, project), key -> project ? SecurityProjectUtils.projectIsDiscoverable(id)
: SecurityEngineUtils.engineIsDiscoverable(id));
}
/**
* The permission an engine gives an app or another engine now, which the app's
* custom reactors, or the other engine's own code, use in place of the signed
* in user's own. One that has ended does not count.
*
* @param grantee the app whose custom code is calling, or the engine whose own
* code is
* @param engineId the engine
* @return the level, or null when the engine gives the grantee none
*/
public static AccessPermissionEnum getGranteePermission(EngineGrantee grantee, String engineId) {
return getGranteePermission(grantee, engineId, false);
}
/**
* The permission an engine or a project gives an app or an engine now, which
* the app's custom reactors, its agent runs, or the engine's own code, use in
* place of the signed in user's own. One that has ended does not count.
*
* @param grantee the app or agent, or the engine whose own code is calling
* @param targetId the engine or project
* @param project whether the target is a project
* @return the level, or null when the target gives the grantee none
*/
public static AccessPermissionEnum getGranteePermission(EngineGrantee grantee, String targetId, boolean project) {
if (grantee == null || grantee.engineId() == null || grantee.engineType() == null || targetId == null) {
return null;
}
Map<EngineGrantee, AccessPermissionEnum> ofTarget;
try {
ofTarget = granteePermissions().byTarget.get(SecurityEngine2EngineUtils.targetKey(targetId, project));
} catch (IllegalStateException e) {
classLogger.error(
"The permissions engines and projects give to apps and engines could not be read, so "
+ "{} {} is given none on {} {}",
grantee.engineType(), grantee.engineId(), project ? "project" : "engine", targetId, e);
return null;
}
return ofTarget == null ? null : ofTarget.get(grantee);
}
/**
* Whether an engine gives any app or other engine a permission now. Most
* engines give none, and the engine proxy looks for the app or engine making a
* call only on one that does.
*
* @param engineId the engine
* @return whether some app or engine holds a permission on the engine that is
* in effect
*/
public static boolean givesAGranteePermission(String engineId) {
return givesAGranteePermission(engineId, false);
}
/**
* Whether an engine or a project gives any app or engine a permission now.
*
* @param targetId the engine or project
* @param project whether it is a project
* @return whether some app or engine holds a permission on it that is in effect
*/
public static boolean givesAGranteePermission(String targetId, boolean project) {
if (targetId == null) {
return false;
}
try {
Map<EngineGrantee, AccessPermissionEnum> ofTarget = granteePermissions().byTarget
.get(SecurityEngine2EngineUtils.targetKey(targetId, project));
return ofTarget != null && !ofTarget.isEmpty();
} catch (IllegalStateException e) {
classLogger.error("The permissions engines and projects give to apps and engines could not be read, so "
+ "{} {} is taken to give none", project ? "project" : "engine", targetId, e);
return false;
}
}
private static String key(String id, boolean project) {
return (project ? "P:" : "E:") + id;
}
private static UserAnswers answersOf(User user) {
refreshIfChanged();
return USER_ANSWERS.get(user, key -> new UserAnswers());
}
private static void refreshIfChanged() {
long version = SecurityModelUsageLimitUtils.version();
if (version != answersVersion) {
synchronized (RELOAD_LOCK) {
if (version != answersVersion) {
USER_ANSWERS.invalidateAll();
LISTED.invalidateAll();
PUBLIC.invalidateAll();
DISCOVERABLE.invalidateAll();
answersVersion = version;
}
}
}
}
private static GranteePermissions granteePermissions() {
long version = SecurityModelUsageLimitUtils.version();
long now = System.nanoTime();
GranteePermissions current = granteePermissions;
if (current != null && current.version == version && now - current.readAt < KEPT_FOR.toNanos()) {
return current;
}
synchronized (RELOAD_LOCK) {
current = granteePermissions;
if (current == null || current.version != version || now - current.readAt >= KEPT_FOR.toNanos()) {
current = new GranteePermissions(version, now, SecurityEngine2EngineUtils.getPermissionsInEffect());
granteePermissions = current;
}
return current;
}
}
/**
* The answers kept for one signed in user, until they are read again.
*/
private static final class UserAnswers {
private final Map<String, Optional<UserAccess>> levels = new ConcurrentHashMap<>();
private final Map<String, Boolean> platformReads = new ConcurrentHashMap<>();
private volatile Boolean admin;
}
/**
* Every permission an engine or a project gives an app or an engine, as read at
* one time, by target ({@link SecurityEngine2EngineUtils#targetKey}).
*/
private static final class GranteePermissions {
private final long version;
private final long readAt;
private final Map<String, Map<EngineGrantee, AccessPermissionEnum>> byTarget;
private GranteePermissions(long version, long readAt,
Map<String, Map<EngineGrantee, AccessPermissionEnum>> byTarget) {
this.version = version;
this.readAt = readAt;
this.byTarget = byTarget;
}
}
}