Skip to content

Commit

Permalink
clairctl: unifiy config, client handling
Browse files Browse the repository at this point in the history
This change makes the main command take a config argument, and reworks
the report command to use it.

It also makes sure that if a config is provided, a properly configured
HTTP client is generated and used throughout.

Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Nov 20, 2020
1 parent ea564d4 commit 9883e80
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 deletions.
10 changes: 7 additions & 3 deletions cmd/clairctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/quay/clair/v4/httptransport"
"github.com/quay/claircore"
"github.com/tomnomnom/linkheader"

"github.com/quay/clair/v4/httptransport"
)

const (
Expand Down Expand Up @@ -63,14 +64,17 @@ type Client struct {
validator map[string]string
}

func NewClient(root string) (*Client, error) {
func NewClient(c *http.Client, root string) (*Client, error) {
if c == nil {
c = http.DefaultClient
}
host, err := url.Parse(root)
if err != nil {
return nil, err
}
return &Client{
host: host,
client: &http.Client{},
client: c,
validator: make(map[string]string),
}, nil
}
Expand Down
1 change: 1 addition & 0 deletions cmd/clairctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ func loadConfig(n string) (*config.Config, error) {
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
return nil, err
}
// Can't use validate, because we're not running in a server "mode".
return &cfg, nil
}
11 changes: 1 addition & 10 deletions cmd/clairctl/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/quay/claircore/updater"
_ "github.com/quay/claircore/updater/defaults"
"github.com/urfave/cli/v2"
"gopkg.in/square/go-jose.v2/jwt"
)

// ExportCmd is the "export-updaters" subcommand.
Expand All @@ -27,14 +26,6 @@ var ExportCmd = &cli.Command{
Name: "strict",
Usage: "Return non-zero exit when updaters report errors.",
},
&cli.PathFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "clair configuration file",
Value: "config.yaml",
TakesFile: true,
EnvVars: []string{"CLAIR_CONF"},
},
},
}

Expand Down Expand Up @@ -72,7 +63,7 @@ func exportAction(c *cli.Context) error {
cfgs[name] = node.Decode
}

cl, _, err := cfg.Client(nil, jwt.Claims{})
cl, _, err := cfg.Client(nil, commonClaim)
if err != nil {
return err
}
Expand Down
14 changes: 2 additions & 12 deletions cmd/clairctl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/jackc/pgx/v4/pgxpool"
"github.com/quay/claircore/libvuln"
"github.com/urfave/cli/v2"
"gopkg.in/square/go-jose.v2/jwt"
)

// ImportCmd is the "import-updaters" subcommand.
Expand All @@ -21,16 +20,7 @@ var ImportCmd = &cli.Command{
Action: importAction,
Usage: "import updates",
ArgsUsage: "in",
Flags: []cli.Flag{
&cli.PathFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "clair configuration file",
Value: "config.yaml",
TakesFile: true,
EnvVars: []string{"CLAIR_CONF"},
},
},
Flags: []cli.Flag{},
}

func importAction(c *cli.Context) error {
Expand All @@ -41,7 +31,7 @@ func importAction(c *cli.Context) error {
return err
}

cl, _, err := cfg.Client(nil, jwt.Claims{})
cl, _, err := cfg.Client(nil, commonClaim)
if err != nil {
return err
}
Expand Down
18 changes: 18 additions & 0 deletions cmd/clairctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (

_ "github.com/quay/claircore/updater/defaults"
"github.com/urfave/cli/v2"
"gopkg.in/square/go-jose.v2/jwt"
)

var (
flagDebug bool

commonClaim = jwt.Claims{}
)

func main() {
Expand All @@ -29,6 +32,7 @@ func main() {
if c.IsSet("D") {
debug.SetOutput(os.Stderr)
}
commonClaim.Issuer = c.String("issuer")
return nil
},
Commands: []*cli.Command{
Expand All @@ -42,6 +46,20 @@ func main() {
Name: "D",
Usage: "print debugging logs",
},
&cli.PathFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "clair configuration file",
Value: "config.yaml",
TakesFile: true,
EnvVars: []string{"CLAIR_CONF"},
},
&cli.StringFlag{
Name: "issuer",
Aliases: []string{"iss"},
Usage: `jwt "issuer" to use when making authenticated requests`,
Value: "clairctl",
},
},
}
log.SetFlags(log.Flags())
Expand Down
19 changes: 18 additions & 1 deletion cmd/clairctl/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,24 @@ func reportAction(c *cli.Context) error {
return errors.New("missing needed arguments")
}

cc, err := NewClient(c.String("host"))
// Do we have a config?
fi, err := os.Stat(c.Path("config"))
useCfg := err == nil && !fi.IsDir()

var cc *Client
if useCfg {
cfg, e := loadConfig(c.Path("config"))
if e != nil {
return e
}
hc, _, e := cfg.Client(nil, commonClaim)
if e != nil {
return e
}
cc, err = NewClient(hc, c.String("host"))
} else {
cc, err = NewClient(nil, c.String("host"))
}
if err != nil {
return err
}
Expand Down

0 comments on commit 9883e80

Please sign in to comment.