-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.go
More file actions
220 lines (178 loc) · 5.4 KB
/
hooks.go
File metadata and controls
220 lines (178 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
var (
hooksPreCommit bool
hooksPrePush bool
hooksUninstall bool
)
var hooksCmd = &cobra.Command{
Use: "init-hooks",
Short: "Initialize git pre-commit/pre-push hooks for security scanning",
Long: `Initialize git hooks to automatically run security scans before commits and pushes.
Examples:
devsecops init-hooks # Install both pre-commit and pre-push hooks
devsecops init-hooks --uninstall # Remove all installed hooks
`,
RunE: func(cmd *cobra.Command, args []string) error {
return runInitHooks()
},
}
func init() {
rootCmd.AddCommand(hooksCmd)
hooksCmd.Flags().BoolVar(&hooksUninstall, "uninstall", false, "Remove installed git hooks")
hooksCmd.Flags().BoolVar(&hooksPreCommit, "pre-commit", true, "Install pre-commit hook (blocks commits with security issues)")
hooksCmd.Flags().BoolVar(&hooksPrePush, "pre-push", true, "Install pre-push hook (warns about security issues)")
}
func runInitHooks() error {
// Get git directory
gitDir, err := getGitDir()
if err != nil {
return fmt.Errorf("not a git repository: %w", err)
}
hooksDir := filepath.Join(gitDir, "hooks")
if hooksUninstall {
return uninstallHooks(hooksDir)
}
return installHooks(hooksDir)
}
// getGitDir finds the .git directory by walking up the directory tree
func getGitDir() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
for {
gitPath := filepath.Join(dir, ".git")
if info, err := os.Stat(gitPath); err == nil && info.IsDir() {
return gitPath, nil
}
parent := filepath.Dir(dir)
if parent == dir {
// Reached filesystem root
return "", fmt.Errorf("no .git directory found")
}
dir = parent
}
}
// installHooks creates pre-commit and pre-push hooks
func installHooks(hooksDir string) error {
// Ensure hooks directory exists
if err := os.MkdirAll(hooksDir, 0o755); err != nil {
return fmt.Errorf("failed to create hooks directory: %w", err)
}
installed := false
// Install pre-commit hook
if hooksPreCommit {
preCommitPath := filepath.Join(hooksDir, "pre-commit")
if err := createPreCommitHook(preCommitPath); err != nil {
return err
}
fmt.Printf("✅ Installed: %s\n", preCommitPath)
installed = true
}
// Install pre-push hook
if hooksPrePush {
prePushPath := filepath.Join(hooksDir, "pre-push")
if err := createPrePushHook(prePushPath); err != nil {
return err
}
fmt.Printf("✅ Installed: %s\n", prePushPath)
installed = true
}
if !installed {
return fmt.Errorf("no hooks to install")
}
fmt.Println()
fmt.Println("🎉 Git hooks installed successfully!")
fmt.Println()
fmt.Println("Behavior:")
fmt.Println(" • pre-commit: Blocks commits if security issues exceed thresholds")
fmt.Println(" • pre-push: Warns about security issues but allows push")
fmt.Println()
fmt.Println("To remove hooks, run: devsecops init-hooks --uninstall")
return nil
}
// uninstallHooks removes pre-commit and pre-push hooks
func uninstallHooks(hooksDir string) error {
preCommitPath := filepath.Join(hooksDir, "pre-commit")
prePushPath := filepath.Join(hooksDir, "pre-push")
preCommitRemoved := false
prePushRemoved := false
// Remove pre-commit hook
if _, err := os.Stat(preCommitPath); err == nil {
if err := os.Remove(preCommitPath); err != nil {
return fmt.Errorf("failed to remove pre-commit hook: %w", err)
}
fmt.Printf("✅ Removed: %s\n", preCommitPath)
preCommitRemoved = true
}
// Remove pre-push hook
if _, err := os.Stat(prePushPath); err == nil {
if err := os.Remove(prePushPath); err != nil {
return fmt.Errorf("failed to remove pre-push hook: %w", err)
}
fmt.Printf("✅ Removed: %s\n", prePushPath)
prePushRemoved = true
}
if !preCommitRemoved && !prePushRemoved {
return fmt.Errorf("no hooks found to uninstall")
}
fmt.Println()
fmt.Println("🎉 Git hooks removed successfully!")
return nil
}
// createPreCommitHook creates the pre-commit hook script
func createPreCommitHook(hookPath string) error {
script := `#!/bin/bash
# DevSecOps Kit - Pre-commit Hook
# Blocks commits if security issues exceed configured thresholds
set -e
echo "🔍 Running security checks before commit..."
echo ""
# Run devsecops scan with threshold enforcement
if ! devsecops scan --fail-on-threshold; then
echo ""
echo "❌ Commit blocked due to security issues."
echo " Please review the findings above and remediate before retrying."
echo ""
exit 1
fi
echo ""
echo "✅ Security checks passed!"
exit 0
`
if err := os.WriteFile(hookPath, []byte(script), 0o755); err != nil {
return fmt.Errorf("failed to create pre-commit hook: %w", err)
}
return nil
}
// createPrePushHook creates the pre-push hook script (warning-only)
func createPrePushHook(hookPath string) error {
script := `#!/bin/bash
# DevSecOps Kit - Pre-push Hook
# Warns about security issues but allows push to proceed
set -e
echo "🔍 Scanning for security issues before push..."
echo ""
# Run devsecops scan (without --fail-on-threshold to allow push)
if devsecops scan; then
echo ""
echo "✅ No blocking security issues detected!"
else
# Scan ran but found issues below thresholds
echo ""
echo "⚠️ Review the findings above. Consider addressing them."
echo " (This is a warning only - push will proceed)"
fi
exit 0
`
if err := os.WriteFile(hookPath, []byte(script), 0o755); err != nil {
return fmt.Errorf("failed to create pre-push hook: %w", err)
}
return nil
}