id: gcp-iam-service-account-key-age-audit-sentinel
namespace: company.cloud
inputs:
- id: project_id
type: STRING
description: Google Cloud Project ID to audit.
- id: max_key_age_days
type: INT
defaults: 90
description: Maximum acceptable service account key age in days before alerting
(CIS benchmark 1.4).
tasks:
- id: audit_service_account_keys
type: io.kestra.plugin.gcp.cli.GCloudCLI
projectId: "{{ inputs.project_id }}"
serviceAccount: "{{ secret('GCP_SERVICE_ACCOUNT') }}"
commands:
- |
# List all service accounts in the project
gcloud iam service-accounts list --format=json > /tmp/sa_list.json
cat << 'EOF' > /tmp/audit_keys.py
import json, subprocess, datetime, sys
with open('/tmp/sa_list.json') as f:
sas = json.load(f)
now = datetime.datetime.now(datetime.timezone.utc)
max_days = int(sys.argv[1])
stale_keys = 0
for sa in sas:
email = sa.get('email')
res = subprocess.run(['gcloud', 'iam', 'service-accounts', 'keys', 'list', f'--iam-account={email}', '--managed-by=user', '--format=json'], capture_output=True, text=True)
if res.returncode == 0 and res.stdout.strip():
try:
keys = json.loads(res.stdout)
for k in keys:
valid_after = k.get('validAfterTime')
if valid_after:
dt = datetime.datetime.fromisoformat(valid_after.replace('Z', '+00:00'))
age = (now - dt).days
if age >= max_days:
stale_keys += 1
except:
pass
print(stale_keys)
EOF
STALE_COUNT=$(python3 /tmp/audit_keys.py "{{ inputs.max_key_age_days }}" 2>/dev/null || echo 0)
echo "::set-output name=stale_count::$STALE_COUNT::"
- id: evaluate_key_rotation
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.audit_service_account_keys.vars.stale_count | int > 0 }}"
then:
- id: notify_security_team
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{
"channel": "#security-alerts",
"attachments": [
{
"color": "warning",
"title": "⚠️ Google Cloud IAM Service Account Stale Keys Alert",
"text": "Project `{{ inputs.project_id }}` has *{{ outputs.audit_service_account_keys.vars.stale_count }}* user-managed service account keys exceeding {{ inputs.max_key_age_days }} days without rotation! Impact: Violates CIS GCP Benchmark 1.4 and SOC2 standards, increasing risk of credential exposure from hardcoded or leaked JSON key files.",
"footer": "Kestra Security Sentinel • Google Cloud IAM"
}
]
}
errors:
- id: alert_pipeline_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{
"channel": "#security-alerts",
"attachments": [
{
"color": "danger",
"title": "⚠️ Google Cloud IAM Service Account Key Age and Rotation Sentinel Pipeline Failure",
"text": "Failed to audit GCP IAM service account keys. Check project permissions.",
"footer": "Kestra Workflow Failure Sentinel"
}
]
}
triggers:
- id: periodic_schedule
type: io.kestra.plugin.core.trigger.Schedule
cron: "*/15 * * * *"
disabled: true
inputs:
project_id: replace-with-your-project-id