Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refresh .security index call between security migrations #114879

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/114879.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 114879
summary: Add refresh `.security` index call between security migrations
area: Security
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.refresh.RefreshAction;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.support.ThreadedActionListener;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.core.TimeValue;
Expand All @@ -24,6 +26,9 @@
import java.util.TreeMap;
import java.util.concurrent.Executor;

import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN;
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;

public class SecurityMigrationExecutor extends PersistentTasksExecutor<SecurityMigrationTaskParams> {

private static final Logger logger = LogManager.getLogger(SecurityMigrationExecutor.class);
Expand Down Expand Up @@ -63,7 +68,15 @@ protected void nodeOperation(AllocatedPersistentTask task, SecurityMigrationTask
return;
}

applyOutstandingMigrations(task, params.getMigrationVersion(), listener);
refreshSecurityIndex(
new ThreadedActionListener<>(
this.getExecutor(),
ActionListener.wrap(
refreshResponse -> applyOutstandingMigrations(task, params.getMigrationVersion(), listener),
listener::onFailure
)
)
);
}

private void applyOutstandingMigrations(AllocatedPersistentTask task, int currentMigrationVersion, ActionListener<Void> listener) {
Expand All @@ -83,13 +96,17 @@ private void applyOutstandingMigrations(AllocatedPersistentTask task, int curren
response -> updateMigrationVersion(
migrationEntry.getKey(),
securityIndexManager.getConcreteIndexName(),
new ThreadedActionListener<>(
this.getExecutor(),
ActionListener.wrap(
updateResponse -> applyOutstandingMigrations(task, migrationEntry.getKey(), listener),
listener::onFailure
)
)
new ThreadedActionListener<>(this.getExecutor(), ActionListener.wrap(updateResponse -> {
refreshSecurityIndex(
new ThreadedActionListener<>(
this.getExecutor(),
ActionListener.wrap(
refreshResponse -> applyOutstandingMigrations(task, migrationEntry.getKey(), listener),
listener::onFailure
)
)
);
}, listener::onFailure))
),
listener::onFailure
)
Expand All @@ -100,6 +117,21 @@ private void applyOutstandingMigrations(AllocatedPersistentTask task, int curren
}
}

/**
* Refresh security index to make sure that docs that were migrated are visible to the next migration and to prevent version conflicts
* or unexpected behaviour by APIs relying on migrated docs.
*/
private void refreshSecurityIndex(ActionListener<Void> listener) {
RefreshRequest refreshRequest = new RefreshRequest(securityIndexManager.getConcreteIndexName());
executeAsyncWithOrigin(
client,
SECURITY_ORIGIN,
RefreshAction.INSTANCE,
refreshRequest,
ActionListener.wrap(response -> listener.onResponse(null), listener::onFailure)
);
}

private void updateMigrationVersion(int migrationVersion, String indexName, ActionListener<Void> listener) {
client.execute(
UpdateIndexMigrationVersionAction.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.EsExecutors;
Expand All @@ -20,10 +22,12 @@
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.client.NoOpClient;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.security.action.UpdateIndexMigrationVersionAction;
import org.elasticsearch.xpack.core.security.action.UpdateIndexMigrationVersionResponse;
import org.elasticsearch.xpack.core.security.support.SecurityMigrationTaskParams;
import org.junit.Before;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
Expand All @@ -40,6 +44,7 @@ public class SecurityMigrationExecutorTests extends ESTestCase {
private SecurityIndexManager securityIndexManager;

private int updateIndexMigrationVersionActionInvocations;
private int refreshActionInvocations;

private boolean clientShouldThrowException = false;

Expand All @@ -51,6 +56,7 @@ public void setUpMocks() {
when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY));
when(threadPool.generic()).thenReturn(EsExecutors.DIRECT_EXECUTOR_SERVICE);
updateIndexMigrationVersionActionInvocations = 0;
refreshActionInvocations = 0;
client = new NoOpClient(threadPool) {
@Override
@SuppressWarnings("unchecked")
Expand All @@ -63,8 +69,15 @@ protected <Request extends ActionRequest, Response extends ActionResponse> void
listener.onFailure(new IllegalStateException("Bad client"));
return;
}
updateIndexMigrationVersionActionInvocations++;
listener.onResponse((Response) new UpdateIndexMigrationVersionResponse());
if (request instanceof RefreshRequest) {
refreshActionInvocations++;
listener.onResponse((Response) new BroadcastResponse(1, 1, 0, List.of()));
} else if (request instanceof UpdateIndexMigrationVersionAction.Request) {
updateIndexMigrationVersionActionInvocations++;
listener.onResponse((Response) new UpdateIndexMigrationVersionResponse());
} else {
fail("Unexpected client request");
}

}
};
Expand All @@ -85,6 +98,7 @@ public void testSuccessfulMigration() {
verify(mockTask, times(1)).markAsCompleted();
verify(mockTask, times(0)).markAsFailed(any());
assertEquals(2, updateIndexMigrationVersionActionInvocations);
assertEquals(3, refreshActionInvocations);
assertEquals(2, migrateInvocations[0]);
}

Expand All @@ -111,6 +125,7 @@ public void testNoMigrationMeetsRequirements() {
verify(mockTask, times(1)).markAsCompleted();
verify(mockTask, times(0)).markAsFailed(any());
assertEquals(0, updateIndexMigrationVersionActionInvocations);
assertEquals(1, refreshActionInvocations);
assertEquals(0, migrateInvocationsCounter[0]);
}

Expand Down Expand Up @@ -140,6 +155,7 @@ public void testPartialMigration() {
securityMigrationExecutor.nodeOperation(mockTask, new SecurityMigrationTaskParams(0, true), mock(PersistentTaskState.class));
verify(mockTask, times(1)).markAsCompleted();
verify(mockTask, times(0)).markAsFailed(any());
assertEquals(3, refreshActionInvocations);
assertEquals(2, updateIndexMigrationVersionActionInvocations);
assertEquals(2, migrateInvocations[0]);
}
Expand All @@ -158,6 +174,7 @@ public void testNoMigrationNeeded() {
verify(mockTask, times(1)).markAsCompleted();
verify(mockTask, times(0)).markAsFailed(any());
assertEquals(0, updateIndexMigrationVersionActionInvocations);
assertEquals(1, refreshActionInvocations);
assertEquals(0, migrateInvocations[0]);
}

Expand Down Expand Up @@ -186,14 +203,13 @@ public int minMappingVersion() {
}))
);

assertThrows(
IllegalStateException.class,
() -> securityMigrationExecutor.nodeOperation(
securityMigrationExecutor.nodeOperation(
mockTask,
new SecurityMigrationTaskParams(0, true),
mock(PersistentTaskState.class)
)
);
);
verify(mockTask, times(1)).markAsFailed(any());
verify(mockTask, times(0)).markAsCompleted();
}

public void testUpdateMigrationVersionThrowsException() {
Expand Down