@@ -143,7 +143,7 @@ private SecurityIndexManager.State dummyState(ClusterHealthStatus indexStatus) {
143
143
144
144
public void testCacheClearOnIndexHealthChange () {
145
145
final AtomicInteger numInvalidation = new AtomicInteger (0 );
146
- final NativeRoleMappingStore store = buildRoleMappingStoreForInvalidationTesting (numInvalidation );
146
+ final NativeRoleMappingStore store = buildRoleMappingStoreForInvalidationTesting (numInvalidation , true );
147
147
148
148
int expectedInvalidation = 0 ;
149
149
// existing to no longer present
@@ -180,7 +180,7 @@ public void testCacheClearOnIndexHealthChange() {
180
180
181
181
public void testCacheClearOnIndexOutOfDateChange () {
182
182
final AtomicInteger numInvalidation = new AtomicInteger (0 );
183
- final NativeRoleMappingStore store = buildRoleMappingStoreForInvalidationTesting (numInvalidation );
183
+ final NativeRoleMappingStore store = buildRoleMappingStoreForInvalidationTesting (numInvalidation , true );
184
184
185
185
store .onSecurityIndexStateChange (
186
186
new SecurityIndexManager .State (Instant .now (), false , true , true , null , concreteSecurityIndexName , null ),
@@ -193,40 +193,59 @@ public void testCacheClearOnIndexOutOfDateChange() {
193
193
assertEquals (2 , numInvalidation .get ());
194
194
}
195
195
196
- private NativeRoleMappingStore buildRoleMappingStoreForInvalidationTesting (AtomicInteger invalidationCounter ) {
196
+ public void testCacheIsNotClearedIfNoRealmsAreAttached () {
197
+ final AtomicInteger numInvalidation = new AtomicInteger (0 );
198
+ final NativeRoleMappingStore store = buildRoleMappingStoreForInvalidationTesting (numInvalidation , false );
199
+
200
+ final SecurityIndexManager .State noIndexState = dummyState (null );
201
+ final SecurityIndexManager .State greenIndexState = dummyState (ClusterHealthStatus .GREEN );
202
+ store .onSecurityIndexStateChange (noIndexState , greenIndexState );
203
+ assertEquals (0 , numInvalidation .get ());
204
+ }
205
+
206
+ private NativeRoleMappingStore buildRoleMappingStoreForInvalidationTesting (AtomicInteger invalidationCounter , boolean attachRealm ) {
197
207
final Settings settings = Settings .builder ().put ("path.home" , createTempDir ()).build ();
198
208
199
209
final ThreadPool threadPool = mock (ThreadPool .class );
200
210
final ThreadContext threadContext = new ThreadContext (settings );
201
211
when (threadPool .getThreadContext ()).thenReturn (threadContext );
202
212
213
+ final String realmName = randomAlphaOfLengthBetween (4 , 8 );
214
+
203
215
final Client client = mock (Client .class );
204
216
when (client .threadPool ()).thenReturn (threadPool );
205
217
when (client .settings ()).thenReturn (settings );
206
218
doAnswer (invocationOnMock -> {
219
+ assertThat (invocationOnMock .getArguments (), Matchers .arrayWithSize (3 ));
220
+ final ClearRealmCacheRequest request = (ClearRealmCacheRequest ) invocationOnMock .getArguments ()[1 ];
221
+ assertThat (request .realms (), Matchers .arrayContaining (realmName ));
222
+
207
223
ActionListener <ClearRealmCacheResponse > listener = (ActionListener <ClearRealmCacheResponse >) invocationOnMock .getArguments ()[2 ];
208
224
invalidationCounter .incrementAndGet ();
209
225
listener .onResponse (new ClearRealmCacheResponse (new ClusterName ("cluster" ), Collections .emptyList (), Collections .emptyList ()));
210
226
return null ;
211
227
}).when (client ).execute (eq (ClearRealmCacheAction .INSTANCE ), any (ClearRealmCacheRequest .class ), any (ActionListener .class ));
212
228
213
- final Environment env = TestEnvironment .newEnvironment (settings );
214
- final RealmConfig realmConfig = new RealmConfig (new RealmConfig .RealmIdentifier ("ldap" , getTestName ()),
215
- settings , env , threadContext );
216
- final CachingUsernamePasswordRealm mockRealm = new CachingUsernamePasswordRealm (realmConfig , threadPool ) {
217
- @ Override
218
- protected void doAuthenticate (UsernamePasswordToken token , ActionListener <AuthenticationResult > listener ) {
219
- listener .onResponse (AuthenticationResult .notHandled ());
220
- }
221
-
222
- @ Override
223
- protected void doLookupUser (String username , ActionListener <User > listener ) {
224
- listener .onResponse (null );
225
- }
226
- };
227
229
final NativeRoleMappingStore store = new NativeRoleMappingStore (Settings .EMPTY , client , mock (SecurityIndexManager .class ),
228
230
mock (ScriptService .class ));
229
- store .refreshRealmOnChange (mockRealm );
231
+
232
+ if (attachRealm ) {
233
+ final Environment env = TestEnvironment .newEnvironment (settings );
234
+ final RealmConfig .RealmIdentifier identifier = new RealmConfig .RealmIdentifier ("ldap" , realmName );
235
+ final RealmConfig realmConfig = new RealmConfig (identifier , settings , env , threadContext );
236
+ final CachingUsernamePasswordRealm mockRealm = new CachingUsernamePasswordRealm (realmConfig , threadPool ) {
237
+ @ Override
238
+ protected void doAuthenticate (UsernamePasswordToken token , ActionListener <AuthenticationResult > listener ) {
239
+ listener .onResponse (AuthenticationResult .notHandled ());
240
+ }
241
+
242
+ @ Override
243
+ protected void doLookupUser (String username , ActionListener <User > listener ) {
244
+ listener .onResponse (null );
245
+ }
246
+ };
247
+ store .refreshRealmOnChange (mockRealm );
248
+ }
230
249
return store ;
231
250
}
232
251
}
0 commit comments