/
BasicSecuritySystem.java
720 lines (613 loc) · 24.8 KB
/
BasicSecuritySystem.java
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
* ome.security.basic.BasicSecuritySystem
*
* Copyright 2006 University of Dundee. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.security.basic;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import ome.api.local.LocalAdmin;
import ome.api.local.LocalQuery;
import ome.api.local.LocalUpdate;
import ome.conditions.ApiUsageException;
import ome.conditions.InternalException;
import ome.conditions.SecurityViolation;
import ome.conditions.SessionTimeoutException;
import ome.model.IObject;
import ome.model.enums.EventType;
import ome.model.internal.Details;
import ome.model.internal.GraphHolder;
import ome.model.internal.Permissions;
import ome.model.internal.Permissions.Right;
import ome.model.internal.Permissions.Role;
import ome.model.internal.Token;
import ome.model.meta.Event;
import ome.model.meta.EventLog;
import ome.model.meta.Experimenter;
import ome.model.meta.ExperimenterGroup;
import ome.model.meta.GroupExperimenterMap;
import ome.security.AdminAction;
import ome.security.SecureAction;
import ome.security.SecurityFilter;
import ome.security.SecurityFilterHolder;
import ome.security.SecuritySystem;
import ome.security.SystemTypes;
import ome.security.policy.DefaultPolicyService;
import ome.security.policy.PolicyService;
import ome.services.messages.EventLogMessage;
import ome.services.messages.EventLogsMessage;
import ome.services.sessions.SessionManager;
import ome.services.sessions.events.UserGroupUpdateEvent;
import ome.services.sessions.state.SessionCache;
import ome.services.sessions.stats.PerSessionStats;
import ome.services.sharing.ShareStore;
import ome.system.EventContext;
import ome.system.OmeroContext;
import ome.system.Principal;
import ome.system.Roles;
import ome.system.ServiceFactory;
import ome.tools.hibernate.ExtendedMetadata;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.proxy.HibernateProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.util.Assert;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
/**
* simplest implementation of {@link SecuritySystem}. Uses an ctor-injected
* {@link EventContext} and the {@link ThreadLocal ThreadLocal-}based
* {@link CurrentDetails} to provide the security infrastructure.
*
* @author Josh Moore, josh.moore at gmx.de
* @see Token
* @see SecuritySystem
* @see Details
* @see Permissions
* @since 3.0-M3
*/
public class BasicSecuritySystem implements SecuritySystem,
ApplicationContextAware, ApplicationListener<EventLogMessage> {
private final static Logger log = LoggerFactory.getLogger(BasicSecuritySystem.class);
protected final OmeroInterceptor interceptor;
protected final SystemTypes sysTypes;
protected final CurrentDetails cd;
protected final TokenHolder tokenHolder;
protected final Roles roles;
protected final SessionManager sessionManager;
protected final ServiceFactory sf;
protected final SecurityFilter filter;
protected final PolicyService policyService;
protected/* final */OmeroContext ctx;
protected/* final */ShareStore store;
/**
* Simplified factory method which generates all the security primitives
* internally. Primarily useful for generated testing instances.
* @param sm the session manager
* @param sf the session factory
* @param cache the session cache
* @return a configured security system
*/
public static BasicSecuritySystem selfConfigure(SessionManager sm,
ServiceFactory sf, SessionCache cache) {
CurrentDetails cd = new CurrentDetails(cache);
SystemTypes st = new SystemTypes();
TokenHolder th = new TokenHolder();
OmeroInterceptor oi = new OmeroInterceptor(new Roles(),
st, new ExtendedMetadata.Impl(),
cd, th, new PerSessionStats(cd));
Roles roles = new Roles();
SecurityFilterHolder holder = new SecurityFilterHolder(
cd, new OneGroupSecurityFilter(roles),
new AllGroupsSecurityFilter(null, roles),
new SharingSecurityFilter(roles, null));
BasicSecuritySystem sec = new BasicSecuritySystem(oi, st, cd, sm,
roles, sf, new TokenHolder(), holder, new DefaultPolicyService());
return sec;
}
/**
* Main public constructor for this {@link SecuritySystem} implementation.
* @param interceptor the OMERO interceptor for Hibernate
* @param sysTypes the system types
* @param cd the current details
* @param sessionManager the session manager
* @param roles the OMERO roles
* @param sf the session factory
* @param tokenHolder the token holder
* @param filter the security filter
* @param policyService the policy service
*/
public BasicSecuritySystem(OmeroInterceptor interceptor,
SystemTypes sysTypes, CurrentDetails cd,
SessionManager sessionManager, Roles roles, ServiceFactory sf,
TokenHolder tokenHolder, SecurityFilter filter,
PolicyService policyService) {
this.sessionManager = sessionManager;
this.policyService = policyService;
this.tokenHolder = tokenHolder;
this.interceptor = interceptor;
this.sysTypes = sysTypes;
this.filter = filter;
this.roles = roles;
this.cd = cd;
this.sf = sf;
}
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
this.ctx = (OmeroContext) arg0;
this.store = this.ctx.getBean("shareStore", ShareStore.class);
}
// ~ Login/logout
// =========================================================================
public void login(Principal principal) {
cd.login(principal);
}
public int logout() {
return cd.logout();
}
// ~ Checks
// =========================================================================
/**
* implements {@link SecuritySystem#isReady()}. Simply checks for null
* values in all the relevant fields of {@link CurrentDetails}
*/
public boolean isReady() {
return cd.isReady();
}
/**
* classes which cannot be created by regular users.
*
* @see <a
* href="http://trac.openmicroscopy.org.uk/ome/ticket/156">ticket156</a>
*/
public boolean isSystemType(Class<? extends IObject> klass) {
return sysTypes.isSystemType(klass);
}
/**
* tests whether or not the current user is either the owner of this entity,
* or the superivsor of this entity, for example as root or as group owner.
*
* @param iObject
* Non-null managed entity.
* @return true if the current user is owner or supervisor of this entity
*/
public boolean isOwnerOrSupervisor(IObject iObject) {
return cd.isOwnerOrSupervisor(iObject);
}
// ~ Read security
// =========================================================================
/**
* enables the read filter such that graph queries will have non-visible
* entities silently removed from the return value. This filter does <em>
* not</em>
* apply to single value loads from the database. See
* {@link ome.security.ACLVoter#allowLoad(Session, Class, Details, long)} for more.
*
* Note: this filter must be disabled on logout, otherwise the necessary
* parameters (current user, current group, etc.) for building the filters
* will not be available. Similarly, while enabling this filter, no calls
* should be made on the given session object.
*
* @param session
* a generic session object which can be used to enable this
* filter. Each {@link SecuritySystem} implementation will
* require a specific session type.
* @see EventHandler#invoke(org.aopalliance.intercept.MethodInvocation)
*/
public void enableReadFilter(Object session) {
if (session == null || !(session instanceof Session)) {
throw new ApiUsageException(
"The Object argument to enableReadFilter"
+ " in the BasicSystemSecurity implementation must be a "
+ " non-null org.hibernate.Session.");
}
checkReady("enableReadFilter");
// beware
// http://opensource.atlassian.com/projects/hibernate/browse/HHH-1932
final EventContext ec = getEventContext();
final Session sess = (Session) session;
filter.enable(sess, ec);
}
public void updateReadFilter(Session session) {
filter.disable(session);
enableReadFilter(session);
}
/**
* disable this filer. All future queries will have no security context
* associated with them and all items will be visible.
*
* @param session
* a generic session object which can be used to disable this
* filter. Each {@link SecuritySystem} implementation will
* require a specifc session type.
* @see EventHandler#invoke(org.aopalliance.intercept.MethodInvocation)
*/
public void disableReadFilter(Object session) {
// Session system doesn't seem to provide this
// i.e. isReady() is false here. Disabling but need to review
// checkReady("disableReadFilter");
Session sess = (Session) session;
filter.disable(sess);
}
// ~ Subsystem disabling
// =========================================================================
public void disable(String... ids) {
if (ids == null || ids.length == 0) {
throw new ApiUsageException("Ids should not be empty.");
}
cd.addAllDisabled(ids);
}
public void enable(String... ids) {
if (ids == null || ids.length == 0) {
cd.clearDisabled();
}
cd.removeAllDisabled(ids);
}
public boolean isDisabled(String id) {
if (id == null) {
throw new ApiUsageException("Id should not be null.");
}
return cd.isDisabled(id);
}
// OmeroInterceptor delegation
// =========================================================================
public Details newTransientDetails(IObject object)
throws ApiUsageException, SecurityViolation {
checkReady("transientDetails");
return interceptor.newTransientDetails(object);
}
public Details checkManagedDetails(IObject object, Details trustedDetails)
throws ApiUsageException, SecurityViolation {
checkReady("managedDetails");
return interceptor.checkManagedDetails(object, trustedDetails);
}
// ~ CurrentDetails delegation (ensures proper settings of Tokens)
// =========================================================================
public boolean isGraphCritical(Details details) {
checkReady("isGraphCritical");
return cd.isGraphCritical(details);
}
public void loadEventContext(boolean isReadOnly) {
loadEventContext(isReadOnly, false);
}
public void loadEventContext(boolean isReadOnly, boolean isClose) {
final LocalAdmin admin = (LocalAdmin) sf.getAdminService();
final LocalUpdate update = (LocalUpdate) sf.getUpdateService();
// Call to session manager throws an exception on failure
final Principal p = clearAndCheckPrincipal();
// ticket:6639 - Rather than catch the RemoveSessionException
// we are going to check the type of the context and if it
// matches, then we know we should do no more loading.
EventContext ec = cd.getCurrentEventContext();
if (ec instanceof BasicSecurityWiring.CloseOnNoSessionContext) {
throw new SessionTimeoutException("closing", ec);
}
// ticket:1855 - Catching SessionTimeoutException in order to permit
// the close of a stateful service.
try {
ec = sessionManager.getEventContext(p);
} catch (SessionTimeoutException ste) {
if (!isClose) {
throw ste;
}
ec = (EventContext) ste.sessionContext;
}
// Refill current details
cd.checkAndInitialize(ec, admin, store);
ec = cd.getCurrentEventContext(); // Replace with callContext
// Experimenter
Experimenter exp;
if (isReadOnly) {
exp = new Experimenter(ec.getCurrentUserId(), false);
} else {
exp = admin.userProxy(ec.getCurrentUserId());
}
tokenHolder.setToken(exp.getGraphHolder());
// isAdmin
boolean isAdmin = false;
for (long gid : ec.getMemberOfGroupsList()) {
if (roles.getSystemGroupId() == gid) {
isAdmin = true;
break;
}
}
// Active group - starting with #3529, the current group and the current
// share values should be definitive as setting the context on
// BasicEventContext will automatically update the global values. For
// security reasons, we need only guarantee that non-admins are
// actually members of the noted groups.
//
// Joined with public group block (ticket:1940)
Long shareId = ec.getCurrentShareId();
Long groupId = ec.getCurrentGroupId();
ExperimenterGroup callGroup = null;
ExperimenterGroup eventGroup = null;
long eventGroupId;
Permissions callPerms;
// Code copied in SessionManagerImpl
if (groupId >= 0) { // negative groupId means all member groups
eventGroupId = groupId;
callGroup = admin.groupProxy(groupId);
eventGroup = callGroup;
callPerms = callGroup.getDetails().getPermissions();
// tickets:2950, 1940, 3529
if (!isAdmin && !ec.getMemberOfGroupsList().contains(groupId)) {
if (!callPerms.isGranted(Role.WORLD, Right.READ)) {
throw new SecurityViolation(String.format(
"User %s is not a member of group %s and cannot login",
ec.getCurrentUserId(), groupId));
}
}
} else {
List<Long> memList = ec.getMemberOfGroupsList();
eventGroupId = memList.get(0);
if (eventGroupId == roles.getUserGroupId() && memList.size() > 1) {
eventGroupId = memList.get(1);
}
log.debug("Choice for event group: " + eventGroupId);
eventGroup = admin.getGroup(eventGroupId);
callGroup = new ExperimenterGroup(groupId, false);
callPerms = Permissions.DUMMY;
}
long sessionId = ec.getCurrentSessionId().longValue();
ome.model.meta.Session sess = null;
if (isReadOnly) {
sess = new ome.model.meta.Session(sessionId, false);
} else {
sess = sf.getQueryService().get(ome.model.meta.Session.class, sessionId);
}
tokenHolder.setToken(callGroup.getGraphHolder());
// In order to less frequently access the ThreadLocal in CurrentDetails
// All properties are now set in one shot, except for Event.
cd.setValues(exp, callGroup, callPerms, isAdmin, isReadOnly, shareId);
// Event
String t = p.getEventType();
if (t == null) {
t = ec.getCurrentEventType();
}
EventType type = new EventType(t);
tokenHolder.setToken(type.getGraphHolder());
Event event = cd.newEvent(sess, type, tokenHolder);
tokenHolder.setToken(event.getGraphHolder());
// If this event is not read only, then lets save this event to prevent
// flushing issues later.
if (!isReadOnly) {
if (event.getExperimenterGroup().getId() < 0) {
event.setExperimenterGroup(eventGroup);
}
cd.updateEvent(update.saveAndReturnObject(event)); // TODO use merge
}
}
private Principal clearAndCheckPrincipal() {
// clear even if this fails. (make SecuritySystem unusable)
invalidateEventContext();
if (cd.size() == 0) {
throw new SecurityViolation(
"Principal is null. Not logged in to SecuritySystem.");
}
final Principal p = cd.getLast();
if (p.getName() == null) {
throw new InternalException(
"Principal.name is null. Security system failure.");
}
return p;
}
public void addLog(String action, Class klass, Long id) {
cd.addLog(action, klass, id);
}
public List<EventLog> getLogs() {
return cd.getLogs();
}
public void clearLogs() {
if (log.isDebugEnabled()) {
log.debug("Clearing EventLogs.");
}
List<EventLog> logs = getLogs();
if (!logs.isEmpty()) {
boolean foundAdminType = false;
final Multimap<String, EventLog> map = ArrayListMultimap.create();
for (EventLog el : getLogs()) {
String t = el.getEntityType();
if (Experimenter.class.getName().equals(t)
|| ExperimenterGroup.class.getName().equals(t)
|| GroupExperimenterMap.class.getName().equals(t)) {
foundAdminType = true;
}
map.put(t, el);
}
if (ctx == null) {
log.error("No context found for publishing");
} else {
// publish message if administrative type is modified
if (foundAdminType) {
this.ctx.publishEvent(new UserGroupUpdateEvent(this));
}
this.ctx.publishEvent(new EventLogsMessage(this, map));
}
}
cd.clearLogs();
}
public void invalidateEventContext() {
if (log.isDebugEnabled()) {
log.debug("Invalidating current EventContext.");
}
cd.invalidateCurrentEventContext();
}
// ~ Tokens & Actions
// =========================================================================
/**
*
* It would be better to catch the
* {@link SecureAction#updateObject(IObject...)} method in a try/finally block,
* but since flush can be so poorly controlled that's not possible. instead,
* we use the one time token which is removed this Object is checked for
* {@link #hasPrivilegedToken(IObject) privileges}.
*
* @param objs
* A managed (non-detached) entity. Not null.
* @param action
* A code-block that will be given the entity argument with a
* {@link #hasPrivilegedToken(IObject)} privileged token}.
*/
public <T extends IObject> T doAction(SecureAction action, T... objs) {
Assert.notNull(objs);
Assert.notEmpty(objs);
Assert.notNull(action);
final LocalQuery query = (LocalQuery) sf.getQueryService();
final List<GraphHolder> ghs = new ArrayList<GraphHolder>();
for (T obj : objs) {
// TODO inject
if (obj.getId() != null && !query.contains(obj)) {
throw new SecurityViolation("Services are not allowed to call "
+ "doAction() on non-Session-managed entities.");
}
// ticket:1794 - use of IQuery.get along with doAction() creates
// two objects (outer proxy and inner target) and only the outer
// proxy has its graph holder modified without this block, leading
// to security violations on flush since no token is present.
if (obj instanceof HibernateProxy) {
HibernateProxy hp = (HibernateProxy) obj;
IObject obj2 = (IObject) hp.getHibernateLazyInitializer().getImplementation();
ghs.add(obj2.getGraphHolder());
}
// FIXME
// Token oneTimeToken = new Token();
// oneTimeTokens.put(oneTimeToken);
ghs.add(obj.getGraphHolder());
}
// Holding onto the graph holders since they protect the access
// to their tokens
for (GraphHolder graphHolder : ghs) {
tokenHolder.setToken(graphHolder); // oneTimeToken
}
T retVal;
try {
retVal = action.updateObject(objs);
} finally {
for (GraphHolder graphHolder : ghs) {
tokenHolder.clearToken(graphHolder);
}
}
return retVal;
}
/**
* Calls {@link #runAsAdmin(AdminAction)} with a null-group id.
*/
public void runAsAdmin(final AdminAction action) {
runAsAdmin(null, action);
}
/**
* merge event is disabled for {@link #runAsAdmin(AdminAction)} because
* passing detached (client-side) entities to this method is particularly
* dangerous.
*/
public void runAsAdmin(final ExperimenterGroup group, final AdminAction action) {
Assert.notNull(action);
// Need to check here so that no exception is thrown
// during the try block below
checkReady("runAsAdmin");
final LocalQuery query = (LocalQuery) sf.getQueryService();
query.execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
BasicEventContext c = cd.current();
boolean wasAdmin = c.isCurrentUserAdmin();
ExperimenterGroup oldGroup = c.getGroup();
try {
c.setAdmin(true);
if (group != null) {
c.setGroup(group, group.getDetails().getPermissions());
}
disable(MergeEventListener.MERGE_EVENT);
enableReadFilter(session);
action.runAsAdmin();
} finally {
c.setAdmin(wasAdmin);
if (group != null) {
c.setGroup(oldGroup, oldGroup.getDetails().getPermissions());
}
enable(MergeEventListener.MERGE_EVENT);
enableReadFilter(session); // Now as non-admin
}
return null;
}
});
}
/**
* @see TokenHolder#copyToken(IObject, IObject)
*/
public void copyToken(IObject source, IObject copy) {
tokenHolder.copyToken(source, copy);
}
/**
* @see TokenHolder#hasPrivilegedToken(IObject)
*/
public boolean hasPrivilegedToken(IObject obj) {
return tokenHolder.hasPrivilegedToken(obj);
}
public void checkRestriction(String name, IObject obj) {
policyService.checkRestriction(name, obj);
}
// ~ Configured Elements
// =========================================================================
public Roles getSecurityRoles() {
return roles;
}
public EventContext getEventContext(boolean refresh) {
EventContext ec = cd.getCurrentEventContext();
if (refresh) {
String uuid = ec.getCurrentSessionUuid();
ec = sessionManager.reload(uuid);
}
return ec;
}
public EventContext getEventContext() {
return getEventContext(false);
}
/**
* Returns the Id of the currently logged in user.
* Returns owner of the share while in share
* @return See above.
*/
public Long getEffectiveUID() {
final EventContext ec = getEventContext();
final Long shareId = ec.getCurrentShareId();
if (shareId != null) {
if (shareId < 0) {
return null;
}
ome.model.meta.Session s = sf.getQueryService().get(
ome.model.meta.Session.class, shareId);
return s.getOwner().getId();
}
return ec.getCurrentUserId();
}
// ~ Helpers
// =========================================================================
/**
* calls {@link #isReady()} and if not throws an {@link ApiUsageException}.
* The {@link SecuritySystem} must be in a valid state to perform several
* functions.
*/
protected void checkReady(String method) {
if (!isReady()) {
throw new ApiUsageException("The security system is not ready.\n"
+ "Cannot execute: " + method);
}
}
public void onApplicationEvent(EventLogMessage elm) {
if (elm != null) {
for (Long id : elm.entityIds) {
addLog(elm.action, elm.entityType, id);
}
}
}
}