【问题标题】:Is it possible to get credentials from Vault in Jenkins file ?是否可以从 Jenkins 文件中的 Vault 获取凭据?
【发布时间】:2018-01-16 01:22:27
【问题描述】:

我想将 ssh 密钥存储到 HashiCorp Vault 中的 git,然后在我的 Jenkins 文件中,我想获取我的密钥并使用它来签出并提交到存储库,是否有可能或者我应该采用传统方式,在 Jenkins 中定义凭据然后使用它。提出此类问题的目的是因为我想将所有机密信息保存在 Vault 中

【问题讨论】:

    标签: git jenkins hashicorp-vault


    【解决方案1】:

    我相信您正在寻找 Hashicorp Vault Plugin - 它可以让您访问 Jenkinsfile 中的秘密 as shown here

    node {
      // define the secrets and the env variables
      def secrets = [
          [$class: 'VaultSecret', path: 'secret/testing', secretValues: [
              [$class: 'VaultSecretValue', envVar: 'testing', vaultKey: 'value_one'],
              [$class: 'VaultSecretValue', envVar: 'testing_again', vaultKey: 'value_two']]],
          [$class: 'VaultSecret', path: 'secret/another_test', secretValues: [
              [$class: 'VaultSecretValue', envVar: 'another_test', vaultKey: 'value']]]
      ]
    
      // optional configuration, if you do not provide this the next higher configuration
      // (e.g. folder or global) will be used
      def configuration = [$class: 'VaultConfiguration',
                           vaultUrl: 'http://my-very-other-vault-url.com',
                           vaultCredentialId: 'my-vault-cred-id']
    
      // inside this block your credentials will be available as env variables
      wrap([$class: 'VaultBuildWrapper', configuration: configuration, vaultSecrets: secrets]) {
          sh 'echo $testing'
          sh 'echo $testing_again'
          sh 'echo $another_test'
      }
    }
    

    【讨论】:

    • teakvinyl,谢谢你的回答,但这不是我需要的,在 Jenkins 设置步骤中不应该知道凭据,当项目构建时,我想从 Vault 获取 ssh 密钥,然后签出并提交带有这些凭据的 git(以传统方式,我们使用预定义凭据的 ID),但我想从 Vault 中获取新检索到的凭据。
    • 哦,对不起,我误解了你。澄清一下,当您说“在 Jenkins 设置步骤中不应该知道凭据”时,您是什么意思?
    • 我的意思是它们不应该是预定义的
    • 不应该预定义保险库凭证?还是 ssh 密钥?
    • SSH 密钥应该存储在 Vault 中,然后我想在 Jenkins 文件中获取 SSH 密钥,然后将其用于连接 git repo。
    【解决方案2】:

    您可以使用相同的 vault jenkins 插件来完成。

    1. 在保管库中创建 ssh 作为机密,并使用读取、列表访问策略为保管库中的 jenkins 创建 AppRole。您现在应该拥有 AppRole 的 RoleId 和 secretId。
    2. 在 jenkins 中,使用 RoleId 和 SecretId 创建一个保管库 AppRole 凭据。
    3. 在管道中使用 withVault()
    withVault(configuration: [timeout: 60, vaultCredentialId: '<ur vault credential>', vaultUrl: '<ur vault URL>'], vaultSecrets: [[path: 'secret/<ur secret ssh path>', secretValues: [[vaultKey: '<ur key of ssh>']]]]) {
                  //use as env.<<ur key of ssh>>
               }
    

    https://github.com/jenkinsci/hashicorp-vault-plugin

    【讨论】:

      猜你喜欢
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 2014-05-11
      • 1970-01-01
      • 2020-10-27
      • 2017-01-27
      • 2012-02-14
      相关资源
      最近更新 更多