I have found a way to access the credentials store in Jenkins:
def getPassword = { username ->
def creds = com.cloudbees.plugins.credentials.CredentialsPr
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")