id: kms-key-rotation-compliance-gate
namespace: company.team
description: |
Read a customer-managed KMS key's real KeyRotationEnabled status, classify it COMPLIANT,
CRITICAL, WARNING or PROBE_FAILED, alert Slack when rotation is disabled, optionally enable
rotation and verify it took, and log a final audit summary either way.
inputs:
- id: key_id
type: STRING
displayName: KMS key ID
description: Customer-managed KMS key ID or ARN to audit. AWS-managed keys
always report automatic rotation on their own schedule and are out of
scope for this gate.
defaults: "<your-kms-key-id>"
- id: aws_region
type: STRING
displayName: AWS region
description: Region passed to io.kestra.plugin.aws.cli.AwsCLI as AWS_DEFAULT_REGION.
defaults: "us-east-1"
- id: violation_severity
type: SELECT
displayName: Violation severity
description: How a disabled-rotation finding is classified. critical pages
immediately, warning logs it as a lower-urgency finding for a key with a
known exception.
values:
- "critical"
- "warning"
defaults: "critical"
- id: auto_remediate
type: BOOL
displayName: Auto remediate
description: When true, together with dry_run false, enables rotation on this
key if it is found disabled, then verifies the change took.
defaults: false
- id: dry_run
type: BOOL
displayName: Dry run
description: Must be false, together with auto_remediate true, for the
remediation to actually run.
defaults: true
- id: use_localstack
type: BOOL
displayName: Use local LocalStack container
description: When true, points the AWS CLI at a local LocalStack container
instead of real AWS. Both GetKeyRotationStatus and EnableKeyRotation are
implemented in LocalStack's community edition.
defaults: true
- id: localstack_endpoint
type: STRING
displayName: LocalStack endpoint
description: Only used when use_localstack is true.
defaults: "http://localhost:4566"
tasks:
- id: route_connection_mode
type: io.kestra.plugin.core.flow.If
description: Picks the AWS CLI endpoint. The LocalStack branch sets
endpointOverride and uses LocalStack's own fixed test credentials, the
live branch omits endpointOverride and uses real AWS credentials.
condition: "{{ inputs.use_localstack }}"
then:
- id: probe_rotation_localstack
type: io.kestra.plugin.aws.cli.AwsCLI
description: "Read-only. Runs kms get-key-rotation-status and captures its own
fields, KeyId and KeyRotationEnabled among them, as named outputs via
the AwsCLI plugin's documented ::{\"outputs\":{}}:: echo convention.
No --query reshaping needed, the command's own JSON is already a flat
object."
accessKeyId: "test"
secretKeyId: "test"
region: "{{ inputs.aws_region }}"
endpointOverride: "{{ inputs.localstack_endpoint }}"
commands:
- |-
aws kms get-key-rotation-status --key-id {{ inputs.key_id }} --output json | tr -d '\n' | xargs -0 -I {} echo '::{"outputs":{}}::'
else:
- id: probe_rotation_live
type: io.kestra.plugin.aws.cli.AwsCLI
description: "Read-only. Same get-key-rotation-status call against real AWS."
accessKeyId: "{{ secret('AWS_ACCESS_KEY_ID') }}"
secretKeyId: "{{ secret('AWS_SECRET_ACCESS_KEY') }}"
region: "{{ inputs.aws_region }}"
commands:
- |-
aws kms get-key-rotation-status --key-id {{ inputs.key_id }} --output json | tr -d '\n' | xargs -0 -I {} echo '::{"outputs":{}}::'
- id: classify_rotation_status
type: io.kestra.plugin.core.debug.Return
description: PROBE_FAILED when neither probe branch returned at all, COMPLIANT
when KeyRotationEnabled is true, otherwise CRITICAL or WARNING per
violation_severity.
format: "{{ (outputs.probe_rotation_localstack is not defined and
outputs.probe_rotation_live is not defined) ? 'PROBE_FAILED' :
(((outputs.probe_rotation_localstack is defined ?
outputs.probe_rotation_localstack.vars.KeyRotationEnabled :
outputs.probe_rotation_live.vars.KeyRotationEnabled) == true) ?
'COMPLIANT' : (inputs.violation_severity == 'critical' ? 'CRITICAL' :
'WARNING')) }}"
- id: route_by_rotation_status
type: io.kestra.plugin.core.flow.Switch
description: Branch A logs a compliant key. Branch B alerts Slack when rotation
is disabled, or the probe itself failed.
value: "{{ outputs.classify_rotation_status.value }}"
cases:
"COMPLIANT":
- id: log_compliant
type: io.kestra.plugin.core.log.Log
message: "KMS key {{ inputs.key_id }} compliant. KeyRotationEnabled=true."
"WARNING":
- id: alert_warning
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{{ {"text": "KMS key " ~ inputs.key_id ~ " WARNING. Automatic rotation is disabled."} | toJson }}
"CRITICAL":
- id: alert_critical
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{{ {"text": "KMS key " ~ inputs.key_id ~ " CRITICAL. Automatic rotation is disabled. Data encrypted under this key has been using the same key material indefinitely."} | toJson }}
"PROBE_FAILED":
- id: alert_probe_failed
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{{ {"text": "KMS key rotation probe for " ~ inputs.key_id ~ " did not return a result. Check aws_region, use_localstack, that the key exists, and AWS credentials."} | toJson }}
- id: remediate_gate
type: io.kestra.plugin.core.flow.If
description: Runs only when the key was not COMPLIANT, auto_remediate is true,
and dry_run is false.
condition: "{{ outputs.classify_rotation_status.value != 'COMPLIANT' and
inputs.auto_remediate and not inputs.dry_run }}"
then:
- id: route_remediation_connection
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.use_localstack }}"
then:
- id: enable_rotation_localstack
type: io.kestra.plugin.aws.cli.AwsCLI
description: Calls kms enable-key-rotation on this one key only, never a bulk
operation.
allowFailure: true
accessKeyId: "test"
secretKeyId: "test"
region: "{{ inputs.aws_region }}"
endpointOverride: "{{ inputs.localstack_endpoint }}"
commands:
- "aws kms enable-key-rotation --key-id {{ inputs.key_id }}"
- id: verify_rotation_localstack
type: io.kestra.plugin.aws.cli.AwsCLI
allowFailure: true
accessKeyId: "test"
secretKeyId: "test"
region: "{{ inputs.aws_region }}"
endpointOverride: "{{ inputs.localstack_endpoint }}"
commands:
- |-
aws kms get-key-rotation-status --key-id {{ inputs.key_id }} --output json | tr -d '\n' | xargs -0 -I {} echo '::{"outputs":{}}::'
else:
- id: enable_rotation_live
type: io.kestra.plugin.aws.cli.AwsCLI
description: Calls kms enable-key-rotation on this one key only, never a bulk
operation.
allowFailure: true
accessKeyId: "{{ secret('AWS_ACCESS_KEY_ID') }}"
secretKeyId: "{{ secret('AWS_SECRET_ACCESS_KEY') }}"
region: "{{ inputs.aws_region }}"
commands:
- "aws kms enable-key-rotation --key-id {{ inputs.key_id }}"
- id: verify_rotation_live
type: io.kestra.plugin.aws.cli.AwsCLI
allowFailure: true
accessKeyId: "{{ secret('AWS_ACCESS_KEY_ID') }}"
secretKeyId: "{{ secret('AWS_SECRET_ACCESS_KEY') }}"
region: "{{ inputs.aws_region }}"
commands:
- |-
aws kms get-key-rotation-status --key-id {{ inputs.key_id }} --output json | tr -d '\n' | xargs -0 -I {} echo '::{"outputs":{}}::'
- id: report_remediation_result
type: io.kestra.plugin.core.flow.If
description: Fails the flow when the verify probe did not return at all or still
reports rotation disabled, so a failed enable is never silently
reported as fixed.
condition: "{{ (outputs.verify_rotation_localstack is not defined and
outputs.verify_rotation_live is not defined) or
((outputs.verify_rotation_localstack is defined ?
outputs.verify_rotation_localstack.vars.KeyRotationEnabled :
outputs.verify_rotation_live.vars.KeyRotationEnabled) != true) }}"
then:
- id: remediation_not_confirmed
type: io.kestra.plugin.core.execution.Fail
errorMessage: "enable-key-rotation ran for key {{ inputs.key_id }}, but rotation
still does not show as enabled."
else:
- id: remediation_confirmed
type: io.kestra.plugin.core.log.Log
message: "Rotation enabled and verified for KMS key {{ inputs.key_id }}."
- id: log_audit_summary
type: io.kestra.plugin.core.log.Log
description: Final audit log confirming the gate ran, regardless of the branch
taken above.
message: "KMS key rotation compliance gate completed. key={{ inputs.key_id }}
status={{ outputs.classify_rotation_status.value }} auto_remediate={{
inputs.auto_remediate }} dry_run={{ inputs.dry_run }}"
errors:
- id: alert_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{{ {"text": "KMS key rotation compliance gate flow failed (execution " ~ execution.id ~ "). Check the execution logs."} | toJson }}
triggers:
- id: daily
type: io.kestra.plugin.core.trigger.Schedule
description: Shipped disabled, and always in safe mode against LocalStack by default.
cron: "0 8 * * *"
disabled: true
inputs:
key_id: "<your-kms-key-id>"
aws_region: "us-east-1"
violation_severity: "critical"
auto_remediate: false
dry_run: true
use_localstack: true
localstack_endpoint: "http://localhost:4566"