[CCR] Add extra validation to unfollow api (#35245)

Validate whether the follow index actually exists and
whether the follow index actually has custom ccr metadata.
This commit is contained in:
Martijn van Groningen 2018-11-06 08:00:34 +01:00 committed by GitHub
parent 14c8a483d5
commit cac67f8bcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 5 deletions

View File

@ -20,6 +20,7 @@ import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.persistent.PersistentTasksCustomMetaData;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
@ -78,6 +79,18 @@ public class TransportUnfollowAction extends TransportMasterNodeAction<UnfollowA
static ClusterState unfollow(String followerIndex, ClusterState current) {
IndexMetaData followerIMD = current.metaData().index(followerIndex);
if (followerIMD == null) {
throw new IndexNotFoundException(followerIndex);
}
if (followerIMD.getCustomData(Ccr.CCR_CUSTOM_METADATA_KEY) == null) {
throw new IllegalArgumentException("index [" + followerIndex + "] is not a follower index");
}
if (followerIMD.getState() != IndexMetaData.State.CLOSE) {
throw new IllegalArgumentException("cannot convert the follower index [" + followerIndex +
"] to a non-follower, because it has not been closed");
}
PersistentTasksCustomMetaData persistentTasks = current.metaData().custom(PersistentTasksCustomMetaData.TYPE);
if (persistentTasks != null) {
@ -92,11 +105,6 @@ public class TransportUnfollowAction extends TransportMasterNodeAction<UnfollowA
}
}
if (followerIMD.getState() != IndexMetaData.State.CLOSE) {
throw new IllegalArgumentException("cannot convert the follower index [" + followerIndex +
"] to a non-follower, because it has not been closed");
}
IndexMetaData.Builder newIMD = IndexMetaData.builder(followerIMD);
// Remove index.xpack.ccr.following_index setting
Settings.Builder builder = Settings.builder();

View File

@ -14,6 +14,7 @@ import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.persistent.PersistentTasksCustomMetaData;
import org.elasticsearch.test.ESTestCase;
@ -104,4 +105,35 @@ public class TransportUnfollowActionTests extends ESTestCase {
equalTo("cannot convert the follower index [follow_index] to a non-follower, because it has not been paused"));
}
public void testUnfollowMissingIndex() {
IndexMetaData.Builder followerIndex = IndexMetaData.builder("follow_index")
.settings(settings(Version.CURRENT).put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true))
.numberOfShards(1)
.numberOfReplicas(0)
.state(IndexMetaData.State.CLOSE)
.putCustom(Ccr.CCR_CUSTOM_METADATA_KEY, new HashMap<>());
ClusterState current = ClusterState.builder(new ClusterName("cluster_name"))
.metaData(MetaData.builder()
.put(followerIndex)
.build())
.build();
expectThrows(IndexNotFoundException.class, () -> TransportUnfollowAction.unfollow("another_index", current));
}
public void testUnfollowNoneFollowIndex() {
IndexMetaData.Builder followerIndex = IndexMetaData.builder("follow_index")
.settings(settings(Version.CURRENT).put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true))
.numberOfShards(1)
.numberOfReplicas(0)
.state(IndexMetaData.State.CLOSE);
ClusterState current = ClusterState.builder(new ClusterName("cluster_name"))
.metaData(MetaData.builder()
.put(followerIndex)
.build())
.build();
expectThrows(IllegalArgumentException.class, () -> TransportUnfollowAction.unfollow("follow_index", current));
}
}