0

Hi I would like to suppress some sensetive information displayed in the logs please help me do that.

CODE::

    pipeline {
    parameters {
    string(defaultValue: '', description: '', name: 'pki_client_cacert_password', trim: true)
    string(defaultValue: '', description: '', name: 'db_url', trim: true)
    }
    stages {
        stage('DeployToDev') {
                steps {
                    script{
                        env.artifacts = sh(
                        returnStdout: true, 
                        script: "/var/lib/jenkins/python_jobs/venv/bin/python3 /var/lib/jenkins/python_jobs/encrypter_creds.py --db_url=${env.db_url} --pki_client_cacert_password='${env.pki_client_cacert_password}'"
                        )
                    }

                }
            }
    }
}

OUTPUT::

+ /var/lib/jenkins/python_jobs/venv/bin/python3 /var/lib/jenkins/python_jobs/encrypter_creds.py --db_url=<hide this from jenkins logs> '--pki_client_cacert_password=<hide this from jenkins logs>'
Vaibhav Chauhan
  • 787
  • 3
  • 11
  • 29

2 Answers2

1

First option is to set it as environment var:

withEnv(["MYSECRET=${params.pki_client_cacert_password}", 
         "MYURL=${env.db_url}"]) {
    env.artifacts = sh(
        returnStdout: true, 
        script: '.. python3 .. encrypter_creds.py --db_url=$MYURL ' +
            ' --pki_client_cacert_password=$MYSECRET'
)

Note the single quotes around the command to prevent any Groovy string interpolation.

Second option is to save something like pki_client_cacert_password (which hopefully doesn't change much) into Jenkins Credentials Store and use it withCredentials:

withCredentials([usernamePassword(
  credentialsId: 'MY_PKI_CLIENT_CREDENTIALS',
  passwordVariable: 'DB_URL',
  usernameVariable: 'PKI_USER')]) {
  env.artifacts = sh(
        returnStdout: true, 
        script: '.. python3 .. encrypter_creds.py --db_url=$DB_URL' +
            ' --pki_client_cacert_password=$PKI_PASSWORD'

}

You can also roll your own third option, e.g. by writing the info you need into a file and modifying your script to read the parameters from that file.

MaratC
  • 4,882
  • 2
  • 16
  • 23
0

I have added @MaratC suggestion however that did not help much, so I ended up adding set +x and set -x more so this question was related to Echo off in Jenkins Console Output which worked as expected

            script{
               withEnv(["ENV_PKI_CLIENT_CACERT_PASSWORD=${params.pki_client_cacert_password}", "ENV_DB_URL=${params.db_url}"]) {
                    env.artifacts = sh(
                        returnStdout: true, 
                        script: """
                            set +x
                            /var/lib/jenkins/python_jobs/venv/bin/python3 /var/lib/jenkins/python_jobs/encrypter_creds.py --db_url=${ENV_DB_URL} --pki_client_cacert_password='${ENV_PKI_CLIENT_CACERT_PASSWORD}'
                            set -x
                            """
                        )           
            }
        }

Jenkins output

 + set +x
[Pipeline] .....
[Pipeline] .....
Vaibhav Chauhan
  • 787
  • 3
  • 11
  • 29