-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck.go
More file actions
410 lines (357 loc) · 11 KB
/
Copy pathcheck.go
File metadata and controls
410 lines (357 loc) · 11 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/robmorgan/infraspec/pkg/gatekeeper"
"github.com/robmorgan/infraspec/pkg/gatekeeper/config"
)
var (
// Check command flags
checkRulesFile string
checkFormat string
checkSeverity string
checkVarFile string
checkExclude string
checkInclude string
checkVerbose bool
checkNoBuiltin bool
checkListRules bool
checkStrict bool
)
var checkCmd = &cobra.Command{
Use: "check [path...]",
Short: "Validate Terraform configurations against security rules",
SilenceUsage: true,
SilenceErrors: true,
Long: `Check Terraform configurations against security rules before applying.
InfraSpec Gatekeeper performs static analysis on Terraform HCL files to detect
security misconfigurations, policy violations, and best practice deviations.
Rule Discovery:
InfraSpec automatically discovers rules from multiple sources:
- Built-in rules (can be disabled with --no-builtin)
- .infraspec.hcl in the repository root (auto-discovered)
- *.spec.hcl files alongside Terraform configurations
- Custom rules file specified with --rules
Examples:
# Check all Terraform files in a directory
infraspec check ./terraform
# Check with custom rules
infraspec check ./terraform --rules my-rules.hcl
# Check with JSON output for CI
infraspec check ./terraform --format json
# List all available rules
infraspec check --list-rules
# Exclude specific rules
infraspec check ./terraform --exclude S3_004,VPC_001
Exit codes:
0 - All checks passed
1 - One or more violations found
2 - Parse error or configuration error`,
Args: func(cmd *cobra.Command, args []string) error {
// Allow no args if --list-rules is specified
if checkListRules {
return nil
}
if len(args) < 1 {
return fmt.Errorf("requires at least 1 path argument")
}
return nil
},
RunE: runCheck,
}
func init() {
checkCmd.Flags().StringVarP(&checkRulesFile, "rules", "r", "", "path to custom rules HCL file")
checkCmd.Flags().StringVarP(&checkFormat, "format", "f", "text", "output format (text, json)")
checkCmd.Flags().StringVarP(&checkSeverity, "severity", "s", "error", "minimum severity to report (error, warning, info)")
checkCmd.Flags().StringVar(&checkVarFile, "var-file", "", "path to tfvars file for variable resolution")
checkCmd.Flags().StringVar(&checkExclude, "exclude", "", "comma-separated list of rule IDs to exclude")
checkCmd.Flags().StringVar(&checkInclude, "include", "", "comma-separated list of rule IDs to include (excludes all others)")
checkCmd.Flags().BoolVarP(&checkVerbose, "verbose", "v", false, "enable verbose output")
checkCmd.Flags().BoolVar(&checkNoBuiltin, "no-builtin", false, "disable built-in rules")
checkCmd.Flags().BoolVar(&checkListRules, "list-rules", false, "list all available rules and exit")
checkCmd.Flags().BoolVar(&checkStrict, "strict", false, "treat unknown values as violations")
RootCmd.AddCommand(checkCmd)
}
func runCheck(cmd *cobra.Command, args []string) error {
// Try to find and load .infraspec.hcl config
var infraspecConfig *config.LoadedConfig
if len(args) > 0 {
configPath, err := config.FindConfigFile(args[0])
if err != nil && checkVerbose {
fmt.Fprintf(os.Stderr, "Warning: error finding config file: %v\n", err)
}
if configPath != "" {
infraspecConfig, err = config.LoadConfigFile(configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error loading config file %s: %v\n", configPath, err)
return ExitError{Code: 2}
}
if checkVerbose {
fmt.Fprintf(os.Stderr, "Loaded config from %s\n", configPath)
}
}
}
// Build checker configuration, merging config file settings with CLI flags
// CLI flags take precedence over config file settings
cfg := gatekeeper.Config{
RulesFile: checkRulesFile,
VarFile: checkVarFile,
Format: checkFormat,
MinSeverity: parseSeverity(checkSeverity),
ExcludeRules: parseCommaSeparated(checkExclude),
IncludeRules: parseCommaSeparated(checkInclude),
Verbose: checkVerbose,
NoBuiltin: checkNoBuiltin,
StrictUnknowns: checkStrict,
}
// Apply config file settings if not overridden by CLI
if infraspecConfig != nil {
// Only apply config file format if CLI didn't specify
if !cmd.Flags().Changed("format") && infraspecConfig.Format != "" {
cfg.Format = infraspecConfig.Format
}
// Only apply config file severity if CLI didn't specify
if !cmd.Flags().Changed("severity") && infraspecConfig.MinSeverity != "" {
cfg.MinSeverity = parseSeverity(infraspecConfig.MinSeverity)
}
// Only apply config file strict if CLI didn't specify
if !cmd.Flags().Changed("strict") {
cfg.StrictUnknowns = infraspecConfig.Strict
}
// Only apply config file no-builtin if CLI didn't specify
if !cmd.Flags().Changed("no-builtin") {
cfg.NoBuiltin = infraspecConfig.NoBuiltin
}
// Add rules from config file
cfg.ConfigRules = infraspecConfig.Rules
}
// Create checker instance
checker, err := gatekeeper.New(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return ExitError{Code: 2}
}
// Handle --list-rules
if checkListRules {
return listRules(checker)
}
// Discover all .tf files from provided paths
tfFiles, err := discoverTerraformFiles(args)
if err != nil {
fmt.Fprintf(os.Stderr, "Error discovering Terraform files: %v\n", err)
return ExitError{Code: 2}
}
if len(tfFiles) == 0 {
fmt.Fprintf(os.Stderr, "Error: no Terraform files found in specified paths\n")
return ExitError{Code: 2}
}
// Discover spec files alongside Terraform files
specFiles, err := discoverSpecFiles(args)
if err != nil && checkVerbose {
fmt.Fprintf(os.Stderr, "Warning: error discovering spec files: %v\n", err)
}
// Load rules from spec files
if len(specFiles) > 0 {
if checkVerbose {
fmt.Fprintf(os.Stderr, "Found %d spec file(s)\n", len(specFiles))
}
if err := checker.LoadSpecFiles(specFiles); err != nil {
fmt.Fprintf(os.Stderr, "Error loading spec files: %v\n", err)
return ExitError{Code: 2}
}
}
// Run the checks
result, err := checker.Check(tfFiles)
if err != nil {
fmt.Fprintf(os.Stderr, "Error running checks: %v\n", err)
return ExitError{Code: 2}
}
// Output the results
if err := checker.Output(result, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "Error formatting output: %v\n", err)
return ExitError{Code: 2}
}
// Return appropriate exit code
if result.HasViolations() {
return ExitError{Code: 1}
}
return nil
}
// ExitError is an error that carries an exit code
type ExitError struct {
Code int
}
func (e ExitError) Error() string {
return fmt.Sprintf("exit code %d", e.Code)
}
// discoverTerraformFiles finds all .tf files in the given paths
func discoverTerraformFiles(paths []string) ([]string, error) {
var tfFiles []string
seen := make(map[string]bool)
for _, path := range paths {
info, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("cannot access path %s: %w", path, err)
}
if info.IsDir() {
// Walk directory recursively
err := filepath.WalkDir(path, func(filePath string, d os.DirEntry, err error) error {
if err != nil {
return err
}
// Skip hidden directories
if d.IsDir() && strings.HasPrefix(d.Name(), ".") {
return filepath.SkipDir
}
// Collect .tf files
if !d.IsDir() && strings.HasSuffix(d.Name(), ".tf") {
absPath, err := filepath.Abs(filePath)
if err != nil {
return err
}
if !seen[absPath] {
seen[absPath] = true
tfFiles = append(tfFiles, absPath)
}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error walking directory %s: %w", path, err)
}
} else if strings.HasSuffix(path, ".tf") {
absPath, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("error getting absolute path for %s: %w", path, err)
}
if !seen[absPath] {
seen[absPath] = true
tfFiles = append(tfFiles, absPath)
}
}
}
return tfFiles, nil
}
// discoverSpecFiles finds all *.spec.hcl files in the same directories as Terraform files
func discoverSpecFiles(paths []string) ([]string, error) {
var specFiles []string
seen := make(map[string]bool)
dirsChecked := make(map[string]bool)
for _, path := range paths {
info, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("cannot access path %s: %w", path, err)
}
if info.IsDir() {
// Walk directory recursively
err := filepath.WalkDir(path, func(filePath string, d os.DirEntry, err error) error {
if err != nil {
return err
}
// Skip hidden directories
if d.IsDir() && strings.HasPrefix(d.Name(), ".") {
return filepath.SkipDir
}
// Collect *.spec.hcl files
if !d.IsDir() && strings.HasSuffix(d.Name(), ".spec.hcl") {
absPath, err := filepath.Abs(filePath)
if err != nil {
return err
}
if !seen[absPath] {
seen[absPath] = true
specFiles = append(specFiles, absPath)
}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error walking directory %s: %w", path, err)
}
} else if strings.HasSuffix(path, ".tf") {
// Check the directory containing this .tf file for spec files
dir := filepath.Dir(path)
absDir, err := filepath.Abs(dir)
if err != nil {
continue
}
if dirsChecked[absDir] {
continue
}
dirsChecked[absDir] = true
entries, err := os.ReadDir(absDir)
if err != nil {
continue
}
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".spec.hcl") {
specPath := filepath.Join(absDir, entry.Name())
if !seen[specPath] {
seen[specPath] = true
specFiles = append(specFiles, specPath)
}
}
}
}
}
return specFiles, nil
}
func listRules(checker *gatekeeper.Checker) error {
rules := checker.ListRules()
if checkFormat == "json" {
return checker.OutputRulesJSON(rules, os.Stdout)
}
// Text output
fmt.Println("Available Rules:")
fmt.Println()
for _, rule := range rules {
severityBadge := formatSeverityBadge(rule.Severity)
fmt.Printf(" %s %-10s %s\n", severityBadge, rule.ID, rule.Name)
if checkVerbose && rule.Description != "" {
fmt.Printf(" %s\n", rule.Description)
}
}
fmt.Printf("\nTotal: %d rules\n", len(rules))
return nil
}
func formatSeverityBadge(severity gatekeeper.Severity) string {
switch severity {
case gatekeeper.SeverityError:
return "[ERROR] "
case gatekeeper.SeverityWarning:
return "[WARNING]"
case gatekeeper.SeverityInfo:
return "[INFO] "
default:
return "[UNKNOWN]"
}
}
func parseSeverity(s string) gatekeeper.Severity {
switch strings.ToLower(s) {
case "error":
return gatekeeper.SeverityError
case "warning", "warn":
return gatekeeper.SeverityWarning
case "info":
return gatekeeper.SeverityInfo
default:
return gatekeeper.SeverityError
}
}
func parseCommaSeparated(s string) []string {
if s == "" {
return nil
}
parts := strings.Split(s, ",")
result := make([]string, 0, len(parts))
for _, p := range parts {
trimmed := strings.TrimSpace(p)
if trimmed != "" {
result = append(result, trimmed)
}
}
return result
}