Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 88bf36a

Browse files
committed
Add username and password to lsp func
1 parent d4311a3 commit 88bf36a

File tree

8 files changed

+71
-28
lines changed

8 files changed

+71
-28
lines changed

commands/cmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
120120
var sb *types.Sbom
121121

122122
if ociDir == "" {
123-
sb, err = sbom.IndexImage(image, dockerCli)
123+
sb, err = sbom.IndexImage(image, sbom.IndexOptions{Cli: dockerCli})
124124
} else {
125125
sb, err = sbom.IndexPath(ociDir, image, dockerCli)
126126
}
@@ -174,7 +174,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
174174

175175
var sb *types.Sbom
176176
if ociDir == "" {
177-
sb, err = sbom.IndexImage(image, dockerCli)
177+
sb, err = sbom.IndexImage(image, sbom.IndexOptions{Cli: dockerCli})
178178
} else {
179179
sb, err = sbom.IndexPath(ociDir, image, dockerCli)
180180
}
@@ -184,7 +184,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
184184
if !includeSbom {
185185
sb.Artifacts = nil
186186
}
187-
return sbom.UploadSbom(sb, workspace, apiKey)
187+
return sbom.Upload(sb, workspace, apiKey)
188188
},
189189
}
190190
uploadCommandFlags := uploadCommand.Flags()
@@ -206,7 +206,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
206206
var sb *types.Sbom
207207

208208
if ociDir == "" {
209-
sb, err = sbom.IndexImage(image, dockerCli)
209+
sb, err = sbom.IndexImage(image, sbom.IndexOptions{Cli: dockerCli})
210210
} else {
211211
sb, err = sbom.IndexPath(ociDir, image, dockerCli)
212212
}

sbom/lsp.go renamed to lsp/lsp.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sbom
17+
package lsp
1818

1919
import (
2020
"crypto/sha256"
2121
"fmt"
2222
"io"
2323

2424
"github.com/anchore/syft/syft/source"
25+
"github.com/docker/index-cli-plugin/sbom"
2526
"github.com/pkg/errors"
2627

2728
"github.com/docker/cli/cli/command"
@@ -30,35 +31,54 @@ import (
3031
"github.com/docker/index-cli-plugin/sbom/util"
3132
)
3233

33-
func Send(image string, tx chan<- string) error {
34+
type Lsp struct {
35+
username string
36+
password string
37+
}
38+
39+
func New() *Lsp {
40+
return &Lsp{}
41+
}
42+
43+
func (l *Lsp) WithAuth(username, password string) *Lsp {
44+
l.username = username
45+
l.password = password
46+
return l
47+
}
48+
49+
func (l *Lsp) Send(image string, tx chan<- string) error {
3450
cmd, err := command.NewDockerCli()
3551
if err != nil {
3652
return errors.Wrap(err, "failed to create docker cli")
3753
}
3854
if err := cmd.Initialize(cliflags.NewClientOptions()); err != nil {
3955
return errors.Wrap(err, "failed to initialize docker cli")
4056
}
41-
sbom, err := IndexImage(image, cmd)
57+
sb, err := sbom.IndexImage(image, sbom.IndexOptions{
58+
Username: l.username,
59+
Password: l.password,
60+
Cli: cmd,
61+
})
4262
if err != nil {
4363
return errors.Wrap(err, "failed to create sbom")
4464
}
45-
err = sendSbom(sbom, tx)
65+
err = sbom.Send(sb, tx)
4666
if err != nil {
4767
return errors.Wrap(err, "failed to send sbom")
4868
}
4969
close(tx)
5070
return nil
5171
}
5272

53-
func SendFileHashes(image string, tx chan<- string) error {
73+
func (l *Lsp) SendFileHashes(image string, tx chan<- string) error {
5474
cmd, err := command.NewDockerCli()
5575
if err != nil {
5676
return errors.Wrap(err, "failed to create docker cli")
5777
}
5878
if err := cmd.Initialize(cliflags.NewClientOptions()); err != nil {
5979
return errors.Wrap(err, "failed to initialize docker cli")
6080
}
61-
cache, err := registry.SaveImage(image, cmd)
81+
cache, err := registry.SaveImage(image, l.username, l.password, cmd)
6282
if err != nil {
6383
return errors.Wrap(err, "failed to copy image")
6484
}

sbom/lsp_test.go renamed to lsp/lsp_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sbom
17+
package lsp
1818

1919
import (
2020
"testing"
@@ -24,7 +24,7 @@ func TestSend(t *testing.T) {
2424
tx := make(chan string, 10)
2525
transactions := make([]string, 0)
2626

27-
err := Send("alpine@sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c", tx)
27+
err := New().Send("alpine@sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c", tx)
2828
if err != nil {
2929
t.Fail()
3030
}
@@ -40,7 +40,7 @@ func TestSendFileHashes(t *testing.T) {
4040
tx := make(chan string, 100)
4141
transactions := make([]string, 0)
4242

43-
err := SendFileHashes("alpine@sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c", tx)
43+
err := New().SendFileHashes("alpine@sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c", tx)
4444
if err != nil {
4545
t.Fail()
4646
}

registry/save.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (c *ImageCache) Cleanup() {
221221
}
222222

223223
// SaveImage stores the v1.Image at path returned in OCI format
224-
func SaveImage(image string, cli command.Cli) (*ImageCache, error) {
224+
func SaveImage(image string, username string, password string, cli command.Cli) (*ImageCache, error) {
225225
skill.Log.Infof("Requesting image %s", image)
226226
ref, err := name.ParseReference(image)
227227
if err != nil {
@@ -288,13 +288,22 @@ func SaveImage(image string, cli command.Cli) (*ImageCache, error) {
288288
}, nil
289289
}
290290
// try remote image next
291-
desc, err := remote.Get(ref, withAuth())
291+
desc, err := remote.Get(ref, WithAuth(username, password))
292292
if err != nil {
293293
return nil, errors.Wrapf(err, "failed to pull image: %s", image)
294294
}
295295
img, err := desc.Image()
296296
if err != nil {
297-
return nil, errors.Wrapf(err, "failed to pull image: %s", image)
297+
ix, err := remote.Index(ref, WithAuth(username, password))
298+
if err != nil {
299+
return nil, errors.Wrapf(err, "failed to pull index: %s", image)
300+
}
301+
manifest, err := ix.IndexManifest()
302+
imageRef, err := name.ParseReference(fmt.Sprintf("%s@%s", ref.Name(), manifest.Manifests[0].Digest.String()))
303+
img, err = remote.Image(imageRef, WithAuth(username, password))
304+
if err != nil {
305+
return nil, errors.Wrapf(err, "failed to pull image: %s", image)
306+
}
298307
}
299308
var digest string
300309
tags := make([]string, 0)
@@ -325,7 +334,14 @@ func SaveImage(image string, cli command.Cli) (*ImageCache, error) {
325334
}, nil
326335
}
327336

328-
func withAuth() remote.Option {
337+
func WithAuth(username string, password string) remote.Option {
338+
// check passed username and password
339+
if username != "" && password != "" {
340+
return remote.WithAuth(&authn.Basic{
341+
Username: username,
342+
Password: password,
343+
})
344+
}
329345
// check registry token env var
330346
if token, ok := os.LookupEnv("ATOMIST_REGISTRY_TOKEN"); ok {
331347
return remote.WithAuth(&authn.Bearer{Token: token})

sbom/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func DiffImages(image1 string, image2 string, cli command.Cli, workspace string,
3232
resultChan := make(chan ImageIndexResult, 2)
3333
var wg sync.WaitGroup
3434
wg.Add(2)
35-
go indexImageAsync(&wg, image1, cli, resultChan)
36-
go indexImageAsync(&wg, image2, cli, resultChan)
35+
go indexImageAsync(&wg, image1, IndexOptions{Cli: cli}, resultChan)
36+
go indexImageAsync(&wg, image2, IndexOptions{Cli: cli}, resultChan)
3737
wg.Wait()
3838
close(resultChan)
3939

sbom/index.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ type ImageIndexResult struct {
4242
Error error
4343
}
4444

45-
func indexImageAsync(wg *sync.WaitGroup, image string, cli command.Cli, resultChan chan<- ImageIndexResult) {
45+
func indexImageAsync(wg *sync.WaitGroup, image string, options IndexOptions, resultChan chan<- ImageIndexResult) {
4646
defer wg.Done()
4747
var (
4848
sbom *types.Sbom
4949
cves *types.VulnerabilitiesByPurls
5050
err error
5151
)
52-
sbom, err = IndexImage(image, cli)
52+
sbom, err = IndexImage(image, options)
5353
if err == nil {
5454
cves, err = query.ForVulnerabilitiesInGraphQL(sbom)
5555
if err == nil {
@@ -63,6 +63,13 @@ func indexImageAsync(wg *sync.WaitGroup, image string, cli command.Cli, resultCh
6363
}
6464
}
6565

66+
type IndexOptions struct {
67+
Username string
68+
Password string
69+
70+
Cli command.Cli
71+
}
72+
6673
func IndexPath(path string, name string, cli command.Cli) (*types.Sbom, error) {
6774
cache, err := registry.ReadImage(name, path)
6875
if err != nil {
@@ -71,19 +78,19 @@ func IndexPath(path string, name string, cli command.Cli) (*types.Sbom, error) {
7178
return indexImage(cache, cli)
7279
}
7380

74-
func IndexImage(image string, cli command.Cli) (*types.Sbom, error) {
81+
func IndexImage(image string, options IndexOptions) (*types.Sbom, error) {
7582
if strings.HasPrefix(image, "sha256:") {
76-
configFilePath := cli.ConfigFile().Filename
83+
configFilePath := options.Cli.ConfigFile().Filename
7784
sbomFilePath := filepath.Join(filepath.Dir(configFilePath), "sbom", "sha256", image[7:], "sbom.json")
7885
if sbom := cachedSbom(sbomFilePath); sbom != nil {
7986
return sbom, nil
8087
}
8188
}
82-
cache, err := registry.SaveImage(image, cli)
89+
cache, err := registry.SaveImage(image, options.Username, options.Password, options.Cli)
8390
if err != nil {
8491
return nil, errors.Wrap(err, "failed to copy image")
8592
}
86-
return indexImage(cache, cli)
93+
return indexImage(cache, options.Cli)
8794
}
8895

8996
func indexImage(cache *registry.ImageCache, cli command.Cli) (*types.Sbom, error) {

sbom/monitor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func WatchImages(cli command.Cli) error {
4545

4646
func indexImageWorker(cli command.Cli, indexJobs <-chan types.ImageSummary) {
4747
for img := range indexJobs {
48-
_, err := IndexImage(img.ID, cli)
48+
_, err := IndexImage(img.ID, IndexOptions{Cli: cli})
4949
if err != nil {
5050
skill.Log.Warnf("Failed to index image %s", img.ID)
5151
delete(imageCache, img.ID)

sbom/upload.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
type TransactionMaker = func() skill.Transaction
3939

4040
// UploadSbom transact an image and its data into the data plane
41-
func UploadSbom(sb *types.Sbom, workspace string, apikey string) error {
41+
func Upload(sb *types.Sbom, workspace string, apikey string) error {
4242
correlationId := uuid.NewString()
4343
context := skill.RequestContext{
4444
Event: skill.EventIncoming{
@@ -65,7 +65,7 @@ func UploadSbom(sb *types.Sbom, workspace string, apikey string) error {
6565
return nil
6666
}
6767

68-
func sendSbom(sb *types.Sbom, entities chan<- string) error {
68+
func Send(sb *types.Sbom, entities chan<- string) error {
6969
correlationId := uuid.NewString()
7070
context := skill.RequestContext{
7171
Event: skill.EventIncoming{

0 commit comments

Comments
 (0)