Enterprise Secret Scanning and Rotation: Architecture, Implementation, and Hardening
Prerequisites
- Familiaritate cu Git și CI/CD
- Acces la un secret manager sau la un provider cloud
Steps
Secret scanning and rotation help enterprises detect exposed credentials early and revoke them before attackers can use them. This guide shows how to deploy scanning, automate rotation, and harden the full secret lifecycle across code, CI/CD, and cloud environments.
Overview
Secret scanning and rotation is the practice of detecting credentials, API keys, tokens, certificates, and other sensitive values in source code, logs, artifacts, and repositories, then automatically or manually replacing them with new values before they can be abused. Enterprises use it to reduce credential theft risk, limit blast radius, and meet governance requirements for secrets lifecycle management.
Architecture
A typical enterprise design combines repository scanning, CI/CD scanning, cloud inventory checks, and a rotation workflow integrated with the identity provider or secret manager.
View as text
[Developer Workstation] ---- 1. commit ----> [Git Repo] [Git Repo] ---- 2. scan ----> [Secret Scanner] [Secret Scanner] ---- 3. alert ----> [SIEM/SOAR] [Secret Scanner] ---- 4. create ticket ----> [ITSM] [SOAR] ---- 5. rotate ----> [Secret Manager] [Secret Manager] ---- 6. update ----> [App / CI] [App / CI] ---- 7. fetch ----> [Secret Manager]
Core components:
- Scanner: GitHub Advanced Security, GitLab Secret Detection, TruffleHog, or Gitleaks.
- Secret store: HashiCorp Vault, AWS Secrets Manager, Azure Key Vault, or Google Secret Manager.
- Rotation automation: CI jobs, Lambda/Functions, or SOAR playbooks.
- Detection pipeline: SIEM integration for alerting and audit.
Implementation Guide
- Install Gitleaks in CI and on developer endpoints.
- Add a baseline scan to detect historical secrets.
- Enforce push protection on repositories.
- Route findings to ticketing and SIEM.
- Automate rotation for high-risk secret classes first.
Example Gitleaks config:
# .gitleaks.toml
[extend]
useDefault = true
[[rules]]
id = "aws-access-key"
description = "AWS Access Key"
regex = '''AKIA[0-9A-Z]{16}'''
keywords = ["AKIA"]
Run scans:
gitleaks detect --source . --config .gitleaks.toml --report-format sarif --report-path gitleaks.sarif
gitleaks protect --staged --config .gitleaks.toml
Code Examples
# Baseline scan for a monorepo
gitleaks detect --source /srv/repo --no-banner --redact --report-path /tmp/gitleaks.json
# GitHub Actions secret scan
name: secret-scan
on: [push, pull_request]
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: gitleaks/gitleaks-action@v2
with:
args: detect --source . --redact --report-format sarif --report-path results.sarif
# Rotate an AWS access key after alerting
import boto3
iam = boto3.client("iam")
keys = iam.list_access_keys(UserName="svc-app")["AccessKeyMetadata"]
old_key = keys[0]["AccessKeyId"]
iam.update_access_key(UserName="svc-app", AccessKeyId=old_key, Status="Inactive")
Security Hardening
- Encrypt scan artifacts and reports at rest with KMS-managed keys.
- Restrict scanner access to read-only repository permissions.
- Store rotation credentials in a dedicated secret manager, not in CI variables.
- Require MFA and just-in-time access for manual rotations.
- Redact secret values in logs and SARIF output.
Comparison
| Product | Pricing | Deployment | Scalability | Security |
|---|---|---|---|---|
| GitHub Advanced Security | Included with enterprise plans | SaaS, repo-native | High across GitHub orgs | Strong push protection and auditability |
| GitLab Secret Detection | Included in higher tiers | SaaS or self-managed | High across GitLab groups | Good CI integration and policy controls |
| Gitleaks | Open source | Self-managed, CI, endpoint | High with your own automation | Strong detection, rotation requires external workflow |
Troubleshooting
-
False positives from test fixtures
WARN rule=aws-access-key file=tests/data/sample.txt line=12 match=AKIA... reason=high entropyFix: add a scoped allowlist for fixture paths and keep it under code review.
-
Rotation fails due to permissions
AccessDenied: User: arn:aws:iam::123456789012:role/rotation-lambda is not authorized to perform: iam:UpdateAccessKeyFix: grant least-privilege IAM permissions only for the target secret class.
-
Scanner misses secrets in large binaries
ERROR scan aborted: file too large: dist/app.bundle.js (exceeds max target size)Fix: exclude build artifacts and scan source plus dependency manifests instead.
Best Practices
- Do scan at commit time, PR time, and nightly on main branches.
- Do rotate immediately for exposed production credentials.
- Do pair detection with ownership metadata so alerts reach the right team.
- Don't rely on regex-only detection for all secrets; combine entropy and provider-specific patterns.
- Don't hardcode secrets in Terraform, Helm values, or container images.
- Don't keep old credentials active after rotation; verify revocation and app reload.
Have a project in mind?
Get an instant AI price estimate for it, or talk directly to our team.
One email a month on what we learn building with AI