Skip to content

Commit

Permalink
[ISSUE alibaba#8169] add CustomEnvironmentPlugin (alibaba#8169)
Browse files Browse the repository at this point in the history
* fix issue alibaba#8169 .

* reset properties.
  • Loading branch information
godhth committed Oct 11, 2022
1 parent a78ee54 commit 7962aaf
Show file tree
Hide file tree
Showing 11 changed files with 413 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.console.filter.XssFilter;
import com.alibaba.nacos.core.code.ControllerMethodsCache;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -51,6 +52,7 @@ public class ConsoleConfig {
*/
@PostConstruct
public void init() {
EnvUtil.customEnvironment();
methodsCache.initClassMethod("com.alibaba.nacos.core.controller");
methodsCache.initClassMethod("com.alibaba.nacos.naming.controllers");
methodsCache.initClassMethod("com.alibaba.nacos.config.server.controller");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 1999-2021 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.plugin.environment.impl;

import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.plugin.environment.spi.CustomEnvironmentPluginService;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Nacos default auth plugin service implementation.
*
* @author xiweng.yy
*/

public class NacosCustomEnvPluginService implements CustomEnvironmentPluginService {

@Override
public Map<String, Object> customValue(Map<String, Object> property) {
String pwd = (String) property.get("db.password.0");
property.put("db.password.0", MD5Utils.md5Hex(pwd, Constants.ENCODE));
return property;
}

@Override
public Set<String> propertyKey() {
Set<String> propertyKey = new HashSet<>();
propertyKey.add("db.password.0");
return propertyKey;
}

@Override
public Integer order() {
return 1;
}

@Override
public String pluginName() {
return "testCustomEnvironmentPluginService";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 1999-2021 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.
#

com.alibaba.nacos.plugin.environment.impl.NacosCustomEnvPluginService
23 changes: 23 additions & 0 deletions plugin/environment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-plugin</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>custom-environment-plugin</artifactId>
<name>custom-environment-plugin ${project.version}</name>
<url>http://nacos.io</url>

<dependencies>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-common</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.alibaba.nacos.plugin.environment;

import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.environment.spi.CustomEnvironmentPluginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
* CustomEnvironment Plugin Management.
*
* @author : huangtianhui
*/
public class CustomEnvironmentPluginManager {

private static final Logger LOGGER = LoggerFactory.getLogger(CustomEnvironmentPluginManager.class);

private static final List<CustomEnvironmentPluginService> SERVICE_LIST = new LinkedList<>();

private static final CustomEnvironmentPluginManager INSTANCE = new CustomEnvironmentPluginManager();

public CustomEnvironmentPluginManager() {
loadInitial();
}

private void loadInitial() {
Collection<CustomEnvironmentPluginService> customEnvironmentPluginServices = NacosServiceLoader.load(
CustomEnvironmentPluginService.class);
for (CustomEnvironmentPluginService customEnvironmentPluginService : customEnvironmentPluginServices) {
if (StringUtils.isBlank(customEnvironmentPluginService.pluginName())) {
LOGGER.warn("[customEnvironmentPluginService] Load customEnvironmentPluginService({}) customEnvironmentPluginName(null/empty) fail."
+ " Please Add customEnvironmentPluginName to resolve.", customEnvironmentPluginService.getClass());
continue;
}
LOGGER.info("[CustomEnvironmentPluginManager] Load customEnvironmentPluginService({}) customEnvironmentPluginName({}) successfully.",
customEnvironmentPluginService.getClass(), customEnvironmentPluginService.pluginName());
}
SERVICE_LIST.addAll(customEnvironmentPluginServices.stream()
.sorted(Comparator.comparingInt(CustomEnvironmentPluginService::order))
.collect(Collectors.toList()));
}

public static CustomEnvironmentPluginManager getInstance() {
return INSTANCE;
}

public Set<String> getPropertyKeys() {
Set<String> keys = new HashSet<>();
for (CustomEnvironmentPluginService customEnvironmentPluginService : SERVICE_LIST) {
keys.addAll(customEnvironmentPluginService.propertyKey());
}
return keys;
}

public Map<String, Object> getCustomValues(Map<String, Object> sourceProperty) {
Map<String, Object> customValuesMap = new HashMap<>();
for (CustomEnvironmentPluginService customEnvironmentPluginService : SERVICE_LIST) {
Set<String> keys = customEnvironmentPluginService.propertyKey();
Map<String, Object> propertyMap = new HashMap<>(keys.size());
for (String key : keys) {
propertyMap.put(key, sourceProperty.get(key));
}
Map<String, Object> targetPropertyMap = customEnvironmentPluginService.customValue(propertyMap);
//Only the current plugin key is allowed
Set<String> targetKeys = new HashSet<>(targetPropertyMap.keySet());
targetKeys.removeAll(keys);
for (String key : targetKeys) {
targetPropertyMap.remove(key);
}
customValuesMap.putAll(targetPropertyMap);
}
for (Map.Entry<String, Object> entry : customValuesMap.entrySet()) {
if (Objects.isNull(entry.getValue())) {
customValuesMap.remove(entry.getKey());
}
}
return customValuesMap;
}

/**
* Injection realization.
*
* @param customEnvironmentPluginService customEnvironmentPluginService implementation
*/
public static synchronized void join(CustomEnvironmentPluginService customEnvironmentPluginService) {
if (Objects.isNull(customEnvironmentPluginService)) {
return;
}
SERVICE_LIST.add(customEnvironmentPluginService);
LOGGER.info("[CustomEnvironmentPluginService] join successfully.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.alibaba.nacos.plugin.environment.spi;

import java.util.Map;
import java.util.Set;

/**
* CustomEnvironment Plugin Service.
*
* @author : huangtianhui
*/
public interface CustomEnvironmentPluginService {
/**
* customValue interface.
*
* @param property property key value
* @return custom key value
*/
Map<String, Object> customValue(Map<String, Object> property);

/**
* propertyKey interface.
*
* @return propertyKey property Key
*/
Set<String> propertyKey();

/**
* order The larger the priority, the higher the priority.
*
* @return order
*/
Integer order();

/**
* pluginName.
*
* @return
*/
String pluginName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.alibaba.nacos.plugin.environment;

import com.alibaba.nacos.plugin.environment.spi.CustomEnvironmentPluginService;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* CustomEnvironment Plugin Test.
*
* @author : huangtianhui
*/
public class CustomEnvironmentPluginManagerTest {
@Test
public void testInstance() {
CustomEnvironmentPluginManager instance = CustomEnvironmentPluginManager.getInstance();
Assert.assertNotNull(instance);
}

@Test
public void testJoin() {
CustomEnvironmentPluginManager.join(new CustomEnvironmentPluginService() {
@Override
public Map<String, Object> customValue(Map<String, Object> property) {
String pwd = (String) property.get("db.password.0");
property.put("db.password.0", "test" + pwd);
return property;
}

@Override
public Set<String> propertyKey() {
Set<String> propertyKey = new HashSet<>();
propertyKey.add("db.password.0");
return propertyKey;
}

@Override
public Integer order() {
return 0;
}

@Override
public String pluginName() {
return "test";
}
});
}
}
1 change: 1 addition & 0 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<modules>
<module>auth</module>
<module>encryption</module>
<module>environment</module>
</modules>
<packaging>pom</packaging>

Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<javatuples.version>1.2</javatuples.version>
<grpc-java.version>1.30.2</grpc-java.version>
<proto-google-common-protos.version>1.17.0</proto-google-common-protos.version>
<protobuf-java.version>3.16.3</protobuf-java.version>
<protobuf-java.version>3.16.1</protobuf-java.version>
<protoc-gen-grpc-java.version>${grpc-java.version}</protoc-gen-grpc-java.version>
<hessian.version>4.0.63</hessian.version>
<mockito-all.version>1.10.19</mockito-all.version>
Expand Down Expand Up @@ -714,6 +714,11 @@
<artifactId>nacos-encryption-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>custom-environment-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-auth</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions sys/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>custom-environment-plugin</artifactId>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit 7962aaf

Please sign in to comment.