@@ -2,103 +2,84 @@ package cmd
22
33import (
44 "fmt"
5- "io"
65 "log/slog"
76 "os"
87
98 "github.com/gatecheckdev/gatecheck/pkg/gatecheck"
109 "github.com/spf13/cobra"
11- "github.com/spf13/viper"
1210)
1311
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+ }
5026
51- slog . Debug ( "read in config" , "filename" , configFilename , "target_filename" , targetFilename )
27+ var err error
5228
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+ }
5633 if err != nil {
5734 return err
5835 }
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- }
6836
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+ }
7441 if err != nil {
7542 return err
7643 }
77- }
7844
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 )
8248 if err != nil {
8349 return err
8450 }
85- }
8651
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+ }
9672
97- if audit && err != nil {
98- slog .Error ("validation failure in audit mode" )
99- fmt .Fprintln (cmd .ErrOrStderr (), err )
10073 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 )
10283
103- return err
84+ return validateCmd
10485}
0 commit comments