|
5 | 5 | */
|
6 | 6 | package org.elasticsearch.xpack.core.security.authz.permission;
|
7 | 7 |
|
| 8 | +import org.elasticsearch.transport.TransportRequest; |
8 | 9 | import org.elasticsearch.xpack.core.security.authz.privilege.ClusterPrivilege;
|
9 | 10 |
|
| 11 | +import java.util.Collection; |
| 12 | +import java.util.Set; |
10 | 13 | import java.util.function.Predicate;
|
| 14 | +import java.util.stream.Collectors; |
11 | 15 |
|
12 | 16 | /**
|
13 |
| - * A permission that is based on privileges for cluster wide actions |
| 17 | + * A permission that is based on privileges for cluster wide actions, with the optional ability to inspect the request object |
14 | 18 | */
|
15 |
| -public final class ClusterPermission { |
16 |
| - |
17 |
| - public static final ClusterPermission NONE = new ClusterPermission(ClusterPrivilege.NONE); |
18 |
| - |
| 19 | +public abstract class ClusterPermission { |
19 | 20 | private final ClusterPrivilege privilege;
|
20 |
| - private final Predicate<String> predicate; |
21 | 21 |
|
22 | 22 | ClusterPermission(ClusterPrivilege privilege) {
|
23 | 23 | this.privilege = privilege;
|
24 |
| - this.predicate = privilege.predicate(); |
25 | 24 | }
|
26 | 25 |
|
27 | 26 | public ClusterPrivilege privilege() {
|
28 | 27 | return privilege;
|
29 | 28 | }
|
30 | 29 |
|
31 |
| - public boolean check(String action) { |
32 |
| - return predicate.test(action); |
| 30 | + public abstract boolean check(String action, TransportRequest request); |
| 31 | + |
| 32 | + /** |
| 33 | + * A permission that is based solely on cluster privileges and does not consider request state |
| 34 | + */ |
| 35 | + public static class SimpleClusterPermission extends ClusterPermission { |
| 36 | + |
| 37 | + public static final SimpleClusterPermission NONE = new SimpleClusterPermission(ClusterPrivilege.NONE); |
| 38 | + |
| 39 | + private final Predicate<String> predicate; |
| 40 | + |
| 41 | + SimpleClusterPermission(ClusterPrivilege privilege) { |
| 42 | + super(privilege); |
| 43 | + this.predicate = privilege.predicate(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean check(String action, TransportRequest request) { |
| 48 | + return predicate.test(action); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * A permission that makes use of both cluster privileges and request inspection |
| 54 | + */ |
| 55 | + public static class ConditionalClusterPermission extends ClusterPermission { |
| 56 | + private final Predicate<String> actionPredicate; |
| 57 | + private final Predicate<TransportRequest> requestPredicate; |
| 58 | + |
| 59 | + public ConditionalClusterPermission(ClusterPrivilege privilege, Predicate<TransportRequest> requestPredicate) { |
| 60 | + super(privilege); |
| 61 | + this.actionPredicate = privilege.predicate(); |
| 62 | + this.requestPredicate = requestPredicate; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean check(String action, TransportRequest request) { |
| 67 | + return actionPredicate.test(action) && requestPredicate.test(request); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * A permission that composes a number of other cluster permissions |
| 73 | + */ |
| 74 | + public static class CompositeClusterPermission extends ClusterPermission { |
| 75 | + private final Collection<ClusterPermission> children; |
| 76 | + |
| 77 | + public CompositeClusterPermission(Collection<ClusterPermission> children) { |
| 78 | + super(buildPrivilege(children)); |
| 79 | + this.children = children; |
| 80 | + } |
| 81 | + |
| 82 | + private static ClusterPrivilege buildPrivilege(Collection<ClusterPermission> children) { |
| 83 | + final Set<String> names = children.stream() |
| 84 | + .map(ClusterPermission::privilege) |
| 85 | + .map(ClusterPrivilege::name) |
| 86 | + .flatMap(Set::stream) |
| 87 | + .collect(Collectors.toSet()); |
| 88 | + return ClusterPrivilege.get(names); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean check(String action, TransportRequest request) { |
| 93 | + return children.stream().anyMatch(p -> p.check(action, request)); |
| 94 | + } |
33 | 95 | }
|
34 | 96 | }
|
0 commit comments