diff --git a/config/src/main/java/com/alibaba/nacos/config/server/configuration/ConfigCommonConfig.java b/config/src/main/java/com/alibaba/nacos/config/server/configuration/ConfigCommonConfig.java index 83884f85c..ac6e28687 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/configuration/ConfigCommonConfig.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/configuration/ConfigCommonConfig.java @@ -33,6 +33,8 @@ public class ConfigCommonConfig extends AbstractDynamicConfig { private int maxPushRetryTimes = 50; + private boolean derbyOpsEnabled = false; + private ConfigCommonConfig() { super(CONFIG_COMMON); resetConfig(); @@ -50,13 +52,28 @@ public class ConfigCommonConfig extends AbstractDynamicConfig { this.maxPushRetryTimes = maxPushRetryTimes; } + public boolean isDerbyOpsEnabled() { + return derbyOpsEnabled; + } + + public void setDerbyOpsEnabled(boolean derbyOpsEnabled) { + this.derbyOpsEnabled = derbyOpsEnabled; + } + @Override protected void getConfigFromEnv() { maxPushRetryTimes = EnvUtil.getProperty("nacos.config.push.maxRetryTime", Integer.class, 50); + derbyOpsEnabled = EnvUtil.getProperty("nacos.config.derby.ops.enabled", Boolean.class, false); } @Override protected String printConfig() { - return "ConfigCommonConfigs{" + "maxPushRetryTimes=" + maxPushRetryTimes + '}'; + return toString(); + } + + @Override + public String toString() { + return "ConfigCommonConfig{" + "maxPushRetryTimes=" + maxPushRetryTimes + ", derbyOpsEnabled=" + derbyOpsEnabled + + '}'; } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java index 9edc610ab..f7afe9a32 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigOpsController.java @@ -17,24 +17,25 @@ package com.alibaba.nacos.config.server.controller; import com.alibaba.nacos.auth.annotation.Secured; -import com.alibaba.nacos.config.server.paramcheck.ConfigDefaultHttpParamExtractor; -import com.alibaba.nacos.core.paramcheck.ExtractorManager; -import com.alibaba.nacos.persistence.configuration.DatasourceConfiguration; -import com.alibaba.nacos.plugin.auth.constant.ActionTypes; import com.alibaba.nacos.common.model.RestResult; import com.alibaba.nacos.common.model.RestResultUtils; import com.alibaba.nacos.common.notify.NotifyCenter; +import com.alibaba.nacos.common.utils.StringUtils; +import com.alibaba.nacos.config.server.configuration.ConfigCommonConfig; import com.alibaba.nacos.config.server.constant.Constants; -import com.alibaba.nacos.persistence.model.event.DerbyImportEvent; +import com.alibaba.nacos.config.server.paramcheck.ConfigDefaultHttpParamExtractor; +import com.alibaba.nacos.config.server.service.dump.DumpService; +import com.alibaba.nacos.config.server.utils.LogUtil; +import com.alibaba.nacos.core.paramcheck.ExtractorManager; +import com.alibaba.nacos.core.utils.WebUtils; +import com.alibaba.nacos.persistence.configuration.DatasourceConfiguration; import com.alibaba.nacos.persistence.datasource.DynamicDataSource; import com.alibaba.nacos.persistence.datasource.LocalDataSourceServiceImpl; -import com.alibaba.nacos.config.server.service.dump.DumpService; +import com.alibaba.nacos.persistence.model.event.DerbyImportEvent; import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate; -import com.alibaba.nacos.config.server.utils.LogUtil; -import com.alibaba.nacos.core.utils.WebUtils; +import com.alibaba.nacos.plugin.auth.constant.ActionTypes; import com.alibaba.nacos.plugin.auth.constant.SignType; import com.alibaba.nacos.sys.utils.ApplicationUtils; -import com.alibaba.nacos.common.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; @@ -106,8 +107,12 @@ public class ConfigOpsController { if (!DatasourceConfiguration.isEmbeddedStorage()) { return RestResultUtils.failed("The current storage mode is not Derby"); } - LocalDataSourceServiceImpl dataSourceService = (LocalDataSourceServiceImpl) DynamicDataSource - .getInstance().getDataSource(); + if (!ConfigCommonConfig.getInstance().isDerbyOpsEnabled()) { + return RestResultUtils.failed( + "Derby ops is disabled, please set `nacos.config.derby.ops.enabled=true` to enabled this feature."); + } + LocalDataSourceServiceImpl dataSourceService = (LocalDataSourceServiceImpl) DynamicDataSource.getInstance() + .getDataSource(); if (StringUtils.startsWithIgnoreCase(sql, selectSign)) { if (!StringUtils.containsIgnoreCase(sql, limitSign)) { sql += limit; @@ -139,6 +144,11 @@ public class ConfigOpsController { response.setResult(RestResultUtils.failed("Limited to embedded storage mode")); return response; } + if (!ConfigCommonConfig.getInstance().isDerbyOpsEnabled()) { + response.setResult(RestResultUtils.failed( + "Derby ops is disabled, please set `nacos.config.derby.ops.enabled=true` to enabled this feature.")); + return response; + } DatabaseOperate databaseOperate = ApplicationUtils.getBean(DatabaseOperate.class); WebUtils.onFileUpload(multipartFile, file -> { NotifyCenter.publishEvent(new DerbyImportEvent(false)); diff --git a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConfigCommonConfigTest.java b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConfigCommonConfigTest.java index baccac41c..1fb936442 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConfigCommonConfigTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/configuration/ConfigCommonConfigTest.java @@ -25,6 +25,8 @@ import org.springframework.mock.env.MockEnvironment; import java.lang.reflect.Constructor; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Nacos config common configs test. @@ -60,11 +62,20 @@ class ConfigCommonConfigTest { commonConfig.setMaxPushRetryTimes(maxPushRetryTimesOld); } + @Test + void testSetDerbyOpsEnabled() { + assertFalse(commonConfig.isDerbyOpsEnabled()); + commonConfig.setDerbyOpsEnabled(true); + assertTrue(commonConfig.isDerbyOpsEnabled()); + } + @Test void testUpgradeFromEvent() { environment.setProperty("nacos.config.push.maxRetryTime", "100"); + environment.setProperty("nacos.config.derby.ops.enabled", "true"); commonConfig.onEvent(ServerConfigChangeEvent.newEvent()); assertEquals(100, commonConfig.getMaxPushRetryTimes()); + assertTrue(commonConfig.isDerbyOpsEnabled()); } @Test diff --git a/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigOpsControllerTest.java b/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigOpsControllerTest.java index 01b7bc3d8..2c4a7f34d 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigOpsControllerTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/controller/ConfigOpsControllerTest.java @@ -17,13 +17,13 @@ package com.alibaba.nacos.config.server.controller; import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.config.server.configuration.ConfigCommonConfig; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.service.dump.DumpService; import com.alibaba.nacos.persistence.configuration.DatasourceConfiguration; import com.alibaba.nacos.persistence.datasource.DynamicDataSource; import com.alibaba.nacos.persistence.datasource.LocalDataSourceServiceImpl; import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate; -import com.alibaba.nacos.sys.env.EnvUtil; import com.alibaba.nacos.sys.utils.ApplicationUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -33,7 +33,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.springframework.core.env.StandardEnvironment; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockServletContext; @@ -79,11 +78,11 @@ class ConfigOpsControllerTest { datasourceConfigurationMockedStatic.close(); dynamicDataSourceMockedStatic.close(); applicationUtilsMockedStatic.close(); + ConfigCommonConfig.getInstance().setDerbyOpsEnabled(false); } @BeforeEach void init() { - EnvUtil.setEnvironment(new StandardEnvironment()); when(servletContext.getContextPath()).thenReturn("/nacos"); ReflectionTestUtils.setField(configOpsController, "dumpService", dumpService); mockMvc = MockMvcBuilders.standaloneSetup(configOpsController).build(); @@ -112,7 +111,7 @@ class ConfigOpsControllerTest { @Test void testDerbyOps() throws Exception { - + ConfigCommonConfig.getInstance().setDerbyOpsEnabled(true); datasourceConfigurationMockedStatic.when(DatasourceConfiguration::isEmbeddedStorage).thenReturn(true); DynamicDataSource dataSource = Mockito.mock(DynamicDataSource.class); dynamicDataSourceMockedStatic.when(DynamicDataSource::getInstance).thenReturn(dataSource); @@ -131,7 +130,7 @@ class ConfigOpsControllerTest { @Test void testImportDerby() throws Exception { - + ConfigCommonConfig.getInstance().setDerbyOpsEnabled(true); datasourceConfigurationMockedStatic.when(DatasourceConfiguration::isEmbeddedStorage).thenReturn(true); applicationUtilsMockedStatic.when(() -> ApplicationUtils.getBean(DatabaseOperate.class)) diff --git a/test/config-test/src/test/java/com/alibaba/nacos/test/base/BaseClusterTest.java b/test/config-test/src/test/java/com/alibaba/nacos/test/base/BaseClusterTest.java index 8a13d1fb3..d239a449f 100644 --- a/test/config-test/src/test/java/com/alibaba/nacos/test/base/BaseClusterTest.java +++ b/test/config-test/src/test/java/com/alibaba/nacos/test/base/BaseClusterTest.java @@ -79,6 +79,7 @@ public class BaseClusterTest extends HttpClient4Test { static { System.getProperties().setProperty("nacos.core.auth.enabled", "false"); System.getProperties().setProperty("embeddedStorage", "true"); + System.getProperties().setProperty("nacos.config.derby.ops.enabled", "true"); String ip = InetUtils.getSelfIP(); clusterInfo = "nacos.member.list=" + ip + ":8847," + ip + ":8848," + ip + ":8849"; diff --git a/test/config-test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java b/test/config-test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java index 31fe10cf9..1bc20fbb6 100644 --- a/test/config-test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java +++ b/test/config-test/src/test/java/com/alibaba/nacos/test/config/ConfigDerbyRaft_DITCase.java @@ -37,7 +37,6 @@ import com.alibaba.nacos.sys.utils.InetUtils; import com.alibaba.nacos.test.base.BaseClusterTest; import com.alibaba.nacos.test.base.ConfigCleanUtils; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.MethodOrderer.MethodName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder;