id: kafka-schema-registry-compatibility-mode-drift-guard
namespace: company.team
description: |
Audit every Confluent Schema Registry subject backing a live Kafka topic against a governed
compatibility level and a governed subject mode, then pin the drifted ones back behind an approval
gate and re-read both settings to prove it landed. A subject left at `NONE` accepts a breaking
schema in silence, so the guard that stops a producer from poisoning its consumers is simply off.
A subject left in `READONLY` or `IMPORT` after a restore refuses every write with a 422 that reads
like a client bug. The registry answers both requests normally either way, so nothing reports it.
Compatibility and mode are checked independently, because they drift independently. A subject with
no override inherits the global setting, which is reported as inherited and judged by the effective
value.
inputs:
- id: schema_registry_url
type: STRING
displayName: Schema Registry URL
description: Base URL of the Confluent Schema Registry, with no trailing slash.
defaults: http://localhost:8081
- id: bootstrap_servers
type: STRING
displayName: Kafka bootstrap servers
description: Comma separated broker list, used to read the live topic set that
scopes the audit.
defaults: localhost:9092
- id: baseline_compatibility
type: SELECT
displayName: Governed compatibility level
description: The level every audited subject must resolve to.
values:
- BACKWARD
- BACKWARD_TRANSITIVE
- FORWARD
- FORWARD_TRANSITIVE
- FULL
- FULL_TRANSITIVE
- NONE
defaults: BACKWARD
- id: baseline_mode
type: SELECT
displayName: Governed subject mode
description: The mode every audited subject must resolve to. READWRITE is the
only mode that accepts producer writes.
values:
- READWRITE
- READONLY
- IMPORT
defaults: READWRITE
- id: inherited_is_compliant
type: BOOL
displayName: Treat an inherited setting as compliant
description: True means a subject with no override is judged by the global
setting. False means every audited subject must carry its own explicit
override.
defaults: true
- id: subjects
type: ARRAY
itemType: STRING
displayName: Subjects to audit
description: Leave empty to audit every registered subject that backs a live
topic under the TopicNameStrategy. Name subjects here to audit those
instead.
defaults: []
- id: exempt_subjects
type: ARRAY
itemType: STRING
displayName: Subjects exempt from the baseline
description: Subjects allowed to sit off baseline, for example one deliberately
held at NONE for a migration. Never read, never changed.
defaults: []
- id: auto_remediate
type: BOOL
displayName: Auto remediate
defaults: false
- id: dry_run
type: BOOL
displayName: Dry run
description: Must be false, together with auto_remediate, for any setting to be written.
defaults: true
variables:
audit_key: "sr_drift_audit_{{ inputs.schema_registry_url | slugify }}"
verify_key: "sr_drift_verify_{{ inputs.schema_registry_url | slugify }}"
tasks:
- id: list_subjects
type: io.kestra.plugin.core.http.Request
description: Read-only. Every subject the registry serves right now.
method: GET
uri: "{{ inputs.schema_registry_url }}/subjects"
- id: list_topics
type: io.kestra.plugin.kafka.TopicList
description: Read-only AdminClient call. The live topic set is what scopes the
audit, so a subject whose topic is gone is never read or written.
properties:
bootstrap.servers: "{{ inputs.bootstrap_servers }}"
- id: read_global_config
type: io.kestra.plugin.core.http.Request
description: Read-only. The global compatibility level, which every subject
without an override inherits.
method: GET
uri: "{{ inputs.schema_registry_url }}/config"
- id: read_global_mode
type: io.kestra.plugin.core.http.Request
description: Read-only. The global mode, which every subject without an override
inherits.
method: GET
uri: "{{ inputs.schema_registry_url }}/mode"
- id: globals
type: io.kestra.plugin.core.output.OutputValues
description: The two inherited values, pulled out once so every subject row is
judged against the same pair.
values:
config_level: "{{ fromJson(outputs.read_global_config.body).compatibilityLevel
?? 'UNKNOWN' }}"
mode: "{{ fromJson(outputs.read_global_mode.body).mode ?? 'UNKNOWN' }}"
- id: scope
type: io.kestra.plugin.core.output.OutputValues
description: Audited subjects are the registered ones that back a live topic (or
the named list) minus the exempt ones. Internal topics are dropped.
values:
subject_list: >-
{{ {'reg': fromJson(outputs.list_subjects.body), 'topics':
outputs.list_topics.topics, 'sel': inputs.subjects, 'ex':
inputs.exempt_subjects} | jq('.reg as $reg | .sel as $sel | .ex as $ex |
([ .topics[] | select(startswith("_") | not) ]) as $pt | (($pt | map(. +
"-value")) + ($pt | map(. + "-key"))) as $exp | (if ($sel | length) > 0
then ($reg - ($reg - $sel)) else ($reg - ($reg - $exp)) end) as $base |
(($base - $ex) | sort)') | first | toJson }}
- id: in_scope
type: io.kestra.plugin.core.flow.If
condition: "{{ fromJson(outputs.scope.values.subject_list) | length > 0 }}"
then:
- id: reset_audit
type: io.kestra.plugin.core.kv.Set
description: The audit accumulator is rebuilt from scratch every run. No ttl, so
the read back inside the run can never hit an expired key.
key: "{{ render(vars.audit_key) }}"
kvType: JSON
value: "{}"
- id: audit
type: io.kestra.plugin.core.flow.Loop
description: Read both settings per subject. One at a time, because each
iteration folds its row into a single accumulator.
concurrencyLimit: 1
values: "{{ outputs.scope.values.subject_list }}"
tasks:
- id: read_config
type: io.kestra.plugin.core.http.Request
description: Read-only. A 404 here is not an error, it means the subject has no
override and inherits the global level.
method: GET
uri: "{{ inputs.schema_registry_url }}/config/{{ item.value }}"
options:
allowFailed: true
- id: read_mode
type: io.kestra.plugin.core.http.Request
description: Read-only. A 404 here means the subject has no mode override and
inherits the global mode.
method: GET
uri: "{{ inputs.schema_registry_url }}/mode/{{ item.value }}"
options:
allowFailed: true
- id: row_merge
type: io.kestra.plugin.core.output.OutputValues
description: Observed facts only. The baseline comparison happens once, in
assess.
values:
merged: >-
{{ {'cur': (kv(render(vars.audit_key), errorOnMissing=false) ??
'{}'), 'subject': item.value, 'ccode': outputs.read_config.code,
'cbody': outputs.read_config.body, 'mcode':
outputs.read_mode.code, 'mbody': outputs.read_mode.body, 'gc':
outputs.globals.values.config_level, 'gm':
outputs.globals.values.mode} | jq('(if ((.ccode == 200 or .ccode
== 404) and (.mcode == 200 or .mcode == 404)) then . else
error("Schema Registry answered an unexpected status for " +
.subject + ": config " + (.ccode | tostring) + ", mode " +
(.mcode | tostring)) end) | (.cur | if type=="string" then (if
.=="" then {} else fromjson end) else . end) as $c | {subject:
.subject, config_override: (.ccode == 200), config_effective:
(if .ccode == 200 then (.cbody | fromjson | .compatibilityLevel)
else .gc end), mode_override: (.mcode == 200), mode_effective:
(if .mcode == 200 then (.mbody | fromjson | .mode) else .gm
end)} as $row | $c + {(.subject): $row}') | first | toJson }}
- id: row_save
type: io.kestra.plugin.core.kv.Set
key: "{{ render(vars.audit_key) }}"
kvType: JSON
value: "{{ outputs.row_merge.values.merged }}"
- id: assess
type: io.kestra.plugin.core.output.OutputValues
description: A subject drifts when its effective value differs from the
baseline. It also drifts when an override is required and it has none.
The two settings are judged separately.
values:
result: >-
{{ {'rows': (kv(render(vars.audit_key), errorOnMissing=false) ??
'{}'), 'bc': inputs.baseline_compatibility, 'bm':
inputs.baseline_mode, 'ok': inputs.inherited_is_compliant} |
jq('(.rows | if type=="string" then (if .=="" then {} else fromjson
end) else . end) as $r | .bc as $bc | .bm as $bm | .ok as $ok | [
$r[] | {subject: .subject, config_override: .config_override,
config_effective: .config_effective, mode_override: .mode_override,
mode_effective: .mode_effective, fix_config: ((.config_effective !=
$bc) or (($ok | not) and (.config_override | not))), fix_mode:
((.mode_effective != $bm) or (($ok | not) and (.mode_override |
not)))} ] as $all | ([ $all[] | select(.fix_config or .fix_mode) ] |
sort_by(.subject)) as $d | {audited: ($r | length), count: ($d |
length), config_count: ([ $d[] | select(.fix_config) ] | length),
mode_count: ([ $d[] | select(.fix_mode) ] | length), subjects: ($d |
map(.subject)), drift: $d, summary: ([ $d[] | .subject + "
[compatibility " + .config_effective + (if .config_override then "
override" else " inherited" end) + (if .fix_config then " DRIFT"
else " ok" end) + ", mode " + .mode_effective + (if .mode_override
then " override" else " inherited" end) + (if .fix_mode then "
DRIFT" else " ok" end) + "]" ] | join("; "))}') | first | toJson }}
- id: route
type: io.kestra.plugin.core.flow.If
condition: "{{ fromJson(outputs.assess.values.result).count > 0 }}"
then:
- id: log_findings
type: io.kestra.plugin.core.log.Log
message: >-
Governance scan on {{ inputs.schema_registry_url }}: {{
fromJson(outputs.assess.values.result).audited }} subjects
audited, {{ fromJson(outputs.assess.values.result).count }} off
baseline ({{ fromJson(outputs.assess.values.result).config_count
}} on compatibility, {{
fromJson(outputs.assess.values.result).mode_count }} on mode).
Baseline is compatibility {{ inputs.baseline_compatibility }} and
mode {{ inputs.baseline_mode }}. Global settings are compatibility
{{ outputs.globals.values.config_level }} and mode {{
outputs.globals.values.mode }}. Drift: {{
fromJson(outputs.assess.values.result).summary }}.
auto_remediate={{ inputs.auto_remediate }} dry_run={{
inputs.dry_run }}.
- id: alert
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: >-
{{ {'text': 'Schema Registry ' ~ inputs.schema_registry_url ~ ': '
~ (fromJson(outputs.assess.values.result).count) ~ ' of ' ~
(fromJson(outputs.assess.values.result).audited) ~ ' subjects are
off the governed baseline (compatibility ' ~
inputs.baseline_compatibility ~ ', mode ' ~ inputs.baseline_mode ~
'). ' ~ (fromJson(outputs.assess.values.result).summary) ~ '.
auto_remediate=' ~ inputs.auto_remediate ~ ' dry_run=' ~
inputs.dry_run ~ '.'} | toJson }}
- id: remediate_gate
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.auto_remediate and not inputs.dry_run }}"
then:
- id: approve
type: io.kestra.plugin.core.flow.Pause
description: Resume with approved=true to pin the subjects listed by
log_findings back to the baseline. Resume without it or kill
the execution to cancel.
onResume:
- id: approved
type: BOOL
displayName: Approve pinning the drifted subjects back to the baseline
required: false
- id: do_fix
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.approve.onResume.approved ?? false }}"
then:
- id: remediate_pin
type: io.kestra.plugin.core.flow.Loop
description: Remediation. Write back only the setting that drifted, one subject
at a time so the registry never takes concurrent config
writes.
concurrencyLimit: 1
values: "{{ fromJson(outputs.assess.values.result).drift | toJson }}"
tasks:
- id: pin_config_gate
type: io.kestra.plugin.core.flow.If
condition: "{{ fromJson(item.value).fix_config }}"
then:
- id: put_config
type: io.kestra.plugin.core.http.Request
description: Set the subject level compatibility. allowFailed lets a rejected
write fall through to the re-read, which is the
authority.
method: PUT
uri: "{{ inputs.schema_registry_url }}/config/{{ fromJson(item.value).subject
}}"
contentType: application/vnd.schemaregistry.v1+json
body: "{{ {'compatibility': inputs.baseline_compatibility} | toJson }}"
options:
allowFailed: true
- id: pin_mode_gate
type: io.kestra.plugin.core.flow.If
condition: "{{ fromJson(item.value).fix_mode }}"
then:
- id: put_mode
type: io.kestra.plugin.core.http.Request
description: Set the subject level mode. allowFailed lets a rejected write fall
through to the re-read.
method: PUT
uri: "{{ inputs.schema_registry_url }}/mode/{{ fromJson(item.value).subject }}"
contentType: application/vnd.schemaregistry.v1+json
body: "{{ {'mode': inputs.baseline_mode} | toJson }}"
options:
allowFailed: true
- id: reset_verify
type: io.kestra.plugin.core.kv.Set
key: "{{ render(vars.verify_key) }}"
kvType: JSON
value: "{}"
- id: verify
type: io.kestra.plugin.core.flow.Loop
description: Read both settings again, straight from the registry, for every
subject that was targeted.
concurrencyLimit: 1
values: "{{ fromJson(outputs.assess.values.result).subjects | toJson }}"
tasks:
- id: recheck_config
type: io.kestra.plugin.core.http.Request
method: GET
uri: "{{ inputs.schema_registry_url }}/config/{{ item.value }}"
options:
allowFailed: true
- id: recheck_mode
type: io.kestra.plugin.core.http.Request
method: GET
uri: "{{ inputs.schema_registry_url }}/mode/{{ item.value }}"
options:
allowFailed: true
- id: recheck_merge
type: io.kestra.plugin.core.output.OutputValues
values:
merged: >-
{{ {'cur': (kv(render(vars.verify_key),
errorOnMissing=false) ?? '{}'), 'subject':
item.value, 'ccode': outputs.recheck_config.code,
'cbody': outputs.recheck_config.body, 'mcode':
outputs.recheck_mode.code, 'mbody':
outputs.recheck_mode.body, 'gc':
outputs.globals.values.config_level, 'gm':
outputs.globals.values.mode} | jq('(if ((.ccode ==
200 or .ccode == 404) and (.mcode == 200 or .mcode
== 404)) then . else error("Schema Registry answered
an unexpected status for " + .subject + ": config "
+ (.ccode | tostring) + ", mode " + (.mcode |
tostring)) end) | (.cur | if type=="string" then (if
.=="" then {} else fromjson end) else . end) as $c |
{subject: .subject, config_override: (.ccode ==
200), config_effective: (if .ccode == 200 then
(.cbody | fromjson | .compatibilityLevel) else .gc
end), mode_override: (.mcode == 200),
mode_effective: (if .mcode == 200 then (.mbody |
fromjson | .mode) else .gm end)} as $row | $c +
{(.subject): $row}') | first | toJson }}
- id: recheck_save
type: io.kestra.plugin.core.kv.Set
key: "{{ render(vars.verify_key) }}"
kvType: JSON
value: "{{ outputs.recheck_merge.values.merged }}"
- id: verify_assess
type: io.kestra.plugin.core.output.OutputValues
description: The same rule as assess, applied to the post write reading.
values:
result: >-
{{ {'rows': (kv(render(vars.verify_key),
errorOnMissing=false) ?? '{}'), 'bc':
inputs.baseline_compatibility, 'bm':
inputs.baseline_mode, 'ok':
inputs.inherited_is_compliant} | jq('(.rows | if
type=="string" then (if .=="" then {} else fromjson end)
else . end) as $r | .bc as $bc | .bm as $bm | .ok as $ok
| ([ $r[] | select(((.config_effective != $bc) or (($ok
| not) and (.config_override | not))) or
((.mode_effective != $bm) or (($ok | not) and
(.mode_override | not)))) ] | sort_by(.subject)) as $bad
| {checked: ($r | length), bad: ($bad | length),
summary: ([ $bad[] | .subject + " [compatibility " +
.config_effective + ", mode " + .mode_effective + "]" ]
| join("; "))}') | first | toJson }}
- id: verify_result
type: io.kestra.plugin.core.flow.If
condition: "{{ fromJson(outputs.verify_assess.values.result).bad > 0 }}"
then:
- id: pin_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: >-
The write ran, but a re-read still shows {{
fromJson(outputs.verify_assess.values.result).bad }}
of {{
fromJson(outputs.verify_assess.values.result).checked
}} targeted subjects off baseline: {{
fromJson(outputs.verify_assess.values.result).summary
}}. The registry may have rejected the write. A client
may have changed the setting back during the pause.
The baseline mode may need a forced transition.
else:
- id: pin_ok
type: io.kestra.plugin.core.log.Log
message: >-
Pinned {{ fromJson(outputs.assess.values.result).count
}} subjects back to compatibility {{
inputs.baseline_compatibility }} and mode {{
inputs.baseline_mode }} ({{
fromJson(outputs.assess.values.result).subjects |
jq('join(", ")') | first }}). A re-read of all {{
fromJson(outputs.verify_assess.values.result).checked
}} confirms every one is on baseline.
else:
- id: not_approved
type: io.kestra.plugin.core.log.Log
message: "Pause resumed without approval or killed. No compatibility level or
mode was changed."
else:
- id: all_clean
type: io.kestra.plugin.core.log.Log
message: >-
All {{ fromJson(outputs.assess.values.result).audited }} audited
subjects on {{ inputs.schema_registry_url }} resolve to
compatibility {{ inputs.baseline_compatibility }} and mode {{
inputs.baseline_mode }}. The safety net is on everywhere. Nothing
to fix.
else:
- id: nothing_in_scope
type: io.kestra.plugin.core.log.Log
message: >-
No audited subjects on {{ inputs.schema_registry_url }}. Either no
registered subject backs a live topic under the TopicNameStrategy or
every match was exempt. Nothing read, nothing changed.
errors:
- id: alert_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: >-
{{ {'text': 'Kafka schema registry compatibility and mode drift guard
failed on ' ~ inputs.schema_registry_url ~ ' (execution ' ~ execution.id ~
'). A subject may still be off the governed baseline. Check the logs.'} |
toJson }}
triggers:
- id: daily_scan
type: io.kestra.plugin.core.trigger.Schedule
description: Shipped disabled and always in safe mode.
cron: "0 6 * * *"
disabled: true
inputs:
auto_remediate: false
dry_run: true