From af519be9cbd13143eb350bae25b5003b234d2c0f Mon Sep 17 00:00:00 2001 From: Yannick Welsch Date: Tue, 11 Aug 2020 12:58:01 +0200 Subject: [PATCH] Ensure repo not in use for wildcard repo deletes (#60947) Repositories can't be unregistered when they are actively being used for snapshots or restores. Wildcard repository deletes could silently bypass the "repo in use" checks however, which is now fixed. --- .../snapshots/SharedClusterSnapshotRestoreIT.java | 3 ++- .../org/elasticsearch/repositories/RepositoriesService.java | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java index 0a0baccb1ad..3c513165234 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java @@ -1604,10 +1604,11 @@ public class SharedClusterSnapshotRestoreIT extends AbstractSnapshotIntegTestCas logger.info("--> execution was blocked on node [{}], trying to delete repository", blockedNode); try { - client.admin().cluster().prepareDeleteRepository("test-repo").execute().get(); + client.admin().cluster().prepareDeleteRepository(randomFrom("test-repo", "test-*", "*")).execute().actionGet(); fail("shouldn't be able to delete in-use repository"); } catch (Exception ex) { logger.info("--> in-use repository deletion failed"); + assertThat(ex.getMessage(), containsString("trying to modify or unregister repository that is currently used")); } logger.info("--> trying to move repository to another location"); diff --git a/server/src/main/java/org/elasticsearch/repositories/RepositoriesService.java b/server/src/main/java/org/elasticsearch/repositories/RepositoriesService.java index 3ee39f53f42..a4940b1d2a6 100644 --- a/server/src/main/java/org/elasticsearch/repositories/RepositoriesService.java +++ b/server/src/main/java/org/elasticsearch/repositories/RepositoriesService.java @@ -205,7 +205,6 @@ public class RepositoriesService extends AbstractLifecycleComponent implements C @Override public ClusterState execute(ClusterState currentState) { - ensureRepositoryNotInUse(currentState, request.name()); Metadata metadata = currentState.metadata(); Metadata.Builder mdBuilder = Metadata.builder(currentState.metadata()); RepositoriesMetadata repositories = metadata.custom(RepositoriesMetadata.TYPE); @@ -214,6 +213,7 @@ public class RepositoriesService extends AbstractLifecycleComponent implements C boolean changed = false; for (RepositoryMetadata repositoryMetadata : repositories.repositories()) { if (Regex.simpleMatch(request.name(), repositoryMetadata.name())) { + ensureRepositoryNotInUse(currentState, repositoryMetadata.name()); logger.info("delete repository [{}]", repositoryMetadata.name()); changed = true; } else { @@ -465,7 +465,7 @@ public class RepositoriesService extends AbstractLifecycleComponent implements C private static void ensureRepositoryNotInUse(ClusterState clusterState, String repository) { if (isRepositoryInUse(clusterState, repository)) { - throw new IllegalStateException("trying to modify or unregister repository that is currently used "); + throw new IllegalStateException("trying to modify or unregister repository that is currently used"); } }