Pulumi Preview Policy Gate with Human Approval
Policy ratchet around pulumi preview that issues, alerts, and holds for approval before applying violating plans.
Flag allowlisted Agent jobs disabled or last failed in msdb, alert Slack, then re-enable and re-run them behind an approval, then re-read sysjobhistory.
SQL Server Agent jobs are the instance's scheduled maintenance: full backups, log backups, index rebuilds, statistics updates, integrity checks. When one of them stops running the instance stops being protected. Because the job simply does not run there is no error anywhere to see. A job gets disabled during an incident at 2am and nobody re-enables it. A job's last run failed on a full disk and no alert was wired up. Either way the only visible effect is the slow accumulation of whatever the job was preventing: a database with no recent backup, a transaction log that never truncates, fragmentation that never gets rebuilt. The gap is usually found during the next incident, which is the worst time to find it.
This blueprint reads msdb.dbo.sysjobs for the enabled flag and
msdb.dbo.sysjobhistory for the last job outcome and flags the jobs that are
either disabled or whose last run failed. It flags only the jobs you name on an
allowlist, the real scheduled maintenance that must stay live, so a job that was
disabled on purpose and left off the list is never touched. It alerts Slack.
When you allow it and a human approves the run, it re-enables the disabled jobs
with sp_update_job, starts each one now with sp_start_job, polls
sysjobactivity until the runs finish, then reads sysjobhistory again and
fails the run if any targeted job is still disabled or its fresh run failed
again.
This blueprint was created by zkasuran.
managed_jobs allowlist are ever flagged, re-enabled or started. A disabled job that is not on the list is left exactly as it is, because the flow cannot know it was not disabled on purpose.dry_run is true and auto_remediate is false, so a normal run only reads msdb and alerts.auto_remediate is true, dry_run is false and a human resumes the Pause with approved: true. A pause resumed without an answer reads as a refusal.concurrencyLimit: 1, so two sp_start_job calls never overlap.sp_start_job is asynchronous. The flow waits for the started jobs to finish in sysjobactivity before it judges them, so it never reads a stale outcome from before the re-run.audit (io.kestra.plugin.jdbc.sqlserver.Query, read-only) joins sysjobs to the allowlist with STRING_SPLIT, reads the last job level outcome (step_id = 0) from sysjobhistory with an OUTER APPLY and keeps the jobs that are disabled or, with include_failed on, last failed. Each row carries its own sp_update_job and sp_start_job statement, built in T-SQL with the job name escaped.targets (io.kestra.plugin.core.output.OutputValues) collects the per job plan and the escaped name list for the re-reads. summary (io.kestra.plugin.core.log.Log) logs each flagged job with its enabled flag and last outcome.route (io.kestra.plugin.core.flow.If):alert posts the flagged jobs to Slack.remediate_gate runs only when auto_remediate is true and dry_run is false.approval_gate (io.kestra.plugin.core.flow.Pause) holds the run until a human resumes it.remediate (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) runs sp_update_job @enabled = 1 then sp_start_job for each flagged job.settle (io.kestra.plugin.core.flow.Sleep) waits briefly for Agent to pick up the runs, then wait_for_jobs (io.kestra.plugin.core.flow.LoopUntil) polls sysjobactivity until no targeted job is still executing.after_state reads the enabled flag and latest outcome back for every touched job and state_log logs the before and after side by side.reverify returns only the jobs still disabled or not succeeding. verify_result fails the run through io.kestra.plugin.core.execution.Fail if there are any.declined logs the findings untouched when the approval was refused.errors posts a Slack alert when the audit cannot run, a statement is rejected, a job does not finish in time or a targeted job is still disabled or failing.sysjobhistory that fails the run when a job is still disabled or its fresh run failed again, so a job that cannot recover is never reported as fixed.msdb stored procedures and waits on sysjobactivity behind an approval.The hard part is not re-enabling a job, it is knowing which jobs matter, doing it under control and proving it worked. Blanket enabling every disabled job would turn jobs back on that someone disabled for a reason. Running a maintenance job blind can drop a heavy rebuild into a busy afternoon. Trusting that sp_start_job succeeded says nothing, because it only requests the run. Kestra reads the real state from msdb first, acts only on an allowlist, gates the change behind two inputs and a human approval, runs the jobs one at a time, waits for them to finish, then reads sysjobhistory again and fails loudly if a job is still disabled or failing. The Slack alert says what was found and whether anything changed. The run history keeps every before and after.
On Kestra 2.0.5 against SQL Server 2022 (16.0.4295.3) on Linux with MSSQL_AGENT_ENABLED=True, a local HTTP sink standing in for Slack. Four Agent jobs were seeded, each with one target server and one step. w34sqljob_backup_ok is enabled, its step succeeds and it was run once (healthy control, must never be flagged). w34sqljob_index_disabled has a step that waits six seconds then succeeds and was then disabled. w34sqljob_integrity_fail is enabled with a step that always RAISERRORs and was run once so its last outcome is a real failure. w34sqljob_adhoc_disabled is disabled and left off the allowlist (disabled on purpose control). Every number below was read back with sqlcmd against msdb.dbo.sysjobs and sysjobhistory, not from the flow log.
| Run | Inputs | Result |
|---|---|---|
| 1 | allowlist backup_ok,index_disabled,integrity_fail, defaults |
2 flagged: index_disabled enabled=0, integrity_fail last=Failed. backup_ok not flagged. Slack said "No change applied". sqlcmd confirmed index_disabled still disabled |
| 2 | same, auto_remediate: true, dry_run: true |
still 2 flagged, still "No change applied". The dry run blocked the write with auto_remediate on |
| 3 | same, auto_remediate: true, dry_run: false, pause resumed approved: false |
declined logged the findings. sqlcmd confirmed index_disabled still disabled, integrity_fail still last=Failed |
| 4 | allowlist backup_ok,index_disabled, auto_remediate: true, dry_run: false, approved: true |
index_disabled re-enabled and run, the poll waited one iteration for the six second step, fresh outcome Succeeded. reverify returned 0 rows. sqlcmd: index_disabled enabled 0 to 1, last NeverRun to Succeeded |
| 5 | allowlist backup_ok only |
0 flagged, clean branch. A healthy allowlisted job is never flagged |
| 6 | allowlist backup_ok,index_disabled,integrity_fail, auto_remediate: true, dry_run: false, approved: true |
integrity_fail re-run, fresh outcome Failed again. reverify returned it and remediation_failed failed the run. The errors block posted to Slack |
| 7 | allowlist empty | 0 flagged, clean branch said the allowlist is empty |
| 8 | unreachable host | audit failed on the TCP connection and the errors block posted to Slack |
Across every run w34sqljob_adhoc_disabled stayed enabled = 0 and was never read or touched, which proves the allowlist gates the action. w34sqljob_backup_ok stayed enabled = 1 with last Succeeded throughout, which proves the audit does not flag a healthy job.
MSSQL_AGENT_ENABLED=True on the Linux image).msdb.dbo.sysjobs, sysjobhistory and sysjobactivity and that can run sp_update_job and sp_start_job. Membership of SQLAgentOperatorRole in msdb is enough, as is sysadmin.SQLSERVER_PASSWORD: password for the login in user. The password input defaults to this secret.SLACK_WEBHOOK_URL: Slack incoming webhook, used by alert and the errors block.SECRET_.| Input | Default | Purpose |
|---|---|---|
url |
jdbc:sqlserver://localhost:1433 |
SQL Server instance to check |
user |
sa |
SQL Server login |
password |
{{ secret('SQLSERVER_PASSWORD') }} |
Password for the login |
managed_jobs |
empty | Comma separated allowlist of job names that must stay live |
include_failed |
true |
Also flag an allowlisted job whose last run failed |
max_wait |
PT3M |
How long to wait for started jobs to finish |
auto_remediate |
false |
Allow the statements to run |
dry_run |
true |
Must be false for a statement to run |
SQLSERVER_PASSWORD and SLACK_WEBHOOK_URL secrets.url and user, then set managed_jobs to the exact names of the backup, log backup, index and integrity jobs that must stay enabled.auto_remediate: true and dry_run: false and approve the pause to re-enable and run them. Read the re-verify result.daily trigger, which always runs in safe mode.sysjobs.name against a LIKE pattern in the audit, so a new maintenance job is covered the day it is created.sysschedules.enabled through sysjobschedules.sysjobschedules and sysjobhistory, so a job that silently stopped firing is caught too.message from sysjobhistory to Slack so an on-call engineer sees why a job failed without opening the server.max_wait for an instance whose maintenance jobs run for many minutes, so the poll does not give up before a long rebuild finishes.