Close derby ops api default. (#12372)

* Close derby ops api default.

* Fix unit test.
This commit is contained in:
杨翊 SionYang 2024-07-18 15:30:15 +08:00 committed by GitHub
parent fc5e502243
commit ed7bd03d4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 55 additions and 18 deletions

View File

@ -33,6 +33,8 @@ public class ConfigCommonConfig extends AbstractDynamicConfig {
private int maxPushRetryTimes = 50; private int maxPushRetryTimes = 50;
private boolean derbyOpsEnabled = false;
private ConfigCommonConfig() { private ConfigCommonConfig() {
super(CONFIG_COMMON); super(CONFIG_COMMON);
resetConfig(); resetConfig();
@ -50,13 +52,28 @@ public class ConfigCommonConfig extends AbstractDynamicConfig {
this.maxPushRetryTimes = maxPushRetryTimes; this.maxPushRetryTimes = maxPushRetryTimes;
} }
public boolean isDerbyOpsEnabled() {
return derbyOpsEnabled;
}
public void setDerbyOpsEnabled(boolean derbyOpsEnabled) {
this.derbyOpsEnabled = derbyOpsEnabled;
}
@Override @Override
protected void getConfigFromEnv() { protected void getConfigFromEnv() {
maxPushRetryTimes = EnvUtil.getProperty("nacos.config.push.maxRetryTime", Integer.class, 50); maxPushRetryTimes = EnvUtil.getProperty("nacos.config.push.maxRetryTime", Integer.class, 50);
derbyOpsEnabled = EnvUtil.getProperty("nacos.config.derby.ops.enabled", Boolean.class, false);
} }
@Override @Override
protected String printConfig() { protected String printConfig() {
return "ConfigCommonConfigs{" + "maxPushRetryTimes=" + maxPushRetryTimes + '}'; return toString();
}
@Override
public String toString() {
return "ConfigCommonConfig{" + "maxPushRetryTimes=" + maxPushRetryTimes + ", derbyOpsEnabled=" + derbyOpsEnabled
+ '}';
} }
} }

View File

@ -17,24 +17,25 @@
package com.alibaba.nacos.config.server.controller; package com.alibaba.nacos.config.server.controller;
import com.alibaba.nacos.auth.annotation.Secured; 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.RestResult;
import com.alibaba.nacos.common.model.RestResultUtils; import com.alibaba.nacos.common.model.RestResultUtils;
import com.alibaba.nacos.common.notify.NotifyCenter; 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.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.DynamicDataSource;
import com.alibaba.nacos.persistence.datasource.LocalDataSourceServiceImpl; 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.persistence.repository.embedded.operate.DatabaseOperate;
import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.plugin.auth.constant.SignType; import com.alibaba.nacos.plugin.auth.constant.SignType;
import com.alibaba.nacos.sys.utils.ApplicationUtils; import com.alibaba.nacos.sys.utils.ApplicationUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
@ -106,8 +107,12 @@ public class ConfigOpsController {
if (!DatasourceConfiguration.isEmbeddedStorage()) { if (!DatasourceConfiguration.isEmbeddedStorage()) {
return RestResultUtils.failed("The current storage mode is not Derby"); return RestResultUtils.failed("The current storage mode is not Derby");
} }
LocalDataSourceServiceImpl dataSourceService = (LocalDataSourceServiceImpl) DynamicDataSource if (!ConfigCommonConfig.getInstance().isDerbyOpsEnabled()) {
.getInstance().getDataSource(); 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.startsWithIgnoreCase(sql, selectSign)) {
if (!StringUtils.containsIgnoreCase(sql, limitSign)) { if (!StringUtils.containsIgnoreCase(sql, limitSign)) {
sql += limit; sql += limit;
@ -139,6 +144,11 @@ public class ConfigOpsController {
response.setResult(RestResultUtils.failed("Limited to embedded storage mode")); response.setResult(RestResultUtils.failed("Limited to embedded storage mode"));
return response; 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); DatabaseOperate databaseOperate = ApplicationUtils.getBean(DatabaseOperate.class);
WebUtils.onFileUpload(multipartFile, file -> { WebUtils.onFileUpload(multipartFile, file -> {
NotifyCenter.publishEvent(new DerbyImportEvent(false)); NotifyCenter.publishEvent(new DerbyImportEvent(false));

View File

@ -25,6 +25,8 @@ import org.springframework.mock.env.MockEnvironment;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import static org.junit.jupiter.api.Assertions.assertEquals; 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. * Nacos config common configs test.
@ -60,11 +62,20 @@ class ConfigCommonConfigTest {
commonConfig.setMaxPushRetryTimes(maxPushRetryTimesOld); commonConfig.setMaxPushRetryTimes(maxPushRetryTimesOld);
} }
@Test
void testSetDerbyOpsEnabled() {
assertFalse(commonConfig.isDerbyOpsEnabled());
commonConfig.setDerbyOpsEnabled(true);
assertTrue(commonConfig.isDerbyOpsEnabled());
}
@Test @Test
void testUpgradeFromEvent() { void testUpgradeFromEvent() {
environment.setProperty("nacos.config.push.maxRetryTime", "100"); environment.setProperty("nacos.config.push.maxRetryTime", "100");
environment.setProperty("nacos.config.derby.ops.enabled", "true");
commonConfig.onEvent(ServerConfigChangeEvent.newEvent()); commonConfig.onEvent(ServerConfigChangeEvent.newEvent());
assertEquals(100, commonConfig.getMaxPushRetryTimes()); assertEquals(100, commonConfig.getMaxPushRetryTimes());
assertTrue(commonConfig.isDerbyOpsEnabled());
} }
@Test @Test

View File

@ -17,13 +17,13 @@
package com.alibaba.nacos.config.server.controller; package com.alibaba.nacos.config.server.controller;
import com.alibaba.nacos.common.utils.JacksonUtils; 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.constant.Constants;
import com.alibaba.nacos.config.server.service.dump.DumpService; import com.alibaba.nacos.config.server.service.dump.DumpService;
import com.alibaba.nacos.persistence.configuration.DatasourceConfiguration; import com.alibaba.nacos.persistence.configuration.DatasourceConfiguration;
import com.alibaba.nacos.persistence.datasource.DynamicDataSource; import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
import com.alibaba.nacos.persistence.datasource.LocalDataSourceServiceImpl; import com.alibaba.nacos.persistence.datasource.LocalDataSourceServiceImpl;
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate; import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.sys.utils.ApplicationUtils; import com.alibaba.nacos.sys.utils.ApplicationUtils;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -33,7 +33,6 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockedStatic; import org.mockito.MockedStatic;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
@ -79,11 +78,11 @@ class ConfigOpsControllerTest {
datasourceConfigurationMockedStatic.close(); datasourceConfigurationMockedStatic.close();
dynamicDataSourceMockedStatic.close(); dynamicDataSourceMockedStatic.close();
applicationUtilsMockedStatic.close(); applicationUtilsMockedStatic.close();
ConfigCommonConfig.getInstance().setDerbyOpsEnabled(false);
} }
@BeforeEach @BeforeEach
void init() { void init() {
EnvUtil.setEnvironment(new StandardEnvironment());
when(servletContext.getContextPath()).thenReturn("/nacos"); when(servletContext.getContextPath()).thenReturn("/nacos");
ReflectionTestUtils.setField(configOpsController, "dumpService", dumpService); ReflectionTestUtils.setField(configOpsController, "dumpService", dumpService);
mockMvc = MockMvcBuilders.standaloneSetup(configOpsController).build(); mockMvc = MockMvcBuilders.standaloneSetup(configOpsController).build();
@ -112,7 +111,7 @@ class ConfigOpsControllerTest {
@Test @Test
void testDerbyOps() throws Exception { void testDerbyOps() throws Exception {
ConfigCommonConfig.getInstance().setDerbyOpsEnabled(true);
datasourceConfigurationMockedStatic.when(DatasourceConfiguration::isEmbeddedStorage).thenReturn(true); datasourceConfigurationMockedStatic.when(DatasourceConfiguration::isEmbeddedStorage).thenReturn(true);
DynamicDataSource dataSource = Mockito.mock(DynamicDataSource.class); DynamicDataSource dataSource = Mockito.mock(DynamicDataSource.class);
dynamicDataSourceMockedStatic.when(DynamicDataSource::getInstance).thenReturn(dataSource); dynamicDataSourceMockedStatic.when(DynamicDataSource::getInstance).thenReturn(dataSource);
@ -131,7 +130,7 @@ class ConfigOpsControllerTest {
@Test @Test
void testImportDerby() throws Exception { void testImportDerby() throws Exception {
ConfigCommonConfig.getInstance().setDerbyOpsEnabled(true);
datasourceConfigurationMockedStatic.when(DatasourceConfiguration::isEmbeddedStorage).thenReturn(true); datasourceConfigurationMockedStatic.when(DatasourceConfiguration::isEmbeddedStorage).thenReturn(true);
applicationUtilsMockedStatic.when(() -> ApplicationUtils.getBean(DatabaseOperate.class)) applicationUtilsMockedStatic.when(() -> ApplicationUtils.getBean(DatabaseOperate.class))

View File

@ -79,6 +79,7 @@ public class BaseClusterTest extends HttpClient4Test {
static { static {
System.getProperties().setProperty("nacos.core.auth.enabled", "false"); System.getProperties().setProperty("nacos.core.auth.enabled", "false");
System.getProperties().setProperty("embeddedStorage", "true"); System.getProperties().setProperty("embeddedStorage", "true");
System.getProperties().setProperty("nacos.config.derby.ops.enabled", "true");
String ip = InetUtils.getSelfIP(); String ip = InetUtils.getSelfIP();
clusterInfo = "nacos.member.list=" + ip + ":8847," + ip + ":8848," + ip + ":8849"; clusterInfo = "nacos.member.list=" + ip + ":8847," + ip + ":8848," + ip + ":8849";

View File

@ -37,7 +37,6 @@ import com.alibaba.nacos.sys.utils.InetUtils;
import com.alibaba.nacos.test.base.BaseClusterTest; import com.alibaba.nacos.test.base.BaseClusterTest;
import com.alibaba.nacos.test.base.ConfigCleanUtils; import com.alibaba.nacos.test.base.ConfigCleanUtils;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.MethodOrderer.MethodName; import org.junit.jupiter.api.MethodOrderer.MethodName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestMethodOrder;