Home > Mobile >  Jenkins SecretFile Decrypt
Jenkins SecretFile Decrypt

Time:09-06

Normally, I can decrypt a password or a secret-text like below

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.Credentials.class,
Jenkins.instance,
null,
null
);
for (creds in jenkinsCredentials) {
if(creds.id == 'XYZ'){ 
for password-------> println(creds.password)  
-----OR------  
for secret-text-----> println(creds.secret) 
                           }
}

But I could not find a way to decrypt a secret-file with this method. When I need to get the last version of the replaced file this becomes a problem. How can I get the uploaded or the replaced last version secret file that is registered in Jenkins credentials?

CodePudding user response:

I have found the working function format for the secretFile

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
import org.apache.commons.io.IOUtils
def jenkinsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.Credentials.class,
Jenkins.instance,
null,
null
);
for (creds in jenkinsCredentials) {
if(creds.id == 'XYZ'){ 
println(IOUtils.toString(creds.getContent(), "UTF-8"))  
                     }
}

So, the new println line works after the fuction add

CodePudding user response:

If you want to get the decrypted content of the file. You can read the content with getContent() which return a InputStream and then use getText() to convert it into a String.

creds.getContent().getText()
  • Related