secure-husky-setup 1.0.10 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +126 -37
- package/package.json +1 -1
package/Readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
> One-command security and CI setup for any Node.js project.
|
|
4
4
|
|
|
5
|
-
Automatically configures **Gitleaks
|
|
5
|
+
Automatically configures **Gitleaks** (secret scanning), **SonarQube** (code quality), **Smoke Tests**, and **Newman API Tests** as git hooks — so your team catches secrets, bad code, and broken APIs before they ever reach your repository.
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -13,37 +13,92 @@ Automatically configures **Gitleaks**, **SonarQube**, **Smoke Tests**, and **New
|
|
|
13
13
|
| Pre-commit | Gitleaks | Scans staged files for hardcoded secrets, API keys, tokens |
|
|
14
14
|
| Pre-commit | SonarQube | Scans staged files for code quality issues, blocks on Quality Gate failure |
|
|
15
15
|
| Pre-push | Smoke Tests | Starts your server and runs `npm test` before every push |
|
|
16
|
-
| Pre-push | Newman | Runs your Postman collections automatically before every push |
|
|
16
|
+
| Pre-push | Newman | Runs your Postman API collections automatically before every push |
|
|
17
17
|
| GitHub Actions | All of the above | Runs the full CI pipeline on every push to any branch |
|
|
18
18
|
|
|
19
19
|
**All hooks run on git diff only** — only changed files are scanned, keeping commits and pushes fast.
|
|
20
20
|
|
|
21
21
|
---
|
|
22
22
|
|
|
23
|
+
## Requirements
|
|
24
|
+
|
|
25
|
+
Before installing this package, make sure your project has the following:
|
|
26
|
+
|
|
27
|
+
### 1. Git Repository (Required)
|
|
28
|
+
Your project must be initialized as a git repository. This package sets up git hooks which only work inside a git repo.
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
git init
|
|
32
|
+
git add .
|
|
33
|
+
git commit -m "initial commit"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
> ⚠️ If you skip this step, the package will fail with **"Not inside a git repository"** during installation.
|
|
37
|
+
|
|
38
|
+
### 2. `start` Script in package.json (Required for Smoke Tests)
|
|
39
|
+
The pre-push hook boots your server using `npm start`. Without it, smoke tests are skipped.
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
"scripts": {
|
|
43
|
+
"start": "node index.js"
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 3. `test` Script in package.json (Required for Smoke Tests)
|
|
48
|
+
The pre-push hook runs `npm test` to verify your app works. Without it, the test step is skipped.
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
"scripts": {
|
|
52
|
+
"test": "jest"
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
> ℹ️ If `start` or `test` scripts are missing, the package will warn you during init but will NOT block your workflow — it simply skips those steps.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
23
60
|
## Installation
|
|
24
61
|
|
|
62
|
+
### Step 1 — Initialize git (if not already done)
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
git init
|
|
66
|
+
git add .
|
|
67
|
+
git commit -m "initial commit"
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Step 2 — Install the package
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npm install secure-husky-setup
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Step 3 — Run init
|
|
77
|
+
|
|
25
78
|
```bash
|
|
26
79
|
npx secure-husky-setup init
|
|
27
80
|
```
|
|
28
81
|
|
|
29
|
-
That's it.
|
|
82
|
+
That's it. Everything is set up automatically — no manual configuration needed.
|
|
30
83
|
|
|
31
84
|
---
|
|
32
85
|
|
|
33
86
|
## What Gets Set Up Automatically
|
|
34
87
|
|
|
88
|
+
After running `init`, your project will have:
|
|
89
|
+
|
|
35
90
|
```
|
|
36
91
|
your-project/
|
|
37
92
|
├── .husky/
|
|
38
|
-
│ ├── pre-commit
|
|
39
|
-
│ └── pre-push
|
|
93
|
+
│ ├── pre-commit ← Gitleaks + SonarQube on staged files
|
|
94
|
+
│ └── pre-push ← Smoke Tests + Newman on changed files
|
|
40
95
|
├── .github/
|
|
41
96
|
│ └── workflows/
|
|
42
|
-
│ └── ci-tests.yml
|
|
97
|
+
│ └── ci-tests.yml ← GitHub Actions CI pipeline
|
|
43
98
|
├── scripts/
|
|
44
|
-
│ └── run-ci-checks.sh
|
|
99
|
+
│ └── run-ci-checks.sh ← Standalone CI script (can be run manually)
|
|
45
100
|
├── sonar-project.properties ← Auto-generated SonarQube config
|
|
46
|
-
└── .gitleaksignore ← Excludes false-positive files
|
|
101
|
+
└── .gitleaksignore ← Excludes false-positive files from Gitleaks
|
|
47
102
|
```
|
|
48
103
|
|
|
49
104
|
---
|
|
@@ -52,39 +107,46 @@ your-project/
|
|
|
52
107
|
|
|
53
108
|
### Pre-Commit Hook
|
|
54
109
|
Every time you run `git commit`, the hook:
|
|
55
|
-
1. Gets the
|
|
110
|
+
1. Gets only the staged files via `git diff --cached`
|
|
56
111
|
2. Copies staged files to a temp directory
|
|
57
|
-
3. Runs **Gitleaks**
|
|
58
|
-
4.
|
|
112
|
+
3. Runs **Gitleaks** — blocks commit if secrets found
|
|
113
|
+
4. Checks if SonarQube server is reachable — skips gracefully if not
|
|
114
|
+
5. Runs **SonarQube** on staged files only — blocks commit if Quality Gate fails
|
|
59
115
|
|
|
60
116
|
```
|
|
61
117
|
git commit
|
|
62
|
-
→ [Gitleaks] Secrets found?
|
|
63
|
-
→ [SonarQube]
|
|
64
|
-
→
|
|
118
|
+
→ [Gitleaks] Secrets found? → BLOCK commit ✖
|
|
119
|
+
→ [SonarQube] Server reachable? → No → skip gracefully
|
|
120
|
+
→ [SonarQube] Quality Gate failed? → BLOCK commit ✖
|
|
121
|
+
→ All clear? → commit goes through ✔
|
|
65
122
|
```
|
|
66
123
|
|
|
67
124
|
### Pre-Push Hook
|
|
68
125
|
Every time you run `git push`, the hook:
|
|
69
126
|
1. Checks git diff between local and remote — skips if nothing changed
|
|
70
|
-
2.
|
|
71
|
-
3.
|
|
72
|
-
4.
|
|
127
|
+
2. Auto-detects which port your server runs on (3000, 5000, 8000, 8080, etc.)
|
|
128
|
+
3. Starts your server (`npm start`)
|
|
129
|
+
4. Runs your tests (`npm test`) — blocks push if tests fail
|
|
130
|
+
5. Looks for `*.postman_collection.json` files and runs them via Newman
|
|
73
131
|
|
|
74
132
|
```
|
|
75
133
|
git push
|
|
76
|
-
→ [
|
|
77
|
-
→ [
|
|
78
|
-
→
|
|
134
|
+
→ [Git Diff] Nothing changed? → skip everything
|
|
135
|
+
→ [Smoke Tests] Server starts? → No → BLOCK push ✖
|
|
136
|
+
→ [Smoke Tests] npm test passes? → No → BLOCK push ✖
|
|
137
|
+
→ [Newman] API tests pass? → No → BLOCK push ✖
|
|
138
|
+
→ All clear? → push goes through ✔
|
|
79
139
|
```
|
|
80
140
|
|
|
81
141
|
---
|
|
82
142
|
|
|
83
143
|
## SonarQube
|
|
84
144
|
|
|
85
|
-
The package is pre-configured to connect to the company SonarQube server. Projects are **created automatically** — no manual setup needed on the SonarQube dashboard.
|
|
145
|
+
The package is pre-configured to connect to the company SonarQube server. Projects are **created automatically** via API — no manual setup needed on the SonarQube dashboard.
|
|
86
146
|
|
|
87
|
-
|
|
147
|
+
**If the server is unreachable** (offline, different network, VPN), the scan is skipped gracefully and your commit goes through normally.
|
|
148
|
+
|
|
149
|
+
**To switch servers**, update these values in `lib/sonarqube.js`:
|
|
88
150
|
```javascript
|
|
89
151
|
const SONAR_HOST_URL = 'http://your-server:9000';
|
|
90
152
|
const SONAR_TOKEN = 'your-token';
|
|
@@ -94,30 +156,51 @@ const SONAR_TOKEN = 'your-token';
|
|
|
94
156
|
|
|
95
157
|
## Newman (Postman API Tests)
|
|
96
158
|
|
|
97
|
-
Newman runs automatically if it finds a `*.postman_collection.json` file anywhere in your project
|
|
159
|
+
Newman runs automatically if it finds a `*.postman_collection.json` file anywhere in your project (outside `node_modules`).
|
|
160
|
+
|
|
161
|
+
**HTML reports** are saved locally to `newman-reports/` after each run — open in browser for detailed results.
|
|
98
162
|
|
|
99
|
-
To add API tests
|
|
163
|
+
**To add API tests:**
|
|
100
164
|
1. Export your Postman collection as JSON
|
|
101
|
-
2. Place it anywhere in your project
|
|
102
|
-
3. Newman
|
|
165
|
+
2. Place it anywhere in your project
|
|
166
|
+
3. Newman finds and runs it automatically on every push
|
|
103
167
|
|
|
104
168
|
---
|
|
105
169
|
|
|
106
|
-
##
|
|
170
|
+
## Gitleaks (Secret Scanning)
|
|
107
171
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
172
|
+
Gitleaks scans only your staged files before every commit. It detects:
|
|
173
|
+
- AWS keys
|
|
174
|
+
- GitHub tokens
|
|
175
|
+
- Stripe keys
|
|
176
|
+
- Private keys
|
|
177
|
+
- Hardcoded passwords
|
|
178
|
+
- And 100+ other secret patterns
|
|
111
179
|
|
|
112
|
-
|
|
180
|
+
If a secret is detected, the commit is blocked immediately:
|
|
181
|
+
```
|
|
182
|
+
Finding: const stripe_key = "sk_live_..."
|
|
183
|
+
RuleID: stripe-access-token
|
|
184
|
+
[Gitleaks] Secrets detected! Commit blocked.
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## Manually Running Checks
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
# Run Gitleaks + SonarQube manually (without committing)
|
|
113
193
|
sh .husky/pre-commit
|
|
194
|
+
|
|
195
|
+
# Run Smoke Tests + Newman manually (without pushing)
|
|
196
|
+
sh scripts/run-ci-checks.sh
|
|
114
197
|
```
|
|
115
198
|
|
|
116
199
|
---
|
|
117
200
|
|
|
118
201
|
## Moving Tests to Pre-Commit
|
|
119
202
|
|
|
120
|
-
By default, Smoke Tests + Newman run on **pre-push**. To move them to pre-commit, add one line to `.husky/pre-commit`:
|
|
203
|
+
By default, Smoke Tests + Newman run on **pre-push**. To move them to pre-commit instead, add one line to `.husky/pre-commit`:
|
|
121
204
|
|
|
122
205
|
```sh
|
|
123
206
|
./scripts/run-ci-checks.sh
|
|
@@ -125,11 +208,17 @@ By default, Smoke Tests + Newman run on **pre-push**. To move them to pre-commit
|
|
|
125
208
|
|
|
126
209
|
---
|
|
127
210
|
|
|
128
|
-
##
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
211
|
+
## Quick Reference
|
|
212
|
+
|
|
213
|
+
| Command | What it does |
|
|
214
|
+
|---------|-------------|
|
|
215
|
+
| `git init` | Initialize git repo (required before install) |
|
|
216
|
+
| `npm install secure-husky-setup` | Install the package |
|
|
217
|
+
| `npx secure-husky-setup init` | Set up all hooks and config |
|
|
218
|
+
| `git commit` | Triggers Gitleaks + SonarQube |
|
|
219
|
+
| `git push` | Triggers Smoke Tests + Newman |
|
|
220
|
+
| `sh .husky/pre-commit` | Run pre-commit checks manually |
|
|
221
|
+
| `sh scripts/run-ci-checks.sh` | Run pre-push checks manually |
|
|
133
222
|
|
|
134
223
|
---
|
|
135
224
|
|