|
| 1 | +# Grafana Setup Guide for TruffleHog Integration |
| 2 | + |
| 3 | +**Zizmor + Grafana Bench (see metrics in one go):** Use the [grafana-bench-stack](../../grafana-bench-stack/README.md) in this repo: `cd grafana-bench-stack && docker compose up -d`, then set `PROMETHEUS_PUSHGATEWAY_URL` and open the **Zizmor (Grafana Bench)** dashboard in Grafana. |
| 4 | + |
| 5 | +## Quick Setup Options |
| 6 | + |
| 7 | +### Option 1: Grafana Cloud (Recommended for Testing - Fastest) |
| 8 | + |
| 9 | +1. **Sign up for Grafana Cloud** (free tier available) |
| 10 | + - Go to https://grafana.com/auth/sign-up/create-user |
| 11 | + - Create a free account (no credit card required for free tier) |
| 12 | + |
| 13 | +2. **Get your Loki credentials:** |
| 14 | + - Go to https://grafana.com/orgs/YOUR_ORG/connections/loki |
| 15 | + - Click "Send logs" → "Push logs" |
| 16 | + - Copy: |
| 17 | + - **LOKI_URL**: `https://logs-prod-XXX.grafana.net/loki/api/v1/push` |
| 18 | + - **LOKI_USERNAME**: Your Grafana Cloud username (usually an ID like `123456`) |
| 19 | + - **LOKI_PASSWORD**: Your API token (create one at https://grafana.com/orgs/YOUR_ORG/api-keys) |
| 20 | + |
| 21 | +3. **Get your Prometheus Pushgateway:** |
| 22 | + - Go to https://grafana.com/orgs/YOUR_ORG/connections/prometheus |
| 23 | + - Click "Send metrics" → "Remote write" |
| 24 | + - You'll need to set up a Prometheus instance or use a remote write endpoint |
| 25 | + - **Alternative**: Use a standalone Prometheus Pushgateway (see Option 2) |
| 26 | + |
| 27 | +### Option 2: Local Docker Setup (For Testing) |
| 28 | + |
| 29 | +Run these commands to set up local instances: |
| 30 | + |
| 31 | +```bash |
| 32 | +# Start Loki |
| 33 | +docker run -d --name loki -p 3100:3100 grafana/loki:latest |
| 34 | + |
| 35 | +# Start Prometheus Pushgateway |
| 36 | +docker run -d --name pushgateway -p 9091:9091 prom/pushgateway:latest |
| 37 | + |
| 38 | +# Start Grafana (optional, for visualization) |
| 39 | +docker run -d --name grafana -p 3000:3000 grafana/grafana:latest |
| 40 | +``` |
| 41 | + |
| 42 | +Then use: |
| 43 | +- **LOKI_URL**: `http://localhost:3100` (or your server IP) |
| 44 | +- **PROMETHEUS_PUSHGATEWAY_URL**: `http://localhost:9091` (or your server IP) |
| 45 | + |
| 46 | +**Note**: For GitHub Actions to reach these, you'll need to expose them publicly (use ngrok, or deploy to a cloud server). |
| 47 | + |
| 48 | +### Option 3: Use Existing Grafana Instance |
| 49 | + |
| 50 | +If you already have Grafana/Loki/Prometheus: |
| 51 | +- **LOKI_URL**: Your Loki instance URL (e.g., `https://loki.yourcompany.com`) |
| 52 | +- **LOKI_USERNAME**: Your Loki username (if auth required) |
| 53 | +- **LOKI_PASSWORD**: Your Loki password/token |
| 54 | +- **PROMETHEUS_PUSHGATEWAY_URL**: Your Pushgateway URL (e.g., `https://pushgateway.yourcompany.com`) |
| 55 | + |
| 56 | +## Adding Secrets to GitHub |
| 57 | + |
| 58 | +### For Testing (Repository Secrets - Quickest) |
| 59 | + |
| 60 | +1. Go to your repository: `https://github.com/YOUR_ORG/security-github-actions` |
| 61 | +2. Click **Settings** → **Secrets and variables** → **Actions** |
| 62 | +3. Click **New repository secret** |
| 63 | +4. Add these secrets: |
| 64 | + |
| 65 | + - **Name**: `LOKI_URL` |
| 66 | + **Value**: Your Loki URL (e.g., `https://logs-prod-XXX.grafana.net/loki/api/v1/push`) |
| 67 | + |
| 68 | + - **Name**: `LOKI_USERNAME` |
| 69 | + **Value**: Your Loki username (Grafana Cloud user ID) |
| 70 | + |
| 71 | + - **Name**: `LOKI_PASSWORD` |
| 72 | + **Value**: Your Loki API token/password |
| 73 | + |
| 74 | + - **Name**: `PROMETHEUS_PUSHGATEWAY_URL` |
| 75 | + **Value**: Your Pushgateway URL (e.g., `http://localhost:9091` or your cloud URL) |
| 76 | + |
| 77 | +### For Production (Organization Secrets - Recommended) |
| 78 | + |
| 79 | +1. Go to your organization: `https://github.com/organizations/YOUR_ORG/settings/secrets/actions` |
| 80 | +2. Click **New organization secret** |
| 81 | +3. Add the same secrets as above |
| 82 | + |
| 83 | +## Setting Up Grafana Dashboards |
| 84 | + |
| 85 | +### 1. Add Data Sources |
| 86 | + |
| 87 | +#### Add Loki Data Source: |
| 88 | +1. In Grafana, go to **Configuration** → **Data sources** |
| 89 | +2. Click **Add data source** → Select **Loki** |
| 90 | +3. Enter your Loki URL |
| 91 | +4. If using Grafana Cloud, use the credentials from step 2 above |
| 92 | +5. Click **Save & test** |
| 93 | + |
| 94 | +#### Add Prometheus Data Source: |
| 95 | +1. Go to **Configuration** → **Data sources** |
| 96 | +2. Click **Add data source** → Select **Prometheus** |
| 97 | +3. Enter your Prometheus URL (the one scraping the Pushgateway) |
| 98 | +4. Click **Save & test** |
| 99 | + |
| 100 | +### 2. Create Dashboard |
| 101 | + |
| 102 | +1. Go to **Dashboards** → **New** → **New dashboard** |
| 103 | +2. Click **Add visualization** |
| 104 | + |
| 105 | +#### Panel 1: Total Secrets Over Time (Prometheus) |
| 106 | +- **Data source**: Prometheus |
| 107 | +- **Query**: |
| 108 | + ```promql |
| 109 | + sum(trufflehog_secrets_total) by (repository) |
| 110 | + ``` |
| 111 | +- **Visualization**: Time series |
| 112 | +- **Title**: "Total Secrets Found by Repository" |
| 113 | + |
| 114 | +#### Panel 2: Verified vs Unverified (Prometheus) |
| 115 | +- **Data source**: Prometheus |
| 116 | +- **Query A**: |
| 117 | + ```promql |
| 118 | + sum(trufflehog_secrets_verified) by (repository) |
| 119 | + ``` |
| 120 | +- **Query B**: |
| 121 | + ```promql |
| 122 | + sum(trufflehog_secrets_unverified) by (repository) |
| 123 | + ``` |
| 124 | +- **Visualization**: Time series |
| 125 | +- **Title**: "Verified vs Unverified Secrets" |
| 126 | + |
| 127 | +#### Panel 3: Secrets by Detector Type (Prometheus) |
| 128 | +- **Data source**: Prometheus |
| 129 | +- **Query**: |
| 130 | + ```promql |
| 131 | + sum(trufflehog_secrets_by_detector_*) by (detector) |
| 132 | + ``` |
| 133 | +- **Visualization**: Bar chart or Pie chart |
| 134 | +- **Title**: "Secrets by Detector Type" |
| 135 | + |
| 136 | +#### Panel 4: Recent Findings Table (Loki) |
| 137 | +- **Data source**: Loki |
| 138 | +- **Query**: |
| 139 | + ```logql |
| 140 | + {job="trufflehog"} |
| 141 | + ``` |
| 142 | +- **Visualization**: Table or Logs |
| 143 | +- **Title**: "Recent Secret Findings" |
| 144 | + |
| 145 | +#### Panel 5: Findings Count Over Time (Loki) |
| 146 | +- **Data source**: Loki |
| 147 | +- **Query**: |
| 148 | + ```logql |
| 149 | + count_over_time({job="trufflehog"}[1h]) |
| 150 | + ``` |
| 151 | +- **Visualization**: Time series |
| 152 | +- **Title**: "Findings Count (Last Hour)" |
| 153 | + |
| 154 | +### 3. Useful Loki Queries for Filtering |
| 155 | + |
| 156 | +**Verified secrets only:** |
| 157 | +```logql |
| 158 | +{job="trufflehog", verified="true"} |
| 159 | +``` |
| 160 | + |
| 161 | +**By specific repository:** |
| 162 | +```logql |
| 163 | +{job="trufflehog", repository="your-repo-name"} |
| 164 | +``` |
| 165 | + |
| 166 | +**By detector type:** |
| 167 | +```logql |
| 168 | +{job="trufflehog", detector="aws"} |
| 169 | +``` |
| 170 | + |
| 171 | +**Combined filter:** |
| 172 | +```logql |
| 173 | +{job="trufflehog", verified="true", repository="your-repo-name"} |
| 174 | +``` |
| 175 | + |
| 176 | +## Testing the Integration |
| 177 | + |
| 178 | +1. **Trigger a workflow** that uses the TruffleHog reusable workflow |
| 179 | +2. **Check workflow logs** for: |
| 180 | + - "✅ Successfully sent X findings to Loki" |
| 181 | + - "✅ Successfully sent X metrics to Prometheus" |
| 182 | +3. **Check Grafana**: |
| 183 | + - Loki: Query `{job="trufflehog"}` - you should see log entries |
| 184 | + - Prometheus: Query `trufflehog_secrets_total` - you should see metrics |
| 185 | + |
| 186 | +## Troubleshooting |
| 187 | + |
| 188 | +### No data appearing in Grafana |
| 189 | + |
| 190 | +1. **Check GitHub Actions logs:** |
| 191 | + - Look for errors in the "Send findings to Loki" or "Send metrics to Prometheus" steps |
| 192 | + - Verify secrets are set correctly |
| 193 | + |
| 194 | +2. **Verify URLs are accessible:** |
| 195 | + - Test Loki URL: `curl -u USERNAME:PASSWORD https://your-loki-url/ready` |
| 196 | + - Test Pushgateway: `curl http://your-pushgateway-url/metrics` |
| 197 | + |
| 198 | +3. **Check Prometheus is scraping Pushgateway:** |
| 199 | + - If using Prometheus, ensure it's configured to scrape the Pushgateway |
| 200 | + - Check Prometheus targets page |
| 201 | + |
| 202 | +4. **Verify data format:** |
| 203 | + - Check Loki logs: Query `{job="trufflehog"}` should return entries |
| 204 | + - Check Prometheus metrics: Query `trufflehog_secrets_total` should return values |
| 205 | + |
| 206 | +### Common Issues |
| 207 | + |
| 208 | +- **401 Unauthorized**: Check LOKI_USERNAME and LOKI_PASSWORD are correct |
| 209 | +- **Connection refused**: Verify URLs are correct and accessible from GitHub Actions runners |
| 210 | +- **No metrics in Prometheus**: Ensure Prometheus is scraping the Pushgateway endpoint |
| 211 | + |
| 212 | +## Quick Test Script |
| 213 | + |
| 214 | +You can test the scripts locally: |
| 215 | + |
| 216 | +```bash |
| 217 | +# Set environment variables |
| 218 | +export LOKI_URL="https://your-loki-url" |
| 219 | +export LOKI_USERNAME="your-username" |
| 220 | +export LOKI_PASSWORD="your-password" |
| 221 | +export REPOSITORY="test/repo" |
| 222 | +export COMMIT_SHA="abc123" |
| 223 | +export BRANCH="main" |
| 224 | +export TRUFFLEHOG_RESULTS_FILE="results.json" |
| 225 | + |
| 226 | +# Create a test results.json |
| 227 | +echo '[{"DetectorName": "AWS", "Verified": true, "SourceMetadata": {"Data": {"Filesystem": {"file": "test.txt", "line": 1}}}, "Redacted": "test"}]' > results.json |
| 228 | + |
| 229 | +# Test Loki script |
| 230 | +python trufflehog/send-to-loki.py |
| 231 | + |
| 232 | +# Test Prometheus script |
| 233 | +export PROMETHEUS_PUSHGATEWAY_URL="http://your-pushgateway-url" |
| 234 | +python trufflehog/send-to-prometheus.py |
| 235 | +``` |
0 commit comments