Credentials, masking and scope
Folder scope, masking, what masking misses, and what is stored on disk.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/credentials.tar.gz && tar -xzf credentials.tar.gz, which creates ~/lab/credentials/. SHA-256: 8e708bc20592561a07ce097ac65197911fb420e4c10f02827a2ce6db5d856327Jenkins prints **** where your token would be, and it's tempting to conclude that the secret is safe. That's what many teams assume. Masking is a text filter applied to the console, and the step that holds the token can do anything it wants with it. In this lesson you'll use credentials the right way, then watch a pipeline print a masked token in a form the filter misses, so you know what the **** does and doesn't buy you.
Binding a credential for one block
A credential lives in Jenkins's store under an ID, and a Jenkinsfile only ever names the ID. withCredentials makes the value available as an environment variable for one block:
pipeline {agent { label 'linux' }stages {stage('Use the token') {steps {withCredentials([string(credentialsId: 'api-token', variable: 'TOKEN')]) {sh 'echo "length: ${#TOKEN}"'sh 'echo "plain: $TOKEN"'}sh 'echo "after the block: ${TOKEN:-unset}"'}}}}
Length 20, so the real value was available to the step. Printing it gave ****, in the trace line too. After the block, the variable is unset, which limits how much of the pipeline can touch the secret. Keep these blocks as short as you can.
What masking misses
pipeline {agent { label 'linux' }stages {stage('Leak') {steps {withCredentials([string(credentialsId: 'api-token', variable: 'TOKEN')]) {sh 'printf %s "$TOKEN" | base64'sh 'printf %s "$TOKEN" | rev'sh "echo groovy interpolated: ${TOKEN}"}}}}}
Three things happened. The base64 form was masked as well, because Jenkins also watches for common encodings of the value. Then rev printed 9876543210-nekot-bal, right there in the console. Anyone who can read this build's log can undo that:
That's the token, recovered from a "masked" log. Any transformation the filter doesn't know about gets past it, and a build could also just curl the value to a remote server, which no log filter can see at all. Masking stops accidents. Anyone who controls the pipeline code gets past it.
The third thing is the warning. In sh "echo ${TOKEN}", Groovy expanded the secret before the shell started, so the value became part of the command text that Jenkins handles and can record. The fix is the same single-quote rule from the parameters lesson: sh 'echo "$TOKEN"'. Treat this warning as an error in review.
| Control | Stops | Doesn't stop |
|---|---|---|
| Masking | A debug echo printing a secret | A transformed value, or sending it elsewhere |
Short withCredentials block | Later stages using the variable by accident | Code inside the block |
| Folder-scoped credentials | Jobs in other folders asking for the ID | Anyone who can edit a job in that folder |
| Separate agents | Other jobs reading files left on an agent | The job itself |
| Short-lived tokens | A leaked value working next week | Use during the build |
Who can bind a credential
Every credential so far sits in the global store, which any job on this Jenkins can bind, including a branch job whose Jenkinsfile anyone with push rights wrote. A folder narrows that. A credential stored in a folder is visible only to jobs inside it. Create a folder for one team, give it a token, and put one job inside and one outside, both running the same Jenkinsfile:
pipeline {agent { label 'linux' }stages {stage('Deploy') {steps {withCredentials([string(credentialsId: 'team-token', variable: 'TOKEN')]) {sh 'echo "team token available, length ${#TOKEN}"'}}}}}
Inside the folder, the job bound the 18-character token. Outside it, the same Jenkinsfile failed at withCredentials with Could not find credentials entry with ID 'team-token', exactly as if the credential didn't exist.
Out of the box, builds run as Jenkins's internal SYSTEM user with every permission, so the folder is the boundary that counts: any job inside it can bind the token, whoever started the build. The Authorize Project plugin can make builds run as the user who triggered them, so a build can do only what that user may do. For a release key, combine a folder with agents that only that folder's jobs can use, through a label nobody else is allowed to request.
A wrong ID, and storage at rest
A wrong ID fails the build with a clear message, at the point of use. On disk, credentials.xml doesn't contain the plaintext (grep -c finds 0 matches), and the value is encrypted with a key in secrets/. That protects a stray copy of the XML file, and it doesn't help if an attacker has both the file and that directory, which is one more reason builds never run on the controller. The API lists IDs and types, and it has no way to read a value back.
Hands-on
In a fresh lab, add a stage to app/Jenkinsfile.leak that prints the token with its characters separated by spaces: sh 'echo "$TOKEN" | sed "s/./& /g"'. Does masking catch that? Then rotate the credential: add a new one with a new ID, change the Jenkinsfile to use it, and delete the old one with ./jk.sh /credentials/store/system/domain/_/credential/api-token/doDelete -X POST. Run both jobs: which one breaks, and how? Finish with ./down.sh. Worked files are in solutions/.
What you should see: masking misses the spaced form, and the console shows l a b - t o k e n - 0 1 2 3 4 5 6 7 8 9 in full. After the rotation, the API lists only api-token-2, and use runs with the new 20-character token. leak, which still names api-token, fails at once with Could not find credentials entry with ID 'api-token'. That's the useful side of rotating by ID: every job you forgot to update fails loudly.
Remember
Your control over a secret ends with whoever can change a pipeline that binds it, since rev was all it took here. Scope credentials to folders, keep tokens short-lived, and rotate any secret you suspect was printed, however masked it looked.
sh "curl -H 'Authorization: Bearer ${TOKEN}' $URL" inside withCredentials. The console shows ****. What should the reviewer say?credentials.xml alone is leaked. How exposed are the secrets in it?JENKINS_HOME, so back them up separately and carefully.