From de8b41180ad77cd7a39e7bf6f6a7b30b8f93123c Mon Sep 17 00:00:00 2001 From: Sicheng Song Date: Mon, 9 Oct 2023 09:21:56 -0700 Subject: [PATCH] Add a setting to control the update connector API (#1465) * Add a setting to control the update connector API Signed-off-by: Sicheng Song * Enabling the update connnector setting in unit test Signed-off-by: Sicheng Song * Enabling the update connnector setting in corresponding unit test Signed-off-by: Sicheng Song --------- Signed-off-by: Sicheng Song --- .../opensearch/ml/plugin/MachineLearningPlugin.java | 1 + .../ml/rest/RestMLUpdateConnectorAction.java | 5 ++++- .../org/opensearch/ml/settings/MLCommonsSettings.java | 3 +++ .../ml/settings/MLFeatureEnabledSetting.java | 11 +++++++++++ .../org/opensearch/ml/utils/MLExceptionUtils.java | 2 ++ .../ml/rest/RestMLUpdateConnectorActionTests.java | 1 + 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/java/org/opensearch/ml/plugin/MachineLearningPlugin.java b/plugin/src/main/java/org/opensearch/ml/plugin/MachineLearningPlugin.java index b4787cc5ed..23b1e9a047 100644 --- a/plugin/src/main/java/org/opensearch/ml/plugin/MachineLearningPlugin.java +++ b/plugin/src/main/java/org/opensearch/ml/plugin/MachineLearningPlugin.java @@ -693,6 +693,7 @@ public List> getSettings() { MLCommonsSettings.ML_COMMONS_REMOTE_MODEL_ELIGIBLE_NODE_ROLES, MLCommonsSettings.ML_COMMONS_LOCAL_MODEL_ELIGIBLE_NODE_ROLES, MLCommonsSettings.ML_COMMONS_REMOTE_INFERENCE_ENABLED, + MLCommonsSettings.ML_COMMONS_UPDATE_CONNECTOR_ENABLED, MLCommonsSettings.ML_COMMONS_MEMORY_FEATURE_ENABLED, MLCommonsSettings.ML_COMMONS_RAG_PIPELINE_FEATURE_ENABLED ); diff --git a/plugin/src/main/java/org/opensearch/ml/rest/RestMLUpdateConnectorAction.java b/plugin/src/main/java/org/opensearch/ml/rest/RestMLUpdateConnectorAction.java index a74ed27ecc..334aa2766a 100644 --- a/plugin/src/main/java/org/opensearch/ml/rest/RestMLUpdateConnectorAction.java +++ b/plugin/src/main/java/org/opensearch/ml/rest/RestMLUpdateConnectorAction.java @@ -8,6 +8,7 @@ import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; import static org.opensearch.ml.plugin.MachineLearningPlugin.ML_BASE_URI; import static org.opensearch.ml.utils.MLExceptionUtils.REMOTE_INFERENCE_DISABLED_ERR_MSG; +import static org.opensearch.ml.utils.MLExceptionUtils.UPDATE_CONNECTOR_DISABLED_ERR_MSG; import static org.opensearch.ml.utils.RestActionUtils.PARAMETER_CONNECTOR_ID; import static org.opensearch.ml.utils.RestActionUtils.getParameterId; @@ -63,7 +64,9 @@ private MLUpdateConnectorRequest getRequest(RestRequest request) throws IOExcept if (!mlFeatureEnabledSetting.isRemoteInferenceEnabled()) { throw new IllegalStateException(REMOTE_INFERENCE_DISABLED_ERR_MSG); } - + if (!mlFeatureEnabledSetting.isUpdateConnectorEnabled()) { + throw new IllegalStateException(UPDATE_CONNECTOR_DISABLED_ERR_MSG); + } if (!request.hasContent()) { throw new IOException("Failed to update connector: Request body is empty"); } diff --git a/plugin/src/main/java/org/opensearch/ml/settings/MLCommonsSettings.java b/plugin/src/main/java/org/opensearch/ml/settings/MLCommonsSettings.java index bf200a3b02..03d4cf8647 100644 --- a/plugin/src/main/java/org/opensearch/ml/settings/MLCommonsSettings.java +++ b/plugin/src/main/java/org/opensearch/ml/settings/MLCommonsSettings.java @@ -114,6 +114,9 @@ private MLCommonsSettings() {} public static final Setting ML_COMMONS_REMOTE_INFERENCE_ENABLED = Setting .boolSetting("plugins.ml_commons.remote_inference.enabled", true, Setting.Property.NodeScope, Setting.Property.Dynamic); + public static final Setting ML_COMMONS_UPDATE_CONNECTOR_ENABLED = Setting + .boolSetting("plugins.ml_commons.update_connector.enabled", false, Setting.Property.NodeScope, Setting.Property.Dynamic); + public static final Setting ML_COMMONS_MODEL_ACCESS_CONTROL_ENABLED = Setting .boolSetting("plugins.ml_commons.model_access_control_enabled", false, Setting.Property.NodeScope, Setting.Property.Dynamic); diff --git a/plugin/src/main/java/org/opensearch/ml/settings/MLFeatureEnabledSetting.java b/plugin/src/main/java/org/opensearch/ml/settings/MLFeatureEnabledSetting.java index 3977c5e932..8f231a061e 100644 --- a/plugin/src/main/java/org/opensearch/ml/settings/MLFeatureEnabledSetting.java +++ b/plugin/src/main/java/org/opensearch/ml/settings/MLFeatureEnabledSetting.java @@ -8,6 +8,7 @@ package org.opensearch.ml.settings; import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_REMOTE_INFERENCE_ENABLED; +import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_UPDATE_CONNECTOR_ENABLED; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.settings.Settings; @@ -15,12 +16,18 @@ public class MLFeatureEnabledSetting { private volatile Boolean isRemoteInferenceEnabled; + private volatile Boolean isUpdateConnectorEnabled; public MLFeatureEnabledSetting(ClusterService clusterService, Settings settings) { isRemoteInferenceEnabled = ML_COMMONS_REMOTE_INFERENCE_ENABLED.get(settings); + isUpdateConnectorEnabled = ML_COMMONS_UPDATE_CONNECTOR_ENABLED.get(settings); + clusterService .getClusterSettings() .addSettingsUpdateConsumer(ML_COMMONS_REMOTE_INFERENCE_ENABLED, it -> isRemoteInferenceEnabled = it); + clusterService + .getClusterSettings() + .addSettingsUpdateConsumer(ML_COMMONS_UPDATE_CONNECTOR_ENABLED, it -> isUpdateConnectorEnabled = it); } /** @@ -31,4 +38,8 @@ public boolean isRemoteInferenceEnabled() { return isRemoteInferenceEnabled; } + public boolean isUpdateConnectorEnabled() { + return isUpdateConnectorEnabled; + } + } diff --git a/plugin/src/main/java/org/opensearch/ml/utils/MLExceptionUtils.java b/plugin/src/main/java/org/opensearch/ml/utils/MLExceptionUtils.java index da42d95382..d3866ad299 100644 --- a/plugin/src/main/java/org/opensearch/ml/utils/MLExceptionUtils.java +++ b/plugin/src/main/java/org/opensearch/ml/utils/MLExceptionUtils.java @@ -20,6 +20,8 @@ public class MLExceptionUtils { public static final String NOT_SERIALIZABLE_EXCEPTION_WRAPPER = "NotSerializableExceptionWrapper: "; public static final String REMOTE_INFERENCE_DISABLED_ERR_MSG = "Remote Inference is currently disabled. To enable it, update the setting \"plugins.ml_commons.remote_inference_enabled\" to true."; + public static final String UPDATE_CONNECTOR_DISABLED_ERR_MSG = + "Update connector API is currently disabled. To enable it, update the setting \"plugins.ml_commons.update_connector_enabled\" to true."; public static String getRootCauseMessage(final Throwable throwable) { String message = ExceptionUtils.getRootCauseMessage(throwable); diff --git a/plugin/src/test/java/org/opensearch/ml/rest/RestMLUpdateConnectorActionTests.java b/plugin/src/test/java/org/opensearch/ml/rest/RestMLUpdateConnectorActionTests.java index 814402fb66..b057190d98 100644 --- a/plugin/src/test/java/org/opensearch/ml/rest/RestMLUpdateConnectorActionTests.java +++ b/plugin/src/test/java/org/opensearch/ml/rest/RestMLUpdateConnectorActionTests.java @@ -67,6 +67,7 @@ public void setup() { client = spy(new NodeClient(Settings.EMPTY, threadPool)); when(mlFeatureEnabledSetting.isRemoteInferenceEnabled()).thenReturn(true); + when(mlFeatureEnabledSetting.isUpdateConnectorEnabled()).thenReturn(true); restMLUpdateConnectorAction = new RestMLUpdateConnectorAction(mlFeatureEnabledSetting); doAnswer(invocation -> {