Iterate and dump specific Jenkins Credentials
Little snippet that can be used from the Script Console to dump specific credentials.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( | |
com.cloudbees.plugins.credentials.common.StandardCredentials.class, | |
Jenkins.instance, // or Jenkins.instance.getItem("someItem"), | |
null, | |
null | |
); | |
String streamToString(InputStream input) { | |
Scanner s = new Scanner(input); | |
StringBuilder builder = new StringBuilder(); | |
while (s.hasNextLine()) { | |
builder.append(s.nextLine() +"\n"); | |
} | |
return builder.toString(); | |
} | |
for (c in creds) { | |
if (c.id == 'mySpecificCredentialID') { | |
if(c instanceof com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey){ | |
println(String.format("id=%s desc=%s key=%s\n", c.id, c.description, c.privateKeySource.getPrivateKeys())) | |
} | |
else if (c instanceof com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl){ | |
println(String.format("id=%s desc=%s user=%s pass=%s\n", c.id, c.description, c.username, c.password)) | |
} | |
else if (c instanceof org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl){ | |
println(streamToString( c.getContent())) | |
} | |
else if (c instanceof org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl){ | |
println(c.getSecret()) | |
} | |
} | |
} |