| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use super::{now_ms, Pool}; |
| 7 | | use sqlx::FromRow; |
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | | const KEPT_FINDINGS: i64 = 200; |
| 13 | |
|
| 14 | |
|
| 15 | | pub const PUSH: &str = "push"; |
| 16 | | pub const HISTORY: &str = "history"; |
| 17 | |
|
| 18 | | #[derive(Clone, Debug, FromRow)] |
| 19 | | pub struct Bypass { |
| 20 | | pub id: i64, |
| 21 | | pub fingerprint: String, |
| 22 | | pub rule: String, |
| 23 | | pub reason: String, |
| 24 | | pub by_name: String, |
| 25 | | pub created_ms: i64, |
| 26 | | } |
| 27 | |
|
| 28 | | #[derive(Clone, Debug, FromRow)] |
| 29 | | pub struct FindingRow { |
| 30 | | pub id: i64, |
| 31 | | pub at_ms: i64, |
| 32 | | pub pusher: String, |
| 33 | | pub via: String, |
| 34 | | pub rule: String, |
| 35 | | pub description: String, |
| 36 | | pub path: String, |
| 37 | | pub line: i64, |
| 38 | | pub fingerprint: String, |
| 39 | | pub excerpt: String, |
| 40 | | } |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | | #[derive(Clone, Debug, FromRow)] |
| 45 | | pub struct HistoryRow { |
| 46 | | pub id: i64, |
| 47 | | pub at_ms: i64, |
| 48 | | pub rule: String, |
| 49 | | pub description: String, |
| 50 | | pub path: String, |
| 51 | | pub line: i64, |
| 52 | | pub fingerprint: String, |
| 53 | | pub excerpt: String, |
| 54 | | pub change_hash: String, |
| 55 | | pub bookmark: String, |
| 56 | | pub resolved_ms: Option<i64>, |
| 57 | | pub resolved_by: String, |
| 58 | | pub resolved_reason: String, |
| 59 | | } |
| 60 | |
|
| 61 | |
|
| 62 | | pub struct NewFinding<'a> { |
| 63 | | pub rule: &'a str, |
| 64 | | pub description: &'a str, |
| 65 | | pub path: &'a str, |
| 66 | | pub line: i64, |
| 67 | | pub fingerprint: &'a str, |
| 68 | | pub excerpt: &'a str, |
| 69 | | } |
| 70 | |
|
| 71 | |
|
| 72 | 17× | pub async fn allowed(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<String>> { |
| 73 | 17× | sqlx::query_scalar("SELECT fingerprint FROM secret_bypasses WHERE repo_id = $1").bind(repo_id).fetch_all(pool).await |
| 74 | 17× | } |
| 75 | |
|
| 76 | 133× | pub async fn bypasses(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<Bypass>> { |
| 77 | 133× | sqlx::query_as("SELECT id, fingerprint, rule, reason, by_name, created_ms FROM secret_bypasses WHERE repo_id = $1 ORDER BY id DESC") |
| 78 | 133× | .bind(repo_id) |
| 79 | 133× | .fetch_all(pool) |
| 80 | 133× | .await |
| 81 | 133× | } |
| 82 | |
|
| 83 | |
|
| 84 | 3× | pub async fn allow(pool: &Pool, repo_id: i64, fingerprint: &str, rule: &str, reason: &str, by: Option<(i64, &str)>) -> sqlx::Result<()> { |
| 85 | 3× | sqlx::query( |
| 86 | 3× | "INSERT INTO secret_bypasses (repo_id, fingerprint, rule, reason, by_account, by_name, created_ms) VALUES ($1, $2, $3, $4, $5, $6, $7) \ |
| 87 | 3× | ON CONFLICT (repo_id, fingerprint) DO UPDATE SET reason = excluded.reason, rule = excluded.rule, by_account = excluded.by_account, by_name = excluded.by_name, created_ms = excluded.created_ms", |
| 88 | | ) |
| 89 | 3× | .bind(repo_id) |
| 90 | 3× | .bind(fingerprint) |
| 91 | 3× | .bind(rule) |
| 92 | 3× | .bind(reason) |
| 93 | 3× | .bind(by.map(|b| b.0)) |
| 94 | 3× | .bind(by.map(|b| b.1).unwrap_or("")) |
| 95 | 3× | .bind(now_ms()) |
| 96 | 3× | .execute(pool) |
| 97 | 3× | .await?; |
| 98 | 3× | Ok(()) |
| 99 | 3× | } |
| 100 | |
|
| 101 | |
|
| 102 | 14× | pub async fn record(pool: &Pool, repo_id: i64, pusher: &str, via: &str, findings: &[NewFinding<'_>]) -> sqlx::Result<()> { |
| 103 | 14× | let now = now_ms(); |
| 104 | 14× | let mut tx = super::begin_write(pool).await?; |
| 105 | 18× | for f in findings { |
| 106 | 18× | sqlx::query( |
| 107 | 18× | "INSERT INTO secret_findings (repo_id, at_ms, pusher, via, rule, description, path, line, fingerprint, excerpt) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", |
| 108 | 18× | ) |
| 109 | 18× | .bind(repo_id) |
| 110 | 18× | .bind(now) |
| 111 | 18× | .bind(pusher) |
| 112 | 18× | .bind(via) |
| 113 | 18× | .bind(f.rule) |
| 114 | 18× | .bind(f.description) |
| 115 | 18× | .bind(f.path) |
| 116 | 18× | .bind(f.line) |
| 117 | 18× | .bind(f.fingerprint) |
| 118 | 18× | .bind(f.excerpt) |
| 119 | 18× | .execute(&mut *tx) |
| 120 | 18× | .await?; |
| 121 | | } |
| 122 | 14× | sqlx::query( |
| 123 | 14× | "DELETE FROM secret_findings WHERE repo_id = $1 AND source = 'push' \ |
| 124 | 14× | AND id NOT IN (SELECT id FROM secret_findings WHERE repo_id = $2 AND source = 'push' ORDER BY id DESC LIMIT $3)", |
| 125 | 14× | ) |
| 126 | 14× | .bind(repo_id) |
| 127 | 14× | .bind(repo_id) |
| 128 | 14× | .bind(KEPT_FINDINGS) |
| 129 | 14× | .execute(&mut *tx) |
| 130 | 14× | .await?; |
| 131 | 14× | tx.commit().await |
| 132 | 14× | } |
| 133 | |
|
| 134 | |
|
| 135 | 134× | pub async fn recent(pool: &Pool, repo_id: i64, limit: i64) -> sqlx::Result<Vec<FindingRow>> { |
| 136 | 134× | sqlx::query_as( |
| 137 | 134× | "SELECT id, at_ms, pusher, via, rule, description, path, line, fingerprint, excerpt FROM secret_findings f \ |
| 138 | 134× | WHERE repo_id = $1 AND source = 'push' \ |
| 139 | 134× | AND NOT EXISTS (SELECT 1 FROM secret_bypasses b WHERE b.repo_id = f.repo_id AND b.fingerprint = f.fingerprint) \ |
| 140 | 134× | ORDER BY id DESC LIMIT $2", |
| 141 | 134× | ) |
| 142 | 134× | .bind(repo_id) |
| 143 | 134× | .bind(limit) |
| 144 | 134× | .fetch_all(pool) |
| 145 | 134× | .await |
| 146 | 134× | } |
| 147 | |
|
| 148 | |
|
| 149 | 3× | pub async fn rule_of(pool: &Pool, repo_id: i64, fingerprint: &str) -> sqlx::Result<Option<String>> { |
| 150 | 3× | sqlx::query_scalar("SELECT rule FROM secret_findings WHERE repo_id = $1 AND fingerprint = $2 ORDER BY id DESC LIMIT 1") |
| 151 | 3× | .bind(repo_id) |
| 152 | 3× | .bind(fingerprint) |
| 153 | 3× | .fetch_optional(pool) |
| 154 | 3× | .await |
| 155 | 3× | } |
| 156 | |
|
| 157 | |
|
| 158 | 22× | pub async fn set_enabled(pool: &Pool, repo_id: i64, on: bool) -> sqlx::Result<()> { |
| 159 | 22× | sqlx::query("UPDATE repositories SET push_protection = $1 WHERE id = $2").bind(on).bind(repo_id).execute(pool).await?; |
| 160 | 22× | Ok(()) |
| 161 | 22× | } |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | 2× | pub async fn remove_bypass(pool: &Pool, repo_id: i64, fingerprint: &str) -> sqlx::Result<Option<Bypass>> { |
| 166 | 2× | let row: Option<Bypass> = sqlx::query_as( |
| 167 | 2× | "SELECT id, fingerprint, rule, reason, by_name, created_ms FROM secret_bypasses WHERE repo_id = $1 AND fingerprint = $2", |
| 168 | 2× | ) |
| 169 | 2× | .bind(repo_id) |
| 170 | 2× | .bind(fingerprint) |
| 171 | 2× | .fetch_optional(pool) |
| 172 | 2× | .await?; |
| 173 | 2× | if let Some(b) = &row { |
| 174 | 1× | sqlx::query("DELETE FROM secret_bypasses WHERE id = $1").bind(b.id).execute(pool).await?; |
| 175 | 1× | } |
| 176 | 2× | Ok(row) |
| 177 | 2× | } |
| 178 | |
|
| 179 | |
|
| 180 | | pub struct NewHistoryFinding<'a> { |
| 181 | | pub finding: NewFinding<'a>, |
| 182 | | pub change_hash: String, |
| 183 | | pub bookmark: String, |
| 184 | | } |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | 20× | pub async fn record_history(pool: &Pool, repo_id: i64, findings: &[NewHistoryFinding<'_>]) -> sqlx::Result<usize> { |
| 190 | 20× | let now = now_ms(); |
| 191 | 20× | let mut added = 0; |
| 192 | 20× | for h in findings { |
| 193 | 7× | let f = &h.finding; |
| 194 | 7× | let r = sqlx::query( |
| 195 | 7× | "INSERT INTO secret_findings (repo_id, at_ms, pusher, via, rule, description, path, line, fingerprint, excerpt, source, change_hash, bookmark) \ |
| 196 | 7× | VALUES ($1, $2, '', '', $3, $4, $5, $6, $7, $8, 'history', $9, $10) \ |
| 197 | 7× | ON CONFLICT (repo_id, fingerprint, path) WHERE source = 'history' DO NOTHING", |
| 198 | 7× | ) |
| 199 | 7× | .bind(repo_id) |
| 200 | 7× | .bind(now) |
| 201 | 7× | .bind(f.rule) |
| 202 | 7× | .bind(f.description) |
| 203 | 7× | .bind(f.path) |
| 204 | 7× | .bind(f.line) |
| 205 | 7× | .bind(f.fingerprint) |
| 206 | 7× | .bind(f.excerpt) |
| 207 | 7× | .bind(&h.change_hash) |
| 208 | 7× | .bind(&h.bookmark) |
| 209 | 7× | .execute(pool) |
| 210 | 7× | .await?; |
| 211 | 7× | added += r.rows_affected() as usize; |
| 212 | | } |
| 213 | 20× | Ok(added) |
| 214 | 20× | } |
| 215 | |
|
| 216 | | const HISTORY_COLUMNS: &str = |
| 217 | | "id, at_ms, rule, description, path, line, fingerprint, excerpt, change_hash, bookmark, resolved_ms, resolved_by, resolved_reason"; |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | 239× | pub async fn history(pool: &Pool, repo_id: i64, limit: i64) -> sqlx::Result<Vec<HistoryRow>> { |
| 222 | 239× | sqlx::query_as(&format!( |
| 223 | 239× | "SELECT {HISTORY_COLUMNS} FROM secret_findings f WHERE repo_id = $1 AND source = 'history' AND resolved_ms IS NULL \ |
| 224 | 239× | AND NOT EXISTS (SELECT 1 FROM secret_bypasses b WHERE b.repo_id = f.repo_id AND b.fingerprint = f.fingerprint) \ |
| 225 | 239× | ORDER BY id LIMIT $2" |
| 226 | 239× | )) |
| 227 | 239× | .bind(repo_id) |
| 228 | 239× | .bind(limit) |
| 229 | 239× | .fetch_all(pool) |
| 230 | 239× | .await |
| 231 | 239× | } |
| 232 | |
|
| 233 | |
|
| 234 | 135× | pub async fn resolved(pool: &Pool, repo_id: i64, limit: i64) -> sqlx::Result<Vec<HistoryRow>> { |
| 235 | 135× | sqlx::query_as(&format!( |
| 236 | 135× | "SELECT {HISTORY_COLUMNS} FROM secret_findings WHERE repo_id = $1 AND source = 'history' AND resolved_ms IS NOT NULL \ |
| 237 | 135× | ORDER BY resolved_ms DESC, id DESC LIMIT $2" |
| 238 | 135× | )) |
| 239 | 135× | .bind(repo_id) |
| 240 | 135× | .bind(limit) |
| 241 | 135× | .fetch_all(pool) |
| 242 | 135× | .await |
| 243 | 135× | } |
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | 2× | pub async fn resolve(pool: &Pool, repo_id: i64, id: i64, by: &str, reason: &str) -> sqlx::Result<Option<HistoryRow>> { |
| 248 | 2× | let done = sqlx::query( |
| 249 | 2× | "UPDATE secret_findings SET resolved_ms = $1, resolved_by = $2, resolved_reason = $3 \ |
| 250 | 2× | WHERE id = $4 AND repo_id = $5 AND source = 'history' AND resolved_ms IS NULL", |
| 251 | 2× | ) |
| 252 | 2× | .bind(now_ms()) |
| 253 | 2× | .bind(by) |
| 254 | 2× | .bind(reason) |
| 255 | 2× | .bind(id) |
| 256 | 2× | .bind(repo_id) |
| 257 | 2× | .execute(pool) |
| 258 | 2× | .await?; |
| 259 | 2× | if done.rows_affected() == 0 { |
| 260 | 1× | return Ok(None); |
| 261 | 1× | } |
| 262 | 1× | sqlx::query_as(&format!("SELECT {HISTORY_COLUMNS} FROM secret_findings WHERE id = $1")).bind(id).fetch_optional(pool).await |
| 263 | 2× | } |
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | | #[derive(Clone, Debug, Default, PartialEq, Eq, FromRow)] |
| 271 | | pub struct ScanRow { |
| 272 | | pub repo_id: i64, |
| 273 | | pub rules_version: i64, |
| 274 | | pub done_heads: String, |
| 275 | | pub target_heads: String, |
| 276 | | pub position: i64, |
| 277 | | pub pending: i64, |
| 278 | | pub changes: i64, |
| 279 | | pub bytes: i64, |
| 280 | | pub requested_ms: Option<i64>, |
| 281 | | pub pass_ms: Option<i64>, |
| 282 | | pub finished_ms: Option<i64>, |
| 283 | | pub error: String, |
| 284 | | } |
| 285 | |
|
| 286 | | const SCAN_COLUMNS: &str = |
| 287 | | "repo_id, rules_version, done_heads, target_heads, position, pending, changes, bytes, requested_ms, pass_ms, finished_ms, error"; |
| 288 | |
|
| 289 | 181× | pub async fn scan_of(pool: &Pool, repo_id: i64) -> sqlx::Result<Option<ScanRow>> { |
| 290 | 181× | sqlx::query_as(&format!("SELECT {SCAN_COLUMNS} FROM secret_scans WHERE repo_id = $1")).bind(repo_id).fetch_optional(pool).await |
| 291 | 181× | } |
| 292 | |
|
| 293 | |
|
| 294 | 20× | pub async fn save_scan(pool: &Pool, s: &ScanRow) -> sqlx::Result<()> { |
| 295 | 20× | sqlx::query(&format!( |
| 296 | 20× | "INSERT INTO secret_scans ({SCAN_COLUMNS}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) \ |
| 297 | 20× | ON CONFLICT (repo_id) DO UPDATE SET rules_version = excluded.rules_version, done_heads = excluded.done_heads, \ |
| 298 | 20× | target_heads = excluded.target_heads, position = excluded.position, pending = excluded.pending, changes = excluded.changes, \ |
| 299 | 20× | bytes = excluded.bytes, requested_ms = excluded.requested_ms, pass_ms = excluded.pass_ms, finished_ms = excluded.finished_ms, \ |
| 300 | 20× | error = excluded.error" |
| 301 | 20× | )) |
| 302 | 20× | .bind(s.repo_id) |
| 303 | 20× | .bind(s.rules_version) |
| 304 | 20× | .bind(&s.done_heads) |
| 305 | 20× | .bind(&s.target_heads) |
| 306 | 20× | .bind(s.position) |
| 307 | 20× | .bind(s.pending) |
| 308 | 20× | .bind(s.changes) |
| 309 | 20× | .bind(s.bytes) |
| 310 | 20× | .bind(s.requested_ms) |
| 311 | 20× | .bind(s.pass_ms) |
| 312 | 20× | .bind(s.finished_ms) |
| 313 | 20× | .bind(&s.error) |
| 314 | 20× | .execute(pool) |
| 315 | 20× | .await?; |
| 316 | 20× | Ok(()) |
| 317 | 20× | } |
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | 1× | pub async fn request_scan(pool: &Pool, repo_id: i64) -> sqlx::Result<()> { |
| 323 | 1× | let now = now_ms(); |
| 324 | 1× | sqlx::query( |
| 325 | 1× | "INSERT INTO secret_scans (repo_id, requested_ms) VALUES ($1, $2) \ |
| 326 | 1× | ON CONFLICT (repo_id) DO UPDATE SET requested_ms = excluded.requested_ms, done_heads = '', target_heads = '', position = 0, pending = 0", |
| 327 | 1× | ) |
| 328 | 1× | .bind(repo_id) |
| 329 | 1× | .bind(now) |
| 330 | 1× | .execute(pool) |
| 331 | 1× | .await?; |
| 332 | 1× | Ok(()) |
| 333 | 1× | } |
| 334 | |
|
| 335 | |
|
| 336 | 12× | pub async fn requested(pool: &Pool) -> sqlx::Result<Vec<i64>> { |
| 337 | 12× | sqlx::query_scalar("SELECT repo_id FROM secret_scans WHERE requested_ms IS NOT NULL ORDER BY requested_ms").fetch_all(pool).await |
| 338 | 12× | } |