Skip to content

Commit 6c04abc

Browse files
authored
Fix compilation and tests for customAuthzEngine (#125469)
Relates: #123812
1 parent 59a55c8 commit 6c04abc

File tree

3 files changed

+68
-52
lines changed

3 files changed

+68
-52
lines changed

plugins/examples/custom-processor/src/main/java/org/elasticsearch/example/customprocessor/ExampleRepeatProcessor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.elasticsearch.example.customprocessor;
22

3+
import org.elasticsearch.cluster.metadata.ProjectId;
34
import org.elasticsearch.ingest.AbstractProcessor;
45
import org.elasticsearch.ingest.ConfigurationUtils;
56
import org.elasticsearch.ingest.IngestDocument;
@@ -44,7 +45,8 @@ public ExampleRepeatProcessor create(
4445
Map<String, Processor.Factory> registry,
4546
String tag,
4647
String description,
47-
Map<String, Object> config
48+
Map<String, Object> config,
49+
ProjectId projectId
4850
) {
4951
String field = ConfigurationUtils.readStringProperty(TYPE, tag, config, FIELD_KEY_NAME);
5052
return new ExampleRepeatProcessor(tag, description, field);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ public void authorizeClusterAction(RequestInfo requestInfo, AuthorizationInfo au
8787
}
8888

8989
@Override
90-
SubscribableListener<IndexAuthorizationResult> void authorizeIndexAction(
90+
public SubscribableListener<IndexAuthorizationResult> authorizeIndexAction(
9191
RequestInfo requestInfo,
9292
AuthorizationInfo authorizationInfo,
9393
AsyncSupplier<ResolvedIndices> indicesAsyncSupplier,
9494
ProjectMetadata project
9595
) {
9696
if (isSuperuser(requestInfo.getAuthentication().getEffectiveSubject().getUser())) {
97-
ActionListener<IndexAuthorizationResult> listener = new SubscribableListener<>();
98-
indicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> {
97+
SubscribableListener<IndexAuthorizationResult> listener = new SubscribableListener<>();
98+
indicesAsyncSupplier.getAsync().addListener(ActionListener.wrap(resolvedIndices -> {
9999
Map<String, IndexAccessControl> indexAccessControlMap = new HashMap<>();
100100
for (String name : resolvedIndices.getLocal()) {
101101
indexAccessControlMap.put(name, new IndexAccessControl(FieldPermissions.DEFAULT, null));
@@ -106,7 +106,7 @@ SubscribableListener<IndexAuthorizationResult> void authorizeIndexAction(
106106
}, listener::onFailure));
107107
return listener;
108108
} else {
109-
return SubscribableListener.succcess(new IndexAuthorizationResult(IndicesAccessControl.DENIED));
109+
return SubscribableListener.newSucceeded(new IndexAuthorizationResult(IndicesAccessControl.DENIED));
110110
}
111111
}
112112

@@ -120,7 +120,7 @@ public void loadAuthorizedIndices(
120120
if (isSuperuser(requestInfo.getAuthentication().getEffectiveSubject().getUser())) {
121121
listener.onResponse(new AuthorizedIndices() {
122122
public Set<String> all(IndexComponentSelector selector) {
123-
return () -> indicesLookup.keySet();
123+
return indicesLookup.keySet();
124124
}
125125
public boolean check(String name, IndexComponentSelector selector) {
126126
return indicesLookup.containsKey(name);
@@ -129,7 +129,7 @@ public boolean check(String name, IndexComponentSelector selector) {
129129
} else {
130130
listener.onResponse(new AuthorizedIndices() {
131131
public Set<String> all(IndexComponentSelector selector) {
132-
return () -> Set.of();
132+
return Set.of();
133133
}
134134
public boolean check(String name, IndexComponentSelector selector) {
135135
return false;

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

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111

1212
import org.elasticsearch.action.search.SearchRequest;
1313
import org.elasticsearch.action.support.PlainActionFuture;
14-
import org.elasticsearch.cluster.metadata.IndexAbstraction;
15-
import org.elasticsearch.cluster.metadata.IndexAbstraction.ConcreteIndex;
14+
import org.elasticsearch.action.support.SubscribableListener;
1615
import org.elasticsearch.cluster.metadata.IndexMetadata;
17-
import org.elasticsearch.cluster.metadata.Metadata;
1816
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1917
import org.elasticsearch.common.settings.Settings;
2018
import org.elasticsearch.index.IndexVersion;
@@ -31,9 +29,6 @@
3129
import org.elasticsearch.xpack.core.security.user.User;
3230

3331
import java.util.Collections;
34-
import java.util.HashMap;
35-
import java.util.Map;
36-
import java.util.stream.Stream;
3732

3833
import static org.hamcrest.Matchers.is;
3934

@@ -52,13 +47,15 @@ public void testGetAuthorizationInfo() {
5247

5348
public void testAuthorizeRunAs() {
5449
final String action = "cluster:monitor/foo";
55-
final TransportRequest request = new TransportRequest() {};
50+
final TransportRequest request = new TransportRequest() {
51+
};
5652
CustomAuthorizationEngine engine = new CustomAuthorizationEngine();
5753
// unauthorized
5854
{
59-
Authentication authentication = Authentication
60-
.newRealmAuthentication(new User("bar", "not_superuser"), new RealmRef("test", "test", "node"))
61-
.runAs(new User("joe", "custom_superuser"), new RealmRef("test", "test", "node"));
55+
Authentication authentication = Authentication.newRealmAuthentication(
56+
new User("bar", "not_superuser"),
57+
new RealmRef("test", "test", "node")
58+
).runAs(new User("joe", "custom_superuser"), new RealmRef("test", "test", "node"));
6259
RequestInfo info = new RequestInfo(authentication, request, action, null);
6360
PlainActionFuture<AuthorizationInfo> future = new PlainActionFuture<>();
6461
engine.resolveAuthorizationInfo(info, future);
@@ -72,9 +69,10 @@ public void testAuthorizeRunAs() {
7269

7370
// authorized
7471
{
75-
Authentication authentication = Authentication
76-
.newRealmAuthentication(new User("bar", "custom_superuser"), new RealmRef("test", "test", "node"))
77-
.runAs(new User("joe", "not_superuser"), new RealmRef("test", "test", "node"));
72+
Authentication authentication = Authentication.newRealmAuthentication(
73+
new User("bar", "custom_superuser"),
74+
new RealmRef("test", "test", "node")
75+
).runAs(new User("joe", "not_superuser"), new RealmRef("test", "test", "node"));
7876
RequestInfo info = new RequestInfo(authentication, request, action, null);
7977
PlainActionFuture<AuthorizationInfo> future = new PlainActionFuture<>();
8078
engine.resolveAuthorizationInfo(info, future);
@@ -103,10 +101,12 @@ public void testAuthorizeClusterAction() {
103101

104102
// unauthorized
105103
{
106-
RequestInfo unauthReqInfo =
107-
new RequestInfo(
108-
Authentication.newRealmAuthentication(new User("joe", "not_superuser"), new RealmRef("test", "test", "node")),
109-
requestInfo.getRequest(), requestInfo.getAction(), null);
104+
RequestInfo unauthReqInfo = new RequestInfo(
105+
Authentication.newRealmAuthentication(new User("joe", "not_superuser"), new RealmRef("test", "test", "node")),
106+
requestInfo.getRequest(),
107+
requestInfo.getAction(),
108+
null
109+
);
110110
PlainActionFuture<AuthorizationInfo> future = new PlainActionFuture<>();
111111
engine.resolveAuthorizationInfo(unauthReqInfo, future);
112112
AuthorizationInfo authzInfo = future.actionGet();
@@ -120,48 +120,59 @@ public void testAuthorizeClusterAction() {
120120

121121
public void testAuthorizeIndexAction() {
122122
CustomAuthorizationEngine engine = new CustomAuthorizationEngine();
123-
ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault()).put(IndexMetadata.builder("index")
124-
.settings(Settings.builder().put("index.version.created", IndexVersion.current()))
125-
.numberOfShards(1)
126-
.numberOfReplicas(0)
127-
.build(),
128-
false
129-
).build();
123+
ProjectMetadata project = ProjectMetadata.builder(randomProjectIdOrDefault())
124+
.put(
125+
IndexMetadata.builder("index")
126+
.settings(Settings.builder().put("index.version.created", IndexVersion.current()))
127+
.numberOfShards(1)
128+
.numberOfReplicas(0)
129+
.build(),
130+
false
131+
)
132+
.build();
130133
// authorized
131134
{
132-
RequestInfo requestInfo =
133-
new RequestInfo(
134-
Authentication.newRealmAuthentication(new User("joe", "custom_superuser"), new RealmRef("test", "test", "node")),
135-
new SearchRequest(), "indices:data/read/search", null);
135+
RequestInfo requestInfo = new RequestInfo(
136+
Authentication.newRealmAuthentication(new User("joe", "custom_superuser"), new RealmRef("test", "test", "node")),
137+
new SearchRequest(),
138+
"indices:data/read/search",
139+
null
140+
);
136141
PlainActionFuture<AuthorizationInfo> future = new PlainActionFuture<>();
137142
engine.resolveAuthorizationInfo(requestInfo, future);
138143
AuthorizationInfo authzInfo = future.actionGet();
139144

140-
PlainActionFuture<IndexAuthorizationResult> resultFuture = new PlainActionFuture<>();
141-
engine.authorizeIndexAction(requestInfo, authzInfo,
142-
listener -> listener.onResponse(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
143-
project, resultFuture);
144-
IndexAuthorizationResult result = resultFuture.actionGet();
145+
final SubscribableListener<IndexAuthorizationResult> resultListener = engine.authorizeIndexAction(
146+
requestInfo,
147+
authzInfo,
148+
() -> SubscribableListener.newSucceeded(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
149+
project
150+
);
151+
IndexAuthorizationResult result = safeAwait(resultListener);
145152
assertThat(result.isGranted(), is(true));
146153
IndicesAccessControl indicesAccessControl = result.getIndicesAccessControl();
147154
assertNotNull(indicesAccessControl.getIndexPermissions("index"));
148155
}
149156

150157
// unauthorized
151158
{
152-
RequestInfo requestInfo =
153-
new RequestInfo(
154-
Authentication.newRealmAuthentication(new User("joe", "not_superuser"), new RealmRef("test", "test", "node")),
155-
new SearchRequest(), "indices:data/read/search", null);
159+
RequestInfo requestInfo = new RequestInfo(
160+
Authentication.newRealmAuthentication(new User("joe", "not_superuser"), new RealmRef("test", "test", "node")),
161+
new SearchRequest(),
162+
"indices:data/read/search",
163+
null
164+
);
156165
PlainActionFuture<AuthorizationInfo> future = new PlainActionFuture<>();
157166
engine.resolveAuthorizationInfo(requestInfo, future);
158167
AuthorizationInfo authzInfo = future.actionGet();
159168

160-
PlainActionFuture<IndexAuthorizationResult> resultFuture = new PlainActionFuture<>();
161-
engine.authorizeIndexAction(requestInfo, authzInfo,
162-
listener -> listener.onResponse(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
163-
project, resultFuture);
164-
IndexAuthorizationResult result = resultFuture.actionGet();
169+
final SubscribableListener<IndexAuthorizationResult> resultListener = engine.authorizeIndexAction(
170+
requestInfo,
171+
authzInfo,
172+
() -> SubscribableListener.newSucceeded(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList())),
173+
project
174+
);
175+
IndexAuthorizationResult result = safeAwait(resultListener);
165176
assertThat(result.isGranted(), is(false));
166177
IndicesAccessControl indicesAccessControl = result.getIndicesAccessControl();
167178
assertNull(indicesAccessControl.getIndexPermissions("index"));
@@ -170,9 +181,12 @@ public void testAuthorizeIndexAction() {
170181

171182
private RequestInfo getRequestInfo() {
172183
final String action = "cluster:monitor/foo";
173-
final TransportRequest request = new TransportRequest() {};
174-
final Authentication authentication =
175-
Authentication.newRealmAuthentication(new User("joe", "custom_superuser"), new RealmRef("test", "test", "node"));
184+
final TransportRequest request = new TransportRequest() {
185+
};
186+
final Authentication authentication = Authentication.newRealmAuthentication(
187+
new User("joe", "custom_superuser"),
188+
new RealmRef("test", "test", "node")
189+
);
176190
return new RequestInfo(authentication, request, action, null);
177191
}
178192
}

0 commit comments

Comments
 (0)