internal/store/security.go

140 lines · 4020 bytes

  1package store
  2
  3import (
  4	"database/sql"
  5	"errors"
  6)
  7
  8// SecurityScan is a repo's code scanning state. A row exists only while
  9// scanning is enabled.
 10type SecurityScan struct {
 11	RepoID      int64
 12	NextRun     string
 13	LastResults string // when results were last uploaded, '' for never
 14	IssueNumber int64
 15}
 16
 17// SecurityFinding is one result from a scan, as last reported.
 18type SecurityFinding struct {
 19	Fingerprint string
 20	Tool        string
 21	Rule        string
 22	Level       string
 23	Path        string
 24	Line        int
 25	Message     string
 26}
 27
 28func (s *Store) EnableSecurityScan(repoID int64, nextRun string) error {
 29	_, err := s.DB.Exec("INSERT OR IGNORE INTO security_scans (repo_id, next_run) VALUES (?, ?)", repoID, nextRun)
 30	return err
 31}
 32
 33// DisableSecurityScan stops scanning and forgets what was reported.
 34func (s *Store) DisableSecurityScan(repoID int64) error {
 35	tx, err := s.DB.Begin()
 36	if err != nil {
 37		return err
 38	}
 39	defer tx.Rollback()
 40	if _, err := tx.Exec("DELETE FROM security_findings WHERE repo_id = ?", repoID); err != nil {
 41		return err
 42	}
 43	if _, err := tx.Exec("DELETE FROM security_scans WHERE repo_id = ?", repoID); err != nil {
 44		return err
 45	}
 46	return tx.Commit()
 47}
 48
 49func (s *Store) SecurityScanFor(repoID int64) (SecurityScan, error) {
 50	var sc SecurityScan
 51	err := s.DB.QueryRow(
 52		"SELECT repo_id, next_run, last_results, issue_number FROM security_scans WHERE repo_id = ?", repoID).
 53		Scan(&sc.RepoID, &sc.NextRun, &sc.LastResults, &sc.IssueNumber)
 54	if errors.Is(err, sql.ErrNoRows) {
 55		return sc, ErrNotFound
 56	}
 57	return sc, err
 58}
 59
 60// DueSecurityScans returns the repos whose next scan is at or before now.
 61// Archived repos are skipped: nobody is going to act on the findings.
 62func (s *Store) DueSecurityScans(nowISO string) ([]Repo, error) {
 63	rows, err := s.DB.Query(repoSelect+`
 64		JOIN security_scans sc ON sc.repo_id = r.id
 65		WHERE sc.next_run <= ?
 66		ORDER BY sc.next_run, r.id`, nowISO)
 67	if err != nil {
 68		return nil, err
 69	}
 70	defer rows.Close()
 71	var out []Repo
 72	for rows.Next() {
 73		r, err := scanRepo(rows)
 74		if err != nil {
 75			return nil, err
 76		}
 77		if r.Settings.Archived {
 78			continue
 79		}
 80		out = append(out, r)
 81	}
 82	return out, rows.Err()
 83}
 84
 85func (s *Store) SetSecurityNext(repoID int64, nextRun string) error {
 86	_, err := s.DB.Exec("UPDATE security_scans SET next_run = ? WHERE repo_id = ?", nextRun, repoID)
 87	return err
 88}
 89
 90func (s *Store) SetSecurityIssue(repoID, number int64) error {
 91	_, err := s.DB.Exec("UPDATE security_scans SET issue_number = ? WHERE repo_id = ?", number, repoID)
 92	return err
 93}
 94
 95func (s *Store) ReportedFindings(repoID int64) ([]SecurityFinding, error) {
 96	rows, err := s.DB.Query(`
 97		SELECT fingerprint, tool, rule, level, path, line, message FROM security_findings
 98		WHERE repo_id = ? ORDER BY fingerprint`, repoID)
 99	if err != nil {
100		return nil, err
101	}
102	defer rows.Close()
103	var out []SecurityFinding
104	for rows.Next() {
105		var f SecurityFinding
106		if err := rows.Scan(&f.Fingerprint, &f.Tool, &f.Rule, &f.Level, &f.Path, &f.Line, &f.Message); err != nil {
107			return nil, err
108		}
109		out = append(out, f)
110	}
111	return out, rows.Err()
112}
113
114// ReplaceFindings swaps in an upload's findings wholesale.
115func (s *Store) ReplaceFindings(repoID int64, findings []SecurityFinding) error {
116	tx, err := s.DB.Begin()
117	if err != nil {
118		return err
119	}
120	defer tx.Rollback()
121	if _, err := tx.Exec("DELETE FROM security_findings WHERE repo_id = ?", repoID); err != nil {
122		return err
123	}
124	for _, f := range findings {
125		if _, err := tx.Exec(`
126			INSERT OR IGNORE INTO security_findings (repo_id, fingerprint, tool, rule, level, path, line, message)
127			VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
128			repoID, f.Fingerprint, f.Tool, f.Rule, f.Level, f.Path, f.Line, f.Message); err != nil {
129			return err
130		}
131	}
132	return tx.Commit()
133}
134
135// StampSecurityResults records that a scan's results arrived.
136func (s *Store) StampSecurityResults(repoID int64) error {
137	_, err := s.DB.Exec(`UPDATE security_scans SET last_results = strftime('%Y-%m-%dT%H:%M:%fZ','now')
138		WHERE repo_id = ?`, repoID)
139	return err
140}