Jenkins凭证通过Groovy访问 [英] Jenkins Credentials Store Access via Groovy

查看:640
本文介绍了Jenkins凭证通过Groovy访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一种访问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 ==用户名? it:null}

if(c){
printlnfound for credential $ {c.id} for username $ {c.username}

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

printlnresult: + credentials_store
} else {
println找不到$ {username}的凭证
}
}

getPassword(XYZ)

但是现在我想为适合的用户获取我无法做到的密码.. 。

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



这是使用这个用户/密码来调用git和从存储库中提取信息。



我总是得到这样的东西:

 结果:com.cloudbees.plugins.credentials.SystemCredentialsProvider$StoreImpl@16 39eab2 



更新



和珍妮博伊萨斯基的提示)与我一起,我发现我正在考虑编译。以下已经为我提供了用户的密码:

  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 ==用户名?它:null}

if(c){
return c.password
} else {
println找不到$ {username}的凭证


$ / code>

此外,通过使用下面的代码片段,您可以迭代整个凭证store:

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

$ b printlncredentials_store:$ {credentials_store}
println描述:$ {credentials_store.description}
println目标:$ {credentials_store.target}
credentials_store.each {printlncredentials_store.each:$ {it}}

credentials_store [0] .credentials.each {it - >
println凭证: - > $ {it}
if(it instanceof com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl){
printlnXXX:username:$ {it .username}密码:$ {it.password}描述:$ {it.description}

}

你会得到如下输出:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b credentials_store:[com.cloudbees.plugins.credentials.SystemCredentialsProvider@5a2822be]
描述:[描述...]
目标:[com.cloudbees.plugins.credentials.SystemCredentialsProvider@5a2822be]
credentials_store.each:com.cloudbees.plugins.credentials.SystemCredentialsProvider@5a2822be
凭证: - > com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@38357ca1
凭证: - > com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@47cf7703
credentials: - > com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@739abac5
XXX:用户名:User1密码:密码描述:用户的描述。
凭证: - > com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@884a53e6
XXX:用户名:User2密码:Password1描述:user1的描述。
结果:[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]

所以通过使用适当的课程在 instanceof 子句,您可以选择您需要的内容。

作品。它获得的凭据,而不是商店。



我没有写任何错误处理,所以如果你没有设置证书对象(或可能如果你有两个)。这部分虽然很容易添加。棘手的部分是获得正确的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 ==用户名? it:null}

if(c){
printlnfound for 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找不到$ {username}的凭证
}
}

getPassword(jeanne)


I have found a way to access the credentials store in 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..

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:

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 instanceof clause you can select what you need.

解决方案

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!

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")

这篇关于Jenkins凭证通过Groovy访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆