Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save briceburg/67b35e4994d1a162355171e7ba859384 to your computer and use it in GitHub Desktop.
Save briceburg/67b35e4994d1a162355171e7ba859384 to your computer and use it in GitHub Desktop.
Print content of secret files from the Jenkins Credentials Store
import com.cloudbees.plugins.credentials.*;
import com.cloudbees.plugins.credentials.domains.Domain;
import org.jenkinsci.plugins.plaincredentials.impl.FileCredentialsImpl;
//
// modify fileName to match the filename of the secret(s) you want to print.
// (ID would probably be more helpful... yay stack overflow copy pasta)
// alternatively comment out the filter [line 15] to dump all secret files.
//
def fileName = "secrets.env"
SystemCredentialsProvider.getInstance().getCredentials().stream().
filter { cred -> cred instanceof FileCredentialsImpl }.
map { fileCred -> (FileCredentialsImpl) fileCred }.
filter { fileCred -> fileName.equals( fileCred.getFileName() ) }.
forEach { fileCred ->
String s = new String( fileCred.getSecretBytes().getPlainData() )
println ""
println "XXXXXX BEGIN a secret file with id=" + fileCred.getId() + " fileName=" + fileName + " XXXXXXXXXXXX"
println s
println ""
}
@SakeebHossain
Copy link

Thanks! Another way to go about this is to just create a job that prints the contents of the secret file:

node('<PICK A NODE TO RUN ON e.g. master>') {
    String text
    withCredentials([file(credentialsId: '<YOUR CREDENTIAL ID HERE>', variable: 'FILE')]) {
        text = readFile(FILE)
    }
    
    println "${text}"
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment