通过Groovy访问Jenkins Credentials商店

时间:2016-02-04 15:58:22

标签: git jenkins groovy

我找到了一种访问credentials store in Jenkins的方法:

transition.addTarget(mTabs);

但现在我想获得相关用户的密码,我无法做到......

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

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

我总是得到这样的东西:

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

更新

在尝试了更多(以及Jeanne Boyarsky的暗示)后,我发现我正在考虑编纂。以下内容已经为我提供了用户的密码:

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

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

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

你会得到这样的输出:

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

因此,使用appropriate class in the instanceof clause,您可以选择所需内容。

3 个答案:

答案 0 :(得分:15)

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

我没有编写任何错误处理,所以如果您没有设置凭据对象(或者可能有两个),它就会爆炸。这部分很容易添加。棘手的部分是获得正确的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")

答案 1 :(得分:9)

jenkins wiki

的官方解决方案
  

打印系统中所有凭据的列表及其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)
}

答案 2 :(得分:0)

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

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