Configuration as code (JCasC)
Export, reload, undo drift, reject a typo, and apply least privilege.
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: da18f3ed49f470c3114b89362b2e172623fc6a317ca19fadba5188e05a1bb1e1An 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:
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:
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:
jenkins:numExecutor: 1
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:
jenkins:numExecutors: 0systemMessage: "Managed by JCasC. Changes made in the UI do not survive a reload."securityRealm:local:allowsSignup: falseusers:- id: adminpassword: ${JENKINS_ADMIN_PASSWORD}- id: viewerpassword: lab-viewer-test-passwordauthorizationStrategy:globalMatrix:entries:- user:name: adminpermissions: ["Overall/Administer"]- user:name: viewerpermissions: ["Overall/Read", "Job/Read"]nodes:- permanent:name: lab-agentlabelString: linuxremoteFS: /home/jenkins/agentnumExecutors: 2launcher: inboundunclassified:location:url: http://controller:8080/
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.
${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.
/configuration-as-code/reload and reports success on HTTP 200. What could go unnoticed?viewer Overall/Read and Job/Read. How do you know the policy works?