Shared libraries and the sandbox

One reviewed pipeline vocabulary, pinned by tag, and what trust it carries.

Advanced13 min · lesson 12 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (21 files, 7 KB): sharedlibs.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/sharedlibs.tar.gz && tar -xzf sharedlibs.tar.gz, which creates ~/lab/sharedlibs/. SHA-256: 28e44ac0ee1a7a0fb5915b7621915575c09a2beaa7af772aa53282d929c4f2c6

Forty repos each have a copy of the same deploy stage, and each has drifted a little. A security fix means forty pull requests, and some will sit for weeks. Shared libraries solve this: common pipeline code lives in one repo, and every Jenkinsfile calls it. But they also come with a catch you need to know. A global library is trusted code, with more power than the Jenkinsfiles that call it, so whoever can change it holds the keys to your Jenkins.

A library is a Git repo

A file in vars/ becomes a step, named after the file:

lib/vars/deployTo.groovy
// A pipeline step every repo can call: deployTo('staging')
def call(String target) {
echo "lib v1: deploying to ${target}"
}

The library is registered with Jenkins through JCasC, like the rest of the configuration:

casc.d/10-library.yaml
unclassified:
globalLibraries:
libraries:
- name: lab
defaultVersion: v1
allowVersionOverride: true
implicit: false
retriever:
modernSCM:
scm:
git:
remote: git://git/lab-lib.git

defaultVersion: v1 is the key line. Jobs that don't ask for a version get tag v1, so a push to the library's main branch can't change every pipeline at once. implicit: false means a Jenkinsfile must load the library explicitly. A Jenkinsfile then calls it like any other step:

app/Jenkinsfile
@Library('lab') _
pipeline {
agent { label 'linux' }
stages {
stage('Deploy') { steps { deployTo 'staging' } }
}
}

The _ after the annotation is required syntax; the annotation has to attach to something. Publish two tagged versions, then run one job on each:

ubuntu@secopslog-cicd:~/lab/sharedlibs · Jenkins 2.580.2
$ ./up.sh
agent lab-agent: online Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ ./seed.sh lab-lib lib/ && git -C lib tag v1 && git -C lib push -q origin v1 && git -C lib tag --list | cat
pushed lab-lib: ea57da1 first commit v1
$ cp lib-v2.groovy lib/vars/deployTo.groovy && ./commit.sh lib/ "v2: count users" && git -C lib tag v2 && git -C lib push -q origin v2 && git -C lib tag --list | cat
pushed: 39da0c6 v2: count users v1 v2
$ ./seed.sh app app/ && ./newjob.sh pinned app && ./newjob.sh latest app Jenkinsfile.v2 && ./newjob.sh sandbox app Jenkinsfile.sandbox
pushed app: 921d274 first commit job pinned reads Jenkinsfile from git://git/app.git job latest reads Jenkinsfile.v2 from git://git/app.git job sandbox reads Jenkinsfile.sandbox from git://git/app.git
$ ./run-job.sh pinned | grep -E "^(lib v|Loading library|build #)"
Loading library lab@v1 lib v1: deploying to staging build #1 result: SUCCESS

Loading library lab@v1 confirms the pin. Version 2 is already on the Git server, and this job didn't change behaviour, because nothing asked for it. A team can move at its own pace with one line in its Jenkinsfile:

app/Jenkinsfile.v2
@Library('lab@v2') _
pipeline {
agent { label 'linux' }
stages {
stage('Deploy') { steps { deployTo 'staging' } }
}
}
ubuntu@secopslog-cicd:~/lab/sharedlibs · Jenkins 2.580.2
$ ./run-job.sh latest | grep -E "^(lib v|Loading library|build #)"
Loading library lab@v2 lib v2: deploying to staging, Jenkins has 2 user(s) build #1 result: SUCCESS

The sandbox, and what's outside it

Version 2 called hudson.model.User.getAll(), which reaches into the controller. Try the identical call directly from a Jenkinsfile:

app/Jenkinsfile.sandbox
pipeline {
agent { label 'linux' }
stages {
stage('Reach for the controller') {
steps {
script { echo "Jenkins has ${hudson.model.User.getAll().size()} user(s)" }
}
}
}
}
ubuntu@secopslog-cicd:~/lab/sharedlibs · Jenkins 2.580.2
$ ./run-job.sh sandbox | grep -E "RejectedAccess|Scripts not permitted|build #" | cut -c1-150; exit ${PIPESTATUS[0]}
Scripts not permitted to use staticMethod hudson.model.User getAll. Administrators can decide whether to approve or reject this signature. org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod hudson.model.User getAll build #1 result: FAILURE
$ ./down.sh
lab stopped and its data removed

A pipeline from Git runs in the Groovy sandbox, and it's stopped with Scripts not permitted to use staticMethod hudson.model.User getAll. The same call worked in the library, because global libraries are trusted and run outside the sandbox. Notice that the job asked for lab@v2 and it just worked, with no approval from anyone.

Trust levels in this lab
1Jenkinsfile
sandboxed; the allowlist decides
2Global library
trusted; full access to the controller JVM
3Script console
admins only; full access
A folder-level library runs in the sandbox instead. Use one for team helpers that do not need extra privileges.

So treat the library repository like the controller itself. Require reviews and protected tags on it, since a change there runs code on the controller for every job that loads it. And allowVersionOverride: true, which this lab enabled so you could try @v2, also lets any Jenkinsfile load any branch in that repo with trusted privileges. Consider turning it off, or keeping experiments in a folder library.

Watch out
When a sandbox rejection appears, an administrator can approve that signature in Manage Jenkins > In-process Script Approval. Each approval widens the allowlist for every job. Don't approve anything that reaches Jenkins, User, files or credentials; move that logic behind a reviewed library step instead.

Hands-on

In a fresh lab, publish the library with tags v1 and v2 (./seed.sh lab-lib lib/, then git -C lib tag and git -C lib push origin v1 v2), and create the pinned and latest jobs. Set allowVersionOverride: false in casc.d/10-library.yaml and apply it with ./jk.sh /configuration-as-code/reload -X POST, which re-reads the YAML without a restart (the JCasC lesson covers it). Run latest, read the error, then run pinned. Finish with ./down.sh.

What you should see: latest fails at once with Version override not permitted for library lab, before any stage runs. pinned still loads lab@v1 and succeeds. With the override off, the library's maintainers decide which version runs, not each Jenkinsfile.

Remember

Pin libraries to tags, so a push can't change every pipeline at once. getAll() was blocked in the Jenkinsfile and allowed in the library, so review a global library like controller code.

Quick check
01A library is registered with defaultVersion: main. Someone merges a mistake to main on a Friday evening. What happens?
Correct — A moving default affects everyone at once, while a tag makes each upgrade a choice.
Incorrect — There is no such cache unless you configure one, and it would not be a safe control.
Incorrect — No approval is required for global library changes. That is part of the danger.
Incorrect — The library version has nothing to do with the calling repo's branch.
02A Jenkinsfile gets Scripts not permitted to use staticMethod .... A developer asks you to approve it in Script Approval so their build can pass. What should you weigh?
Incorrect — Approvals do not expire. That's part of why they deserve care.
Incorrect — An approval is global, whoever asked for it.
Incorrect — Approvals are tied to signatures, not plugin pins.
Correct — If the call reaches into the controller, put it behind a reviewed step.
03Why is write access to a global library's repository so sensitive?
Incorrect — It grants nothing on other repositories. The concern is what its code can do.
Incorrect — Pipeline code runs on the controller. Agents run the steps it sends them.
Correct — In the lab the same call that was rejected in a Jenkinsfile worked in the library.
Incorrect — A library doesn't store credentials, though its code could reach them.

Related