Commit eb2ad4cd3e
Verified · cmc ci/build: success ci/quality: success ci/test: success
Layout: unified · split
.gitbay/wiki/Admin.org +10 −2
| @@ -462,8 +462,16 @@ The instance's code scan, which a repository admin turns on with | ||
| 462 | 462 | runner host that serves a scanned repository. Build it from |
| 463 | 463 | =deploy/Containerfile.security= (Opengrep and its rules, both pinned |
| 464 | 464 | by sha256); the file has the commands. |
| 465 | - =schedule= ("0 4 * * *") — when each enabled repository's default | |
| 466 | branch is scanned. =repo security run= queues one now. | |
| 465 | - =most_often= ("daily") and =least_often= ("monthly") — the range of | |
| 466 | frequencies a repository may choose with =repo settings | |
| 467 | security-frequency=, from =daily=, =weekly= (Sunday) and =monthly= | |
| 468 | (the 1st). A repository whose choice falls outside a narrowed range | |
| 469 | scans at the nearest frequency in it, from its next scan on. | |
| 470 | - =default= ("daily") — the frequency of a repository that has not | |
| 471 | chosen one; within the range. | |
| 472 | - =hour= (4) — the hour, UTC, every scan is due. =repo security run= | |
| 473 | queues one now. | |
| 474 | - =schedule= is gone; a config that sets it is refused at load. | |
| 467 | 475 | - =steps= — shell steps that write SARIF into =$GITBAY_RESULTS=. The |
| 468 | 476 | default runs Opengrep with the rules in the image at |
| 469 | 477 | =/opt/opengrep-rules=. |
.gitbay/wiki/Parity.org +1
| @@ -251,6 +251,7 @@ there shows the app's rendering rather than the one the web page shows. | ||
| 251 | 251 | | code scan now | yes | no | yes | |
| 252 | 252 | | code scan dismiss, reopen | yes | yes | no | |
| 253 | 253 | | code scan ignores, rules | yes | yes | no | |
| 254 | | code scan frequency | yes | yes | no | | |
| 254 | 255 | | dependency checks on/off | yes | yes | yes | |
| 255 | 256 | | dependency status | yes | yes | yes | |
| 256 | 257 | | advisory dismiss, reopen | yes | yes | no | |
.gitbay/wiki/Users.org +1
| @@ -250,6 +250,7 @@ gitbay repo security dismiss you/project 3f9a0c1d2e --reason false-positive # o | ||
| 250 | 250 | gitbay repo security reopen you/project 3f9a0c1d2e |
| 251 | 251 | gitbay repo settings security-ignore you/project 'testdata/**' # paths set aside from the scan |
| 252 | 252 | gitbay repo settings security-exclude-rule you/project swift.lang.crypto.insecure-random |
| 253 | gitbay repo settings security-frequency you/project weekly # daily, weekly or monthly, as the instance allows | |
| 253 | 254 | gitbay repo settings git-daemon you/project on # expose over git:// |
| 254 | 255 | gitbay repo topics add you/project cli forge # free-form tags, shown on the web |
| 255 | 256 | gitbay repo search forge # find repos by name/description/topic |
cmd/gitbay/main.go +1
| @@ -651,6 +651,7 @@ func repoCmd() *cobra.Command { | ||
| 651 | 651 | pass("secret-scan-allow", passOpts{server: []string{"repo", "settings", "secret-scan-allow"}, needsRepo: true}), |
| 652 | 652 | pass("security-ignore", passOpts{server: []string{"repo", "settings", "security-ignore"}, needsRepo: true}), |
| 653 | 653 | pass("security-exclude-rule", passOpts{server: []string{"repo", "settings", "security-exclude-rule"}, needsRepo: true}), |
| 654 | pass("security-frequency", passOpts{server: []string{"repo", "settings", "security-frequency"}, needsRepo: true}), | |
| 654 | 655 | pass("require-mr", passOpts{server: []string{"repo", "settings", "require-mr"}, needsRepo: true}), |
| 655 | 656 | pass("description", passOpts{server: []string{"repo", "settings", "description"}, needsRepo: true}), |
| 656 | 657 | pass("website", passOpts{server: []string{"repo", "settings", "website"}, needsRepo: true}), |
cmd/gitbay/summaries_gen.go +1
| @@ -220,6 +220,7 @@ var summaries = map[string]string{ | ||
| 220 | 220 | "repo settings secret-scan": "scan pushes for credentials: off, warn the pusher, or block the push", |
| 221 | 221 | "repo settings secret-scan-allow": "paths whose secret scan findings are ignored", |
| 222 | 222 | "repo settings security-exclude-rule": "code scanning rules, or families of them, whose findings are set aside", |
| 223 | "repo settings security-frequency": "how often the code scan runs, within what the instance allows", | |
| 223 | 224 | "repo settings security-ignore": "paths whose code scanning findings are set aside", |
| 224 | 225 | "repo settings show": "show settings", |
| 225 | 226 | "repo settings unprotect": "unprotect a branch", |
internal/ci/sched.go +8 −1
| @@ -31,6 +31,13 @@ func NextRun(expr string, after time.Time) string { | ||
| 31 | 31 | return isoNow(n) |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | // SecurityNextRun is when a repository that chose freq is next scanned: | |
| 35 | // at the configured hour, daily, on Sunday, or on the 1st. | |
| 36 | func SecurityNextRun(sec config.Security, freq string, after time.Time) string { | |
| 37 | field := map[string]string{"daily": "* * *", "weekly": "* * 0", "monthly": "1 * *"}[sec.Frequency(freq)] | |
| 38 | return NextRun(fmt.Sprintf("0 %d %s", sec.Hour, field), after.UTC()) | |
| 39 | } | |
| 40 | ||
| 34 | 41 | // Scheduler fires scheduled builds. repoDir maps a repo to its bare path. |
| 35 | 42 | // Security is the instance's code scanning job; with no image it never |
| 36 | 43 | // queues. |
| @@ -139,7 +146,7 @@ func (s *Scheduler) runSecurityDue(now time.Time) { | ||
| 139 | 146 | return |
| 140 | 147 | } |
| 141 | 148 | for _, repo := range due { |
| 142 | s.St.SetSecurityNext(repo.ID, NextRun(s.Security.Schedule, now)) | |
| 149 | s.St.SetSecurityNext(repo.ID, SecurityNextRun(s.Security, repo.Settings.SecurityFrequency, now)) | |
| 143 | 150 | if _, err := QueueSecurityScan(s.St, s.Security, s.RepoDir(repo.OwnerName, repo.Name), repo); err != nil { |
| 144 | 151 | slog.Warn("scheduler: queueing security scan", "repo", repo.Path(), "err", err) |
| 145 | 152 | } |
internal/ci/sched_test.go +23 −1
| @@ -177,7 +177,7 @@ func TestSchedulerQueuesSecurityScan(t *testing.T) { | ||
| 177 | 177 | t.Fatalf("queued with no image: %+v", builds) |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | s.Security = config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: []string{"true"}} | |
| 180 | s.Security = config.Security{Image: "localhost/gitbay-security:1", Hour: 4, MostOften: "daily", LeastOften: "monthly", Default: "daily", Steps: []string{"true"}} | |
| 181 | 181 | st.SetSecurityNext(repoID, now.Add(-time.Minute).UTC().Format("2006-01-02T15:04:05Z")) |
| 182 | 182 | s.RunDue(now) |
| 183 | 183 | builds, _ := st.ListBuilds(repoID, store.BuildFilter{}, 10) |
| @@ -204,3 +204,25 @@ func TestSecurityJobNameIsReserved(t *testing.T) { | ||
| 204 | 204 | t.Fatalf("Parse = %v", err) |
| 205 | 205 | } |
| 206 | 206 | } |
| 207 | ||
| 208 | func TestSecurityNextRun(t *testing.T) { | |
| 209 | sec := config.Default().Security | |
| 210 | sec.Hour = 6 | |
| 211 | // Wednesday 2026-11-04 10:00 UTC. | |
| 212 | after := time.Date(2026, 11, 4, 10, 0, 0, 0, time.UTC).In(time.FixedZone("UTC-6", -6*3600)) | |
| 213 | for chosen, want := range map[string]string{ | |
| 214 | "": "2026-11-05T06:00:00Z", | |
| 215 | "daily": "2026-11-05T06:00:00Z", | |
| 216 | "weekly": "2026-11-08T06:00:00Z", | |
| 217 | "monthly": "2026-12-01T06:00:00Z", | |
| 218 | } { | |
| 219 | if got := SecurityNextRun(sec, chosen, after); got != want { | |
| 220 | t.Errorf("%q: %s, want %s", chosen, got, want) | |
| 221 | } | |
| 222 | } | |
| 223 | // A choice the instance no longer allows scans at the nearest allowed. | |
| 224 | sec.LeastOften, sec.Default = "weekly", "weekly" | |
| 225 | if got := SecurityNextRun(sec, "monthly", after); got != "2026-11-08T06:00:00Z" { | |
| 226 | t.Errorf("monthly outside the range: %s", got) | |
| 227 | } | |
| 228 | } | |
internal/config/config.go +58 −8
| @@ -12,6 +12,7 @@ import ( | ||
| 12 | 12 | "net" |
| 13 | 13 | "os" |
| 14 | 14 | "path/filepath" |
| 15 | "slices" | |
| 15 | 16 | "strconv" |
| 16 | 17 | "strings" |
| 17 | 18 | "time" |
| @@ -231,11 +232,46 @@ type Deps struct { | ||
| 231 | 232 | // enable` schedules. It is unavailable until an operator names Image, an |
| 232 | 233 | // image built from deploy/Containerfile.security and provisioned on the |
| 233 | 234 | // runner hosts. Steps write SARIF into $GITBAY_RESULTS; the runner uploads |
| 234 | // it when they succeed. | |
| 235 | // it when they succeed. Repositories scan at a frequency between MostOften | |
| 236 | // and LeastOften, Default unless they choose one, at Hour UTC. | |
| 235 | 237 | type Security struct { |
| 236 | Image string `toml:"image"` | |
| 237 | Schedule string `toml:"schedule"` | |
| 238 | Steps []string `toml:"steps"` | |
| 238 | Image string `toml:"image"` | |
| 239 | Hour int `toml:"hour"` | |
| 240 | MostOften string `toml:"most_often"` | |
| 241 | LeastOften string `toml:"least_often"` | |
| 242 | Default string `toml:"default"` | |
| 243 | Steps []string `toml:"steps"` | |
| 244 | } | |
| 245 | ||
| 246 | // ScanFrequencies are the code scan frequencies, most often first. Weekly | |
| 247 | // scans run on Sunday, monthly ones on the 1st. | |
| 248 | var ScanFrequencies = []string{"daily", "weekly", "monthly"} | |
| 249 | ||
| 250 | // Frequencies returns the frequencies a repository may choose, most often | |
| 251 | // first. | |
| 252 | func (s Security) Frequencies() []string { | |
| 253 | lo, hi := s.bounds() | |
| 254 | return ScanFrequencies[lo : hi+1] | |
| 255 | } | |
| 256 | ||
| 257 | // Frequency is the frequency a repository scans at: its choice, or the | |
| 258 | // default, moved to the nearest one the instance allows. | |
| 259 | func (s Security) Frequency(chosen string) string { | |
| 260 | if chosen == "" { | |
| 261 | chosen = s.Default | |
| 262 | } | |
| 263 | lo, hi := s.bounds() | |
| 264 | return ScanFrequencies[max(lo, min(slices.Index(ScanFrequencies, chosen), hi))] | |
| 265 | } | |
| 266 | ||
| 267 | // bounds are the indexes of MostOften and LeastOften in ScanFrequencies, | |
| 268 | // or the whole list when they are unset or out of order. | |
| 269 | func (s Security) bounds() (int, int) { | |
| 270 | lo, hi := slices.Index(ScanFrequencies, s.MostOften), slices.Index(ScanFrequencies, s.LeastOften) | |
| 271 | if lo < 0 || hi < lo { | |
| 272 | return 0, len(ScanFrequencies) - 1 | |
| 273 | } | |
| 274 | return lo, hi | |
| 239 | 275 | } |
| 240 | 276 | |
| 241 | 277 | // DefaultSecuritySteps run Opengrep with the rules the image carries. |
| @@ -591,7 +627,8 @@ func Default() Config { | ||
| 591 | 627 | GitDaemon: GitDaemon{Port: 9418}, |
| 592 | 628 | Mirrors: Mirrors{PullIntervalMinutes: 15}, |
| 593 | 629 | Deps: Deps{CheckIntervalHours: 24}, |
| 594 | Security: Security{Schedule: "0 4 * * *", Steps: DefaultSecuritySteps}, | |
| 630 | Security: Security{Hour: 4, MostOften: "daily", LeastOften: "monthly", Default: "daily", | |
| 631 | Steps: DefaultSecuritySteps}, | |
| 595 | 632 | Limits: Limits{ |
| 596 | 633 | MaxPackBytes: 2 << 30, // 2 GiB |
| 597 | 634 | MaxBlobBytes: 100 << 20, |
| @@ -612,6 +649,9 @@ func Load(path string) (Config, error) { | ||
| 612 | 649 | if err != nil { |
| 613 | 650 | return cfg, err |
| 614 | 651 | } |
| 652 | if md.IsDefined("security", "schedule") { | |
| 653 | return cfg, errors.New("security.schedule is replaced by security.hour, most_often, least_often and default") | |
| 654 | } | |
| 615 | 655 | if u := md.Undecoded(); len(u) > 0 { |
| 616 | 656 | return cfg, fmt.Errorf("unknown config key %q", u[0].String()) |
| 617 | 657 | } |
| @@ -675,10 +715,20 @@ func (c Config) Validate() error { | ||
| 675 | 715 | if c.Server.Root == "" { |
| 676 | 716 | errs = append(errs, errors.New("server.root is required")) |
| 677 | 717 | } |
| 718 | sec := c.Security | |
| 719 | if sec.Hour < 0 || sec.Hour > 23 { | |
| 720 | errs = append(errs, fmt.Errorf("security.hour must be 0 to 23, got %d", sec.Hour)) | |
| 721 | } | |
| 722 | if err := oneOf("security.most_often", sec.MostOften, ScanFrequencies...); err != nil { | |
| 723 | errs = append(errs, err) | |
| 724 | } else if err := oneOf("security.least_often", sec.LeastOften, ScanFrequencies...); err != nil { | |
| 725 | errs = append(errs, err) | |
| 726 | } else if slices.Index(ScanFrequencies, sec.MostOften) > slices.Index(ScanFrequencies, sec.LeastOften) { | |
| 727 | errs = append(errs, fmt.Errorf("security.most_often %q is less often than security.least_often %q", sec.MostOften, sec.LeastOften)) | |
| 728 | } else if !slices.Contains(sec.Frequencies(), sec.Default) { | |
| 729 | errs = append(errs, fmt.Errorf("security.default must be one of %v, got %q", sec.Frequencies(), sec.Default)) | |
| 730 | } | |
| 678 | 731 | if c.Security.Image != "" { |
| 679 | if len(strings.Fields(c.Security.Schedule)) != 5 { | |
| 680 | errs = append(errs, errors.New("security.schedule must be a five-field cron expression")) | |
| 681 | } | |
| 682 | 732 | if len(c.Security.Steps) == 0 { |
| 683 | 733 | errs = append(errs, errors.New("security.steps must name at least one step when security.image is set")) |
| 684 | 734 | } |
internal/config/config_test.go +35
| @@ -519,3 +519,38 @@ func TestMailInboundPassword(t *testing.T) { | ||
| 519 | 519 | t.Fatal("two lines accepted") |
| 520 | 520 | } |
| 521 | 521 | } |
| 522 | ||
| 523 | func TestSecurityFrequencies(t *testing.T) { | |
| 524 | cfg, err := Load(writeConfig(t, minimal)) | |
| 525 | if err != nil { | |
| 526 | t.Fatal(err) | |
| 527 | } | |
| 528 | if s := cfg.Security; s.Hour != 4 || s.Default != "daily" || strings.Join(s.Frequencies(), ",") != "daily,weekly,monthly" { | |
| 529 | t.Fatalf("defaults: %+v %v", s, s.Frequencies()) | |
| 530 | } | |
| 531 | cfg, err = Load(writeConfig(t, minimal+"\n[security]\nhour = 0\nmost_often = \"weekly\"\ndefault = \"weekly\"\n")) | |
| 532 | if err != nil { | |
| 533 | t.Fatal(err) | |
| 534 | } | |
| 535 | s := cfg.Security | |
| 536 | if s.Hour != 0 || strings.Join(s.Frequencies(), ",") != "weekly,monthly" { | |
| 537 | t.Fatalf("narrowed: %+v %v", s, s.Frequencies()) | |
| 538 | } | |
| 539 | for chosen, want := range map[string]string{"": "weekly", "daily": "weekly", "weekly": "weekly", "monthly": "monthly"} { | |
| 540 | if got := s.Frequency(chosen); got != want { | |
| 541 | t.Errorf("Frequency(%q) = %q, want %q", chosen, got, want) | |
| 542 | } | |
| 543 | } | |
| 544 | for body, want := range map[string]string{ | |
| 545 | "schedule = \"0 4 * * *\"\n": "security.schedule", | |
| 546 | "hour = 24\n": "security.hour", | |
| 547 | "most_often = \"hourly\"\n": "security.most_often", | |
| 548 | "least_often = \"yearly\"\n": "security.least_often", | |
| 549 | "most_often = \"monthly\"\nleast_often = \"weekly\"\n": "security.most_often", | |
| 550 | "least_often = \"weekly\"\ndefault = \"monthly\"\n": "security.default", | |
| 551 | } { | |
| 552 | if _, err := Load(writeConfig(t, minimal+"\n[security]\n"+body)); err == nil || !strings.Contains(err.Error(), want) { | |
| 553 | t.Errorf("want %q, got %v\n%s", want, err, body) | |
| 554 | } | |
| 555 | } | |
| 556 | } | |
internal/control/security.go +45 −2
| @@ -15,6 +15,7 @@ import ( | ||
| 15 | 15 | "gitbay.org/gitbay/internal/botissue" |
| 16 | 16 | "gitbay.org/gitbay/internal/ci" |
| 17 | 17 | "gitbay.org/gitbay/internal/codescan" |
| 18 | "gitbay.org/gitbay/internal/config" | |
| 18 | 19 | "gitbay.org/gitbay/internal/deps" |
| 19 | 20 | "gitbay.org/gitbay/internal/gitutil" |
| 20 | 21 | "gitbay.org/gitbay/internal/policy" |
| @@ -70,6 +71,11 @@ func init() { | ||
| 70 | 71 | Usage: "repo settings security-exclude-rule <owner/name> [<rule>...] (none clears the list)", |
| 71 | 72 | Examples: []string{"repo settings security-exclude-rule krz/gitbay swift.lang.crypto.insecure-random"}, |
| 72 | 73 | Run: runSecurityExcludeRule}) |
| 74 | register(Command{Path: []string{"repo", "settings", "security-frequency"}, | |
| 75 | Summary: "how often the code scan runs, within what the instance allows", | |
| 76 | Usage: "repo settings security-frequency <owner/name> daily|weekly|monthly", | |
| 77 | Examples: []string{"repo settings security-frequency krz/gitbay weekly"}, | |
| 78 | Run: runSecurityFrequency}) | |
| 73 | 79 | register(Command{Path: []string{"runner", "results"}, |
| 74 | 80 | Summary: "upload a code scan's SARIF from stdin", |
| 75 | 81 | Usage: "runner results <build-id>", |
| @@ -81,6 +87,7 @@ func init() { | ||
| 81 | 87 | // findings, the ones dismissed, and how many it set aside. |
| 82 | 88 | type SecurityOut struct { |
| 83 | 89 | Enabled bool `json:"enabled"` |
| 90 | Frequency string `json:"frequency,omitempty"` | |
| 84 | 91 | NextRun string `json:"next_run,omitempty"` |
| 85 | 92 | LastResults string `json:"last_results,omitempty"` |
| 86 | 93 | IssueNumber int64 `json:"issue_number,omitempty"` |
| @@ -116,7 +123,7 @@ func runSecurityEnable(c *Ctx, args []string) int { | ||
| 116 | 123 | if c.Cfg.Security.Image == "" { |
| 117 | 124 | return c.fail(protocol.ExitFailure, "this instance has no code scanning image; an operator sets [security] image") |
| 118 | 125 | } |
| 119 | next := ci.NextRun(c.Cfg.Security.Schedule, time.Now()) | |
| 126 | next := ci.SecurityNextRun(c.Cfg.Security, repo.Settings.SecurityFrequency, time.Now()) | |
| 120 | 127 | if err := c.Store.EnableSecurityScan(repo.ID, next); err != nil { |
| 121 | 128 | return c.fail(protocol.ExitFailure, "%v", err) |
| 122 | 129 | } |
| @@ -196,7 +203,8 @@ func runSecurityStatus(c *Ctx, args []string) int { | ||
| 196 | 203 | if err != nil { |
| 197 | 204 | return c.fail(protocol.ExitFailure, "%v", err) |
| 198 | 205 | } |
| 199 | out := SecurityOut{Enabled: true, NextRun: scan.NextRun, LastResults: scan.LastResults, | |
| 206 | freq := c.Cfg.Security.Frequency(repo.Settings.SecurityFrequency) | |
| 207 | out := SecurityOut{Enabled: true, Frequency: freq, NextRun: scan.NextRun, LastResults: scan.LastResults, | |
| 200 | 208 | IssueNumber: scan.IssueNumber, Findings: []SecurityFinding{}, Dismissed: []SecurityFinding{}} |
| 201 | 209 | json.Unmarshal([]byte(scan.Aside), &out.Aside) |
| 202 | 210 | for _, f := range codescan.ByPlace(reported) { |
| @@ -217,6 +225,7 @@ func runSecurityStatus(c *Ctx, args []string) int { | ||
| 217 | 225 | v := c.view(w) |
| 218 | 226 | v.fields( |
| 219 | 227 | "scanning", "on", |
| 228 | "frequency", freq, | |
| 220 | 229 | "next scan", scan.NextRun, |
| 221 | 230 | "last results", c.when(scan.LastResults), |
| 222 | 231 | "tracked in", tracked, |
| @@ -241,6 +250,7 @@ func runSecurityStatus(c *Ctx, args []string) int { | ||
| 241 | 250 | }, func() screen { |
| 242 | 251 | s := screen{fields: []field{ |
| 243 | 252 | {"Scanning", []cell{cGlyph("ok"), cText("on")}}, |
| 253 | {"Frequency", []cell{cText(freq)}}, | |
| 244 | 254 | {"Next scan", []cell{cText(scan.NextRun)}}, |
| 245 | 255 | {"Last results", []cell{cAge(scan.LastResults)}}, |
| 246 | 256 | }} |
| @@ -568,6 +578,39 @@ func resolveFingerprint(findings []store.SecurityFinding, given string) (string, | ||
| 568 | 578 | return match, "" |
| 569 | 579 | } |
| 570 | 580 | |
| 581 | // runSecurityFrequency sets how often the repository is scanned and, when | |
| 582 | // scanning is on, moves the next scan to match. | |
| 583 | func runSecurityFrequency(c *Ctx, args []string) int { | |
| 584 | if len(args) != 2 || !slices.Contains(config.ScanFrequencies, args[1]) { | |
| 585 | return c.usage() | |
| 586 | } | |
| 587 | freq := args[1] | |
| 588 | repo, code := resolveRepo(c, args[0], policy.CanAdmin) | |
| 589 | if code >= 0 { | |
| 590 | return code | |
| 591 | } | |
| 592 | if allowed := c.Cfg.Security.Frequencies(); !slices.Contains(allowed, freq) { | |
| 593 | return c.fail(protocol.ExitFailure, "this instance scans %s", strings.Join(allowed, ", ")) | |
| 594 | } | |
| 595 | if _, err := c.Store.UpdateRepoSettings(repo.ID, func(s *store.RepoSettings) { s.SecurityFrequency = freq }); err != nil { | |
| 596 | return c.fail(protocol.ExitFailure, "%v", err) | |
| 597 | } | |
| 598 | next := "" | |
| 599 | if _, err := c.Store.SecurityScanFor(repo.ID); err == nil { | |
| 600 | next = ci.SecurityNextRun(c.Cfg.Security, freq, time.Now()) | |
| 601 | if err := c.Store.SetSecurityNext(repo.ID, next); err != nil { | |
| 602 | return c.fail(protocol.ExitFailure, "%v", err) | |
| 603 | } | |
| 604 | } | |
| 605 | return c.emit(map[string]any{"frequency": freq, "next_run": next}, func(w io.Writer) { | |
| 606 | if next == "" { | |
| 607 | fmt.Fprintf(w, "%s scans %s once scanning is on\n", repo.Path(), freq) | |
| 608 | return | |
| 609 | } | |
| 610 | fmt.Fprintf(w, "%s scans %s; next scan %s\n", repo.Path(), freq, next) | |
| 611 | }) | |
| 612 | } | |
| 613 | ||
| 571 | 614 | // maxSecurityList bounds the ignore and exclude-rule settings. |
| 572 | 615 | const maxSecurityList = 50 |
| 573 | 616 | |
internal/control/security_test.go +52 −2
| @@ -7,6 +7,7 @@ import ( | ||
| 7 | 7 | "path/filepath" |
| 8 | 8 | "strings" |
| 9 | 9 | "testing" |
| 10 | "time" | |
| 10 | 11 | |
| 11 | 12 | "gitbay.org/gitbay/internal/ci" |
| 12 | 13 | "gitbay.org/gitbay/internal/codescan" |
| @@ -24,7 +25,8 @@ const testSARIF = `{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"opengre | ||
| 24 | 25 | func TestSecurityScanRoundTrip(t *testing.T) { |
| 25 | 26 | st, repo, u, root := browseFixture(t) |
| 26 | 27 | p := repo.Path() |
| 27 | sec := config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: config.DefaultSecuritySteps} | |
| 28 | sec := config.Default().Security | |
| 29 | sec.Image = "localhost/gitbay-security:1" | |
| 28 | 30 | |
| 29 | 31 | c, out := runnerCtx(st, u.ID, root) |
| 30 | 32 | if code := runSecurityEnable(c, []string{p}); code != protocol.ExitFailure || !strings.Contains(out.String(), "no code scanning image") { |
| @@ -202,7 +204,8 @@ const twoFindings = `{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"openg | ||
| 202 | 204 | func TestSecurityDismiss(t *testing.T) { |
| 203 | 205 | st, repo, u, root := browseFixture(t) |
| 204 | 206 | p := repo.Path() |
| 205 | sec := config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: config.DefaultSecuritySteps} | |
| 207 | sec := config.Default().Security | |
| 208 | sec.Image = "localhost/gitbay-security:1" | |
| 206 | 209 | run := func(f func(*Ctx, []string) int, stdin string, args ...string) (int, string) { |
| 207 | 210 | t.Helper() |
| 208 | 211 | c, out := runnerCtx(st, u.ID, root) |
| @@ -310,3 +313,50 @@ func TestDefaultBranchRefusesOptionShape(t *testing.T) { | ||
| 310 | 313 | t.Errorf("a normal branch: exit %d: %s", code, out) |
| 311 | 314 | } |
| 312 | 315 | } |
| 316 | ||
| 317 | func TestSecurityFrequency(t *testing.T) { | |
| 318 | st, repo, u, root := browseFixture(t) | |
| 319 | p := repo.Path() | |
| 320 | sec := config.Default().Security | |
| 321 | sec.Image = "localhost/gitbay-security:1" | |
| 322 | run := func(f func(*Ctx, []string) int, args ...string) (int, string) { | |
| 323 | t.Helper() | |
| 324 | c, out := runnerCtx(st, u.ID, root) | |
| 325 | c.Cfg.Security = sec | |
| 326 | c.JSON = true | |
| 327 | return f(c, args), out.String() | |
| 328 | } | |
| 329 | // Before scanning is on the choice is kept and nothing is scheduled. | |
| 330 | if code, out := run(runSecurityFrequency, p, "monthly"); code != protocol.ExitOK || !strings.Contains(out, `"next_run":""`) { | |
| 331 | t.Fatalf("monthly while off: %d %s", code, out) | |
| 332 | } | |
| 333 | run(runSecurityEnable, p) | |
| 334 | if sc, _ := st.SecurityScanFor(repo.ID); !strings.Contains(sc.NextRun, "-01T04:00:00Z") { | |
| 335 | t.Fatalf("enable ignored the monthly choice: %q", sc.NextRun) | |
| 336 | } | |
| 337 | code, out := run(runSecurityFrequency, p, "weekly") | |
| 338 | if code != protocol.ExitOK { | |
| 339 | t.Fatalf("weekly: %d %s", code, out) | |
| 340 | } | |
| 341 | sc, _ := st.SecurityScanFor(repo.ID) | |
| 342 | next, err := time.Parse("2006-01-02T15:04:05Z", sc.NextRun) | |
| 343 | if err != nil || next.Weekday() != time.Sunday || next.Hour() != 4 || !strings.Contains(out, sc.NextRun) { | |
| 344 | t.Fatalf("next run %q (%v), out %s", sc.NextRun, err, out) | |
| 345 | } | |
| 346 | if _, out := run(runSecurityStatus, p); !strings.Contains(out, `"frequency":"weekly"`) { | |
| 347 | t.Fatalf("status: %s", out) | |
| 348 | } | |
| 349 | if code, _ := run(runSecurityFrequency, p, "hourly"); code != protocol.ExitUsage { | |
| 350 | t.Fatalf("hourly: exit %d", code) | |
| 351 | } | |
| 352 | // Narrowed: daily is refused, and the stored monthly scans weekly. | |
| 353 | sec.LeastOften, sec.Default, sec.MostOften = "weekly", "weekly", "weekly" | |
| 354 | if code, out := run(runSecurityFrequency, p, "daily"); code != protocol.ExitFailure || !strings.Contains(out, "scans weekly") { | |
| 355 | t.Fatalf("daily outside the range: %d %s", code, out) | |
| 356 | } | |
| 357 | sec.MostOften, sec.LeastOften = "daily", "daily" | |
| 358 | sec.Default = "daily" | |
| 359 | if _, out := run(runSecurityStatus, p); !strings.Contains(out, `"frequency":"daily"`) { | |
| 360 | t.Fatalf("weekly outside the range: %s", out) | |
| 361 | } | |
| 362 | } | |
internal/httpd/depspage_test.go +25
| @@ -75,6 +75,31 @@ func TestSettingsPageRendersSecurityTriage(t *testing.T) { | ||
| 75 | 75 | } |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | // The frequency dropdown offers what the instance allows, with the | |
| 79 | // repository's frequency selected. | |
| 80 | func TestSettingsPageRendersScanFrequency(t *testing.T) { | |
| 81 | var sb strings.Builder | |
| 82 | err := web.Render(&sb, "settings.html", settingsPage{ | |
| 83 | repoPage: testRepoPage(), ScanAvailable: true, | |
| 84 | ScanFrequencies: []string{"weekly", "monthly"}, ScanFrequency: "weekly", ScanHour: 4, | |
| 85 | }) | |
| 86 | if err != nil { | |
| 87 | t.Fatal(err) | |
| 88 | } | |
| 89 | page := sb.String() | |
| 90 | for _, want := range []string{ | |
| 91 | `value="security-frequency"`, `<option value="weekly" selected>weekly</option>`, | |
| 92 | `<option value="monthly">monthly</option>`, "04:00 UTC", | |
| 93 | } { | |
| 94 | if !strings.Contains(page, want) { | |
| 95 | t.Errorf("settings page does not show %q", want) | |
| 96 | } | |
| 97 | } | |
| 98 | if strings.Contains(page, `value="daily"`) { | |
| 99 | t.Error("offers a frequency the instance does not allow") | |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 78 | 103 | // The advisories offer dismissal, the dismissed ones reopening, and the |
| 79 | 104 | // ones nothing imports are named. |
| 80 | 105 | func TestSettingsPageRendersAdvisoryTriage(t *testing.T) { |
internal/httpd/settings.go +20 −10
| @@ -26,14 +26,19 @@ type settingsPage struct { | ||
| 26 | 26 | // ScanAvailable is whether the instance has a code scanning image; |
| 27 | 27 | // without one the toggle would only ever be refused. |
| 28 | 28 | ScanAvailable bool |
| 29 | Runners []store.RepoRunner | |
| 30 | Access []accessRow | |
| 31 | Hooks []hookRow | |
| 32 | Deliveries []deliveryRow | |
| 33 | Notice string | |
| 34 | Saved bool | |
| 35 | Reauth bool // Notice is the stale-session refusal: link to sign in | |
| 36 | Submitted map[string]string | |
| 29 | // ScanFrequencies are the frequencies the instance allows, ScanFrequency | |
| 30 | // the one this repository scans at, and ScanHour the hour UTC. | |
| 31 | ScanFrequencies []string | |
| 32 | ScanFrequency string | |
| 33 | ScanHour int | |
| 34 | Runners []store.RepoRunner | |
| 35 | Access []accessRow | |
| 36 | Hooks []hookRow | |
| 37 | Deliveries []deliveryRow | |
| 38 | Notice string | |
| 39 | Saved bool | |
| 40 | Reauth bool // Notice is the stale-session refusal: link to sign in | |
| 41 | Submitted map[string]string | |
| 37 | 42 | } |
| 38 | 43 | |
| 39 | 44 | type accessRow struct { |
| @@ -113,8 +118,11 @@ func (s *Server) settingsFormWith(w http.ResponseWriter, r *http.Request, u stor | ||
| 113 | 118 | repoPage: p, Topics: topics, Branches: branches, |
| 114 | 119 | DepsEnabled: deps.Enabled, Deps: deps, |
| 115 | 120 | Security: security, ScanAvailable: s.cfg.Security.Image != "", |
| 116 | Runners: runners, | |
| 117 | Access: access, Hooks: hooks, Deliveries: deliveries, | |
| 121 | ScanFrequencies: s.cfg.Security.Frequencies(), | |
| 122 | ScanFrequency: s.cfg.Security.Frequency(repo.Settings.SecurityFrequency), | |
| 123 | ScanHour: s.cfg.Security.Hour, | |
| 124 | Runners: runners, | |
| 125 | Access: access, Hooks: hooks, Deliveries: deliveries, | |
| 118 | 126 | Notice: notice, |
| 119 | 127 | Saved: strings.HasPrefix(notice, "Saved "), |
| 120 | 128 | Reauth: s.reauthNotice(w, notice, r.URL.Path), |
| @@ -169,6 +177,8 @@ func (s *Server) settingsSubmit(w http.ResponseWriter, r *http.Request, u store. | ||
| 169 | 177 | verb = "enable" |
| 170 | 178 | } |
| 171 | 179 | argv = []string{"repo", "security", verb, repo} |
| 180 | case "security-frequency": | |
| 181 | argv = []string{"repo", "settings", "security-frequency", repo, v("security-frequency")} | |
| 172 | 182 | case "deps-dismiss": |
| 173 | 183 | argv = []string{"repo", "deps", "dismiss", repo, v("advisory"), "--reason", v("reason")} |
| 174 | 184 | case "deps-reopen": |
internal/store/repos.go +3
| @@ -46,6 +46,9 @@ type RepoSettings struct { | ||
| 46 | 46 | // or families, whose code scanning findings are set aside. |
| 47 | 47 | SecurityIgnore []string `json:"security_ignore,omitempty"` |
| 48 | 48 | SecurityExcludeRules []string `json:"security_exclude_rules,omitempty"` |
| 49 | // SecurityFrequency is the code scan frequency chosen, empty for the | |
| 50 | // instance default. | |
| 51 | SecurityFrequency string `json:"security_frequency,omitempty"` | |
| 49 | 52 | } |
| 50 | 53 | |
| 51 | 54 | // Path returns the canonical owner/name form. |
internal/web/templates/settings.html +6
| @@ -180,6 +180,12 @@ | ||
| 180 | 180 | <div class="check"><input type="checkbox" id="security-enabled" name="security" value="on"{{if .Security.Enabled}} checked{{end}}></div> |
| 181 | 181 | <div><button type="submit" class="btn">Save</button></div> |
| 182 | 182 | </form> |
| 183 | {{if .ScanAvailable}}<form method="post" action="{{$base}}" class="setform"> | |
| 184 | <input type="hidden" name="field" value="security-frequency"> | |
| 185 | <div><label for="security-frequency">Frequency</label><p class="hint">Daily, weekly on Sunday, or monthly on the 1st, at {{printf "%02d:00" .ScanHour}} UTC.</p></div> | |
| 186 | <div><select id="security-frequency" name="security-frequency">{{$freq := .ScanFrequency}}{{range .ScanFrequencies}}<option value="{{.}}"{{if eq . $freq}} selected{{end}}>{{.}}</option>{{end}}</select></div> | |
| 187 | <div><button type="submit" class="btn">Save</button></div> | |
| 188 | </form>{{end}} | |
| 183 | 189 | {{if .Security.Enabled}} |
| 184 | 190 | <p class="meta">Next scan {{.Security.NextRun}} · last results {{if .Security.LastResults}}{{when .Security.LastResults}}{{else}}never{{end}}{{if .Security.IssueNumber}} · <a href="/{{.Repo.OwnerName}}/{{.Repo.Name}}/issues/{{.Security.IssueNumber}}">#{{.Security.IssueNumber}}</a>{{end}}</p> |
| 185 | 191 | {{with .Security.Aside}}{{if or .Ignored .Unused}}<p class="meta">Set aside by the last scan:{{if .Ignored}} {{.Ignored}} under the paths or rules below, or marked vendored or generated in <code>.gitattributes</code>.{{end}}{{range $lib, $n := .Unused}} {{$n}} from {{$lib}} rules, which no manifest names.{{end}}</p>{{end}}{{end}} |