Jenkins Credentials Store Access via Groovy

后端 未结 3 788
长情又很酷
长情又很酷 2021-01-31 09:35

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

def getPassword = { username ->
    def creds = com.cloudbees.plugins.credentials.CredentialsPr         


        
3条回答
  •  感情败类
    2021-01-31 10:36

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

提交回复
热议问题