Configuration as code (JCasC)

Export, reload, undo drift, reject a typo, and apply least privilege.

Advanced13 min · lesson 14 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (20 files, 7 KB): jcasc.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/jcasc.tar.gz && tar -xzf jcasc.tar.gz, which creates ~/lab/jcasc/. SHA-256: da18f3ed49f470c3114b89362b2e172623fc6a317ca19fadba5188e05a1bb1e1

An admin changes the number of executors on a Saturday to get a stuck release out and forgets to mention it. Three months later a build runs on the controller and nobody can say when that became possible. Configuration you can click is configuration that drifts. With Configuration as Code, the YAML in Git is the truth, and a reload puts Jenkins back to match it. You've been relying on this all course. Here you'll test what it does and how it fails.

Seeing the configuration

Jenkins can export what it's running as YAML. That makes a good starting point if you're moving an existing server to JCasC, though the export needs cleaning up:

ubuntu@secopslog-cicd:~/lab/jcasc · Jenkins 2.580.2
$ ./up.sh
agent lab-agent: online Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ ./jk.sh /configuration-as-code/export -X POST | sed -n '/^jenkins:/,/^[a-z]/p' | grep -E 'numExecutors|allowsSignup|systemMessage|loggedInUsers'
loggedInUsersCanDoAnything: numExecutors: 2 numExecutors: 0 allowsSignup: false

Drift, and undoing it

Simulate that Saturday: change the executor count through the admin script console, an effect you could equally get in the UI. Then reload the YAML:

ubuntu@secopslog-cicd:~/lab/jcasc · Jenkins 2.580.2
$ ./jk.sh /scriptText -X POST --data-urlencode 'script=Jenkins.get().setNumExecutors(5); println Jenkins.get().numExecutors'
5
$ ./jk.sh /configuration-as-code/reload -X POST -o /dev/null; ./jk.sh /scriptText -X POST --data-urlencode 'script=println Jenkins.get().numExecutors'
0

Five executors, then zero again after the reload. Reloading corrects anything JCasC manages, and it's how you apply a change after merging it. Settings your YAML doesn't mention stay as they are, so drift in those won't be corrected. Run a reload after each merge and on a schedule, so a hand edit doesn't last long. Before you schedule it, check what a reload touches on your server: it re-applies everything in the YAML, including nodes: and securityRealm, so test once that it leaves connected agents and running builds alone.

What a typo does

This file has numExecutor, missing an s:

00-base-typo.yaml
jenkins:
numExecutor: 1
ubuntu@secopslog-cicd:~/lab/jcasc · Jenkins 2.580.2
$ cp casc.d/00-base.yaml /tmp/base.ok; cp 00-base-typo.yaml casc.d/00-base.yaml; ./jk.sh /configuration-as-code/reload -X POST -o /dev/null -w '%{http_code}\n' 2>&1 | cut -c1-120; docker compose logs --no-log-prefix --tail 400 controller | grep -oE 'Invalid configuration elements for type.*|No configurator for.*|Attribute .* not.*' | tail -1 | cut -c1-150; cp /tmp/base.ok casc.d/00-base.yaml; rm /tmp/base.ok
200 Invalid configuration elements for type: class jenkins.model.Jenkins : numExecutor.

The HTTP call returned 200, yet the reload failed. The real error, Invalid configuration elements, is in the controller's log. So a pipeline that applies JCasC mustn't trust the status code alone, and should check the logs or compare an export afterwards. JCasC being strict here is useful, though: a misspelled security setting that silently did nothing would be much worse.

Least privilege as a file

loggedInUsersCanDoAnything makes everyone an admin, and it's only acceptable with one user. This version adds a read-only user with a matrix, and a banner. Matrix permissions are named group and action: Overall/Read lets someone see Jenkins at all, Job/Read lets them see a job, and Job/Build lets them start it:

00-base-matrix.yaml
jenkins:
numExecutors: 0
systemMessage: "Managed by JCasC. Changes made in the UI do not survive a reload."
securityRealm:
local:
allowsSignup: false
users:
- id: admin
password: ${JENKINS_ADMIN_PASSWORD}
- id: viewer
password: lab-viewer-test-password
authorizationStrategy:
globalMatrix:
entries:
- user:
name: admin
permissions: ["Overall/Administer"]
- user:
name: viewer
permissions: ["Overall/Read", "Job/Read"]
nodes:
- permanent:
name: lab-agent
labelString: linux
remoteFS: /home/jenkins/agent
numExecutors: 2
launcher: inbound
unclassified:
location:
url: http://controller:8080/
ubuntu@secopslog-cicd:~/lab/jcasc · Jenkins 2.580.2
$ cp 00-base-matrix.yaml casc.d/00-base.yaml; ./jk.sh /configuration-as-code/reload -X POST -o /dev/null; ./jk.sh /configuration-as-code/export -X POST | grep -E 'systemMessage'
systemMessage: "Managed by JCasC. Changes made in the UI do not survive a reload."
$ ./seed.sh app app/ && ./newjob.sh hello app && ./run-job.sh hello | tail -1
pushed app: 1575034 first commit job hello reads Jenkinsfile from git://git/app.git build #1 result: SUCCESS
$ JK_USER=viewer JK_PASS=lab-viewer-test-password ./jk.sh '/job/hello/api/json?tree=name,color' | jq -c 'del(._class)'
{"name":"hello","color":"blue"}
$ JK_USER=viewer JK_PASS=lab-viewer-test-password ./jk.sh /job/hello/build -X POST
curl: (22) The requested URL returned error: 403
$ JK_USER=viewer JK_PASS=lab-viewer-test-password ./jk.sh /scriptText -X POST --data-urlencode 'script=println 1'
curl: (22) The requested URL returned error: 403
$ ./down.sh
lab stopped and its data removed

The viewer can read the job, but can't start it and can't use the script console. Each 403 is a permission the viewer doesn't have. Test the denied paths like this whenever you change permissions, since a mistake in an authorization policy doesn't show up as a broken page for anyone with more access than you're testing. Real deployments connect the securityRealm to LDAP or OIDC, and give permissions to groups.

Watch out
The viewer's password is written in this YAML to keep the lab short. Real configs use ${VAR} for every secret, set from a secret store as with the admin password. In particular, treat /scriptText as root on the controller: only admins can reach it, and that's where an attacker with admin goes first. The exported YAML can include encrypted secrets, so review it before committing.

Hands-on

In a fresh lab, apply 00-base-matrix.yaml, then give viewer Job/Build in the YAML and reload. Confirm viewer-build now succeeds. Add a third user, deployer, with Overall/Read, Job/Read and Job/Build, and test each with JK_USER. Then break the file on purpose and work out a check that would catch it in a pipeline. Finish with ./down.sh. One answer for each part is in solutions/.

What you should see: after the reload, both viewer and deployer can start hello (HTTP 201). deployer can read the job and gets 403 from /scriptText. With the typo file, the checked reload prints Invalid configuration elements for type: class jenkins.model.Jenkins : numExecutor. and exits 1, where a bare reload returned 200. The script in solutions/ reads the controller log written since the reload, which is the check a pipeline needs.

Remember

Make the YAML the only way to change settings and reload it on a schedule. Check the log after a reload, because the typo still returned 200. Test permissions from the side that should fail.

Quick check
01An automation job applies JCasC with /configuration-as-code/reload and reports success on HTTP 200. What could go unnoticed?
Incorrect — The lab got a 200 on a file that was rejected.
Incorrect — Duration isn't the concern. A failed apply is.
Correct — Check the logs or compare an export afterwards, as the status code alone misleads.
Incorrect — The password handling isn't what this status hides.
02Someone changed the executor count in the UI. The YAML says 0, and a reload runs. What happens to a setting that is *not* in the YAML but was also changed by hand?
Correct — That's why coverage matters: a setting outside the YAML can drift without anyone noticing.
Incorrect — JCasC does not reset unmentioned settings.
Incorrect — A reload ignores what the file does not mention.
Incorrect — JCasC doesn't write to your YAML.
03You give viewer Overall/Read and Job/Read. How do you know the policy works?
Incorrect — That shows what was configured, not what it denies.
Correct — Test the denied path too, because the risk is access you did not mean to grant.
Incorrect — Executors and permissions are unrelated.
Incorrect — An admin run says nothing about what a viewer can do.

Related