【问题标题】:How to access Vault credentials in pipeline如何在管道中访问 Vault 凭据
【发布时间】:2019-05-01 05:11:47
【问题描述】:

我们在管道中使用 Vault 插件从 Vault 读取凭据。现在我们还想用 Vault 的 PKI 引擎生成 TLS 证书。为此,我需要管道文件中的 Jenkins 的 appRole 秘密 ID。该密钥在 Jenkins 中配置为“Vault App Role Credential”,我不知道如何访问它。

我想做的是这样的:

withCredentials([VaultAppRoleCredential(credentialsId: 'vault_credentials'), roleIdVariable: 'roleId', secretIdVariable: 'secretId']) {
stage('generate certificate') {
    // authenticate with credentials against Vault
    // ...
}

}

我目前的解决方法是复制凭据并将 roleId 和 secretId 额外存储在 Jenkins 的用户名+密码凭据中。

【问题讨论】:

  • 根据我对 Jenkins Pipeline 和 Vault REST API 的了解,这似乎对我有用。但是,为了便于使用,我建议将数据作为实际 JSON 而不是 JSON 字符串传输。
  • curl 命令可以使用正确的凭据。不起作用的是 withCredentials 部分。我目前的解决方法是复制凭据并将 roleId 和 secretId 额外存储在 Jenkins 的用户名+密码凭据中。编辑问题以使要点更清楚。
  • 你看到了吗:github.com/jenkinsci/…
  • 是的,但它只解释了如何从 Vault 中获取机密以及如何配置对 Vault 的访问。我的问题是我想访问用于访问存储在某些 Vault 特定凭据类型中的 Vault 的凭据。问题更多是关于凭据绑定插件,而不是关于 Vault。我想我需要某种 VaultAppRole 绑定,例如此处描述的 AmazonWebServicesCredentialsBinding:jenkins.io/doc/pipeline/steps/credentials-binding
  • 是的,您可能需要扩展插件的 API 才能获得所需的功能。

标签: jenkins-pipeline hashicorp-vault


【解决方案1】:

这是我的工作示例,如何使用 Vault Credentials Token 并使用它来访问 Vault 机密:

// Specify how to access secrets in Vault
def configuration = [
vaultUrl: 'https://hcvault.global.nibr.novartis.net',
vaultCredentialId: 'poc-vault-token',
engineVersion: 2
]

def secrets = [
[path: 'secret/projects/intd/common/accounts', engineVersion: 2, secretValues: 
    [
        [vaultKey: 'TEST_SYS_USER'],
        [vaultKey: 'TEST_SYS_PWD']
    ]
  ]
]

... [omitted pipeline]

stage ('Get Vault Secrets') {
  steps  {
    script {
      withCredentials([[$class: 'VaultTokenCredentialBinding', credentialsId: 'poc-vault-token', vaultAddr: 'https://hcvault.global.nibr.novartis.net'], usernamePassword(credentialsId: 'artifactory-jenkins-user-password', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
        withVault([configuration: configuration, vaultSecrets: secrets]) {  
          sh """
            echo $env.VAULT_ADDR > hcvault-address.txt
            echo $env.VAULT_TOKEN > hcvault-token.txt
            echo $env.TEST_SYS_USER > sys-user-account.txt
          """.stripIndent()
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2020-08-27
    • 1970-01-01
    • 2021-10-23
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多