Skip to content

Commit

Permalink
clairctl: Add delete command
Browse files Browse the repository at this point in the history
Delete capability was added to the API recently, this
adds the ability to invoke that endpoint directly through
clairctl.

Signed-off-by: crozzy <joseph.crosland@gmail.com>
  • Loading branch information
crozzy committed Nov 3, 2022
1 parent 070a611 commit e431960
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cmd/clairctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,40 @@ func (c *Client) VulnerabilityReport(ctx context.Context, id claircore.Digest) (
return &report, nil
}

func (c *Client) DeleteIndexReports(ctx context.Context, ds []claircore.Digest) error {
var (
req *http.Request
res *http.Response
)
u, err := c.host.Parse(path.Join(c.host.RequestURI(), httptransport.IndexAPIPath))
if err != nil {
return err
}
req = c.request(ctx, u, http.MethodDelete)

req.Body = codec.JSONReader(ds)
res, err = c.client.Do(req)
if err != nil {
zlog.Debug(ctx).
Err(err).
Stringer("url", req.URL).
Msg("request failed")
return err
}
defer res.Body.Close()
zlog.Debug(ctx).
Str("method", res.Request.Method).
Str("path", res.Request.URL.Path).
Str("status", res.Status).
Send()
switch res.StatusCode {
case http.StatusOK:
default:
return fmt.Errorf("unexpected return status: %d", res.StatusCode)
}
return nil
}

func (c *Client) request(ctx context.Context, u *url.URL, m string) *http.Request {
req := &http.Request{
Method: m,
Expand Down
68 changes: 68 additions & 0 deletions cmd/clairctl/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"errors"
"os"

"github.com/quay/claircore"
"github.com/urfave/cli/v2"

"github.com/quay/clair/v4/internal/httputil"
)

var DeleteCmd = &cli.Command{
Name: "delete",
Description: "Delete index reports for given manifest digests.",
Action: deleteAction,
Usage: "deletes index reports for given manifest digests",
ArgsUsage: "digest...",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "host",
Usage: "URL for the clairv4 v1 API.",
Value: "http://localhost:6060/",
EnvVars: []string{"CLAIR_API"},
},
},
}

func deleteAction(c *cli.Context) error {
args := c.Args()
if args.Len() == 0 {
return errors.New("missing needed arguments")
}
ds := []claircore.Digest{}
for i := 0; i < args.Len(); i++ {
d, err := claircore.ParseDigest(args.Get(i))
if err != nil {
return err
}
ds = append(ds, d)
}

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 := httputil.Client(nil, &commonClaim, cfg)
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
}
err = cc.DeleteIndexReports(c.Context, ds)
if err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions cmd/clairctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
ReportCmd,
ExportCmd,
ImportCmd,
DeleteCmd,
},
Flags: []cli.Flag{
&cli.BoolFlag{
Expand Down

0 comments on commit e431960

Please sign in to comment.