Skip to content

Commit f344e02

Browse files
committed
feat:support partial configuration encrypt.
1 parent 1ccd1bb commit f344e02

File tree

18 files changed

+558
-149
lines changed

18 files changed

+558
-149
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>polaris-common</artifactId>
7+
<groupId>com.tencent.polaris</groupId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>polaris-encrypt</artifactId>
14+
<name>Polaris Common Encrypt</name>
15+
<description>Polaris Common Encrypt JAR</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.tencent.polaris</groupId>
20+
<artifactId>polaris-model</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.slf4j</groupId>
25+
<artifactId>slf4j-api</artifactId>
26+
<version>${slf4j.version}</version>
27+
<scope>provided</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.bouncycastle</groupId>
31+
<artifactId>bcpkix-jdk15to18</artifactId>
32+
<version>${bouncycastle.version}</version>
33+
</dependency>
34+
</dependencies>
35+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.encrypt;
19+
20+
/**
21+
* TSF 配置加密提供器接口
22+
*
23+
* @author hongweizhu
24+
*/
25+
public abstract class ConfigEncryptProvider {
26+
27+
/**
28+
* 加密
29+
*
30+
* @param content 明文
31+
* @param password 密码
32+
* @return 密文
33+
*/
34+
public abstract String encrypt(String content, String password);
35+
36+
/**
37+
* 解密
38+
*
39+
* @param encryptedContent 密文
40+
* @param password 密码
41+
* @return 明文
42+
*/
43+
public abstract String decrypt(String encryptedContent, String password);
44+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.encrypt;
19+
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
public class ConfigEncryptProviderFactory {
24+
25+
private static final Logger log = LoggerFactory.getLogger(ConfigEncryptProviderFactory.class);
26+
27+
private static ConfigEncryptProvider configEncryptProvider = null;
28+
29+
public static ConfigEncryptProvider getInstance() {
30+
if (null == configEncryptProvider) {
31+
try {
32+
Class<?> providerClass = Class.forName(EncryptConfig.getProviderClass());
33+
configEncryptProvider = (ConfigEncryptProvider) providerClass.newInstance();
34+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
35+
log.error("get config encrypt provider error", e);
36+
}
37+
}
38+
return configEncryptProvider;
39+
}
40+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.encrypt;
19+
20+
import com.tencent.polaris.api.utils.StringUtils;
21+
22+
public class EncryptConfig {
23+
24+
private static final String TSF_PASSWORD_KEY = "tsf_config_encrypt_password";
25+
26+
private static final String PASSWORD_KEY = "config_encrypt_password";
27+
28+
static {
29+
// TSF 环境变量
30+
if (null != System.getenv(TSF_PASSWORD_KEY)) {
31+
password = System.getenv(TSF_PASSWORD_KEY);
32+
}
33+
// TSF JVM参数
34+
if (null != System.getProperty(TSF_PASSWORD_KEY)) {
35+
password = System.getProperty(TSF_PASSWORD_KEY);
36+
}
37+
// 环境变量
38+
if (null != System.getenv(PASSWORD_KEY)) {
39+
password = System.getenv(PASSWORD_KEY);
40+
}
41+
// JVM参数
42+
if (null != System.getProperty(PASSWORD_KEY)) {
43+
password = System.getProperty(PASSWORD_KEY);
44+
}
45+
}
46+
47+
/**
48+
* 加密前缀
49+
*/
50+
public static String ENCRYPT_PREFIX = "ENC(";
51+
/**
52+
* 加密后缀
53+
*/
54+
public static String ENCRYPT_SUFFIX = ")";
55+
56+
/**
57+
* 密码
58+
*/
59+
private static String password;
60+
61+
/**
62+
* 加解密提供器类名
63+
*/
64+
private static String providerClass = "com.tencent.polaris.encrypt.impl.ConfigEncryptAESProvider";
65+
66+
/**
67+
* 是否开启配置,判断 password 是否为空
68+
*/
69+
public static Boolean getEnabled() {
70+
return StringUtils.isNotBlank(password);
71+
}
72+
73+
public static String getPassword() {
74+
return EncryptConfig.password;
75+
}
76+
77+
public static void setPassword(String password) {
78+
EncryptConfig.password = password;
79+
}
80+
81+
public static ConfigEncryptProvider getProvider() {
82+
return ConfigEncryptProviderFactory.getInstance();
83+
}
84+
85+
public static String getProviderClass() {
86+
return providerClass;
87+
}
88+
89+
public static void setProviderClass(String providerClass) {
90+
EncryptConfig.providerClass = providerClass;
91+
}
92+
93+
/**
94+
* 是否需要进行解密
95+
*
96+
* @param content 判断对象
97+
* @return true:需要解密;false:不需要解密
98+
*/
99+
public static Boolean needDecrypt(Object content) {
100+
if (null == content) {
101+
return false;
102+
} else {
103+
String stringValue = String.valueOf(content);
104+
return stringValue.startsWith(ENCRYPT_PREFIX) && stringValue.endsWith(ENCRYPT_SUFFIX);
105+
}
106+
}
107+
108+
/**
109+
* 获取真实密文
110+
*
111+
* @param content 原始配置值
112+
* @return 真实密文
113+
*/
114+
public static String realContent(Object content) {
115+
if (null != content) {
116+
String stringValue = String.valueOf(content);
117+
return stringValue.substring(ENCRYPT_PREFIX.length(), stringValue.length() - ENCRYPT_SUFFIX.length());
118+
}
119+
return null;
120+
}
121+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.encrypt.impl;
19+
20+
import com.tencent.polaris.encrypt.ConfigEncryptProvider;
21+
import com.tencent.polaris.encrypt.util.AESUtil;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
25+
public class ConfigEncryptAESProvider extends ConfigEncryptProvider {
26+
27+
private static final Logger log = LoggerFactory.getLogger(ConfigEncryptAESProvider.class);
28+
29+
@Override
30+
public String encrypt(String content, String password) {
31+
try {
32+
return AESUtil.encrypt(content, password);
33+
} catch (Exception e) {
34+
log.error("[TSF SDK] Error on encrypting.", e);
35+
throw e;
36+
}
37+
}
38+
39+
@Override
40+
public String decrypt(String encryptedContent, String password) {
41+
try {
42+
return AESUtil.decrypt(encryptedContent, password);
43+
} catch (Exception e) {
44+
log.error("[TSF SDK] Error on decrypting.", e);
45+
throw e;
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)