@@ -2,103 +2,84 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
- "io"
6
5
"log/slog"
7
6
"os"
8
7
9
8
"github.com/gatecheckdev/gatecheck/pkg/gatecheck"
10
9
"github.com/spf13/cobra"
11
- "github.com/spf13/viper"
12
10
)
13
11
14
- func newValidateCommand () * cobra.Command {
15
- cmd := & cobra.Command {
16
- Use : "validate [FILE]" ,
17
- Short : "compare vulnerabilities to configured thresholds" ,
18
- Args : cobra .ExactArgs (1 ),
19
- RunE : runValidate ,
20
- }
21
-
22
- cmd .Flags ().StringP ("config" , "f" , "" , "threshold configuration file" )
23
-
24
- cmd .Flags ().String ("epss-file" , "" , "use this file for epss scores, will not query API" )
25
- _ = viper .BindPFlag ("cli.validate.epss-file" , cmd .Flags ().Lookup ("epss-file" ))
26
-
27
- cmd .Flags ().String ("kev-file" , "" , "use this file for kev catalog, will not query API" )
28
- _ = viper .BindPFlag ("cli.validate.kev-file" , cmd .Flags ().Lookup ("kev-file" ))
29
-
30
- cmd .Flags ().Bool ("audit" , false , "audit mode - will run all rules but wil always exit 0 for validation failures" )
31
- _ = viper .BindPFlag ("cli.validate.audit" , cmd .Flags ().Lookup ("audit" ))
32
-
33
- return cmd
34
- }
35
-
36
- // runValidate
37
- //
38
- // shell: gatecheck validate
39
- func runValidate (cmd * cobra.Command , args []string ) error {
40
- configFilename , _ := cmd .Flags ().GetString ("config" )
41
- targetFilename := args [0 ]
42
-
43
- epssURL := viper .GetString ("api.epss-url" )
44
- kevURL := viper .GetString ("api.kev-url" )
45
-
46
- epssFilename := viper .GetString ("cli.validate.epss-file" )
47
- kevFilename := viper .GetString ("cli.validate.kev-file" )
48
-
49
- audit := viper .GetBool ("cli.validate.audit" )
12
+ var validateCmd = & cobra.Command {
13
+ Use : "validate [FILE]" ,
14
+ Short : "compare vulnerabilities to configured thresholds" ,
15
+ Args : cobra .ExactArgs (1 ),
16
+ PreRunE : func (cmd * cobra.Command , args []string ) error {
17
+ configFilename := RuntimeConfig .ConfigFilename .Value ().(string )
18
+
19
+ RuntimeConfig .gatecheckConfig = gatecheck .NewDefaultConfig ()
20
+ if configFilename != "" {
21
+ err := gatecheck .NewConfigDecoder (configFilename ).Decode (RuntimeConfig .gatecheckConfig )
22
+ if err != nil {
23
+ return err
24
+ }
25
+ }
50
26
51
- slog . Debug ( "read in config" , "filename" , configFilename , "target_filename" , targetFilename )
27
+ var err error
52
28
53
- config := gatecheck .NewDefaultConfig ()
54
- if configFilename != "" {
55
- err := LoadConfigFromFile (config , configFilename )
29
+ epssFilename := RuntimeConfig .EPSSFilename .Value ().(string )
30
+ if epssFilename != "" {
31
+ RuntimeConfig .epssFile , err = os .Open (epssFilename )
32
+ }
56
33
if err != nil {
57
34
return err
58
35
}
59
- } else {
60
- slog .Warn ("no configuration file given, will use default configuration file" )
61
- }
62
-
63
- slog .Debug ("open target file" , "filename" , targetFilename )
64
- targetFile , err := os .Open (targetFilename )
65
- if err != nil {
66
- return err
67
- }
68
36
69
- var epssFile , kevFile io.Reader
70
-
71
- if epssFilename != "" {
72
- slog .Debug ("open epss file" , "filename" , epssFilename )
73
- epssFile , err = os .Open (epssFilename )
37
+ kevFilename := RuntimeConfig .KEVFilename .Value ().(string )
38
+ if kevFilename != "" {
39
+ RuntimeConfig .kevFile , err = os .Open (kevFilename )
40
+ }
74
41
if err != nil {
75
42
return err
76
43
}
77
- }
78
44
79
- if kevFilename != "" {
80
- slog .Debug ("open kev file" , "filename" , kevFilename )
81
- kevFile , err = os .Open (kevFilename )
45
+ targetFilename := args [ 0 ]
46
+ slog .Debug ("open target file" , "filename" , targetFilename )
47
+ RuntimeConfig . targetFile , err = os .Open (targetFilename )
82
48
if err != nil {
83
49
return err
84
50
}
85
- }
86
51
87
- err = gatecheck .Validate (
88
- config ,
89
- targetFile ,
90
- targetFilename ,
91
- gatecheck .WithEPSSURL (epssURL ),
92
- gatecheck .WithKEVURL (kevURL ),
93
- gatecheck .WithEPSSFile (epssFile ),
94
- gatecheck .WithKEVFile (kevFile ),
95
- )
52
+ return nil
53
+ },
54
+ RunE : func (cmd * cobra.Command , args []string ) error {
55
+
56
+ err := gatecheck .Validate (
57
+ RuntimeConfig .gatecheckConfig ,
58
+ RuntimeConfig .targetFile ,
59
+ args [0 ],
60
+ gatecheck .WithEPSSURL (RuntimeConfig .EPSSURL .Value ().(string )),
61
+ gatecheck .WithKEVURL (RuntimeConfig .KEVURL .Value ().(string )),
62
+ gatecheck .WithEPSSFile (RuntimeConfig .epssFile ),
63
+ gatecheck .WithKEVFile (RuntimeConfig .kevFile ),
64
+ )
65
+
66
+ audit := RuntimeConfig .Audit .Value ().(bool )
67
+ if audit && err != nil {
68
+ slog .Error ("validation failure in audit mode" )
69
+ fmt .Fprintln (cmd .ErrOrStderr (), err )
70
+ return nil
71
+ }
96
72
97
- if audit && err != nil {
98
- slog .Error ("validation failure in audit mode" )
99
- fmt .Fprintln (cmd .ErrOrStderr (), err )
100
73
return nil
101
- }
74
+ },
75
+ }
76
+
77
+ func newValidateCommand () * cobra.Command {
78
+
79
+ RuntimeConfig .ConfigFilename .SetupCobra (validateCmd )
80
+ RuntimeConfig .EPSSFilename .SetupCobra (validateCmd )
81
+ RuntimeConfig .KEVFilename .SetupCobra (validateCmd )
82
+ RuntimeConfig .Audit .SetupCobra (validateCmd )
102
83
103
- return err
84
+ return validateCmd
104
85
}
0 commit comments