编辑此页面

配置中的秘密

使用加密的配置值来保护敏感的密码、秘密、令牌和密钥。

秘密配置可以表示为 ${handler::value},其中 handler 是用于解码或解密 valueio.smallrye.config.SecretKeysHandler 的名称。

加密配置值

要加密和后续解密配置值,请添加以下托管依赖项

pom.xml
<dependency>
    <groupId>io.smallrye.config</groupId>
    <artifactId>smallrye-config-crypto</artifactId>
</dependency>

使用 Quarkus CLI 命令在 application.properties 中添加新的加密值或加密现有值

CLI
quarkus config set --encrypt my.secret 1234

有关如何安装 Quarkus CLI 和使用它的更多信息,请参阅 Quarkus CLI 指南

配置属性 my.secret 将添加到 application.properties 中,其值为 1234,经过加密和 **Base64** 编码,并带有表达式 ${aes-gcm-nopadding::},以及用于解密值的必需的 secret handler。如果不存在,还会生成一个加密密钥并设置为 smallrye.config.secret-handler.aes-gcm-nopadding.encryption-key

my.secret=${aes-gcm-nopadding::DLTb_9zxThxeT5iAQqswEl5Dn1ju4FdM9hIyVip35t5V}

smallrye.config.secret-handler.aes-gcm-nopadding.encryption-key=DDne5obnfH1RSeTg71xSZg
默认的 secret handler 使用 AES/GCM/NoPadding 算法,并要求使用表达式 ${aes-gcm-nopadding::value} 来解密 value

读取加密配置

Quarkus 配置系统在查找 my.secret 时会自动解密配置值。

用于加密值的加密密钥必须与用于解密值的密钥相同,并设置为 smallrye.config.secret-handler.aes-gcm-nopadding.encryption-key

class BusinessBean {
    @Inject
    SmallRyeConfig config;

    public void businessMethod() {
        ConfigValue mySecret = config.getConfigValue("my.secret");
        mySecret.getValue(); (1)
    }
}
1 返回值为 1234

将秘密存储在密钥库中

虽然加密值比明文值更好,但我们仍然希望避免将它们直接设置在 application.properties 中。

Java KeyStore 被用作基于文件的 Vault。敏感数据可以导入到此 Vault 中并安全地存储为 Java SecretKey 值。要使用 KeyStore ConfigSource,请添加以下托管依赖项

<dependency>
    <groupId>io.smallrye.config</groupId>
    <artifactId>smallrye-config-source-keystore</artifactId>
</dependency>

创建密钥库

以下命令创建一个简单的密钥库

echo DLTb_9zxThxeT5iAQqswEl5Dn1ju4FdM9hIyVip35t5V | keytool -importpass -alias my.secret -keystore properties -storepass arealpassword -storetype PKCS12 -v

-alias my.secret 选项将配置属性名 my.secret 存储在密钥库中,值为 DLTb_9zxThxeT5iAQqswEl5Dn1ju4FdM9hIyVip35t5V-storepass arealpassword 是访问密钥库所需的密码。

我们还需要安全地存储加密密钥。您不应该将密钥与其余秘密一起存储,因此我们可以为密钥创建另一个 KeyStore

echo DDne5obnfH1RSeTg71xSZg | keytool -importpass -alias smallrye.config.secret-handler.aes-gcm-nopadding.encryption-key -keystore key -storepass anotherpassword -storetype PKCS12 -v

使用密钥库

要使用新创建的 KeyStore`s,请将以下配置添加到 `application.properties

smallrye.config.source.keystore."properties".path=properties (1)
smallrye.config.source.keystore."properties".password=arealpassword (2)
smallrye.config.source.keystore."properties".handler=aes-gcm-nopadding (3)

smallrye.config.source.keystore."key".path=key (4)
smallrye.config.source.keystore."key".password=anotherpassword (5)
1 包含属性秘密的 ´KeyStore` 的路径
2 用于提取 KeyStore 秘密的 KeyStore 密码
3 用于解密 KeyStore 秘密的 SecretKeyHandler
4 包含加密密钥的 ´KeyStore` 的路径。
5 用于提取加密密钥的 KeyStore 密码

保护密钥库密码

您需要在 application.properties 中指定一个 KeyStore 密码,以便 Quarkus 能够从密钥库中提取秘密。此密钥库密码是一个敏感值,因此您应该考虑如何最小化泄露风险以及如何保护它。

您应该注意的一件重要事情是,泄露此密码并不一定意味着密钥库中存储的实际秘密也会泄露,因为未经授权的人还需要访问实际的密钥库文件。将密钥库文件的访问权限限制在一小组角色,并让 Quarkus 进程在一个角色中运行,将使得外部人员更难访问密钥库。密钥库密码可以设置为环境变量,并且应该定期更改此密码,以限制攻击者可以尝试访问密钥库的时间窗口。