Skip to content

Commit

Permalink
fix flag initialization and naming (#1950)
Browse files Browse the repository at this point in the history
Signed-off-by: pxp928 <parth.psu@gmail.com>
  • Loading branch information
pxp928 committed Jun 6, 2024
1 parent 4204cb0 commit 42599e4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
13 changes: 12 additions & 1 deletion cmd/guaccollect/cmd/osv.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ you have access to read and write to the respective blob store.`,
viper.GetString("interval"),
viper.GetBool("service-poll"),
viper.GetBool("publish-to-queue"),
viper.GetInt("daysSinceLastScan"),
viper.GetInt("last-scan"),
)
if err != nil {
fmt.Printf("unable to validate flags: %v\n", err)
Expand Down Expand Up @@ -232,5 +232,16 @@ func initializeNATsandCertifier(ctx context.Context, blobAddr, pubsubAddr string
}

func init() {
set, err := cli.BuildFlags([]string{"interval",
"last-scan", "header-file"})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
os.Exit(1)
}
osvCmd.PersistentFlags().AddFlagSet(set)
if err := viper.BindPFlags(osvCmd.PersistentFlags()); err != nil {
fmt.Fprintf(os.Stderr, "failed to bind flags: %v", err)
os.Exit(1)
}
rootCmd.AddCommand(osvCmd)
}
2 changes: 0 additions & 2 deletions cmd/guaccollect/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ func init() {
"service-poll",
"enable-prometheus",
"publish-to-queue",
"interval",
"daysSinceLastScan",
"gql-addr",
})
if err != nil {
Expand Down
32 changes: 20 additions & 12 deletions cmd/guaccollect/cmd/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/Khan/genqlient/graphql"
"github.com/guacsec/guac/pkg/certifier"
"github.com/guacsec/guac/pkg/certifier/certify"
"github.com/guacsec/guac/pkg/certifier/components/root_package"
sc "github.com/guacsec/guac/pkg/certifier/components/source"
"github.com/guacsec/guac/pkg/certifier/scorecard"
"github.com/guacsec/guac/pkg/cli"
"github.com/guacsec/guac/pkg/logging"
Expand All @@ -46,6 +46,8 @@ type scorecardOptions struct {
interval time.Duration
// enable/disable message publish to queue
publishToQueue bool
// setting "daysSinceLastScan" to 0 does not check the timestamp on the scorecard that exist
daysSinceLastScan int
}

var scorecardCmd = &cobra.Command{
Expand Down Expand Up @@ -75,6 +77,7 @@ you have access to read and write to the respective blob store.`,
viper.GetString("interval"),
viper.GetBool("service-poll"),
viper.GetBool("publish-to-queue"),
viper.GetInt("last-scan"),
)
if err != nil {
fmt.Printf("unable to validate flags: %v\n", err)
Expand Down Expand Up @@ -111,13 +114,13 @@ you have access to read and write to the respective blob store.`,
httpClient := http.Client{Transport: transport}
gqlclient := graphql.NewClient(opts.graphqlEndpoint, &httpClient)

sourceQueryFunc, err := getSourceQuery(gqlclient)
query, err := sc.NewCertifier(gqlclient, opts.daysSinceLastScan)
if err != nil {
logger.Errorf("error: %v", err)
logger.Errorf("unable to create source query: %v\n", err)
os.Exit(1)
}

initializeNATsandCertifier(ctx, opts.blobAddr, opts.pubsubAddr, opts.poll, opts.publishToQueue, opts.interval, sourceQueryFunc())
initializeNATsandCertifier(ctx, opts.blobAddr, opts.pubsubAddr, opts.poll, opts.publishToQueue, opts.interval, query)
},
}

Expand All @@ -128,7 +131,7 @@ func validateScorecardFlags(
blobAddr,
interval string,
poll bool,
pubToQueue bool) (scorecardOptions, error) {
pubToQueue bool, daysSince int) (scorecardOptions, error) {

var opts scorecardOptions

Expand All @@ -144,17 +147,22 @@ func validateScorecardFlags(
return opts, fmt.Errorf("failed to parser duration with error: %w", err)
}
opts.interval = i
opts.daysSinceLastScan = daysSince

return opts, nil
}

func getSourceQuery(client graphql.Client) (func() certifier.QueryComponents, error) {
return func() certifier.QueryComponents {
packageQuery := root_package.NewPackageQuery(client, 0)
return packageQuery
}, nil
}

func init() {
set, err := cli.BuildFlags([]string{"interval",
"last-scan", "header-file"})
if err != nil {
fmt.Fprintf(os.Stderr, "failed to setup flag: %v", err)
os.Exit(1)
}
scorecardCmd.PersistentFlags().AddFlagSet(set)
if err := viper.BindPFlags(scorecardCmd.PersistentFlags()); err != nil {
fmt.Fprintf(os.Stderr, "failed to bind flags: %v", err)
os.Exit(1)
}
rootCmd.AddCommand(scorecardCmd)
}
2 changes: 1 addition & 1 deletion guac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ blob-addr: file:///tmp/blobstore?no_tmp_dir=true
# certifier interval
interval: 20m
# days since the last vulnerability scan was run. 0 means only run once
daysSinceLastScan: 0
last-scan: 0

# CSub setup
csub-addr: localhost:2782
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func init() {

set.StringP("interval", "i", "5m", "if polling set interval, m, h, s, etc.")

set.IntP("daysSinceLastScan", "l", 0, "days since the last vulnerability scan was run. 0 means only run once")
set.IntP("last-scan", "l", 0, "days since the last vulnerability scan was run. Default 0 means only run once")

set.BoolP("cert-good", "g", false, "enable to certifyGood, otherwise defaults to certifyBad")
set.BoolP("package-name", "n", false, "if type is package, enable if attestation is at package-name level (for all versions), defaults to specific version")
Expand Down

0 comments on commit 42599e4

Please sign in to comment.