Skip to content

ci(pre-commit): pinned gosec hook on changed packages (closes #1374) - #1376

Merged
cristim merged 3 commits into
mainfrom
ci/pre-commit-gosec-hook
Jul 16, 2026
Merged

ci(pre-commit): pinned gosec hook on changed packages (closes #1374)#1376
cristim merged 3 commits into
mainfrom
ci/pre-commit-gosec-hook

Conversation

@cristim

@cristim cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member

What / Why

Replaces the single whole-repo `gosec ./...` invocation in the pre-commit hook with a script (`scripts/gosec-hook.sh`) that:

  • Pins gosec v2.26.1 -- matches the `securego/gosec@4a3bd8af` SHA in `ci.yml`. Auto-installed to `~/.cache/pre-commit-gosec/v2.26.1/gosec` via `go install` on first use; version detected with `go version -m` so the "dev" string that `go install` embeds does not fool the check. Never modifies the system-wide gosec binary.
  • Per-changed-package scoping -- reads the staged `.go` file paths passed by pre-commit (`pass_filenames: true`), maps each file to its owning Go module (root / `pkg/` / `providers/aws` / `providers/azure` / `providers/gcp`) via prefix matching, deduplicates package paths, then runs gosec once per affected module. Fast: a single-file change takes ~0.4 s; two-module change ~1.4 s.
  • Same `-exclude=` rule list as the previous hook (G101, G104, G115, G117, G118, G122, G204, G301, G304, G402, G505, G702, G703, G705, G706) so local and CI pre-commit verdicts remain aligned.
  • Handles edge cases: deleted files are skipped (checked for existence before scanning); `testdata/` paths are filtered out; no staged `.go` files exits 0 cleanly.
  • Exits 1 on any finding; exits 0 on clean.

Fail-test proof

  1. Created `gosec_test_finding.go` at repo root importing `crypto/md5` (G501, not excluded).
  2. Ran `bash scripts/gosec-hook.sh gosec_test_finding.go` -- exited 1 with the G501 finding printed.
  3. Removed the scratch file; re-ran on `cmd/multi_service_csv.go providers/aws/service_client.go` -- exited 0 in 1.4 s.
  4. `pre-commit run gosec --files cmd/multi_service_csv.go providers/aws/service_client.go` -- Passed.

Gate results

Test Files Duration Exit
Clean (cmd + providers/aws) 2 files, 2 modules 1.4 s 0
Deliberate G501 finding 1 file, root module 0.4 s 1
Post-cleanup clean pass 2 files, 2 modules 1.4 s 0
pre-commit integration gosec hook only - Passed

Notes

PR #1363 (in-flight) changes the CI `security-scan` job to loop all modules with gosec v2.26.1. This hook is already aligned: same version pin, same per-module approach, same exclusion list.

Summary by CodeRabbit

  • Chores
    • Improved pre-commit security scanning to target only Go packages affected by staged changes (scoped by owning module).
    • Added a dedicated pre-commit hook script to replace the previous whole-repo scanner invocation.
    • Pinned the scanner version and added automatic, per-user caching for faster/consistent runs.
    • Standardized exclusions and reduced noise by skipping deleted files, test data, and vendor content.
    • Hooks now exit cleanly when no applicable Go files are staged.

@cristim cristim added triaged Item has been triaged priority/p3 Polish / idea / may never ship severity/low Minor harm urgency/this-quarter Within the quarter impact/internal Team-internal only effort/s Hours type/chore Maintenance / non-user-visible labels Jul 16, 2026
@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 18 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 50f7e23b-3b35-4061-8b5a-019c5b6753f5

📥 Commits

Reviewing files that changed from the base of the PR and between 21a58d5 and aabd957.

📒 Files selected for processing (3)
  • .pre-commit-config.yaml
  • scripts/gosec-hook.sh
  • terraform/environments/azure/ci-cd-permissions/sp.tf
📝 Walkthrough

Walkthrough

The pre-commit configuration now delegates gosec scanning to a script that filters staged Go files, resolves owning modules and packages, ensures gosec v2.28.0 is available, and scans each affected module. A Terraform resource also receives a formatting-only change.

Changes

Changed-package gosec scanning

Layer / File(s) Summary
Hook entry and gosec setup
.pre-commit-config.yaml, scripts/gosec-hook.sh
The hook passes staged filenames to the centralized script, which configures exclusions and validates or installs the pinned gosec binary.
Affected package resolution
scripts/gosec-hook.sh
Deleted files and testdata/ paths are filtered, remaining files are mapped to owning modules and packages, and duplicate targets are removed.
Per-module gosec execution
scripts/gosec-hook.sh
gosec runs once per owning module with directory and rule exclusions, returning an aggregated failure status.

Terraform formatting

Layer / File(s) Summary
Federated identity resource formatting
terraform/environments/azure/ci-cd-permissions/sp.tf
The resource block ending changes line structure without changing its configured values.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PreCommit
  participant GosecHook
  participant GoModules
  participant Gosec
  PreCommit->>GosecHook: Pass staged filenames
  GosecHook->>Gosec: Ensure pinned cached binary
  GosecHook->>GoModules: Resolve owning module packages
  GosecHook->>Gosec: Scan affected packages per module
  Gosec-->>PreCommit: Return aggregated status
Loading

Possibly related issues

  • Issue 1374 — Covers the same pinned pre-commit gosec scanning of changed packages across multiple modules.
  • Issue 1382 — Relates to the gosec version pin maintained by the new hook script.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: switching the pre-commit gosec hook to scan changed packages via a pinned script.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ci/pre-commit-gosec-hook

Comment @coderabbitai help to get the list of available commands.

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

PR #1382 bumped the CI pin to gosec v2.28.0. When this PR lands, please align the pin in scripts/gosec-hook.sh (or whichever file contains it) to v2.28.0 as well, so both CI and the pre-commit hook run the same version.

@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

Aligned in ea75569: the hook pin in scripts/gosec-hook.sh (GOSEC_VERSION) and the config comment are now v2.28.0, matching the CI pin. Re-verified with v2.28.0: auto-install into ~/.cache/pre-commit-gosec/v2.28.0/ works, clean pass on cmd/ + pkg/ + providers/aws (exit 0, ~1.6 s warm), deliberate G501 scratch file fails with exit 1, clean pass restored after removal.

@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@scripts/gosec-hook.sh`:
- Around line 40-42: Update the MODULE_DIRS declaration in scripts/gosec-hook.sh
to include tests/e2e as a recognized module root, preserving longest-prefix
ordering so staged files there are scanned relative to their standalone Go
module.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 840851e4-c6c2-4081-8bb1-87ce8ec036a7

📥 Commits

Reviewing files that changed from the base of the PR and between 157f9e2 and ea75569.

📒 Files selected for processing (2)
  • .pre-commit-config.yaml
  • scripts/gosec-hook.sh

Comment thread scripts/gosec-hook.sh Outdated
cristim added a commit that referenced this pull request Jul 16, 2026
Address the Major CR finding on #1376 and the red pre-commit CI job.

- scripts/gosec-hook.sh: add tests/e2e to MODULE_DIRS. It is a standalone
  Go module (tests/e2e/go.mod) but the hook only knew providers/* and pkg,
  so staged files there were scanned from the repo root as ./tests/e2e,
  breaking module/package resolution. Now mirrors the per-module loop in
  ci.yml (root, pkg, providers/{aws,azure,gcp}, tests/e2e).

- terraform/environments/azure/ci-cd-permissions/sp.tf: strip the extra
  trailing blank line so the file ends with a single newline. The
  end-of-file-fixer pre-commit hook runs --all-files in CI and flagged
  this pre-existing whitespace on every PR.

The pre-commit gocyclo failure is pre-existing repo-wide debt (four AWS
service-client functions at complexity 11, below golangci's threshold of
15 but above the hook's -over 10). Not touched by this PR; tracked in the
follow-up issue.
@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

Pushed 21a58d5 addressing the red pre-commit CI job:

Also fixed the Major CR finding (tests/e2e module registration) - see thread reply.

@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

cristim added a commit that referenced this pull request Jul 16, 2026
Address the Major CR finding on #1376 and the red pre-commit CI job.

- scripts/gosec-hook.sh: add tests/e2e to MODULE_DIRS. It is a standalone
  Go module (tests/e2e/go.mod) but the hook only knew providers/* and pkg,
  so staged files there were scanned from the repo root as ./tests/e2e,
  breaking module/package resolution. Now mirrors the per-module loop in
  ci.yml (root, pkg, providers/{aws,azure,gcp}, tests/e2e).

- terraform/environments/azure/ci-cd-permissions/sp.tf: strip the extra
  trailing blank line so the file ends with a single newline. The
  end-of-file-fixer pre-commit hook runs --all-files in CI and flagged
  this pre-existing whitespace on every PR.

The pre-commit gocyclo failure is pre-existing repo-wide debt (four AWS
service-client functions at complexity 11, below golangci's threshold of
15 but above the hook's -over 10). Not touched by this PR; tracked in the
follow-up issue.
@cristim
cristim force-pushed the ci/pre-commit-gosec-hook branch from 21a58d5 to 5302dea Compare July 16, 2026 18:31
@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

Rebased onto main (post-#1389, which fixes the gocyclo -over 10 debt from #1207). gocyclo now 0 violations; the Lint Code + pre-commit gocyclo step should be green on this head.

@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

cristim added 3 commits July 16, 2026 21:42
Replace the whole-repo gosec invocation with a per-module,
per-changed-package script (scripts/gosec-hook.sh) that:

- Pins gosec v2.26.1 (matches securego/gosec SHA in ci.yml); auto-installs
  to ~/.cache/pre-commit-gosec/v2.26.1/gosec on first use via go install,
  detected via go version -m so the "dev" string from go install does not
  fool the version check.
- Accepts staged .go file paths from pre-commit (pass_filenames: true),
  resolves each to its owning Go module (root / pkg / providers/aws /
  providers/azure / providers/gcp), then runs gosec once per affected module
  scanning only the packages that contain changed files.
- Skips deleted files and testdata/ paths cleanly.
- Same -exclude= rule list as the previous hook; keeps local and CI
  pre-commit verdicts aligned.
- Exits nonzero on any finding; exits 0 when no live .go files are staged.

Tested: clean pass on cmd/ + providers/aws/ in ~1.4 s; deliberate G501
(crypto/md5 import) correctly fails with exit 1; clean pass restored after
removing the scratch file.
PR #1384 bumps the CI Security Scanning gosec pin to v2.28.0; align the
pre-commit hook so local and CI verdicts stay in agreement. Updates the
GOSEC_VERSION pin in scripts/gosec-hook.sh and the doc mentions in the
hook config comment.

Re-verified with v2.28.0: auto-install to the versioned cache dir works;
clean pass on cmd/ + pkg/ + providers/aws in ~1.6 s warm; deliberate G501
(crypto/md5) scratch file fails with exit 1; clean pass restored after
removal.
Address the Major CR finding on #1376 and the red pre-commit CI job.

- scripts/gosec-hook.sh: add tests/e2e to MODULE_DIRS. It is a standalone
  Go module (tests/e2e/go.mod) but the hook only knew providers/* and pkg,
  so staged files there were scanned from the repo root as ./tests/e2e,
  breaking module/package resolution. Now mirrors the per-module loop in
  ci.yml (root, pkg, providers/{aws,azure,gcp}, tests/e2e).

- terraform/environments/azure/ci-cd-permissions/sp.tf: strip the extra
  trailing blank line so the file ends with a single newline. The
  end-of-file-fixer pre-commit hook runs --all-files in CI and flagged
  this pre-existing whitespace on every PR.

The pre-commit gocyclo failure is pre-existing repo-wide debt (four AWS
service-client functions at complexity 11, below golangci's threshold of
15 but above the hook's -over 10). Not touched by this PR; tracked in the
follow-up issue.
@cristim
cristim force-pushed the ci/pre-commit-gosec-hook branch from 5302dea to aabd957 Compare July 16, 2026 18:42
@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

Rebased onto main (post-#1364, Lint now green on main); should be fully green on this head.

@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cristim
cristim merged commit cd6562e into main Jul 16, 2026
18 checks passed
@cristim
cristim deleted the ci/pre-commit-gosec-hook branch July 16, 2026 18:59
@cristim

cristim commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

Merged to main (closes #1374): a pinned (gosec v2.28.0), per-changed-package, module-aware pre-commit hook that surfaces gosec findings at commit time instead of first failing in CI. Verified it blocks a commit on a deliberate G501 finding and passes clean otherwise. First fully-green PR post-#1364 (all jobs green, no debt exception). Note: the hook currently applies a -exclude rule set that CI does not; aligning both to one shared source is tracked in #1394.

cristim added a commit that referenced this pull request Jul 17, 2026
The latest securego/gosec release is v2.28.0. Update the self-install
in the Security Scanning job so CI runs the current version.

PR #1376 (pre-commit gosec hook) is still open; a comment has been
posted there asking to align its pin to v2.28.0 when it lands.
cristim added a commit that referenced this pull request Jul 17, 2026
…#1382) (#1384)

* ci: bump gosec pin from v2.26.1 to v2.28.0

The latest securego/gosec release is v2.28.0. Update the self-install
in the Security Scanning job so CI runs the current version.

PR #1376 (pre-commit gosec hook) is still open; a comment has been
posted there asking to align its pin to v2.28.0 when it lands.

* ci: retire 10 #nosec annotations obsolete in gosec v2.28.0

Tested each annotation by temporarily removing it and running gosec
v2.28.0 on the owning module. Findings that no longer appear indicate
the rule was removed or no longer triggers on the pattern.

Retired (finding gone in v2.28.0):
- G201 x6: postgres_analytics.go (x4) and analytics_postgres.go (x2)
  -- gosec v2.28.0 removed the G201 SQL-format-string rule entirely
- G101 x2: email/templates.go -- gosec no longer flags email copy text
  containing the word "password" as a hardcoded credential
- G101 x1: credentials/resolver.go:27 (azure_client_secret constant) --
  gosec no longer flags this string; sibling GCP constants still flagged
- G122 x1 (partial): deploy/frontend.go annotation updated from
  G304,G122 to G304 -- G122 is not a recognized rule in v2.28.0

Kept (finding still returned after removing the annotation):
- G302,G304: pkg/common/audit.go -- 0644 file permission + path variable
- G404: pkg/retry/exponential.go and providers/aws/recommendations/ratelimiter.go
- G115, G101, G704, G703, G304, G204, G117, G505, G706, G104, G703,
  G705: all remaining annotations (verified per-module)

Full verification: gosec v2.28.0 on all 6 modules -> 0 findings;
go build ./... and go vet ./... clean; tests pass on all touched packages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/s Hours impact/internal Team-internal only priority/p3 Polish / idea / may never ship severity/low Minor harm triaged Item has been triaged type/chore Maintenance / non-user-visible urgency/this-quarter Within the quarter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant