Skip to content

Commit 5a1b8ed

Browse files
Merge pull request #139 from ldornele/HYPERFLEET-1019
HYPERFLEET-1019 - docs: add LeakTK secret scanning to pre-commit hooks
2 parents 652cdfb + 4328695 commit 5a1b8ed

1 file changed

Lines changed: 270 additions & 10 deletions

File tree

hyperfleet/docs/pre-commit-hooks.md

Lines changed: 270 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
Status: Active
33
Owner: HyperFleet Platform Team
4-
Last Updated: 2026-04-27
4+
Last Updated: 2026-05-14
55
---
66

77
# Pre-Commit Hooks Setup Guide
@@ -24,7 +24,7 @@ Last Updated: 2026-04-27
2424

2525
## Overview
2626

27-
HyperFleet uses a **centralized hook registry** at [`hyperfleet-hooks`](https://github.com/openshift-hyperfleet/hyperfleet-hooks) to enforce consistent commit message format and code quality across all repositories. The [pre-commit](https://pre-commit.com/) framework automatically downloads, builds, and caches hook binaries — no manual installation of individual tools is required.
27+
HyperFleet uses a **centralized hook registry** at [`hyperfleet-hooks`](https://github.com/openshift-hyperfleet/hyperfleet-hooks) to enforce consistent commit message format and code quality across all repositories. Additionally, **LeakTK** provides secret scanning to prevent credentials and API keys from being committed. The [pre-commit](https://pre-commit.com/) framework automatically downloads, builds, and caches hook binaries — no manual installation of individual tools is required.
2828

2929
### How It Works
3030

@@ -53,13 +53,34 @@ When a developer runs `git commit`, the pre-commit framework intercepts the oper
5353
pip install pre-commit
5454
```
5555

56-
- Go 1.25+ (for the `commitlint` hook built automatically by pre-commit on first run)
56+
- Go 1.25+ (for the `commitlint` hook and LeakTK secret scanning — both built automatically by pre-commit on first run)
5757
- `make` targets (`lint`, `gofmt`, `go-vet`) in the consuming repo (for Go tooling hooks)
5858

59+
**LeakTK builds with `CGO_ENABLED=0` by default** — no system dependencies required. This works on all platforms (Linux, macOS, Windows).
60+
61+
<details>
62+
<summary>Optional: Building LeakTK with CGO enabled</summary>
63+
64+
If you need CGO support, install the following system dependencies before running `make install-hooks`:
65+
66+
```bash
67+
# Fedora/RHEL
68+
sudo dnf install btrfs-progs-devel
69+
70+
# Ubuntu/Debian
71+
sudo apt install libbtrfs-dev
72+
```
73+
74+
Then set `CGO_ENABLED=1` in your environment.
75+
76+
</details>
77+
5978
---
6079

6180
## Available Hooks
6281

82+
### HyperFleet Hooks
83+
6384
All hooks are defined in the [`hyperfleet-hooks`](https://github.com/openshift-hyperfleet/hyperfleet-hooks) repository:
6485

6586
| Hook ID | Stage | Language | Description |
@@ -71,6 +92,22 @@ All hooks are defined in the [`hyperfleet-hooks`](https://github.com/openshift-h
7192

7293
The Go tooling hooks use `language: system` and delegate to existing Make targets rather than reimplementing tool resolution. This leverages each repo's [bingo](https://github.com/bwplotka/bingo)-managed tool versions (see [dependency pinning standard](https://github.com/openshift-hyperfleet/architecture/blob/main/hyperfleet/standards/dependency-pinning.md)).
7394

95+
### Secret Scanning Hook
96+
97+
**LeakTK** provides secret scanning to prevent credentials, API keys, and other secrets from being committed:
98+
99+
| Hook ID | Stage | Language | Description |
100+
|---------|-------|----------|-------------|
101+
| `leaktk.git.pre-commit` | `pre-commit` | `golang` | LeakTK: Scans staged files for secrets using Gitleaks engine with Red Hat-specific patterns |
102+
103+
**Key features**:
104+
-**Open-source** — no VPN requirement, works for all contributors
105+
-**Developed by Red Hat InfoSec** — same team that created rh-pre-commit
106+
-**Gitleaks-powered** — uses proven secret detection patterns
107+
- ⏱️ **First-time compilation** — takes 3-5 minutes on first commit, then cached
108+
109+
See the [Secret Scanning Migration](#secret-scanning-migration-rh-pre-commit-leaktk) section for migration details.
110+
74111
---
75112

76113
## Standard Configuration
@@ -83,6 +120,13 @@ All HyperFleet repositories **SHOULD** use the same `.pre-commit-config.yaml`. T
83120
default_install_hook_types: [pre-commit, commit-msg]
84121

85122
repos:
123+
# Secret scanning
124+
- repo: https://github.com/leaktk/leaktk
125+
rev: v0.3.2 # Check https://github.com/leaktk/leaktk/releases for latest
126+
hooks:
127+
- id: leaktk.git.pre-commit
128+
129+
# HyperFleet code quality hooks
86130
- repo: https://github.com/openshift-hyperfleet/hyperfleet-hooks
87131
rev: v0.1.1 # pin to a specific tag
88132
hooks:
@@ -105,17 +149,27 @@ The file hygiene hooks (`trailing-whitespace`, `end-of-file-fixer`, `check-added
105149

106150
> **Note:** `default_install_hook_types: [pre-commit, commit-msg]` means a single `pre-commit install` command installs hooks for **both** the `pre-commit` and `commit-msg` stages. Without this setting, you would need to run `pre-commit install --hook-type commit-msg` separately to enable commit message validation.
107151

152+
> **Important:** On the **first commit** after running `make install-hooks`, LeakTK will compile from source (3-5 minutes). Subsequent commits use the cached binary and run instantly.
153+
108154
---
109155

110156
## Migration Guide
111157

158+
This section covers two migration scenarios:
159+
1. **Adding Pre-commit Hooks** — adding hooks to an existing HyperFleet repository
160+
2. **Secret Scanning Migration** — migrating from rh-pre-commit to LeakTK
161+
162+
---
163+
164+
### Adding Pre-commit Hooks to a Repository
165+
112166
Follow these steps to add pre-commit hooks to an existing HyperFleet repository.
113167

114-
### Step 1: Add `.pre-commit-config.yaml`
168+
#### Step 1: Add `.pre-commit-config.yaml`
115169

116170
Copy the [Standard Configuration](#standard-configuration) into a `.pre-commit-config.yaml` file in the repo root.
117171

118-
### Step 2: Add `install-hooks` Makefile target
172+
#### Step 2: Add `install-hooks` Makefile target
119173

120174
Add the target to your Makefile (see [Makefile Conventions — Optional Targets](https://github.com/openshift-hyperfleet/architecture/blob/main/hyperfleet/standards/makefile-conventions.md#optional-targets)):
121175

@@ -125,7 +179,7 @@ install-hooks: ## Install pre-commit hooks
125179
pre-commit install
126180
```
127181

128-
### Step 3: Add Make aliases for Go tooling hooks (Go repos only)
182+
#### Step 3: Add Make aliases for Go tooling hooks (Go repos only)
129183

130184
The Go tooling hooks expect `make gofmt` and `make go-vet` targets. If your repo uses different names (e.g., `fmt` and `vet`), add aliases:
131185

@@ -137,7 +191,7 @@ gofmt: fmt ## Alias for fmt
137191
go-vet: vet ## Alias for vet
138192
```
139193

140-
### Step 4: Update documentation
194+
#### Step 4: Update documentation
141195

142196
Add `pre-commit` to the prerequisites section in your `README.md`:
143197

@@ -161,7 +215,7 @@ Add the hook installation step to your getting started section or `CONTRIBUTING.
161215
make install-hooks
162216
```
163217

164-
### Step 5: Install and verify
218+
#### Step 5: Install and verify
165219

166220
```bash
167221
make install-hooks
@@ -180,7 +234,7 @@ Test with an invalid commit (should fail):
180234
git commit --allow-empty -m "bad commit message"
181235
```
182236

183-
### Step 6: Fix existing violations
237+
#### Step 6: Fix existing violations
184238

185239
Run hooks against the entire codebase to fix any pre-existing violations (trailing whitespace, missing EOF newlines, etc.). Without this step, the first contributor who touches an unrelated file with a trailing whitespace or missing newline gets a hook failure they didn't cause.
186240

@@ -195,7 +249,7 @@ git add -p
195249
git commit -m "HYPERFLEET-XXX - chore: fix pre-commit baseline violations"
196250
```
197251

198-
### Step 7: Commit the hook configuration
252+
#### Step 7: Commit the hook configuration
199253

200254
```bash
201255
git add .pre-commit-config.yaml Makefile README.md CONTRIBUTING.md
@@ -204,6 +258,167 @@ git commit -m "HYPERFLEET-XXX - chore: add pre-commit hooks"
204258

205259
---
206260

261+
### Secret Scanning Migration: rh-pre-commit → LeakTK
262+
263+
#### Why Migrate to LeakTK?
264+
265+
[**LeakTK**](https://github.com/leaktk/leaktk) is an open-source secret scanning toolkit developed by Red Hat's Information Security team — the same team that created rh-pre-commit.
266+
267+
**Key benefits over rh-pre-commit**:
268+
- ✅ **No VPN requirement** — works for Red Hat associates and external contributors
269+
- ✅ **Open-source** — MIT licensed, publicly accessible on GitHub
270+
- ✅ **Can be committed to repos** — configuration lives in repository files
271+
- ✅ **Enforces on all developers** — configuration in repo ensures everyone uses it
272+
- ✅ **Same detection engine** — both use Gitleaks with Red Hat-specific patterns (verified by InfoSec team)
273+
274+
#### Comparison: rh-pre-commit vs LeakTK
275+
276+
Both tools perform secret scanning using Gitleaks as the underlying engine. Here are the key differences:
277+
278+
| Feature | rh-pre-commit | LeakTK |
279+
|---------|--------------|---------|
280+
| **Secret scanning** | ✅ Gitleaks engine | ✅ Gitleaks engine |
281+
| **Red Hat patterns** | ✅ Customized patterns | ✅ Customized patterns |
282+
| **VPN required** | ⚠️ Only during installation | ❌ Never |
283+
| **Open-source** | ❌ Internal Red Hat tool | ✅ Public GitHub repository |
284+
| **Installation** | ✅ Pre-built binary | ⚠️ Compiles from source (first time only) |
285+
286+
287+
#### Migration Steps
288+
289+
**Step 1: Ensure system requirements**
290+
291+
Verify that all team members have Go 1.25+ installed:
292+
293+
```bash
294+
go version # Should show 1.25.0 or later
295+
```
296+
297+
**LeakTK builds with `CGO_ENABLED=0` by default** — no additional system dependencies required. This works on all platforms (Linux, macOS, Windows).
298+
299+
<details>
300+
<summary>Optional: If CGO support is needed</summary>
301+
302+
Install system dependencies before running `make install-hooks`:
303+
304+
```bash
305+
# Fedora/RHEL
306+
sudo dnf install btrfs-progs-devel
307+
308+
# Ubuntu/Debian
309+
sudo apt install libbtrfs-dev
310+
```
311+
312+
Then set `CGO_ENABLED=1` in your environment.
313+
314+
**Note:** macOS does not require btrfs dependencies — btrfs is a Linux-only filesystem and not available via Homebrew.
315+
316+
</details>
317+
318+
**Step 2: Update `.pre-commit-config.yaml`**
319+
320+
Replace the rh-pre-commit configuration with LeakTK:
321+
322+
```yaml
323+
# Before (rh-pre-commit)
324+
repos:
325+
- repo: https://gitlab.cee.redhat.com/infosec-public/developer-workbench/tools
326+
rev: rh-pre-commit-2.3.2
327+
hooks:
328+
- id: rh-pre-commit
329+
330+
# After (LeakTK)
331+
repos:
332+
- repo: https://github.com/leaktk/leaktk
333+
rev: v0.3.2 # Check https://github.com/leaktk/leaktk/releases for latest
334+
hooks:
335+
- id: leaktk.git.pre-commit
336+
```
337+
338+
**Step 3: Notify team**
339+
340+
Inform all developers that:
341+
1. System requirements must be met (Go 1.25+ only — no other dependencies needed)
342+
2. They should reinstall hooks: `make install-hooks`
343+
3. The **first commit** will take 3-5 minutes (one-time compilation)
344+
4. Subsequent commits will run instantly (cached binary)
345+
346+
**Step 4: Commit the migration**
347+
348+
```bash
349+
git add .pre-commit-config.yaml
350+
git commit -m "HYPERFLEET-XXX - chore: migrate from rh-pre-commit to LeakTK"
351+
```
352+
353+
**Step 5: Update CI/CD pipelines**
354+
355+
If your repository enforces hooks in CI, update your pipeline configuration to use LeakTK instead of rh-pre-commit. The following steps apply regardless of CI platform (Prow, GitHub Actions, GitLab CI, etc.):
356+
357+
1. **Ensure Go 1.25+ is available** in your CI environment
358+
- Verify with `go version` or install if needed
359+
360+
2. **Install pre-commit framework**:
361+
```bash
362+
pip install pre-commit
363+
```
364+
365+
3. **Run the repository's hook installation target** to ensure consistent setup:
366+
```bash
367+
make install-hooks
368+
```
369+
This mirrors the developer workflow and ensures pre-commit hooks are registered. The first run compiles LeakTK (3-5 minutes) — cache to avoid recompilation on subsequent runs.
370+
371+
4. **Cache the pre-commit environment** to avoid recompiling LeakTK on every run
372+
- Cache directory: `~/.cache/pre-commit/`
373+
- Cache key: tie to `.pre-commit-config.yaml` content (e.g., hash of the file)
374+
- This reduces subsequent runs from minutes to seconds
375+
376+
5. **Replace rh-pre-commit invocations** with pre-commit:
377+
```bash
378+
# Before (rh-pre-commit)
379+
rh-pre-commit run --all-files
380+
381+
# After (pre-commit with LeakTK)
382+
pre-commit run --all-files
383+
```
384+
385+
**Note:** If your repo uses the `make install-hooks` wrapper, you can also run hooks via `make` targets that delegate to pre-commit.
386+
387+
**Notes:**
388+
- The **first pipeline run** after migration compiles LeakTK (3-5 minutes). Subsequent runs use the cached binary.
389+
- **LeakTK builds with `CGO_ENABLED=0` by default** — no btrfs-progs-devel or other system dependencies required.
390+
- Search for `rh-pre-commit`, `LeakTK`, `make install-hooks`, and `pre-commit` in your CI configuration to locate and update all relevant pipeline steps.
391+
392+
#### What Changes After Migration
393+
394+
**During development**:
395+
```bash
396+
# First commit after migration (one-time)
397+
git commit -m "feat: add feature"
398+
# LeakTK compiles (3-5 minutes), then scans
399+
# ✅ Commit succeeds if no secrets found
400+
401+
# All subsequent commits
402+
git commit -m "feat: another feature"
403+
# LeakTK uses cached binary (instant)
404+
```
405+
406+
**Example output when a secret is detected**:
407+
```text
408+
leaktk.git.pre-commit
409+
┌─────────────────────────────────────────────────────────────────────────────┐
410+
│ 1 secret(s) detected in staged files │
411+
├─────────────────────────────────────────────────────────────────────────────┤
412+
│ config/database.yml:12 │
413+
│ Password = "admin123" # Generic Password │
414+
└─────────────────────────────────────────────────────────────────────────────┘
415+
```
416+
417+
The commit will be blocked until secrets are removed.
418+
419+
---
420+
421+
207422
## Troubleshooting
208423

209424
### `pre-commit: command not found`
@@ -229,6 +444,48 @@ Your Makefile needs the `gofmt` alias. Add it pointing to your existing formatti
229444
gofmt: fmt ## Alias for fmt
230445
```
231446

447+
### LeakTK compilation fails with "Go version too old"
448+
449+
LeakTK requires Go 1.25+:
450+
451+
```bash
452+
# Check version
453+
go version
454+
455+
# Update (Fedora/RHEL)
456+
sudo dnf update golang
457+
458+
# Or download from https://go.dev/dl/
459+
```
460+
461+
### LeakTK compilation fails with "btrfs/ioctl.h: No such file or directory"
462+
463+
This error occurs when LeakTK tries to build with CGO enabled but the btrfs development headers are missing.
464+
465+
**Solution 1 (Recommended):** Build without CGO — no system dependencies required:
466+
467+
```bash
468+
# Clear cached build and rebuild
469+
pre-commit clean
470+
CGO_ENABLED=0 pre-commit install
471+
```
472+
473+
**Solution 2:** Install btrfs development headers (Linux only):
474+
475+
```bash
476+
# Fedora/RHEL
477+
sudo dnf install btrfs-progs-devel
478+
479+
# Ubuntu/Debian
480+
sudo apt install libbtrfs-dev
481+
```
482+
483+
**Note:** macOS users should use Solution 1 — btrfs is Linux-only and not available on macOS.
484+
485+
### First commit takes 3-5 minutes
486+
487+
This is **expected behavior** on the first commit after installing LeakTK. The pre-commit framework is compiling LeakTK from source. Subsequent commits will use the cached binary and run instantly.
488+
232489
### Hook runs but uses wrong tool version
233490

234491
Pre-commit caches hook environments. Clear the cache and reinstall:
@@ -274,3 +531,6 @@ This updates the `rev` field in `.pre-commit-config.yaml` to the latest tag.
274531
- [pre-commit documentation](https://pre-commit.com/)
275532
- [hyperfleet-hooks repository](https://github.com/openshift-hyperfleet/hyperfleet-hooks)
276533
- [Sentinel pilot PR (hyperfleet-sentinel#102)](https://github.com/openshift-hyperfleet/hyperfleet-sentinel/pull/102) — reference implementation
534+
- [LeakTK GitHub Repository](https://github.com/leaktk/leaktk)
535+
- [Git Hooks Installation Guide](https://github.com/leaktk/leaktk/blob/main/docs/install_git_hooks.md)
536+
- [PR #248 - pre-commit.com integration](https://github.com/leaktk/leaktk/pull/248) ✅ Merged 2026-05-13

0 commit comments

Comments
 (0)