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 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
+ '}';
}
}

View File

@ -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));

View File

@ -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

View File

@ -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))

View File

@ -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";

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.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;