git 通过 Groovy 访问 Jenkins 凭证存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35205665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:55:05  来源:igfitidea点击:

Jenkins Credentials Store Access via Groovy

gitjenkinsgroovy

提问by khmarbaise

I have found a way to access the credentials store in Jenkins:

我找到了一种访问Jenkins 凭证存储的方法

def getPassword = { username ->
    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
        jenkins.model.Jenkins.instance
    )

    def c = creds.findResult { it.username == username ? it : null }

    if ( c ) {
        println "found credential ${c.id} for username ${c.username}"

        def credentials_store = jenkins.model.Jenkins.instance.getExtensionList(
            'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
            )[0].getStore()

         println "result: " + credentials_store
    } else {
      println "could not find credential for ${username}"
    }
}

getPassword("XYZ")

But now i would like to get the password for the appropriate user which i can't do...

但是现在我想获取我无法执行的相应用户的密码...

I always get unknown method etc. if i try to access passord etc.

如果我尝试访问密码等,我总是会得到未知的方法等。

The reason for doing this is to use this user/password to call git and extract information from repository..

这样做的原因是使用这个用户/密码来调用 git 并从存储库中提取信息..

I always get something like this:

我总是得到这样的东西:

result: com.cloudbees.plugins.credentials.SystemCredentialsProvider$StoreImpl@1639eab2

Update

更新

After experimenting more (and the hint of Jeanne Boyarsky) with it i found that i was thinking to compilcated. The following already gives me the password for the user:

在对它进行了更多实验(以及 Jeanne Boyarsky 的暗示)之后,我发现我正在考虑复杂化。以下内容已经为我提供了用户的密码:

def getUserPassword = { username ->
    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
            com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
            jenkins.model.Jenkins.instance
            )

    def c = creds.findResult { it.username == username ? it : null }

    if ( c ) {
        return c.password
    } else {
        println "could not find credential for ${username}"
    }
}

Furthermore by using the following snippet you can iterate over the whole credentials store:

此外,通过使用以下代码段,您可以遍历整个凭证存储:

def credentials_store = jenkins.model.Jenkins.instance.getExtensionList(
        'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
        )

println "credentials_store: ${credentials_store}"
println " Description: ${credentials_store.description}"
println " Target: ${credentials_store.target}"
credentials_store.each {  println "credentials_store.each: ${it}" }

credentials_store[0].credentials.each { it ->
    println "credentials: -> ${it}"
    if (it instanceof com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) {
        println "XXX: username: ${it.username} password: ${it.password} description: ${it.description}"
    }
}

And you will get an output like this:

你会得到这样的输出:

[(master)]:
credentials_store: [com.cloudbees.plugins.credentials.SystemCredentialsProvider@5a2822be]
 Description: [The descriptions...]
 Target: [com.cloudbees.plugins.credentials.SystemCredentialsProvider@5a2822be]
credentials_store.each: com.cloudbees.plugins.credentials.SystemCredentialsProvider@5a2822be
credentials: -> com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@38357ca1
credentials: -> com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@47cf7703
credentials: -> com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@739abac5
XXX: username: User1 password: Password description: The description of the user.
credentials: -> com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@884a53e6
XXX: username: User2 password: Password1 description: The description of the user1.
Result:   [com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@38357ca1, com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@47cf7703, com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@739abac5, com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@884a53e6]

So by using the appropriate class in the instanceofclauseyou can select what you need.

因此,通过instanceof子句中使用适当的类,您可以选择所需的内容。

采纳答案by Jeanne Boyarsky

This works. It gets the credentials rather than the store.

这有效。它获取凭据而不是商店。

I didn't write any error handling so it blows up if you don't have a credentials object set up (or probably if you have two). That part is easy to add though. The tricky part is getting the right APIs!

我没有写任何错误处理,所以如果你没有设置凭证对象(或者可能如果你有两个),它就会爆炸。不过,这部分很容易添加。棘手的部分是获得正确的 API!

def getPassword = { username ->
    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
        jenkins.model.Jenkins.instance
    )

    def c = creds.findResult { it.username == username ? it : null }

    if ( c ) {
        println "found credential ${c.id} for username ${c.username}"

        def systemCredentialsProvider = jenkins.model.Jenkins.instance.getExtensionList(
            'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
            ).first()

      def password = systemCredentialsProvider.credentials.first().password

      println password


    } else {
      println "could not find credential for ${username}"
    }
}

getPassword("jeanne")

回答by maosmurf

The official solution n the jenkins wiki

jenkins wiki的官方解决方案

Printing a list of all the credentials in the system and their IDs.

打印系统中所有凭据及其 ID 的列表。

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.Credentials.class,
        Jenkins.instance,
        null,
        null
);
for (c in creds) {
    println(c.id + ": " + c.description)
}

回答by zett42

If you just want to retrieve the credentials for a given credentials ID, the simplest way is to use the withCredentialspipeline step to bind credentials to variables.

如果您只想检索给定凭据 ID 的凭据,最简单的方法是使用withCredentials管道步骤将凭据绑定到变量。

withCredentials([usernamePassword( credentialsId: 'myCredentials', 
                     usernameVariable: 'MYUSER', passwordVariable: 'MYPWD' )]) { 
    echo "User: $MYUSER, Pwd: $MYPWD" 
}