id: git-crypt-secret-encryption-and-integrity-gate
namespace: company.security
description: |
Clone a Git repository and audit it for git-crypt secret encryption integrity: verify that
files configured for git-crypt in .gitattributes carry the real 10-byte cryptographic magic
header, flag files matching sensitive filename patterns that were never configured for
encryption, and detect headers truncated below the minimum valid encrypted size.
inputs:
- id: git_repository_url
type: STRING
displayName: Git Repository URL
description: Git repository HTTPS or SSH clone URL to audit for git-crypt secret
integrity.
defaults: "https://github.com/example-org/infrastructure-gitops.git"
- id: git_branch
type: STRING
displayName: Git Branch
description: Branch or Git ref to clone and audit.
defaults: "main"
- id: secret_patterns
type: STRING
displayName: Sensitive File Patterns
description: Comma-separated glob patterns representing sensitive files that
must be protected by git-crypt.
defaults: "*.env*,*.key,*.pem,*.p12,*secret*,*credential*,*vault*.yml"
- id: slack_channel
type: STRING
displayName: Slack Notification Channel
description: Slack notification channel for security alerts.
defaults: "#security-alerts"
triggers:
- id: pull_request_webhook
type: io.kestra.plugin.core.trigger.Webhook
key: "{{ secret('WEBHOOK_SECRET_KEY') }}"
disabled: true
- id: weekly_security_schedule
type: io.kestra.plugin.core.trigger.Schedule
cron: "0 8 * * 1"
disabled: true
tasks:
- id: audit_workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
tasks:
- id: clone_repository
type: io.kestra.plugin.git.Clone
url: "{{ inputs.git_repository_url }}"
branch: "{{ inputs.git_branch }}"
username: "kestra-audit-bot"
password: "{{ secret('GIT_DEPLOY_TOKEN') }}"
- id: audit_git_crypt_integrity
type: io.kestra.plugin.scripts.shell.Commands
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: alpine:3.20
commands:
- apk add --no-cache git git-crypt python3 bash
- |
cat << 'EOF' > /tmp/audit_git_crypt.py
import os
import sys
import json
import fnmatch
# 10 bytes magic header sequence, followed by a 12-byte nonce (git-crypt uses AES-CTR,
# no separate auth tag): a real encrypted file is never smaller than 22 bytes.
MAGIC_HEADER = bytes([0, 71, 73, 84, 67, 82, 89, 80, 84, 0])
MAGIC_LEN = len(MAGIC_HEADER)
NONCE_LEN = 12
MIN_VALID_ENCRYPTED_SIZE = MAGIC_LEN + NONCE_LEN
repo_root = os.getcwd()
gitattributes_path = os.path.join(repo_root, ".gitattributes")
git_crypt_patterns = []
if os.path.exists(gitattributes_path):
with open(gitattributes_path, "r", encoding="utf-8", errors="ignore") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
parts = line.split()
if len(parts) >= 2:
attrs = parts[1:]
if any("git-crypt" in attr for attr in attrs):
pattern = parts[0]
git_crypt_patterns.append(pattern)
input_patterns_raw = """{{ inputs.secret_patterns }}"""
sensitive_patterns = [p.strip() for p in input_patterns_raw.split(",") if p.strip()]
tracked_files = []
try:
import subprocess
res = subprocess.run(["git", "ls-files"], capture_output=True, text=True, check=True)
tracked_files = [line.strip() for line in res.stdout.splitlines() if line.strip()]
except Exception as e:
for root, dirs, files in os.walk(repo_root):
if ".git" in dirs:
dirs.remove(".git")
for file in files:
rel = os.path.relpath(os.path.join(root, file), repo_root)
tracked_files.append(rel)
scanned_files_count = len(tracked_files)
encrypted_files = []
plaintext_leaks = []
untracked_sensitive_files = []
corrupted_headers = []
for rel_path in tracked_files:
full_path = os.path.join(repo_root, rel_path)
if not os.path.isfile(full_path):
continue
basename = os.path.basename(rel_path)
is_configured_git_crypt = any(
fnmatch.fnmatch(rel_path, pat) or fnmatch.fnmatch(basename, pat)
for pat in git_crypt_patterns
)
is_sensitive_candidate = any(
fnmatch.fnmatch(rel_path, pat) or fnmatch.fnmatch(basename, pat)
for pat in sensitive_patterns
)
if is_configured_git_crypt or is_sensitive_candidate:
try:
with open(full_path, "rb") as bf:
header = bf.read(MAGIC_LEN)
file_size = os.fstat(bf.fileno()).st_size
except Exception as err:
header = b""
file_size = 0
if header == MAGIC_HEADER and file_size < MIN_VALID_ENCRYPTED_SIZE:
corrupted_headers.append(rel_path)
elif header == MAGIC_HEADER:
encrypted_files.append(rel_path)
elif is_configured_git_crypt:
plaintext_leaks.append(rel_path)
elif is_sensitive_candidate:
untracked_sensitive_files.append(rel_path)
has_git_crypt_dir = os.path.isdir(os.path.join(repo_root, ".git-crypt"))
if plaintext_leaks:
audit_status = "CRITICAL_PLAINTEXT_SECRET_LEAK"
elif corrupted_headers:
audit_status = "CORRUPTED_CRYPT_HEADER"
elif untracked_sensitive_files:
audit_status = "UNTRACKED_SECRET_PATTERN_EXPOSED"
else:
audit_status = "ALL_SECRETS_ENCRYPTED_CLEAN"
summary = {
"audit_status": audit_status,
"has_git_crypt_configured": len(git_crypt_patterns) > 0 or has_git_crypt_dir,
"scanned_files_count": scanned_files_count,
"encrypted_files_count": len(encrypted_files),
"plaintext_leaks_count": len(plaintext_leaks),
"untracked_sensitive_count": len(untracked_sensitive_files),
"corrupted_headers_count": len(corrupted_headers),
"plaintext_leaked_files": plaintext_leaks,
"untracked_sensitive_files": untracked_sensitive_files,
"corrupted_header_files": corrupted_headers
}
print('::{"outputs": ' + json.dumps(summary) + '}::')
EOF
python3 /tmp/audit_git_crypt.py
- id: evaluate_security_findings
type: io.kestra.plugin.core.flow.Switch
value: "{{ outputs.audit_git_crypt_integrity.vars.audit_status }}"
cases:
CRITICAL_PLAINTEXT_SECRET_LEAK:
- id: alert_plaintext_secret_leak
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": "{{ inputs.slack_channel }}",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "CRITICAL: Plaintext Secret Leak Detected in Git Repository"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Repository:* {{ inputs.git_repository_url }}\n*Branch:* {{ inputs.git_branch }}\n*Audit Status:* `CRITICAL_PLAINTEXT_SECRET_LEAK`\n*Files Configured in .gitattributes but Unencrypted:*\n```{{ outputs.audit_git_crypt_integrity.vars.plaintext_leaked_files }}```"
}
}
]
}
- id: block_pull_request_leak
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Git-crypt audit failed: {{
outputs.audit_git_crypt_integrity.vars.plaintext_leaks_count }}
files configured for git-crypt are committed without cryptographic
encryption headers."
UNTRACKED_SECRET_PATTERN_EXPOSED:
- id: alert_untracked_sensitive_file
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": "{{ inputs.slack_channel }}",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "WARNING: Sensitive Files Committed Without Git-crypt Protection"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Repository:* {{ inputs.git_repository_url }}\n*Branch:* {{ inputs.git_branch }}\n*Audit Status:* `UNTRACKED_SECRET_PATTERN_EXPOSED`\n*Unprotected Sensitive Files Detected:*\n```{{ outputs.audit_git_crypt_integrity.vars.untracked_sensitive_files }}```\n*Remediation:* Add rules to `.gitattributes` (e.g., `path/to/secret filter=git-crypt diff=git-crypt`) and re-encrypt before merging."
}
}
]
}
- id: block_unprotected_secret_merge
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Git-crypt audit failed: {{
outputs.audit_git_crypt_integrity.vars.untracked_sensitive_count }}
sensitive files match credential patterns but are missing git-crypt
encryption in .gitattributes."
CORRUPTED_CRYPT_HEADER:
- id: alert_corrupted_header
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": "{{ inputs.slack_channel }}",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "ALERT: Corrupted Git-crypt Header Detected"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Repository:* {{ inputs.git_repository_url }}\n*Branch:* {{ inputs.git_branch }}\n*Audit Status:* `CORRUPTED_CRYPT_HEADER`\nFiles below have the git-crypt magic header but are smaller than the minimum valid encrypted size (header + nonce), indicating truncation or a corrupted merge:\n```{{ outputs.audit_git_crypt_integrity.vars.corrupted_header_files }}```"
}
}
]
}
- id: fail_on_corrupted_header
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Git-crypt audit failed: Corrupted cryptographic headers detected."
defaults:
- id: log_clean_audit
type: io.kestra.plugin.core.log.Log
message: "Git-crypt security audit passed successfully. Scanned {{
outputs.audit_git_crypt_integrity.vars.scanned_files_count }} files:
{{ outputs.audit_git_crypt_integrity.vars.encrypted_files_count }}
encrypted secret files verified with valid cryptographic magic
headers."