-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
- Loading branch information
Showing
5 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
) | ||
|
||
var debug = log.New(ioutil.Discard, "debug: ", log.LstdFlags|log.Lmsgprefix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var ( | ||
flagDebug bool | ||
) | ||
|
||
func main() { | ||
var exit int | ||
defer func() { os.Exit(exit) }() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
app := &cli.App{ | ||
Name: "clairctl", | ||
Version: "0.1.0", | ||
Description: "A command-line tool for clair v4.", | ||
EnableBashCompletion: true, | ||
Before: func(c *cli.Context) error { | ||
if c.IsSet("D") { | ||
debug.SetOutput(os.Stderr) | ||
} | ||
return nil | ||
}, | ||
Commands: []*cli.Command{ | ||
ManifestCmd, | ||
}, | ||
Flags: []cli.Flag{ | ||
&cli.BoolFlag{ | ||
Name: "D", | ||
Usage: "print debugging logs", | ||
}, | ||
}, | ||
} | ||
log.SetFlags(log.Flags()) | ||
|
||
if err := app.RunContext(ctx, os.Args); err != nil { | ||
exit = 1 | ||
if err, ok := err.(cli.ExitCoder); ok { | ||
exit = err.ExitCode() | ||
} | ||
log.Println(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/google/go-containerregistry/pkg/authn" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
"github.com/google/go-containerregistry/pkg/v1/remote/transport" | ||
"github.com/quay/claircore" | ||
"github.com/urfave/cli/v2" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
var ManifestCmd = &cli.Command{ | ||
Name: "manifest", | ||
Description: "print a clair manifest for the provided container", | ||
Action: manifestAction, | ||
} | ||
|
||
func manifestAction(c *cli.Context) error { | ||
debug.Println("manifest") | ||
args := c.Args() | ||
if args.Len() == 0 { | ||
return errors.New("missing needed arguments") | ||
} | ||
|
||
result := make(chan *claircore.Manifest) | ||
done := make(chan struct{}) | ||
eg, ctx := errgroup.WithContext(c.Context) | ||
go func() { | ||
defer close(done) | ||
buf := bufio.NewWriter(os.Stdout) | ||
defer buf.Flush() | ||
enc := json.NewEncoder(buf) | ||
for m := range result { | ||
enc.Encode(m) | ||
buf.Flush() | ||
} | ||
}() | ||
|
||
for i := 0; i < args.Len(); i++ { | ||
name := args.Get(i) | ||
debug.Printf("%s: fetching", name) | ||
eg.Go(func() error { | ||
m, err := Inspect(ctx, name) | ||
if err != nil { | ||
debug.Printf("%s: err: %v", name) | ||
return err | ||
} | ||
debug.Printf("%s: ok", name) | ||
result <- m | ||
return nil | ||
}) | ||
} | ||
if err := eg.Wait(); err != nil { | ||
return err | ||
} | ||
close(result) | ||
<-done | ||
return nil | ||
} | ||
|
||
func Inspect(ctx context.Context, r string) (*claircore.Manifest, error) { | ||
ref, err := name.ParseReference(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
repo := ref.Context() | ||
auth, err := authn.DefaultKeychain.Resolve(repo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
rt, err := transport.New(repo.Registry, auth, http.DefaultTransport, []string{repo.Scope("pull")}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
desc, err := remote.Get(ref, remote.WithTransport(rt)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
img, err := desc.Image() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
h, err := img.Digest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
ccd, err := claircore.ParseDigest(h.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
out := claircore.Manifest{ | ||
Hash: ccd, | ||
} | ||
debug.Printf("%s: found manifest %v", r, ccd) | ||
|
||
ls, err := img.Layers() | ||
if err != nil { | ||
return nil, err | ||
} | ||
debug.Printf("%s: found %d layers", r, len(ls)) | ||
|
||
rURL := url.URL{ | ||
Scheme: repo.Scheme(), | ||
Host: repo.RegistryStr(), | ||
} | ||
c := http.Client{ | ||
Transport: rt, | ||
} | ||
|
||
for _, l := range ls { | ||
d, err := l.Digest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
ccd, err := claircore.ParseDigest(d.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
u, err := rURL.Parse(path.Join("/", "v2", strings.TrimPrefix(repo.RepositoryStr(), repo.RegistryStr()), "blobs", d.String())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, u.String(), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res, err := c.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res.Body.Close() | ||
|
||
res.Request.Header.Del("User-Agent") | ||
out.Layers = append(out.Layers, &claircore.Layer{ | ||
Hash: ccd, | ||
URI: res.Request.URL.String(), | ||
Headers: res.Request.Header, | ||
}) | ||
} | ||
|
||
return &out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters