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,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