Skip to content

Commit f49fd61

Browse files
committed
Fix compilation and tests for customAuthzEngine
Relates: elastic#123812
1 parent 59a55c8 commit f49fd61

File tree

2 files changed

+73
-51
lines changed

2 files changed

+73
-51
lines changed

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: 67 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,67 @@ 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+
() -> {
149+
final var resolvedIndicesListener = new SubscribableListener<ResolvedIndices>();
150+
resolvedIndicesListener.onResponse(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList()));
151+
return resolvedIndicesListener;
152+
},
153+
project
154+
);
155+
IndexAuthorizationResult result = safeAwait(resultListener);
145156
assertThat(result.isGranted(), is(true));
146157
IndicesAccessControl indicesAccessControl = result.getIndicesAccessControl();
147158
assertNotNull(indicesAccessControl.getIndexPermissions("index"));
148159
}
149160

150161
// unauthorized
151162
{
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);
163+
RequestInfo requestInfo = new RequestInfo(
164+
Authentication.newRealmAuthentication(new User("joe", "not_superuser"), new RealmRef("test", "test", "node")),
165+
new SearchRequest(),
166+
"indices:data/read/search",
167+
null
168+
);
156169
PlainActionFuture<AuthorizationInfo> future = new PlainActionFuture<>();
157170
engine.resolveAuthorizationInfo(requestInfo, future);
158171
AuthorizationInfo authzInfo = future.actionGet();
159172

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();
173+
final SubscribableListener<IndexAuthorizationResult> resultListener = engine.authorizeIndexAction(
174+
requestInfo,
175+
authzInfo,
176+
() -> {
177+
final var resolvedIndicesListener = new SubscribableListener<ResolvedIndices>();
178+
resolvedIndicesListener.onResponse(new ResolvedIndices(Collections.singletonList("index"), Collections.emptyList()));
179+
return resolvedIndicesListener;
180+
},
181+
project
182+
);
183+
IndexAuthorizationResult result = safeAwait(resultListener);
165184
assertThat(result.isGranted(), is(false));
166185
IndicesAccessControl indicesAccessControl = result.getIndicesAccessControl();
167186
assertNull(indicesAccessControl.getIndexPermissions("index"));
@@ -170,9 +189,12 @@ public void testAuthorizeIndexAction() {
170189

171190
private RequestInfo getRequestInfo() {
172191
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"));
192+
final TransportRequest request = new TransportRequest() {
193+
};
194+
final Authentication authentication = Authentication.newRealmAuthentication(
195+
new User("joe", "custom_superuser"),
196+
new RealmRef("test", "test", "node")
197+
);
176198
return new RequestInfo(authentication, request, action, null);
177199
}
178200
}

0 commit comments

Comments
 (0)