Secret Scanning
Secret scanning helps you detect accidentally hardcoded credentials, such as API keys, OAuth tokens, and passwords, before they are leaked or committed to version control.
Overview
Unlike license or vulnerability scans that focus on external dependencies, secret scanning analyzes your own source code and configuration files. Trivy uses an extensive library of rule sets to detect common formats of secrets (e.g., AWS Keys, GitHub Tokens, Google API Keys).
Configuration Example
Customizing Secret Detection
Trivy allows you to customize secret detection using a configuration file (e.g., trivy-secret.yaml). In this file, you can define custom regex patterns or set up allowlists (exceptions).
By default, Kreate looks for a file named trivy-secret.yaml in the root directory of your project.
Example trivy-secret.yaml:
How it Works
The trivySecretScan task runs Trivy in fs (file system) mode with the secret scanner enabled. It specifically targets the files defined in sourceFiles.
Severity Levels
Severity levels help assess the risk of a finding:
Severity | Example Findings |
|---|---|
| Highly sensitive secrets (e.g., AWS Secret Access Keys, Private SSH Keys). |
| Important credentials (e.g., GitHub Personal Access Tokens). |
| General secrets or API tokens with restricted permissions. |
| Low-risk tokens or test credentials. |
Tips & Best Practices
Pre-commit Hooks: While Kreate runs the scan as a Gradle task, it is highly recommended to also use pre-commit hooks to get immediate feedback before a
git commitis even performed.Clean History: If a secret is found, it is not enough to just delete it and commit again. The secret remains in your Git history.
Revoke the secret (rotation) immediately!
If necessary, remove it from the history using tools like
bfg-repo-cleanerorgit-filter-repo.
Narrow Scope: Only scan files that might actually contain secrets. Scanning large binary files or build artifacts will slow down the task unnecessarily and may lead to false positives.
How to Run
To execute the secret scan individually, use the following Gradle command:
Alternatively, the scan is automatically included when running the lifecycle task: