id: rabbitmq-missing-queue-binding-restore-guard
namespace: company.team
description: Inspect a missing RabbitMQ queue binding without touching queue
data and restore one direct binding only after approval and fresh topology
checks.
concurrency:
limit: 1
behavior: QUEUE
inputs:
- id: rabbitmq_host
type: STRING
defaults: "{{ secret('RABBITMQ_HOST') }}"
description: RabbitMQ broker hostname reachable by the Kestra worker.
- id: rabbitmq_port
type: STRING
defaults: "{{ secret('RABBITMQ_PORT') }}"
- id: rabbitmq_management_port
type: STRING
defaults: "{{ secret('RABBITMQ_MANAGEMENT_PORT') }}"
description: RabbitMQ management API port, normally 15672.
- id: rabbitmq_username
type: STRING
defaults: "{{ secret('RABBITMQ_USERNAME') }}"
- id: rabbitmq_password
type: SECRET
defaults: "{{ secret('RABBITMQ_PASSWORD') }}"
- id: rabbitmq_vhost
type: STRING
defaults: "{{ secret('RABBITMQ_VHOST') }}"
- id: exchange
type: STRING
defaults: orders.events
description: Existing direct exchange to inspect and, when approved, declare
idempotently.
- id: queue
type: STRING
defaults: orders.consumer
description: Existing durable non-exclusive queue whose binding must be present.
- id: routing_key
type: STRING
defaults: orders.created
description: One routing key whose empty-arguments direct binding must be present.
- id: auto_remediate
type: BOOL
defaults: false
description: Allow the approved run to declare the exchange and queue and
restore the binding.
- id: dry_run
type: BOOL
defaults: true
description: Keep the run read-only. The read-only and dry-run paths make no
publish, consume or topology write.
- id: slack_webhook_url
type: STRING
defaults: "{{ secret('SLACK_WEBHOOK_URL') }}"
description: Slack incoming webhook for findings and failed executions.
triggers:
- id: schedule
type: io.kestra.plugin.core.trigger.Schedule
cron: "*/15 * * * *"
disabled: true
inputs:
auto_remediate: false
dry_run: true
tasks:
- id: validate_identifiers
type: io.kestra.plugin.core.execution.Fail
description: Reject control characters before any broker access because
management API names and native AMQP names must identify the same
resource.
condition: >-
{{ {'vhost': inputs.rabbitmq_vhost, 'exchange': inputs.exchange, 'queue':
inputs.queue, 'key': inputs.routing_key} | toJson | jq('[.vhost,
.exchange, .queue, .key] | any(.[]; explode | any(.[]; . < 32 or . ==
127))') | first }}
errorMessage: "RabbitMQ identifiers must not contain ASCII control characters.
No broker read or topology write was attempted."
- id: list_initial_bindings
type: io.kestra.plugin.core.http.Request
description: The AMQP plugin has no binding-list task, so this plain management
API GET is the smallest read that inspects topology without consuming
queue data.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/bindings/{{ inputs.rabbitmq_vhost | urlencode | replace({'+':
'%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: initial_read_guard
type: io.kestra.plugin.core.flow.If
description: Fail closed when the management API cannot return the binding list.
condition: "{{ outputs.list_initial_bindings.code != 200 }}"
then:
- id: fail_initial_read
type: io.kestra.plugin.core.execution.Fail
errorMessage: "RabbitMQ binding inventory failed with HTTP {{
outputs.list_initial_bindings.code }}. No topology change was
attempted."
else:
- id: initial_binding_present
type: io.kestra.plugin.core.flow.If
description: Compare decoded management API fields exactly; a substring or
lookalike never counts as the target binding.
condition: >-
{{ {'bindings': fromJson(outputs.list_initial_bindings.body), 'want':
{'source': inputs.exchange, 'destination': inputs.queue,
'routing_key': inputs.routing_key}} | toJson | jq('.bindings as $b |
.want as $w | (any($b[]; .source == $w.source and .destination ==
$w.destination and .destination_type == "queue" and .routing_key ==
$w.routing_key and (.arguments // {}) == {})) and (all($b[]; .source
!= $w.source or (.destination_type == "queue" and (.destination !=
$w.destination or (.routing_key == $w.routing_key and (.arguments //
{}) == {})))))') | first }}
then:
- id: log_healthy
type: io.kestra.plugin.core.log.Log
message: "RabbitMQ binding inventory found the exact queue binding {{
inputs.exchange }} -> {{ inputs.queue }} with routing key {{
inputs.routing_key }}. No message was published or consumed."
else:
- id: alert_missing_binding
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Report the missing topology object through the native Slack task.
url: "{{ inputs.slack_webhook_url }}"
payload: "{{ {'text': 'RabbitMQ exact binding absent or routing conflict:
vhost=' ~ inputs.rabbitmq_vhost ~ ', exchange=' ~ inputs.exchange
~ ', queue=' ~ inputs.queue ~ ', routing_key=' ~
inputs.routing_key ~ ', execution=' ~ execution.id ~ ',
auto_remediate=' ~ inputs.auto_remediate ~ ', dry_run=' ~
inputs.dry_run ~ '. No queue data was consumed.'} | toJson }}"
- id: remediation_gate
type: io.kestra.plugin.core.flow.If
description: Enter the repair path only when both safety inputs request it.
condition: "{{ inputs.auto_remediate and not inputs.dry_run }}"
then:
- id: approval
type: io.kestra.plugin.core.flow.Pause
pauseDuration: PT30M
behavior: FAIL
description: "Approve only vhost={{ inputs.rabbitmq_vhost }}, exchange={{
inputs.exchange }}, queue={{ inputs.queue }}, routing_key={{
inputs.routing_key }}, exchange_type=DIRECT, exchange
durable=true auto_delete=false, queue durable=true
exclusive=false auto_delete=false, binding arguments={}."
onResume:
- id: approved
type: BOOL
defaults: false
description: Set true only after checking exchange and queue ownership.
- id: approved_path
type: io.kestra.plugin.core.flow.If
description: Continue only after an explicit approval value of true.
condition: "{{ outputs.approval.onResume.approved ?? false }}"
then:
- id: list_after_pause
type: io.kestra.plugin.core.http.Request
description: Re-read the exact binding after approval so an out-of-band repair
cancels this write plan.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/bindings/{{ inputs.rabbitmq_vhost | urlencode |
replace({'+': '%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: after_pause_read_guard
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.list_after_pause.code != 200 }}"
then:
- id: fail_after_pause_read
type: io.kestra.plugin.core.execution.Fail
errorMessage: "RabbitMQ binding recheck failed with HTTP {{
outputs.list_after_pause.code }}. No topology write
was attempted."
else:
- id: recovered_during_pause
type: io.kestra.plugin.core.flow.If
condition: >-
{{ {'bindings':
fromJson(outputs.list_after_pause.body), 'want':
{'source': inputs.exchange, 'destination':
inputs.queue, 'routing_key': inputs.routing_key}} |
toJson | jq('.bindings as $b | .want as $w |
(any($b[]; .source == $w.source and .destination ==
$w.destination and .destination_type == "queue" and
.routing_key == $w.routing_key and (.arguments // {})
== {})) and (all($b[]; .source != $w.source or
(.destination_type == "queue" and (.destination !=
$w.destination or (.routing_key == $w.routing_key and
(.arguments // {}) == {})))))') | first }}
then:
- id: log_recovered_during_pause
type: io.kestra.plugin.core.log.Log
message: "The exact binding appeared while approval was pending; no topology
write was sent."
else:
- id: read_exchange_before_declare
type: io.kestra.plugin.core.http.Request
description: Read the exchange declaration arguments immediately before the
exchange write; the management API has no native
AMQP equivalent.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/exchanges/{{ inputs.rabbitmq_vhost |
urlencode | replace({'+': '%20'}) }}/{{
inputs.exchange | urlencode | replace({'+':
'%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: read_binding_before_declare
type: io.kestra.plugin.core.http.Request
description: Re-read the binding immediately before the exchange declaration to
keep the target set narrow.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/bindings/{{ inputs.rabbitmq_vhost |
urlencode | replace({'+': '%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: exchange_eligibility
type: io.kestra.plugin.core.flow.If
description: Allow only a missing exchange or one whose exact direct declaration
arguments match the requested durable exchange.
condition: >-
{{ {'binding_code':
outputs.read_binding_before_declare.code,
'bindings':
fromJson(outputs.read_binding_before_declare.body),
'want': {'source': inputs.exchange, 'destination':
inputs.queue, 'routing_key': inputs.routing_key},
'exchange_code':
outputs.read_exchange_before_declare.code,
'exchange_body':
outputs.read_exchange_before_declare.body} |
toJson | jq('. as $x | ($x.bindings as $b |
$x.want as $w | ($x.binding_code == 200 and
all($b[]; .source != $w.source or
(.destination_type == "queue" and (.destination !=
$w.destination or (.routing_key == $w.routing_key
and (.arguments // {}) == {})))) and (if
$x.exchange_code == 404 then true elif
$x.exchange_code == 200 then (($x.exchange_body |
fromjson) as $e | $e.type == "direct" and
$e.durable == true and $e.auto_delete == false and
($e.arguments // {}) == {}) else false end)))') |
first }}
then:
- id: declare_exchange
type: io.kestra.plugin.amqp.DeclareExchange
description: Idempotently declare the exact durable direct exchange after the
fresh eligibility reads.
host: "{{ inputs.rabbitmq_host }}"
port: "{{ inputs.rabbitmq_port }}"
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
virtualHost: "{{ inputs.rabbitmq_vhost }}"
name: "{{ inputs.exchange }}"
exchangeType: DIRECT
durability: true
autoDelete: false
else:
- id: fail_exchange_eligibility
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Fresh inventory contains a routing conflict or the exchange
declaration is outside the supported durable
direct empty-arguments shape."
- id: read_queue_before_create
type: io.kestra.plugin.core.http.Request
description: Read the queue declaration arguments immediately before the queue
write; the management API has no native AMQP
equivalent.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/queues/{{ inputs.rabbitmq_vhost | urlencode
| replace({'+': '%20'}) }}/{{ inputs.queue |
urlencode | replace({'+': '%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: read_binding_before_create
type: io.kestra.plugin.core.http.Request
description: Re-read the binding immediately before the queue declaration.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/bindings/{{ inputs.rabbitmq_vhost |
urlencode | replace({'+': '%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: queue_eligibility
type: io.kestra.plugin.core.flow.If
description: Allow only a missing queue or one whose exact durable non-exclusive
declaration arguments match the requested queue.
condition: >-
{{ {'binding_code':
outputs.read_binding_before_create.code,
'bindings':
fromJson(outputs.read_binding_before_create.body),
'want': {'source': inputs.exchange, 'destination':
inputs.queue, 'routing_key': inputs.routing_key},
'queue_code':
outputs.read_queue_before_create.code,
'queue_body':
outputs.read_queue_before_create.body} | toJson |
jq('. as $x | ($x.bindings as $b | $x.want as $w |
($x.binding_code == 200 and all($b[]; .source !=
$w.source or (.destination_type == "queue" and
(.destination != $w.destination or (.routing_key
== $w.routing_key and (.arguments // {}) == {}))))
and (if $x.queue_code == 404 then true elif
$x.queue_code == 200 then (($x.queue_body |
fromjson) as $q | $q.durable == true and
$q.exclusive == false and $q.auto_delete == false
and ($q.arguments // {}) == {}) else false
end)))') | first }}
then:
- id: create_queue
type: io.kestra.plugin.amqp.CreateQueue
description: Idempotently declare the exact durable non-exclusive queue after
the fresh eligibility reads.
host: "{{ inputs.rabbitmq_host }}"
port: "{{ inputs.rabbitmq_port }}"
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
virtualHost: "{{ inputs.rabbitmq_vhost }}"
name: "{{ inputs.queue }}"
durability: true
autoDelete: false
exclusive: false
else:
- id: fail_queue_eligibility
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Fresh inventory contains a routing conflict or the queue
declaration is outside the supported durable
non-exclusive empty-arguments shape."
- id: read_exchange_before_bind
type: io.kestra.plugin.core.http.Request
description: Re-read exchange declaration arguments immediately before the
binding write.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/exchanges/{{ inputs.rabbitmq_vhost |
urlencode | replace({'+': '%20'}) }}/{{
inputs.exchange | urlencode | replace({'+':
'%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: read_queue_before_bind
type: io.kestra.plugin.core.http.Request
description: Re-read queue declaration arguments immediately before the binding
write.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/queues/{{ inputs.rabbitmq_vhost | urlencode
| replace({'+': '%20'}) }}/{{ inputs.queue |
urlencode | replace({'+': '%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: read_binding_before_bind
type: io.kestra.plugin.core.http.Request
description: Re-read the binding immediately before QueueBind and refuse a
conflicting non-empty argument set.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/bindings/{{ inputs.rabbitmq_vhost |
urlencode | replace({'+': '%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: binding_eligibility
type: io.kestra.plugin.core.flow.If
description: Bind only when fresh reads show the exact supported declarations
and no same-destination binding conflict. Any
alternate key, wildcard, non-empty arguments or
destination-type lookalike fails closed.
condition: >-
{{ {'binding_code':
outputs.read_binding_before_bind.code, 'bindings':
fromJson(outputs.read_binding_before_bind.body),
'want': {'source': inputs.exchange, 'destination':
inputs.queue, 'routing_key': inputs.routing_key},
'exchange_code':
outputs.read_exchange_before_bind.code,
'exchange_body':
outputs.read_exchange_before_bind.body,
'queue_code': outputs.read_queue_before_bind.code,
'queue_body': outputs.read_queue_before_bind.body}
| toJson | jq('. as $x | ($x.bindings as $b |
$x.want as $w | ($x.binding_code == 200 and
$x.exchange_code == 200 and $x.queue_code == 200
and all($b[]; .source != $w.source or
(.destination_type == "queue" and (.destination !=
$w.destination or (.routing_key == $w.routing_key
and (.arguments // {}) == {})))) and
(($x.exchange_body | fromjson) as $e | $e.type ==
"direct" and $e.durable == true and $e.auto_delete
== false and ($e.arguments // {}) == {}) and
(($x.queue_body | fromjson) as $q | $q.durable ==
true and $q.exclusive == false and $q.auto_delete
== false and ($q.arguments // {}) == {})))') |
first }}
then:
- id: bind_if_missing
type: io.kestra.plugin.core.flow.If
description: Send QueueBind only when the exact binding is absent and the
complete inventory has no same-destination
route; alternate keys, wildcards, non-empty
arguments and destination-type lookalikes are
refused.
condition: >-
{{ {'binding_code':
outputs.read_binding_before_bind.code,
'bindings':
fromJson(outputs.read_binding_before_bind.body),
'want': {'source': inputs.exchange,
'destination': inputs.queue, 'routing_key':
inputs.routing_key}} | toJson | jq('.bindings
as $b | .want as $w | any($b[]; .source ==
$w.source and .destination == $w.destination
and .destination_type == "queue" and
.routing_key == $w.routing_key and (.arguments
// {}) == {})') | first == false }}
then:
- id: restore_binding
type: io.kestra.plugin.amqp.QueueBind
description: Restore only the exact empty-arguments direct binding after the
final eligibility reads.
host: "{{ inputs.rabbitmq_host }}"
port: "{{ inputs.rabbitmq_port }}"
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
virtualHost: "{{ inputs.rabbitmq_vhost }}"
exchange: "{{ inputs.exchange }}"
queue: "{{ inputs.queue }}"
routingKey: "{{ inputs.routing_key }}"
else:
- id: log_binding_already_present
type: io.kestra.plugin.core.log.Log
message: "The exact binding appeared before QueueBind; no bind was sent."
else:
- id: fail_binding_eligibility
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Fresh exchange or queue reads no longer match the supported
direct topology. No QueueBind was sent."
- id: list_verification_binding
type: io.kestra.plugin.core.http.Request
description: Verify the exact binding through the management API after all
native topology writes.
uri: "http://{{ inputs.rabbitmq_host }}:{{ inputs.rabbitmq_management_port
}}/api/bindings/{{ inputs.rabbitmq_vhost |
urlencode | replace({'+': '%20'}) }}"
method: GET
headers:
Accept: application/json
options:
allowFailed: true
auth:
type: BASIC
username: "{{ inputs.rabbitmq_username }}"
password: "{{ inputs.rabbitmq_password }}"
- id: fail_if_unverified
type: io.kestra.plugin.core.execution.Fail
condition: >-
{{ {'code':
outputs.list_verification_binding.code,
'bindings':
fromJson(outputs.list_verification_binding.body),
'want': {'source': inputs.exchange, 'destination':
inputs.queue, 'routing_key': inputs.routing_key}}
| toJson | jq('. as $x | $x.bindings as $b |
$x.want as $w | ($x.code == 200 and (any($b[];
.source == $w.source and .destination ==
$w.destination and .destination_type == "queue"
and .routing_key == $w.routing_key and (.arguments
// {}) == {})) and (all($b[]; .source != $w.source
or (.destination_type == "queue" and (.destination
!= $w.destination or (.routing_key ==
$w.routing_key and (.arguments // {}) == {}))))))
| not') | first }}
errorMessage: "Post-write inventory did not contain the exact RabbitMQ binding
without routing conflicts."
- id: log_repaired
type: io.kestra.plugin.core.log.Log
message: "RabbitMQ binding restored and verified from the management API.
Exchange={{ inputs.exchange }}, queue={{
inputs.queue }}, routing key={{ inputs.routing_key
}}. No application message was consumed."
else:
- id: log_declined
type: io.kestra.plugin.core.log.Log
message: "Approval was declined or omitted. RabbitMQ was unchanged after the
missing binding finding."
else:
- id: log_report_only
type: io.kestra.plugin.core.log.Log
message: "Missing binding reported. No topology write was allowed because
auto_remediate={{ inputs.auto_remediate }} and dry_run={{
inputs.dry_run }}."
errors:
- id: alert_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert Slack through the native task when a broker read, write or
verification task fails.
url: "{{ inputs.slack_webhook_url }}"
payload: "{{ {'text': 'RabbitMQ binding guard failed in execution ' ~
execution.id ~ ' for exchange=' ~ inputs.exchange ~ ', queue=' ~
inputs.queue ~ ', routing_key=' ~ inputs.routing_key ~ '. Inspect the
broker before retrying.'} | toJson }}"