将 spring.cloud.vault.token 存储在已签入 VCS(例如 Git)的 application.properties 中可能会危及存储在 Vault 中的所有机密。
解决方案是不要将 Vault 令牌作为纯文本存储在 application.properties 中。有几种选择。
从application.properties 中删除 Vault 令牌
只需从application.properties 中删除spring.cloud.vault.token,而是通过系统属性-Dspring.cloud.vault.token=00000000-0000-0000-0000-000000000000(启动应用程序时)或环境变量SPRING_CLOUD_VAULT_TOKEN 提供它。如果您使用容器(Docker 或 Kubernetes),环境变量特别方便。
将加密的 Vault 令牌存储在 application.properties
您可以将spring.cloud.vault.token 属性保留在application.properties 中,如果它是加密的。
Spring Cloud Config支持解密以{cipher}开头的属性:
spring.cloud.vault.token={cipher}encrypted_vault_token
要使用属性加密和解密,您需要以下依赖项(Gradle 示例):
implementation 'org.springframework.cloud:spring-cloud-context:2.2.2.RELEASE'
implementation 'org.bouncycastle:bcprov-jdk15on:1.64'
对称加密
加密属性的最简单方法是使用对称加密。
想出一个对称密钥(例如s3cr3t)。
要加密 Vault 令牌,您可以使用 Spring Boot CLI 和 Spring Boot Cloud CLI:
curl "https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.2.2.RELEASE/spring-boot-cli-2.2.2.RELEASE-bin.tar.gz" -o spring-boot-cli-bin.tar.gz
tar -xf spring-boot-cli-bin.tar.gz
cd spring-2.2.2.RELEASE
bin/spring install org.springframework.cloud:spring-cloud-cli:2.2.1.RELEASE
bin/spring encrypt 00000000-0000-0000-0000-000000000000 --key s3cr3t
# 507cd1614682535ab8237b448ca73dc74058d3ae9145d63a7381ee67f3046eb1598da6960abdbf2dbf22c47206db5222e45fc74fd6122bc707b61c62f5051e0f
bin/spring decrypt 507cd1614682535ab8237b448ca73dc74058d3ae9145d63a7381ee67f3046eb1598da6960abdbf2dbf22c47206db5222e45fc74fd6122bc707b61c62f5051e0f --key s3cr3t
# 00000000-0000-0000-0000-000000000000
将对称密钥传递给ENCRYPT_KEY 环境变量中的应用程序。
绝对不能将对称加密密钥签入 VCS。
非对称加密
考虑使用公钥和私钥对的非对称加密作为对称加密的更安全替代方案。
您需要生成一个密钥库,而不是对称加密密钥(使用 JDK 附带的 keytool 实用程序或 openssl)。
在bootstrap.properties 中指定以下属性:
encrypt.keyStore.location
encrypt.keyStore.password
encrypt.keyStore.alias
encrypt.keyStore.type
密钥库必须安装在encrypt.keyStore.location 中指定的位置,并且从不签入 VCS。
另外,解锁密钥库的密码最好传入ENCRYPT_KEYSTORE_PASSWORD 环境变量。
在 Spring Cloud Config 中了解 key management。