Skip to content

Commit e4f3478

Browse files
committed
Fix CustomAuthorizationEngine
1 parent b892e44 commit e4f3478

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

plugins/examples/security-authorization-engine/src/main/java/org/elasticsearch/example/CustomAuthorizationEngine.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.List;
4242
import java.util.Map;
4343
import java.util.Set;
44+
import java.util.function.Supplier;
4445
import java.util.stream.Collectors;
4546

4647
/**
@@ -87,26 +88,28 @@ public void authorizeClusterAction(RequestInfo requestInfo, AuthorizationInfo au
8788
}
8889

8990
@Override
90-
SubscribableListener<IndexAuthorizationResult> void authorizeIndexAction(
91+
public SubscribableListener<IndexAuthorizationResult> authorizeIndexAction(
9192
RequestInfo requestInfo,
9293
AuthorizationInfo authorizationInfo,
9394
AsyncSupplier<ResolvedIndices> indicesAsyncSupplier,
9495
ProjectMetadata project
9596
) {
9697
if (isSuperuser(requestInfo.getAuthentication().getEffectiveSubject().getUser())) {
97-
ActionListener<IndexAuthorizationResult> listener = new SubscribableListener<>();
98-
indicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> {
99-
Map<String, IndexAccessControl> indexAccessControlMap = new HashMap<>();
100-
for (String name : resolvedIndices.getLocal()) {
101-
indexAccessControlMap.put(name, new IndexAccessControl(FieldPermissions.DEFAULT, null));
102-
}
103-
IndicesAccessControl indicesAccessControl =
104-
new IndicesAccessControl(true, Collections.unmodifiableMap(indexAccessControlMap));
105-
listener.onResponse(new IndexAuthorizationResult(indicesAccessControl));
106-
}, listener::onFailure));
98+
SubscribableListener<IndexAuthorizationResult> listener = new SubscribableListener<>();
99+
indicesAsyncSupplier.getAsync().addListener(listener.delegateFailureAndWrap(
100+
(delegateListener, resolvedIndices) -> {
101+
Map<String, IndexAccessControl> indexAccessControlMap = new HashMap<>();
102+
for (String name : resolvedIndices.getLocal()) {
103+
indexAccessControlMap.put(name, new IndexAccessControl(FieldPermissions.DEFAULT, null));
104+
}
105+
IndicesAccessControl indicesAccessControl =
106+
new IndicesAccessControl(true, Collections.unmodifiableMap(indexAccessControlMap));
107+
listener.onResponse(new IndexAuthorizationResult(indicesAccessControl));
108+
})
109+
);
107110
return listener;
108111
} else {
109-
return SubscribableListener.succcess(new IndexAuthorizationResult(IndicesAccessControl.DENIED));
112+
return SubscribableListener.newSucceeded(new IndexAuthorizationResult(IndicesAccessControl.DENIED));
110113
}
111114
}
112115

@@ -119,19 +122,21 @@ public void loadAuthorizedIndices(
119122
) {
120123
if (isSuperuser(requestInfo.getAuthentication().getEffectiveSubject().getUser())) {
121124
listener.onResponse(new AuthorizedIndices() {
122-
public Set<String> all(IndexComponentSelector selector) {
123-
return () -> indicesLookup.keySet();
125+
public Supplier<Set<String>> all() {
126+
return indicesLookup::keySet;
124127
}
125-
public boolean check(String name, IndexComponentSelector selector) {
128+
129+
public boolean check(String name) {
126130
return indicesLookup.containsKey(name);
127131
}
128132
});
129133
} else {
130134
listener.onResponse(new AuthorizedIndices() {
131-
public Set<String> all(IndexComponentSelector selector) {
135+
public Supplier<Set<String>> all() {
132136
return () -> Set.of();
133137
}
134-
public boolean check(String name, IndexComponentSelector selector) {
138+
139+
public boolean check(String name) {
135140
return false;
136141
}
137142
});
@@ -259,6 +264,6 @@ private boolean isSuperuser(User user) {
259264

260265
private boolean isSuperuser(AuthorizationInfo authorizationInfo) {
261266
assert authorizationInfo instanceof CustomAuthorizationInfo;
262-
return Arrays.asList(((CustomAuthorizationInfo)authorizationInfo).asMap().get("roles")).contains("custom_superuser");
267+
return Arrays.asList(((CustomAuthorizationInfo) authorizationInfo).asMap().get("roles")).contains("custom_superuser");
263268
}
264269
}

plugins/examples/security-authorization-engine/src/test/java/org/elasticsearch/example/CustomAuthorizationEngineTests.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.elasticsearch.action.search.SearchRequest;
1313
import org.elasticsearch.action.support.PlainActionFuture;
14+
import org.elasticsearch.action.support.SubscribableListener;
1415
import org.elasticsearch.cluster.metadata.IndexAbstraction;
1516
import org.elasticsearch.cluster.metadata.IndexAbstraction.ConcreteIndex;
1617
import org.elasticsearch.cluster.metadata.IndexMetadata;
@@ -52,7 +53,8 @@ public void testGetAuthorizationInfo() {
5253

5354
public void testAuthorizeRunAs() {
5455
final String action = "cluster:monitor/foo";
55-
final TransportRequest request = new TransportRequest() {};
56+
final TransportRequest request = new TransportRequest() {
57+
};
5658
CustomAuthorizationEngine engine = new CustomAuthorizationEngine();
5759
// unauthorized
5860
{
@@ -125,8 +127,8 @@ public void testAuthorizeIndexAction() {
125127
.numberOfShards(1)
126128
.numberOfReplicas(0)
127129
.build(),
128-
false
129-
).build();
130+
false
131+
).build();
130132
// authorized
131133
{
132134
RequestInfo requestInfo =
@@ -138,9 +140,10 @@ public void testAuthorizeIndexAction() {
138140
AuthorizationInfo authzInfo = future.actionGet();
139141

140142
PlainActionFuture<IndexAuthorizationResult> resultFuture = new PlainActionFuture<>();
141-
engine.authorizeIndexAction(requestInfo, authzInfo,
142-
listener -> listener.onResponse(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
143-
project, resultFuture);
143+
SubscribableListener<IndexAuthorizationResult> l = engine.authorizeIndexAction(requestInfo, authzInfo,
144+
() -> SubscribableListener.newSucceeded(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
145+
project);
146+
l.addListener(resultFuture);
144147
IndexAuthorizationResult result = resultFuture.actionGet();
145148
assertThat(result.isGranted(), is(true));
146149
IndicesAccessControl indicesAccessControl = result.getIndicesAccessControl();
@@ -159,8 +162,8 @@ public void testAuthorizeIndexAction() {
159162

160163
PlainActionFuture<IndexAuthorizationResult> resultFuture = new PlainActionFuture<>();
161164
engine.authorizeIndexAction(requestInfo, authzInfo,
162-
listener -> listener.onResponse(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
163-
project, resultFuture);
165+
() -> SubscribableListener.newSucceeded(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
166+
project).addListener(resultFuture);
164167
IndexAuthorizationResult result = resultFuture.actionGet();
165168
assertThat(result.isGranted(), is(false));
166169
IndicesAccessControl indicesAccessControl = result.getIndicesAccessControl();
@@ -170,7 +173,8 @@ public void testAuthorizeIndexAction() {
170173

171174
private RequestInfo getRequestInfo() {
172175
final String action = "cluster:monitor/foo";
173-
final TransportRequest request = new TransportRequest() {};
176+
final TransportRequest request = new TransportRequest() {
177+
};
174178
final Authentication authentication =
175179
Authentication.newRealmAuthentication(new User("joe", "custom_superuser"), new RealmRef("test", "test", "node"));
176180
return new RequestInfo(authentication, request, action, null);

0 commit comments

Comments
 (0)