Jenkinsfile:对于 Bitbucket REST API,是否有更安全的替代明文用户名:密码/PAT?

我使用 Jenkinsfile 进行 Bitbucket REST API 调用。

Jenkinsfile 的原始版本使用超级用户的用户名:密码作为 curl 的 -u 参数。例如

pipeline {
  agent any
  stages {
    stage( "1" ) {
      steps {
        script {
          def CRED = "super_user:super_password"
          def url = "https://bitbucket.company.com/rest/build-status/1.0/commits"
          def commit = 0000000000000000000000000000000000000001
          def dict = [:]
          dict.state = "INPROGRESS"
          dict.key = "foo_002"
          dict.url = "http://server:8080/blue/organizations/jenkins/job/detail/job/002/pipeline"
          def cmd = "curl " +
                    "-f " +
                    "-L " +
                    "-u ${CRED} " +
                    "-H \\\"Content-Type: application/json\\\" " +
                    "-X POST ${url}/${commit} " +
                    "-d \\\''${JsonOutput.toJson(dict)}'\\\'"
          sh(script: cmd)
        }
      }
    }
  }
}

我不认为 def CRED = "super_user:super_password" 是安全的——即用户名/密码以明文形式存在。所以我四处寻找替代品。

建议我使用个人访问令牌 (PAT) 而不是用户名/密码。

recently learned认为 PAT 实际上是现有用户的“另一个密码”。 IE。如果注意到的 super_user 在 Bitbucket 网络用户界面中创建了一个 PAT——以“00000000000000000000000000000000000000000000”为例——那么对上述 Jenkinsfile 的唯一更改是:

def CRED = "super_user:00000000000000000000000000000000000000000000"

为什么认为这更安全?明文 super_user:00000000000000000000000000000000000000000000 的存在是否与明文 super_user:super_password 的存在一样具有安全漏洞?

This Bitbucket REST API documentation 提供了 curl 命令的示例,用于调用更新提交构建状态的 REST API,这是上述 Jenkinsfile 实现的。

由于调用 REST API 最终归结为 curl 命令——即在 shell 提示中调用的东西,无论是人工还是 Jenkinsfile 脚本——什么是保护用户名:密码的流行约定/PAT 使其不是 Jenkinsfile 中的明文(或通过调用 readFile() 等读取的文件)?

maxiuping 回答:Jenkinsfile:对于 Bitbucket REST API,是否有更安全的替代明文用户名:密码/PAT?

您需要使用 Jenkins 凭证存储,并且不要在 curl 命令中对凭证变量使用双引号以避免字符串插值。

pipeline {
agent any
stages {
stage( "1" ) {
  steps {
    script {
     withCredentials([usernamePassword(credentialsId: 'bitBucketCreds',passwordVariable: 'password',usernameVariable: 'username')]) {
      String url = "https://bitbucket.company.com/rest/build-status/1.0/commits"
      String commit = '0000000000000000000000000000000000000001'
      Map dict = [:]
      dict.state = "INPROGRESS"
      dict.key = "foo_002"
      dict.url = "http://server:8080/blue/organizations/jenkins/job/detail/job/002/pipeline"
      List command = []
      command.add("curl -f -L")
      command.add('-u ${username}:${password}')
      command.add("-H \\\"Content-Type: application/json\\\"")
      command.add("-X POST ${url}/${commit}")
      command.add("-d \\\''${JsonOutput.toJson(dict)}'\\\'")
                         
      sh(script: command.join(' '))
     }
    }
   }
  }
 }
}
本文链接:https://www.f2er.com/4476.html

大家都在问