【问题标题】:Storing and retriveing a JKS from Azure key vault从 Azure 密钥保管库存储和检索 JKS
【发布时间】:2018-07-31 05:24:18
【问题描述】:

我想引用 Azure Key vault 中的 Java 密钥存储,而不是将其与作为 Docker 映像部署到 Kubernetes 集群中的 Spring Boot 应用程序一起打包。

根据 Azure 文档,仅允许将 .PFX 文件导入 Key Vault。目前,我成功地从 Spring Boot 中打包和检索 JKS,但我正在寻找更安全的方法并希望在代码库之外拥有我的证书。

任何指针和代码 sn-ps 都会有所帮助。

【问题讨论】:

    标签: spring-boot azure-keyvault jks


    【解决方案1】:

    我知道这很旧,但回答这个问题是因为我遇到了完全相同的问题,并且必须阅读很多内容才能弄清楚。

    Tutorial from Microsoft

    这篇文章会给你一个很好的概述。

    总而言之,如果您想使用存储在 azure 密钥库中的自签名证书为您的应用程序启用 SSL,以下是步骤

    1. 在你的 pom 中添加 azure-spring-boot-starter-keyvault-certificates 依赖项。

    2. 如下添加ssl配置和keyvault配置

      server:
        port: 8443
        ssl:
          key-alias: <keystore name>
          key-store-password: <password>
          keyStoreType: AzureKeyVault
          key-store-type: AzureKeyVault
      
      azure:
        keyvault:
          uri: <keystore uri>
          client-id: <client-id>
          client-secret: <secret>
          enabled: true
          tenant-id: <tenant-id>
      
      

    这将在您的应用程序上启用 ssl 并使用来自 azure 的密钥存储用于 https。

    如果您只需要为出站 TLS 加载信任库,事情会简单一些。您只需要如下配置密钥保管库。

    
        azure:
          keyvault:
            uri: <keystore uri>
            client-id: <client-id>
            client-secret: <secret>
            enabled: true
            tenant-id: <tenant-id>
    
    

    然后,使用密钥库初始化您的 SSLContext。

    
        KeyStore azureKeyVaultKeyStore = KeyStore.getInstance("AzureKeyVault");
                KeyVaultLoadStoreParameter parameter = new KeyVaultLoadStoreParameter(
                        System.getProperty("azure.keyvault.uri"),
                        System.getProperty("azure.keyvault.tenant-id"),
                        System.getProperty("azure.keyvault.client-id"),
                        System.getProperty("azure.keyvault.client-secret"));
                azureKeyVaultKeyStore.load(parameter);
                SSLContext sslContext = SSLContexts.custom()
                        .loadTrustMaterial(azureKeyVaultKeyStore, null)
                        .build();
    
    

    这会将密钥保管库中可用的所有证书加载到信任存储区。如果您只需要出站 TLS 的证书,我不确定是否需要这样做,因为 Azure 密钥保管库只能存储包含私钥和公钥对的证书。

    【讨论】:

    • 感谢您的详细解释。但我想通过 Spring Boot 应用程序中的 Azure Key vault 证书对 kafka 消费者验证执行类似的步骤。任何帮助将不胜感激。
    【解决方案2】:

    一种解决方案是将密钥作为 base64 编码字符串作为键/值对存储在 Azure 密钥保管库中,将其设置为环境变量,然后在构建中将其解码为服务器上的文件。

    编码:openssl base64 -A -in keystore.jks

    从 Azure Key Vault 设置为环境变量

    解码:echo $KEYSTORE_BASE64 | base64 --decode &gt; keystore.jks

    【讨论】:

    • @Riddle 之类的。我最终将密钥作为环境变量保存在我的 CI 管道中,但我已将 base64 加密字符串存储在 Azure 密钥库以及备份中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 2021-04-02
    • 1970-01-01
    相关资源
    最近更新 更多