1.背景
现代互联网充斥着各种攻击、病毒、钓鱼、欺诈等手段,层出不穷。对于一个公司而已最基本的财富无非是代码和数据,“配置属性加密”的应用场景假设如果攻击者通过某些手段拿到部分敏感代码或配置,甚至是全部源代码和配置时,我们的基础设施账号依然不被泄漏。当然手段多种多种多样,比如以某台中毒的内网机器为肉机,对其他电脑进行ARP攻击抓去通信数据进行分析,或者获取某个账号直接拿到源代码或者配置,等等诸如此类。
2.思路
采用比较安全的对称加密算法;
对基础设施账号密码等关键信息进行加密;
构建时、运行时传入密钥,在加载属性前进行解密;
开发环境可以将密钥放置在代码中,测试、灰度、生产等环境放置在构建脚本或者启动脚本中;
如果自动化部署甚至可以有专门的程序来管理这些密钥(目前没有,暂不考虑);
值得思考的问题?
当内网被攻破,当源码泄漏,当zkui被攻破,当开发账号泄漏,当运维账号泄漏,我们如何保证生产环境的基础设施安全,如何防范关键信息泄漏?
3.技术框架
Jasypt是一个优秀的加密库,支持密码、Digest认证、文本、对象加密,此外密码加密复合RFC2307标准。http://www.jasypt.org/download.html
ulisesbocchio/jasypt-spring-boot,集成Spring Boot,在程序引导时对属性进行解密。https://github.com/ulisesbocchio/jasypt-spring-boot
4.处理过程
4.1 spring boot 密码形式
1.添加maven依赖引入jasypt
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.8</version>
</dependency>
2.配置加密参数
jasypt:
encryptor:
#这里可以理解成是加解密的时候使用的密钥
password: password
写一个测试方法,这里直接在单元测试里面来实现给密码加密,得到字符串密码
@Autowired
StringEncryptor stringEncryptor;
@Test
public void encryptPwd() {
String result = stringEncryptor.encrypt("yourpassword");
System.out.println("==================");
System.out.println(result);
System.out.println("==================");
}
把得到的密文写到需要使用到的地方,加密后的字符串需要放到ENC里面,格式如下:
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false
username: root
password: ENC(4TyrSSgQd2DCHnXVwkdKMQ==)
driver-class-name: com.mysql.jdbc.Driver
5.启动时加载命令模式
通过命令行运行 jasypt-1.9.2.jar 包命令来加密解密:
1. 在jar包所在目录打开命令行,运行如下加密命令:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=security algorithm=PBEWithMD5AndDES
运行结果如下:
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.91-b14
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: root
password: security
----OUTPUT----------------------
i00VogiiZ1FpZR9McY7XNw==
2. 使用刚才加密出来的结果进行解密,执行如下解密命令:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="i00VogiiZ1FpZR9McY7XNw==" password=security algorithm=PBEWithMD5AndD
运行结果如下:
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.91-b14
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: i00VogiiZ1FpZR9McY7XNw==
password: security
----OUTPUT----------------------
root
4.2 spring boot 密码文件形式(这种比较流行)
0. 准备工作:
0.1 生成密码文件
使用附件加密工具(亦可@运维 帮忙从测试环境获取)生成密码文件:
a. 执行java -jar jasypt-password-tool.jar
b. 根据提示操作生成密码文件(一级),键入命令genkeyfile <filepath>
如:genkeyfile E:\\password.dat
0.2 生成加密后的密码
c. 根据提示,使用密码文件对数据库明文密码加密,键入命令encrypt -f <plaintext> <filepath>
如:encrypt -f 12345678 E:\\password.dat
生成密文:DzANZvtzC/JrnRTz8te1s/J/Pw=
1. 应用pom.xml新增依赖:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.18</version>
</dependency>
2. 配置文件新增配置(path指向加密文件):
jasypt.encryptor.password-file-path=/home/jasypt/pwd.dat
3. 配置加密密码,如下:
hikari.datasource.write.password=ENC(DzANZvtzC/JrnRTz8te1s/J/Pw=)
4. 启动应用,看是否能成功启动。测试再具体测下insert、update等sql是否正常。
注:若应用启动失败,请检查mybatis相关包是否冲突,如下:
<exclusion>
<artifactId>mybatis-spring</artifactId>
<groupId>org.mybatis</groupId>
</exclusion>
若有任何问题,请及时反馈,谢谢。
4.3、spring 配置
1、使用spring mvc集成,可继承PropertyPlaceholderConfigurer子类
public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Override
protected String convertProperty(String propertyName, String propertyValue) {
//针对数据库连接密码加密
if (propertyName.equalsIgnoreCase("spring.datasource.password")) {
propertyValue = propertyValue.replace("eszxdr", "");
}
return super.convertProperty(propertyName, propertyValue);
}
}
2、实例化自定义的PropertyPlaceholderConfigurer对象
使用xml方式配置
<bean id="propertyConfigurer" class="com.spring.boot.test.util.MyPropertPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.poperties</value>
</list>
</property>
</bean>
使用java配置方式
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(){
PropertyPlaceholderConfigurer placeholderConfigurer=new MyPropertyPlaceholderConfigurer();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource resource = resolver.getResource("classpath:application.properties");
placeholderConfigurer.setLocation(resource);
return placeholderConfigurer;
}
3、以上方式不支持yaml文件,无法使用spring boot,因为使用spring boot 数据库连接会在PropertyPlaceHolderConfigurer之前初始化
参考文章:https://blog.csdn.net/clypm/article/details/79539124
参考文章:https://blog.csdn.net/u014421556/article/details/78832427#commentsedit