Skip to content

Commit f1e3974

Browse files
authored
Incorporate docker host and context functionality (#8)
1 parent 62afe56 commit f1e3974

7 files changed

Lines changed: 79 additions & 1109 deletions

File tree

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
- "v*"
1010

1111
env:
12-
GO_VERSION: "1.17.x"
12+
GO_VERSION: "1.18.x"
1313

1414
jobs:
1515
quality-gate:

.github/workflows/validations.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
pull_request:
88

99
env:
10-
GO_VERSION: "1.17.x"
10+
GO_VERSION: "1.18.x"
1111

1212
jobs:
1313

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ $(RESULTS_DIR):
6868
bootstrap-tools:
6969
$(call title,Bootstrapping tools)
7070
mkdir -p $(TEMP_DIR)
71-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TEMP_DIR)/ v1.42.1
71+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TEMP_DIR)/ v1.45.0
7272
curl -sSfL https://raw.githubusercontent.com/wagoodman/go-bouncer/master/bouncer.sh | sh -s -- -b $(TEMP_DIR)/ v0.3.0
7373
curl -sSfL https://raw.githubusercontent.com/anchore/chronicle/main/install.sh | sh -s -- -b $(TEMP_DIR)/ v0.3.0
7474
.github/scripts/goreleaser-install.sh -b $(TEMP_DIR)/ v1.5.0

cmd/root.go

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"bytes"
5+
"context"
56
"errors"
67
"fmt"
78
"strings"
@@ -19,7 +20,9 @@ import (
1920
"github.com/wagoodman/go-partybus"
2021

2122
"github.com/anchore/stereoscope"
23+
"github.com/anchore/stereoscope/pkg/file"
2224
"github.com/anchore/stereoscope/pkg/image"
25+
stereoscopeDocker "github.com/anchore/stereoscope/pkg/image/docker"
2326
"github.com/anchore/syft/syft"
2427
"github.com/anchore/syft/syft/event"
2528
"github.com/anchore/syft/syft/pkg/cataloger"
@@ -37,7 +40,7 @@ const (
3740
shortDescription = "View the packaged-based Software Bill Of Materials (SBOM) for an image"
3841
)
3942

40-
func cmd(_ command.Cli) *cobra.Command {
43+
func cmd(dockerCli command.Cli) *cobra.Command {
4144
c := &cobra.Command{
4245
Use: "sbom",
4346
Short: shortDescription,
@@ -47,7 +50,7 @@ func cmd(_ command.Cli) *cobra.Command {
4750
SilenceUsage: true,
4851
SilenceErrors: true,
4952
Version: version.FromBuild().Version,
50-
RunE: run,
53+
RunE: newRunner(dockerCli).run,
5154
ValidArgsFunction: dockerImageValidArgsFunction,
5255
}
5356

@@ -174,7 +177,17 @@ func validateInputArgs(cmd *cobra.Command, args []string) error {
174177
return cobra.ExactArgs(1)(cmd, args)
175178
}
176179

177-
func run(_ *cobra.Command, args []string) error {
180+
type runner struct {
181+
client command.Cli
182+
}
183+
184+
func newRunner(client command.Cli) runner {
185+
return runner{
186+
client: client,
187+
}
188+
}
189+
190+
func (r runner) run(_ *cobra.Command, args []string) error {
178191
writer, err := makeWriter([]string{appConfig.Format}, appConfig.Output)
179192
if err != nil {
180193
return err
@@ -186,16 +199,16 @@ func run(_ *cobra.Command, args []string) error {
186199
}
187200
}()
188201

189-
si := source.Input{
190-
UserInput: args[0],
191-
Scheme: source.ImageScheme,
192-
ImageSource: image.DockerDaemonSource,
193-
Location: args[0],
194-
Platform: appConfig.Platform,
202+
var platform *image.Platform
203+
if appConfig.Platform != "" {
204+
platform, err = image.NewPlatform(appConfig.Platform)
205+
if err != nil {
206+
return fmt.Errorf("invalid platform provided: %w", err)
207+
}
195208
}
196209

197210
return eventLoop(
198-
sbomExecWorker(si, writer),
211+
sbomExecWorker(args[0], r.client, platform, writer),
199212
setupSignals(),
200213
eventSubscription,
201214
stereoscope.Cleanup,
@@ -236,21 +249,42 @@ func generateSBOM(src *source.Source) (*sbom.SBOM, error) {
236249
return &s, nil
237250
}
238251

239-
func sbomExecWorker(si source.Input, writer sbom.Writer) <-chan error {
252+
func sbomExecWorker(userInput string, dockerCli command.Cli, platform *image.Platform, writer sbom.Writer) <-chan error {
240253
errs := make(chan error)
241254
go func() {
242255
defer close(errs)
243256

244-
src, cleanup, err := source.New(si, nil, appConfig.Exclusions)
245-
if cleanup != nil {
246-
defer cleanup()
257+
provider := stereoscopeDocker.NewProviderFromDaemon(
258+
userInput,
259+
file.NewTempDirGenerator(internal.ApplicationName),
260+
dockerCli.Client(),
261+
platform,
262+
)
263+
img, err := provider.Provide(context.Background())
264+
defer func() {
265+
if err := img.Cleanup(); err != nil {
266+
log.Warnf("failed to clean up image: %+v", err)
267+
}
268+
}()
269+
if err != nil {
270+
errs <- fmt.Errorf("failed to fetch the image %q: %w", userInput, err)
271+
return
247272
}
273+
274+
err = img.Read()
275+
if err != nil {
276+
errs <- fmt.Errorf("failed to read the image %q: %w", userInput, err)
277+
return
278+
}
279+
280+
src, err := source.NewFromImage(img, userInput)
248281
if err != nil {
249-
errs <- fmt.Errorf("failed to construct source from user input %q: %w", si.UserInput, err)
282+
errs <- fmt.Errorf("failed to construct source from user input %q: %w", userInput, err)
250283
return
251284
}
285+
src.Exclusions = appConfig.Exclusions
252286

253-
s, err := generateSBOM(src)
287+
s, err := generateSBOM(&src)
254288
if err != nil {
255289
errs <- err
256290
return

go.mod

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module github.com/docker/sbom-cli-plugin
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/Microsoft/hcsshim v0.9.2 // indirect
77
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
8-
github.com/anchore/stereoscope v0.0.0-20220307154759-8a5a70c227d3
9-
github.com/anchore/syft v0.41.1
8+
github.com/anchore/stereoscope v0.0.0-20220322123031-7a744f443e99
9+
github.com/anchore/syft v0.42.2
1010
github.com/containerd/containerd v1.5.10 // indirect
1111
github.com/containerd/continuity v0.2.2 // indirect
1212
github.com/docker/cli v20.10.12+incompatible
@@ -15,7 +15,7 @@ require (
1515
github.com/fvbommel/sortorder v1.0.2 // indirect
1616
github.com/gookit/color v1.4.2
1717
github.com/hashicorp/go-multierror v1.1.1
18-
github.com/moby/sys/mount v0.3.0 // indirect
18+
github.com/moby/sys/mount v0.3.1 // indirect
1919
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
2020
github.com/sirupsen/logrus v1.8.1
2121
github.com/spf13/cobra v1.3.0
@@ -33,13 +33,14 @@ require (
3333

3434
require (
3535
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
36-
github.com/CycloneDX/cyclonedx-go v0.4.0 // indirect
36+
github.com/CycloneDX/cyclonedx-go v0.5.0 // indirect
3737
github.com/Microsoft/go-winio v0.5.1 // indirect
3838
github.com/acobaugh/osrelease v0.1.0 // indirect
39+
github.com/anchore/go-macholibre v0.0.0-20220308212642-53e6d0aaf6fb // indirect
3940
github.com/anchore/go-rpmdb v0.0.0-20210914181456-a9c52348da63 // indirect
4041
github.com/anchore/go-testutils v0.0.0-20200925183923-d5f45b0d3c04 // indirect
4142
github.com/anchore/go-version v1.2.2-0.20200701162849-18adb9c92b9b // indirect
42-
github.com/anchore/packageurl-go v0.0.0-20210922164639-b3fa992ebd29 // indirect
43+
github.com/anchore/packageurl-go v0.1.1-0.20220314153042-1bcd40e5206b // indirect
4344
github.com/andybalholm/brotli v1.0.4 // indirect
4445
github.com/beorn7/perks v1.0.1 // indirect
4546
github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect
@@ -84,7 +85,7 @@ require (
8485
github.com/mitchellh/go-homedir v1.1.0 // indirect
8586
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
8687
github.com/mitchellh/mapstructure v1.4.3 // indirect
87-
github.com/moby/sys/mountinfo v0.5.0 // indirect
88+
github.com/moby/sys/mountinfo v0.6.0 // indirect
8889
github.com/morikuni/aec v1.0.0 // indirect
8990
github.com/nwaples/rardecode v1.1.0 // indirect
9091
github.com/olekukonko/tablewriter v0.0.5 // indirect

0 commit comments

Comments
 (0)