id: copacetic-patch-on-registry-push
namespace: company.security
inputs:
- id: repository
type: STRING
displayName: Repository
description: Repository in the registry, without the registry host, as the push
event reports it in `target.repository`.
defaults: demo/nginx
- id: tag
type: STRING
displayName: Tag
defaults: 1.29.0-alpine
- id: digest
type: STRING
displayName: Digest
description: Manifest digest from the push event. Leave empty on a manual run to
patch whatever the tag points to now.
required: false
variables:
registry: localhost:5000
registry_tls: disabled
staging_prefix: copa-staging
patched_suffix: -patched
buildkit_addr: tcp://localhost:1234
trivy_server: http://localhost:4954
approval_window: P7D
trivy_image: aquasec/trivy:0.76.0
copa_image: ghcr.io/project-copacetic/copa-action:v0.15.0
regctl_image: ghcr.io/regclient/regctl:v0.11.6-alpine
tasks:
- id: target
type: io.kestra.plugin.core.output.OutputValues
description: A push event carries the digest, so the scan reads exactly the
pushed image. The patched image goes to a staging repository first and
only reaches the source repository once verified.
values:
source: "{{ vars.registry }}/{{ inputs.repository }}{{ (inputs.digest ?? '') ==
'' ? (':' ~ inputs.tag) : ('@' ~ inputs.digest) }}"
staging: "{{ vars.registry }}/{{ vars.staging_prefix }}/{{ inputs.repository
}}:{{ inputs.tag }}{{ vars.patched_suffix }}"
published: "{{ vars.registry }}/{{ inputs.repository }}:{{ inputs.tag }}{{
vars.patched_suffix }}"
- id: label_image
type: io.kestra.plugin.core.execution.Labels
labels:
image: "{{ inputs.repository }}:{{ inputs.tag }}"
- id: scan_source
type: io.kestra.plugin.scripts.shell.Commands
description: Trivy runs in client mode, so the vulnerability database lives on
the Trivy server and is not downloaded on every run. `--ignore-unfixed`
keeps only vulnerabilities that have a fixed package version, which are
the ones Copacetic can patch.
containerImage: "{{ vars.trivy_image }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: host
outputFiles:
- report.json
commands:
- >-
trivy image --server {{ vars.trivy_server }} --image-src remote
--pkg-types os --ignore-unfixed --disable-telemetry --skip-version-check
--quiet --format json --output report.json {{
outputs.target.values.source }}
- id: before
type: io.kestra.plugin.core.output.OutputValues
values:
fixable: >-
{{ read(outputs.scan_source.outputFiles['report.json']) |
jq('[.Results[]? | select(.Class == "os-pkgs") | .Vulnerabilities[]?] |
length') | first }}
cves: >-
{{ read(outputs.scan_source.outputFiles['report.json']) |
jq('[.Results[]? | select(.Class == "os-pkgs") | .Vulnerabilities[]? |
.VulnerabilityID] | unique | length') | first }}
packages: >-
{{ read(outputs.scan_source.outputFiles['report.json']) |
jq('[.Results[]? | select(.Class == "os-pkgs") | .Vulnerabilities[]? |
.PkgName] | unique | join(", ")') | first }}
package_count: >-
{{ read(outputs.scan_source.outputFiles['report.json']) |
jq('[.Results[]? | select(.Class == "os-pkgs") | .Vulnerabilities[]? |
.PkgName] | unique | length') | first }}
severities: >-
{{ read(outputs.scan_source.outputFiles['report.json']) |
jq('[.Results[]? | select(.Class == "os-pkgs") | .Vulnerabilities[]?] as
$v | ["CRITICAL", "HIGH", "MEDIUM", "LOW", "UNKNOWN"] | map(. as $s |
{s: $s, n: ($v | map(select(.Severity == $s)) | length)}) |
map(select(.n > 0) | .s + " " + (.n | tostring)) | join(", ")') | first
}}
os: >-
{{ read(outputs.scan_source.outputFiles['report.json']) |
jq('((.Metadata.OS.Family // "unknown") + " " + (.Metadata.OS.Name //
"")) + (if .Metadata.OS.EOSL then " (end of life)" else "" end)') |
first }}
digest: >-
{{ read(outputs.scan_source.outputFiles['report.json']) |
jq('(.Metadata.RepoDigests // [])[0] // "" | split("@") | last') | first
}}
findings: >-
{{ read(outputs.scan_source.outputFiles['report.json']) |
jq('[.Results[]? | select(.Class == "os-pkgs") | .Vulnerabilities[]? | .
+ {rank: (if .Severity == "CRITICAL" then 0 elif .Severity == "HIGH"
then 1 elif .Severity == "MEDIUM" then 2 elif .Severity == "LOW" then 3
else 4 end)}] | sort_by(.rank, .PkgName, .VulnerabilityID) |
map(.Severity + " " + .VulnerabilityID + " " + .PkgName + " " +
.InstalledVersion + " -> " + .FixedVersion) | join("\n")') | first }}
- id: nothing_to_patch
type: io.kestra.plugin.core.flow.If
condition: "{{ (outputs.before.values.fixable | number) == 0 }}"
then:
- id: label_clean
type: io.kestra.plugin.core.execution.Labels
labels:
outcome: nothing-to-patch
fixable_before: "0"
- id: log_clean
type: io.kestra.plugin.core.log.Log
message: "{{ inputs.repository }}:{{ inputs.tag }} ({{ outputs.before.values.os
}}) has no OS package vulnerability with a fix available. Nothing to
patch."
- id: stop_clean
type: io.kestra.plugin.core.execution.Exit
state: SUCCESS
- id: label_pending
type: io.kestra.plugin.core.execution.Labels
labels:
outcome: awaiting-approval
fixable_before: "{{ outputs.before.values.fixable }}"
- id: show_findings
type: io.kestra.plugin.core.log.Log
message: |
{{ outputs.target.values.source }} ({{ outputs.before.values.os }})
Fixable OS package vulnerabilities: {{ outputs.before.values.fixable }} ({{ outputs.before.values.cves }} CVEs; {{ outputs.before.values.severities }}) in {{ outputs.before.values.packages }}.
{{ outputs.before.values.findings }}
Resume with PATCH to update these packages with Copacetic and publish {{ outputs.target.values.published }} once a re-scan confirms the fix, or REJECT to leave the registry unchanged.
- id: approve
type: io.kestra.plugin.core.flow.Pause
description: Nothing is built or pushed before someone resumes this execution.
If nobody answers within `approval_window`, the execution is cancelled.
pauseDuration: "{{ vars.approval_window }}"
behavior: CANCEL
onResume:
- id: decision
type: SELECT
displayName: Decision
description: PATCH runs Copacetic and publishes the patched tag only if a
re-scan shows fewer fixable vulnerabilities. REJECT changes nothing.
isRadio: true
expression: >-
{{ [ {'label': 'PATCH: fix ' ~ outputs.before.values.fixable ~ '
vulnerabilities (' ~ outputs.before.values.cves ~ ' CVEs) in ' ~
outputs.before.values.package_count ~ ' packages', 'value': 'PATCH'},
{'label': 'REJECT: leave ' ~ inputs.repository ~ ':' ~ inputs.tag ~ '
as it is', 'value': 'REJECT'} ] }}
- id: note
type: STRING
displayName: Note
description: Why you decided this (a change ticket, for example). Kept in the
execution.
required: false
- id: rejected
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.approve.onResume.decision == 'REJECT' }}"
then:
- id: label_rejected
type: io.kestra.plugin.core.execution.Labels
labels:
outcome: rejected
- id: log_rejected
type: io.kestra.plugin.core.log.Log
level: WARN
message: "Patching {{ inputs.repository }}:{{ inputs.tag }} was rejected{{
(outputs.approve.onResume.note ?? '') == '' ? '' : ': ' ~
outputs.approve.onResume.note }}. Nothing was built or pushed; fixable
vulnerabilities left in the image: {{ outputs.before.values.fixable
}}."
- id: stop_rejected
type: io.kestra.plugin.core.execution.Exit
state: WARNING
- id: patch
type: io.kestra.plugin.scripts.shell.Commands
description: |
Copacetic patches the digest Trivy scanned, so a tag moved while the approval waited changes nothing. It reads the Trivy report, has BuildKit upgrade only the listed packages on top of the original layers, and pushes the result to the staging repository. It fails if a package cannot be brought to its fixed version.
The OpenVEX document lists every vulnerability the patch addresses. Copacetic logs to stderr, which Kestra records as errors, so `2>&1` keeps a successful patch at INFO.
containerImage: "{{ vars.copa_image }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: host
inputFiles:
report.json: "{{ outputs.scan_source.outputFiles['report.json'] }}"
outputFiles:
- vex.json
commands:
- >-
copa patch --image {{ vars.registry }}/{{ inputs.repository }}@{{
outputs.before.values.digest }} --report report.json --tag {{
outputs.target.values.staging }} --addr {{ vars.buildkit_addr }} --push
--timeout 20m --progress plain --format openvex --output vex.json 2>&1
- id: scan_patched
type: io.kestra.plugin.scripts.shell.Commands
containerImage: "{{ vars.trivy_image }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: host
outputFiles:
- report.json
commands:
- >-
trivy image --server {{ vars.trivy_server }} --image-src remote
--pkg-types os --ignore-unfixed --disable-telemetry --skip-version-check
--quiet --format json --output report.json {{
outputs.target.values.staging }}
- id: after
type: io.kestra.plugin.core.output.OutputValues
values:
fixable: >-
{{ read(outputs.scan_patched.outputFiles['report.json']) |
jq('[.Results[]? | select(.Class == "os-pkgs") | .Vulnerabilities[]?] |
length') | first }}
digest: >-
{{ read(outputs.scan_patched.outputFiles['report.json']) |
jq('(.Metadata.RepoDigests // [])[0] // "" | split("@") | last') | first
}}
remaining: >-
{{ read(outputs.scan_patched.outputFiles['report.json']) |
jq('[.Results[]? | select(.Class == "os-pkgs") | .Vulnerabilities[]? |
.Severity + " " + .VulnerabilityID + " " + .PkgName + " " +
.InstalledVersion + " -> " + .FixedVersion] | join("\n")') | first }}
- id: verify
type: io.kestra.plugin.core.execution.Assert
description: The staged image must be a new image with fewer fixable
vulnerabilities than the original. Otherwise the flow fails here and
nothing is published.
conditions:
- "{{ outputs.after.values.digest != '' and outputs.after.values.digest !=
outputs.before.values.digest }}"
- "{{ (outputs.after.values.fixable | number) <
(outputs.before.values.fixable | number) }}"
errorMessage: "The patched image {{ outputs.target.values.staging }} still has
{{ outputs.after.values.fixable }} fixable vulnerabilities (the original
had {{ outputs.before.values.fixable }}), so it was not published."
- id: publish
type: io.kestra.plugin.scripts.shell.Commands
description: Copies the verified digest, not the staging tag, so the published
image is byte for byte the one that was scanned. Within one registry only
the manifest is written; the layers are mounted.
containerImage: "{{ vars.regctl_image }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
networkMode: host
env:
REGCTL_HOST: "reg={{ vars.registry }},tls={{ vars.registry_tls }}"
VERIFIED: "{{ vars.registry }}/{{ vars.staging_prefix }}/{{ inputs.repository
}}@{{ outputs.after.values.digest }}"
PUBLISHED: "{{ outputs.target.values.published }}"
EXPECTED_DIGEST: "{{ outputs.after.values.digest }}"
commands:
- regctl --host "$REGCTL_HOST" image copy "$VERIFIED" "$PUBLISHED"
- DIGEST=$(regctl --host "$REGCTL_HOST" image digest "$PUBLISHED")
- test "$DIGEST" = "$EXPECTED_DIGEST" || { echo "$PUBLISHED resolves to
$DIGEST, expected $EXPECTED_DIGEST"; exit 1; }
- echo "::{\"outputs\":{\"digest\":\"$DIGEST\"}}::"
- id: label_patched
type: io.kestra.plugin.core.execution.Labels
labels:
outcome: patched
fixable_after: "{{ outputs.after.values.fixable }}"
- id: log_summary
type: io.kestra.plugin.core.log.Log
message: |
Published {{ outputs.target.values.published }} ({{ outputs.publish.vars.digest }}), approved{{ (outputs.approve.onResume.note ?? '') == '' ? '' : ' with the note "' ~ outputs.approve.onResume.note ~ '"' }}.
Fixable OS package vulnerabilities: {{ outputs.before.values.fixable }} before, {{ outputs.after.values.fixable }} after.
{% if (outputs.after.values.fixable | number) == 0 %}None remain.{% else %}Still open:
{{ outputs.after.values.remaining }}{% endif %}
errors:
- id: label_failed
type: io.kestra.plugin.core.execution.Labels
labels:
outcome: failed
- id: log_failure
type: io.kestra.plugin.core.log.Log
level: ERROR
message: "Patching {{ inputs.repository }}:{{ inputs.tag }} failed in {{
(errorLogs() | last).taskId ?? 'an unknown task' }}: {{ (errorLogs() |
last).message ?? 'see the execution logs' }}. {{
outputs.target.values.published ?? 'The patched tag' }} was not
published."
outputs:
- id: fixable_before
type: STRING
value: "{{ outputs.before.values.fixable ?? '' }}"
- id: fixable_after
type: STRING
value: "{{ outputs.after.values.fixable ?? '' }}"
- id: patched_image
type: STRING
value: "{{ (outputs.publish.vars.digest ?? '') == '' ? '' :
outputs.target.values.published ~ '@' ~ outputs.publish.vars.digest }}"
triggers:
- id: on_push
type: io.kestra.plugin.core.trigger.Webhook
description: The registry posts one event per request. Only pushes of a tagged
single-platform manifest start an execution; the flow's own pushes
(staging repository, patched suffix) are ignored, and the registry gets a
204 for them.
key: "{{ secret('REGISTRY_WEBHOOK_KEY') }}"
when: >-
{%- set event = trigger.body.events[0] -%} {{ event.action == 'push' and
(event.target.tag ?? '') != '' and
(['application/vnd.oci.image.manifest.v1+json',
'application/vnd.docker.distribution.manifest.v2+json'] contains
event.target.mediaType) and not (event.target.repository |
startsWith(vars.staging_prefix ~ '/')) and not (event.target.tag |
endsWith(vars.patched_suffix)) }}
inputs:
repository: "{{ trigger.body.events[0].target.repository }}"
tag: "{{ trigger.body.events[0].target.tag }}"
digest: "{{ trigger.body.events[0].target.digest }}"