DevSecOps and supply chain
Scan source and images in the pipeline, publish an SBOM, sign the image with cosign, and reject unsigned images on OpenShift.
A pipeline that only runs tests will happily build a secret that was committed on Friday and an image with a critical flaw in a base layer. Supply-chain controls sit in the same pipeline as the build, and again at admission, so a signed image is the only kind the cluster will run.
This lab adds four gates to the pipeline from GitLab CI or OpenShift Pipelines: secret detection, an SBOM, an image scan, and a signature. An ImagePolicy then rejects unsigned images in the production project.
Prerequisites
- A project
payments-prodand a built image in a registry the cluster can pull cosign,syftandgitleakson the runner image, or as task steps- Cluster-admin only if you later add a cluster-wide
ClusterImagePolicy. The namespaced policy below does not need it
Scan before you ship
Run secret detection on the clone, before the image exists. gitleaks exits non-zero when it finds a credential, which fails the pipeline.
gitleaks detect --source . --redact --exit-code 1
After the image is pushed, write a CycloneDX SBOM and scan the image. Keep the SBOM next to the tag you are about to deploy. syft reads the image layers. roxctl image scan asks Red Hat Advanced Cluster Security, which is the scanner we run in the lab. Quay's scanner is the same kind of gate if the registry is Quay.
IMAGE=image-registry.openshift-image-registry.svc:5000/payments/payments:a1b2c3d
syft "$IMAGE" -o cyclonedx-json > sbom.cdx.json
roxctl image scan --image "$IMAGE" --output json > image-scan.json
Fail the pipeline when the scan reports a fixable critical vulnerability. Do not fail the whole estate on a finding you have already accepted: ACS policies are where that exception is recorded, with an expiry, so it does not live in someone's head.
Sign, then require the signature
A signature binds a person or a pipeline identity to a digest. Generate a key pair once and keep the private key in the CI secret store. --tlog-upload=false keeps the lab on the key pair alone. Add a transparency log when you move to Red Hat Trusted Artifact Signer.
cosign generate-key-pair
DIGEST=$(skopeo inspect --tls-verify=false docker://"$IMAGE" | jq -r .Digest)
cosign sign --key cosign.key --tlog-upload=false "${IMAGE%:*}@${DIGEST}"
cosign verify --key cosign.pub --insecure-ignore-tlog=true "${IMAGE%:*}@${DIGEST}"
Put cosign verify in the pipeline after the scan and before the GitOps tag is updated. A failed verify must fail the job.
Admission is the second line, for the deploy that did not come through the pipeline. This ImagePolicy applies only to payments-prod. keyData is the base64 encoding of cosign.pub.
base64 -w0 cosign.pub
apiVersion: config.openshift.io/v1
kind: ImagePolicy
metadata:
name: require-signature
namespace: payments-prod
spec:
scopes:
- image-registry.openshift-image-registry.svc:5000/payments/payments
policy:
rootOfTrust:
policyType: PublicKey
publicKey:
keyData: <base64 of cosign.pub>
signedIdentity:
matchPolicy: MatchRepository
MatchRepository allows the tag you already deploy, as long as that tag's signature verifies. A stricter policy uses MatchRepoDigestOrExact and deploys by digest. When signatures are recorded in Rekor, set rekorKeyData to the base64 of that Rekor public key as well.
Confirm the policy with an unsigned tag. The pod should stay in ImagePullBackOff or be rejected at admission, while the signed tag from the pipeline still runs.
Where each control sits
| Control | Catches | Where it runs |
|---|---|---|
| Secret scan | A key or password committed to Git | First pipeline stage, on the clone |
| SBOM | "What is in this image?" months later | Published beside the signed digest |
| Image scan | Known vulnerabilities in layers | After build, before the tag is promoted |
cosign verify |
An image the pipeline did not sign | The release job |
ImagePolicy |
A hand-rolled oc set image of an unsigned tag |
The production project |
None of these replace patching. A signature says who built the image. It does not say the image is safe. The scan is what says that, and only for the vulnerabilities your scanner knew about on that day. Re-scan images you have not rebuilt when the scanner's data updates.
What's next
- The pipeline this plugs into is GitLab CI or OpenShift Pipelines.
- Promotion of a signed tag is a merge request in OpenShift GitOps.
- The same "measure it" habit, applied to lead time and failures, is Measuring DevOps.