1111
1212import org .elasticsearch .action .search .SearchRequest ;
1313import 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 ;
1615import org .elasticsearch .cluster .metadata .IndexMetadata ;
17- import org .elasticsearch .cluster .metadata .Metadata ;
1816import org .elasticsearch .cluster .metadata .ProjectMetadata ;
1917import org .elasticsearch .common .settings .Settings ;
2018import org .elasticsearch .index .IndexVersion ;
3129import org .elasticsearch .xpack .core .security .user .User ;
3230
3331import java .util .Collections ;
34- import java .util .HashMap ;
35- import java .util .Map ;
36- import java .util .stream .Stream ;
3732
3833import 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