Skip to content

Commit 845021d

Browse files
Test queryable built-in role synchronization (#118964)
Adds more tests for built-in roles synchronization, and fixes a bug where `synchronizationInProgress` hasn't been reset properly. Resolves #118806
1 parent 74f37d1 commit 845021d

File tree

5 files changed

+557
-17
lines changed

5 files changed

+557
-17
lines changed

muted-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,6 @@ tests:
276276
- class: org.elasticsearch.xpack.ccr.rest.ShardChangesRestIT
277277
method: testShardChangesNoOperation
278278
issue: https://github.com/elastic/elasticsearch/issues/118800
279-
- class: org.elasticsearch.xpack.security.QueryableReservedRolesIT
280-
method: testDeletingAndCreatingSecurityIndexTriggersSynchronization
281-
issue: https://github.com/elastic/elasticsearch/issues/118806
282279
- class: org.elasticsearch.smoketest.DocsClientYamlTestSuiteIT
283280
method: test {yaml=reference/indices/shard-stores/line_150}
284281
issue: https://github.com/elastic/elasticsearch/issues/118896

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/QueryableBuiltInRolesSynchronizer.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,33 @@ public void clusterChanged(ClusterChangedEvent event) {
199199
}
200200
}
201201

202+
/**
203+
* @return {@code true} if the synchronization of built-in roles is in progress, {@code false} otherwise
204+
*/
205+
public boolean isSynchronizationInProgress() {
206+
return synchronizationInProgress.get();
207+
}
208+
202209
private void syncBuiltInRoles(final QueryableBuiltInRoles roles) {
203210
if (synchronizationInProgress.compareAndSet(false, true)) {
204-
final Map<String, String> indexedRolesDigests = readIndexedBuiltInRolesDigests(clusterService.state());
205-
if (roles.rolesDigest().equals(indexedRolesDigests)) {
206-
logger.debug("Security index already contains the latest built-in roles indexed, skipping synchronization");
207-
return;
208-
}
209-
executor.execute(() -> doSyncBuiltinRoles(indexedRolesDigests, roles, ActionListener.wrap(v -> {
210-
logger.info("Successfully synced [" + roles.roleDescriptors().size() + "] built-in roles to .security index");
211-
synchronizationInProgress.set(false);
212-
}, e -> {
213-
handleException(e);
211+
try {
212+
final Map<String, String> indexedRolesDigests = readIndexedBuiltInRolesDigests(clusterService.state());
213+
if (roles.rolesDigest().equals(indexedRolesDigests)) {
214+
logger.debug("Security index already contains the latest built-in roles indexed, skipping roles synchronization");
215+
synchronizationInProgress.set(false);
216+
} else {
217+
executor.execute(() -> doSyncBuiltinRoles(indexedRolesDigests, roles, ActionListener.wrap(v -> {
218+
logger.info("Successfully synced [" + roles.roleDescriptors().size() + "] built-in roles to .security index");
219+
synchronizationInProgress.set(false);
220+
}, e -> {
221+
handleException(e);
222+
synchronizationInProgress.set(false);
223+
})));
224+
}
225+
} catch (Exception e) {
226+
logger.error("Failed to sync built-in roles", e);
214227
synchronizationInProgress.set(false);
215-
})));
228+
}
216229
}
217230
}
218231

@@ -452,6 +465,10 @@ static class MarkRolesAsSyncedTask implements ClusterStateTaskListener {
452465
this.newRoleDigests = newRoleDigests;
453466
}
454467

468+
public Map<String, String> getNewRoleDigests() {
469+
return newRoleDigests;
470+
}
471+
455472
Tuple<ClusterState, Map<String, String>> execute(ClusterState state) {
456473
IndexMetadata indexMetadata = state.metadata().index(concreteSecurityIndexName);
457474
if (indexMetadata == null) {

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/QueryableReservedRolesProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* The reserved roles are static and do not change during runtime, hence this provider will never notify any listeners.
2424
* </p>
2525
*/
26-
public final class QueryableReservedRolesProvider implements QueryableBuiltInRoles.Provider {
26+
public class QueryableReservedRolesProvider implements QueryableBuiltInRoles.Provider {
2727

2828
private final Supplier<QueryableBuiltInRoles> reservedRolesSupplier;
2929

0 commit comments

Comments
 (0)