Last active
September 9, 2020 09:19
-
-
Save marcelaraujo/57e73da98e5aae1de1d0f93a910777a2 to your computer and use it in GitHub Desktop.
List Jenkins 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
import com.cloudbees.plugins.credentials.CredentialsNameProvider | |
import com.cloudbees.plugins.credentials.Credentials | |
import com.cloudbees.plugins.credentials.CredentialsProvider | |
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials | |
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey | |
import org.jenkinsci.plugins.plaincredentials.StringCredentials | |
import org.jenkinsci.plugins.plaincredentials.FileCredentials | |
String checksum( String input ) { | |
def digest = java.security.MessageDigest.getInstance("SHA-256") | |
digest.update( input.bytes ) | |
new BigInteger(1,digest.digest()).toString(16).padLeft(32, '0') | |
} | |
def printCredentials(Credentials credential) { | |
if(credential instanceof StandardUsernamePasswordCredentials) | |
println(""" | |
Username Password | |
ID: ${credential.id} | |
Description: ${credential.description} | |
Username: ${credential.username} | |
Password: ${credential.password?.getPlainText()}\n\n | |
""") | |
else if(credential instanceof SSHUserPrivateKey) | |
println(""" | |
SSH User Private Key | |
ID: ${credential.id} | |
Description: ${credential.description} | |
Key: | |
${credential.privateKeySource?.getPrivateKey()} | |
Passphrase: | |
${credential.passphrase?.getPlainText()}\n\n | |
""") | |
else if(credential instanceof StringCredentials) | |
println(""" | |
String | |
ID: ${credential.id} | |
Description: ${credential.description} | |
Secret: ${credential.secret?.getPlainText()}\n\n | |
""") | |
else if(credential instanceof FileCredentials) | |
println(""" | |
File | |
ID: ${credential.id} | |
Description: ${credential.description} | |
Content: | |
${credential.content?.text}\n\n | |
""") | |
else | |
println(""" | |
${credential.class.name} | |
ID: ${credential.id} | |
Description: ${credential.description}\n\n | |
""") | |
} | |
Jenkins.instance.getAllItems(com.cloudbees.hudson.plugins.folder.Folder.class).each{ f -> | |
for(Credentials credential : CredentialsProvider.lookupCredentials( Credentials.class, f)) { | |
printCredentials(credential) | |
} | |
} | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment