Skip to content

Commit

Permalink
fix namespace quota and parameter optimize (#12581)
Browse files Browse the repository at this point in the history
* 34567

* fix namespace quota,fix some security problem

* Revert "34567"

This reverts commit 25743c6.

* avoid print content to log

* check style

* check style

* check style

* check style
  • Loading branch information
shiyiyue1102 committed Sep 3, 2024
1 parent 98a6e22 commit ac48fb3
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor;
import com.alibaba.nacos.client.config.impl.LocalEncryptedDataKeyProcessor;
import com.alibaba.nacos.client.config.impl.ServerListManager;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.config.utils.ParamUtils;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.utils.LogUtils;
Expand Down Expand Up @@ -174,8 +173,8 @@ private String getConfigInner(String tenant, String dataId, String group, long t
// changing config needed in the same time, while nacos server is down.
String content = LocalConfigInfoProcessor.getFailover(worker.getAgentName(), dataId, group, tenant);
if (content != null) {
LOGGER.warn("[{}] [get-config] get failover ok, dataId={}, group={}, tenant={}, config={}",
worker.getAgentName(), dataId, group, tenant, ContentUtils.truncateContent(content));
LOGGER.warn("[{}] [get-config] get failover ok, dataId={}, group={}, tenant={}",
worker.getAgentName(), dataId, group, tenant);
cr.setContent(content);
String encryptedDataKey = LocalEncryptedDataKeyProcessor
.getEncryptDataKeyFailover(agent.getName(), dataId, group, tenant);
Expand Down Expand Up @@ -203,8 +202,8 @@ private String getConfigInner(String tenant, String dataId, String group, long t

content = LocalConfigInfoProcessor.getSnapshot(worker.getAgentName(), dataId, group, tenant);
if (content != null) {
LOGGER.warn("[{}] [get-config] get snapshot ok, dataId={}, group={}, tenant={}, config={}",
worker.getAgentName(), dataId, group, tenant, ContentUtils.truncateContent(content));
LOGGER.warn("[{}] [get-config] get snapshot ok, dataId={}, group={}, tenant={}",
worker.getAgentName(), dataId, group, tenant);
}
cr.setContent(content);
String encryptedDataKey = LocalEncryptedDataKeyProcessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.alibaba.nacos.client.config.common.GroupKey;
import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager;
import com.alibaba.nacos.client.config.filter.impl.ConfigResponse;
import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.env.SourceType;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
Expand Down Expand Up @@ -879,9 +878,8 @@ public void checkLocalConfig(CacheData cacheData) {
cacheData.setUseLocalConfigInfo(true);
cacheData.setLocalConfigInfoVersion(file.lastModified());
cacheData.setContent(content);
LOGGER.warn(
"[{}] [failover-change] failover file created. dataId={}, group={}, tenant={}, md5={}, content={}",
envName, dataId, group, tenant, md5, ContentUtils.truncateContent(content));
LOGGER.warn("[{}] [failover-change] failover file created. dataId={}, group={}, tenant={}, md5={}",
envName, dataId, group, tenant, md5);
return;
}

Expand All @@ -901,9 +899,8 @@ public void checkLocalConfig(CacheData cacheData) {
cacheData.setUseLocalConfigInfo(true);
cacheData.setLocalConfigInfoVersion(file.lastModified());
cacheData.setContent(content);
LOGGER.warn(
"[{}] [failover-change] failover file changed. dataId={}, group={}, tenant={}, md5={}, content={}",
envName, dataId, group, tenant, md5, ContentUtils.truncateContent(content));
LOGGER.warn("[{}] [failover-change] failover file changed. dataId={}, group={}, tenant={}, md5={}",
envName, dataId, group, tenant, md5);
}
}

Expand Down Expand Up @@ -937,9 +934,9 @@ private void refreshContentAndCheck(RpcClient rpcClient, CacheData cacheData, bo
cacheData.setType(response.getConfigType());
}
if (notify) {
LOGGER.info("[{}] [data-received] dataId={}, group={}, tenant={}, md5={}, content={}, type={}",
agent.getName(), cacheData.dataId, cacheData.group, cacheData.tenant, cacheData.getMd5(),
ContentUtils.truncateContent(response.getContent()), response.getConfigType());
LOGGER.info("[{}] [data-received] dataId={}, group={}, tenant={}, md5={}, type={}", agent.getName(),
cacheData.dataId, cacheData.group, cacheData.tenant, cacheData.getMd5(),
response.getConfigType());
}
cacheData.checkListenerMd5();
} catch (Exception e) {
Expand Down Expand Up @@ -1268,8 +1265,8 @@ public boolean publishConfig(String dataId, String group, String tenant, String
this.getName(), dataId, group, tenant, response.getErrorCode(), response.getMessage());
return false;
} else {
LOGGER.info("[{}] [publish-single] ok, dataId={}, group={}, tenant={}, config={}", getName(),
dataId, group, tenant, ContentUtils.truncateContent(content));
LOGGER.info("[{}] [publish-single] ok, dataId={}, group={}, tenant={}", getName(),
dataId, group, tenant);
return true;
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.config.server.model.capacity.TenantCapacity;
import com.alibaba.nacos.config.server.service.capacity.TenantCapacityPersistService;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoPersistService;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.core.namespace.injector.AbstractNamespaceDetailInjector;
import com.alibaba.nacos.core.namespace.model.Namespace;
import org.springframework.stereotype.Service;
Expand All @@ -31,12 +34,24 @@ public class NamespaceConfigInfoService extends AbstractNamespaceDetailInjector

private final ConfigInfoPersistService configInfoPersistService;

public NamespaceConfigInfoService(ConfigInfoPersistService configInfoPersistService) {
private final TenantCapacityPersistService tenantCapacityPersistService;

public NamespaceConfigInfoService(ConfigInfoPersistService configInfoPersistService,
TenantCapacityPersistService tenantCapacityPersistService) {
this.configInfoPersistService = configInfoPersistService;
this.tenantCapacityPersistService = tenantCapacityPersistService;
}

@Override
public void injectDetail(Namespace namespace) {
// set tenant quota
TenantCapacity tenantCapacity = tenantCapacityPersistService.getTenantCapacity(namespace.getNamespace());
if (tenantCapacity != null && tenantCapacity.getQuota() != null && tenantCapacity.getQuota() > 0) {
namespace.setQuota(tenantCapacity.getQuota());
} else {
namespace.setQuota(PropertyUtil.getDefaultTenantQuota());
}
// set config count.
int configCount = configInfoPersistService.configInfoCount(namespace.getNamespace());
namespace.setConfigCount(configCount);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.config.server.service;

import com.alibaba.nacos.config.server.model.capacity.TenantCapacity;
import com.alibaba.nacos.config.server.service.capacity.TenantCapacityPersistService;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoPersistService;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.core.namespace.model.Namespace;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

@ExtendWith(SpringExtension.class)
public class NamespaceConfigInfoServiceTest {

@Mock
private ConfigInfoPersistService configInfoPersistService;

@Mock
private TenantCapacityPersistService tenantCapacityPersistService;

MockedStatic<PropertyUtil> propertyUtilMockedStatic;

@BeforeEach
void setUp() throws Exception {
propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class);

}

@AfterEach
void after() throws Exception {
propertyUtilMockedStatic.close();
}

@Test
public void testInjectDetailNotDefault() {

String namespaceId = "test1234";
TenantCapacity tenantCapacity = new TenantCapacity();
tenantCapacity.setQuota(1023);

when(tenantCapacityPersistService.getTenantCapacity(namespaceId)).thenReturn(tenantCapacity);
when(configInfoPersistService.configInfoCount(namespaceId)).thenReturn(101);
Namespace namespace = new Namespace(namespaceId, "test123ShowName");
NamespaceConfigInfoService namespaceConfigInfoService = new NamespaceConfigInfoService(configInfoPersistService,
tenantCapacityPersistService);
namespaceConfigInfoService.injectDetail(namespace);
assertEquals(101, namespace.getConfigCount());
assertEquals(1023, namespace.getQuota());

}

@Test
public void testInjectDetailDefaultQuota() {

String namespaceId = "test1234";
TenantCapacity tenantCapacity = new TenantCapacity();
tenantCapacity.setQuota(0);
when(tenantCapacityPersistService.getTenantCapacity(namespaceId)).thenReturn(tenantCapacity);
when(configInfoPersistService.configInfoCount(namespaceId)).thenReturn(105);

when(PropertyUtil.getDefaultTenantQuota()).thenReturn(1025);
Namespace namespace = new Namespace(namespaceId, "test123ShowName");
NamespaceConfigInfoService namespaceConfigInfoService = new NamespaceConfigInfoService(configInfoPersistService,
tenantCapacityPersistService);
namespaceConfigInfoService.injectDetail(namespace);

assertEquals(105, namespace.getConfigCount());
assertEquals(1025, namespace.getQuota());
}

}

0 comments on commit ac48fb3

Please sign in to comment.