22
22
import org .elasticsearch .xpack .core .security .authc .Authentication ;
23
23
import org .elasticsearch .xpack .core .security .authc .Authentication .RealmRef ;
24
24
import org .elasticsearch .xpack .core .security .authc .AuthenticationField ;
25
+ import org .elasticsearch .xpack .core .security .authz .AuthorizationEngine .AuthorizationInfo ;
26
+ import org .elasticsearch .xpack .core .security .authz .AuthorizationServiceField ;
27
+ import org .elasticsearch .xpack .core .security .authz .accesscontrol .IndicesAccessControl ;
25
28
import org .elasticsearch .xpack .core .security .user .User ;
26
29
import org .elasticsearch .xpack .security .audit .AuditTrailService ;
27
- import org .elasticsearch .xpack .core .security .authz .AuthorizationEngine .AuthorizationInfo ;
28
30
29
31
import java .util .Collections ;
30
32
34
36
import static org .elasticsearch .xpack .security .authz .AuthorizationService .ORIGINATING_ACTION_KEY ;
35
37
import static org .elasticsearch .xpack .security .authz .AuthorizationServiceTests .authzInfoRoles ;
36
38
import static org .elasticsearch .xpack .security .authz .SecuritySearchOperationListener .ensureAuthenticatedUserIsSame ;
39
+ import static org .hamcrest .Matchers .is ;
40
+ import static org .hamcrest .Matchers .nullValue ;
37
41
import static org .mockito .Matchers .eq ;
38
42
import static org .mockito .Mockito .mock ;
39
43
import static org .mockito .Mockito .times ;
@@ -69,6 +73,8 @@ public void testOnNewContextSetsAuthentication() throws Exception {
69
73
AuditTrailService auditTrailService = mock (AuditTrailService .class );
70
74
Authentication authentication = new Authentication (new User ("test" , "role" ), new RealmRef ("realm" , "file" , "node" ), null );
71
75
authentication .writeToContext (threadContext );
76
+ IndicesAccessControl indicesAccessControl = mock (IndicesAccessControl .class );
77
+ threadContext .putTransient (AuthorizationServiceField .INDICES_PERMISSIONS_KEY , indicesAccessControl );
72
78
73
79
SecuritySearchOperationListener listener = new SecuritySearchOperationListener (threadContext , licenseState , auditTrailService );
74
80
listener .onNewScrollContext (testSearchContext );
@@ -77,6 +83,9 @@ public void testOnNewContextSetsAuthentication() throws Exception {
77
83
assertEquals (authentication , contextAuth );
78
84
assertEquals (scroll , testSearchContext .scrollContext ().scroll );
79
85
86
+ assertThat (testSearchContext .scrollContext ().getFromContext (AuthorizationServiceField .INDICES_PERMISSIONS_KEY ),
87
+ is (indicesAccessControl ));
88
+
80
89
verify (licenseState ).isAuthAllowed ();
81
90
verifyZeroInteractions (auditTrailService );
82
91
}
@@ -86,6 +95,8 @@ public void testValidateSearchContext() throws Exception {
86
95
testSearchContext .scrollContext (new ScrollContext ());
87
96
testSearchContext .scrollContext ().putInContext (AuthenticationField .AUTHENTICATION_KEY ,
88
97
new Authentication (new User ("test" , "role" ), new RealmRef ("realm" , "file" , "node" ), null ));
98
+ final IndicesAccessControl indicesAccessControl = mock (IndicesAccessControl .class );
99
+ testSearchContext .scrollContext ().putInContext (AuthorizationServiceField .INDICES_PERMISSIONS_KEY , indicesAccessControl );
89
100
testSearchContext .scrollContext ().scroll = new Scroll (TimeValue .timeValueSeconds (2L ));
90
101
XPackLicenseState licenseState = mock (XPackLicenseState .class );
91
102
when (licenseState .isAuthAllowed ()).thenReturn (true );
@@ -97,6 +108,7 @@ public void testValidateSearchContext() throws Exception {
97
108
Authentication authentication = new Authentication (new User ("test" , "role" ), new RealmRef ("realm" , "file" , "node" ), null );
98
109
authentication .writeToContext (threadContext );
99
110
listener .validateSearchContext (testSearchContext , Empty .INSTANCE );
111
+ assertThat (threadContext .getTransient (AuthorizationServiceField .INDICES_PERMISSIONS_KEY ), is (indicesAccessControl ));
100
112
verify (licenseState ).isAuthAllowed ();
101
113
verifyZeroInteractions (auditTrailService );
102
114
}
@@ -107,6 +119,7 @@ public void testValidateSearchContext() throws Exception {
107
119
Authentication authentication = new Authentication (new User ("test" , "role" ), new RealmRef (realmName , "file" , nodeName ), null );
108
120
authentication .writeToContext (threadContext );
109
121
listener .validateSearchContext (testSearchContext , Empty .INSTANCE );
122
+ assertThat (threadContext .getTransient (AuthorizationServiceField .INDICES_PERMISSIONS_KEY ), is (indicesAccessControl ));
110
123
verify (licenseState , times (2 )).isAuthAllowed ();
111
124
verifyZeroInteractions (auditTrailService );
112
125
}
@@ -123,6 +136,7 @@ public void testValidateSearchContext() throws Exception {
123
136
final InternalScrollSearchRequest request = new InternalScrollSearchRequest ();
124
137
SearchContextMissingException expected =
125
138
expectThrows (SearchContextMissingException .class , () -> listener .validateSearchContext (testSearchContext , request ));
139
+ assertThat (threadContext .getTransient (AuthorizationServiceField .INDICES_PERMISSIONS_KEY ), nullValue ());
126
140
assertEquals (testSearchContext .id (), expected .id ());
127
141
verify (licenseState , times (3 )).isAuthAllowed ();
128
142
verify (auditTrailService ).accessDenied (eq (null ), eq (authentication ), eq ("action" ), eq (request ),
@@ -141,6 +155,7 @@ public void testValidateSearchContext() throws Exception {
141
155
threadContext .putTransient (ORIGINATING_ACTION_KEY , "action" );
142
156
final InternalScrollSearchRequest request = new InternalScrollSearchRequest ();
143
157
listener .validateSearchContext (testSearchContext , request );
158
+ assertThat (threadContext .getTransient (AuthorizationServiceField .INDICES_PERMISSIONS_KEY ), is (indicesAccessControl ));
144
159
verify (licenseState , times (4 )).isAuthAllowed ();
145
160
verifyNoMoreInteractions (auditTrailService );
146
161
}
@@ -159,6 +174,7 @@ public void testValidateSearchContext() throws Exception {
159
174
final InternalScrollSearchRequest request = new InternalScrollSearchRequest ();
160
175
SearchContextMissingException expected =
161
176
expectThrows (SearchContextMissingException .class , () -> listener .validateSearchContext (testSearchContext , request ));
177
+ assertThat (threadContext .getTransient (AuthorizationServiceField .INDICES_PERMISSIONS_KEY ), nullValue ());
162
178
assertEquals (testSearchContext .id (), expected .id ());
163
179
verify (licenseState , times (5 )).isAuthAllowed ();
164
180
verify (auditTrailService ).accessDenied (eq (null ), eq (authentication ), eq ("action" ), eq (request ),
0 commit comments