Skip to content

Commit 9d1be41

Browse files
authored
refactor: fix auth error handling (#7615)
Signed-off-by: knqyf263 <[email protected]>
1 parent cb16d43 commit 9d1be41

File tree

9 files changed

+33
-73
lines changed

9 files changed

+33
-73
lines changed

internal/dbtest/fake.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ func NewFakeDB(t *testing.T, dbPath string, opts FakeDBOptions) *oci.Artifact {
6262
opt := ftypes.RegistryOptions{
6363
Insecure: false,
6464
}
65-
art, err := oci.NewArtifact("dummy", true, opt, oci.WithImage(img))
66-
require.NoError(t, err)
67-
68-
return art
65+
return oci.NewArtifact("dummy", true, opt, oci.WithImage(img))
6966
}
7067

7168
func ArchiveDir(t *testing.T, dir string) string {

pkg/db/db.go

+17-26
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,23 @@ func (c *Client) Download(ctx context.Context, dst string, opt types.RegistryOpt
153153
log.Debug("No metadata file")
154154
}
155155

156-
art, err := c.initOCIArtifact(opt)
157-
if err != nil {
158-
return xerrors.Errorf("OCI artifact error: %w", err)
159-
}
160-
161-
if err = art.Download(ctx, dst, oci.DownloadOption{MediaType: dbMediaType}); err != nil {
156+
art := c.initOCIArtifact(opt)
157+
if err := art.Download(ctx, dst, oci.DownloadOption{MediaType: dbMediaType}); err != nil {
158+
var terr *transport.Error
159+
if errors.As(err, &terr) {
160+
for _, diagnostic := range terr.Errors {
161+
// For better user experience
162+
if diagnostic.Code == transport.DeniedErrorCode || diagnostic.Code == transport.UnauthorizedErrorCode {
163+
// e.g. https://aquasecurity.github.io/trivy/latest/docs/references/troubleshooting/#db
164+
log.Warnf("See %s", doc.URL("/docs/references/troubleshooting/", "db"))
165+
break
166+
}
167+
}
168+
}
162169
return xerrors.Errorf("database download error: %w", err)
163170
}
164171

165-
if err = c.updateDownloadedAt(ctx, dst); err != nil {
172+
if err := c.updateDownloadedAt(ctx, dst); err != nil {
166173
return xerrors.Errorf("failed to update downloaded_at: %w", err)
167174
}
168175
return nil
@@ -194,27 +201,11 @@ func (c *Client) updateDownloadedAt(ctx context.Context, dbDir string) error {
194201
return nil
195202
}
196203

197-
func (c *Client) initOCIArtifact(opt types.RegistryOptions) (*oci.Artifact, error) {
204+
func (c *Client) initOCIArtifact(opt types.RegistryOptions) *oci.Artifact {
198205
if c.artifact != nil {
199-
return c.artifact, nil
200-
}
201-
202-
art, err := oci.NewArtifact(c.dbRepository.String(), c.quiet, opt)
203-
if err != nil {
204-
var terr *transport.Error
205-
if errors.As(err, &terr) {
206-
for _, diagnostic := range terr.Errors {
207-
// For better user experience
208-
if diagnostic.Code == transport.DeniedErrorCode || diagnostic.Code == transport.UnauthorizedErrorCode {
209-
// e.g. https://aquasecurity.github.io/trivy/latest/docs/references/troubleshooting/#db
210-
log.Warnf("See %s", doc.URL("/docs/references/troubleshooting/", "db"))
211-
break
212-
}
213-
}
214-
}
215-
return nil, xerrors.Errorf("OCI artifact error: %w", err)
206+
return c.artifact
216207
}
217-
return art, nil
208+
return oci.NewArtifact(c.dbRepository.String(), c.quiet, opt)
218209
}
219210

220211
func (c *Client) ShowInfo() error {

pkg/fanal/artifact/image/remote_sbom.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ func (a Artifact) inspectOCIReferrerSBOM(ctx context.Context) (artifact.Referenc
8787
func (a Artifact) parseReferrer(ctx context.Context, repo string, desc v1.Descriptor) (artifact.Reference, error) {
8888
const fileName string = "referrer.sbom"
8989
repoName := fmt.Sprintf("%s@%s", repo, desc.Digest)
90-
referrer, err := oci.NewArtifact(repoName, true, a.artifactOption.ImageOption.RegistryOptions)
91-
if err != nil {
92-
return artifact.Reference{}, xerrors.Errorf("OCI error: %w", err)
93-
}
9490

9591
tmpDir, err := os.MkdirTemp("", "trivy-sbom-*")
9692
if err != nil {
@@ -99,6 +95,7 @@ func (a Artifact) parseReferrer(ctx context.Context, repo string, desc v1.Descri
9995
defer os.RemoveAll(tmpDir)
10096

10197
// Download SBOM to local filesystem
98+
referrer := oci.NewArtifact(repoName, true, a.artifactOption.ImageOption.RegistryOptions)
10299
if err = referrer.Download(ctx, tmpDir, oci.DownloadOption{
103100
MediaType: desc.ArtifactType,
104101
Filename: fileName,

pkg/javadb/client.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,8 @@ func (u *Updater) Update() error {
5959
log.Info("Downloading the Java DB...")
6060

6161
// TODO: support remote options
62-
var a *oci.Artifact
63-
if a, err = oci.NewArtifact(u.repo.String(), u.quiet, u.registryOption); err != nil {
64-
return xerrors.Errorf("oci error: %w", err)
65-
}
66-
if err = a.Download(context.Background(), dbDir, oci.DownloadOption{MediaType: mediaType}); err != nil {
62+
art := oci.NewArtifact(u.repo.String(), u.quiet, u.registryOption)
63+
if err = art.Download(context.Background(), dbDir, oci.DownloadOption{MediaType: mediaType}); err != nil {
6764
return xerrors.Errorf("DB download error: %w", err)
6865
}
6966

pkg/module/command.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ func Install(ctx context.Context, dir, repo string, quiet bool, opt types.Regist
2323
}
2424

2525
log.Info("Installing the module from the repository...", log.String("repo", repo))
26-
artifact, err := oci.NewArtifact(repo, quiet, opt)
27-
if err != nil {
28-
return xerrors.Errorf("module initialize error: %w", err)
29-
}
26+
art := oci.NewArtifact(repo, quiet, opt)
3027

3128
dst := filepath.Join(dir, ref.Context().Name())
3229
log.Debug("Installing the module...", log.String("dst", dst))
33-
34-
if err = artifact.Download(ctx, dst, oci.DownloadOption{MediaType: mediaType}); err != nil {
30+
if err = art.Download(ctx, dst, oci.DownloadOption{MediaType: mediaType}); err != nil {
3531
return xerrors.Errorf("module download error: %w", err)
3632
}
3733

pkg/oci/artifact.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type Artifact struct {
5757
}
5858

5959
// NewArtifact returns a new artifact
60-
func NewArtifact(repo string, quiet bool, registryOpt types.RegistryOptions, opts ...Option) (*Artifact, error) {
60+
func NewArtifact(repo string, quiet bool, registryOpt types.RegistryOptions, opts ...Option) *Artifact {
6161
art := &Artifact{
6262
repository: repo,
6363
quiet: quiet,
@@ -67,7 +67,7 @@ func NewArtifact(repo string, quiet bool, registryOpt types.RegistryOptions, opt
6767
for _, o := range opts {
6868
o(art)
6969
}
70-
return art, nil
70+
return art
7171
}
7272

7373
func (a *Artifact) populate(ctx context.Context, opt types.RegistryOptions) error {

pkg/oci/artifact_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ func TestArtifact_Download(t *testing.T) {
116116
},
117117
}, nil)
118118

119-
artifact, err := oci.NewArtifact("repo", true, ftypes.RegistryOptions{}, oci.WithImage(img))
120-
require.NoError(t, err)
121-
119+
artifact := oci.NewArtifact("repo", true, ftypes.RegistryOptions{}, oci.WithImage(img))
122120
err = artifact.Download(context.Background(), tempDir, oci.DownloadOption{
123121
MediaType: tt.mediaType,
124122
})

pkg/policy/policy.go

+4-14
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,16 @@ func NewClient(cacheDir string, quiet bool, checkBundleRepo string, opts ...Opti
8989
}, nil
9090
}
9191

92-
func (c *Client) populateOCIArtifact(registryOpts types.RegistryOptions) error {
92+
func (c *Client) populateOCIArtifact(registryOpts types.RegistryOptions) {
9393
if c.artifact == nil {
9494
log.Debug("Loading check bundle", log.String("repository", c.checkBundleRepo))
95-
art, err := oci.NewArtifact(c.checkBundleRepo, c.quiet, registryOpts)
96-
if err != nil {
97-
return xerrors.Errorf("OCI artifact error: %w", err)
98-
}
99-
c.artifact = art
95+
c.artifact = oci.NewArtifact(c.checkBundleRepo, c.quiet, registryOpts)
10096
}
101-
return nil
10297
}
10398

10499
// DownloadBuiltinPolicies download default policies from GitHub Pages
105100
func (c *Client) DownloadBuiltinPolicies(ctx context.Context, registryOpts types.RegistryOptions) error {
106-
if err := c.populateOCIArtifact(registryOpts); err != nil {
107-
return xerrors.Errorf("OPA bundle error: %w", err)
108-
}
101+
c.populateOCIArtifact(registryOpts)
109102

110103
dst := c.contentDir()
111104
if err := c.artifact.Download(ctx, dst, oci.DownloadOption{MediaType: policyMediaType}); err != nil {
@@ -165,10 +158,7 @@ func (c *Client) NeedsUpdate(ctx context.Context, registryOpts types.RegistryOpt
165158
return false, nil
166159
}
167160

168-
if err = c.populateOCIArtifact(registryOpts); err != nil {
169-
return false, xerrors.Errorf("OPA bundle error: %w", err)
170-
}
171-
161+
c.populateOCIArtifact(registryOpts)
172162
digest, err := c.artifact.Digest(ctx)
173163
if err != nil {
174164
return false, xerrors.Errorf("digest error: %w", err)

pkg/policy/policy_test.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ func TestClient_LoadBuiltinPolicies(t *testing.T) {
116116
}, nil)
117117

118118
// Mock OCI artifact
119-
art, err := oci.NewArtifact("repo", true, ftypes.RegistryOptions{}, oci.WithImage(img))
120-
require.NoError(t, err)
121-
119+
art := oci.NewArtifact("repo", true, ftypes.RegistryOptions{}, oci.WithImage(img))
122120
c, err := policy.NewClient(tt.cacheDir, true, "", policy.WithOCIArtifact(art))
123121
require.NoError(t, err)
124122

@@ -257,9 +255,7 @@ func TestClient_NeedsUpdate(t *testing.T) {
257255
require.NoError(t, err)
258256
}
259257

260-
art, err := oci.NewArtifact("repo", true, ftypes.RegistryOptions{}, oci.WithImage(img))
261-
require.NoError(t, err)
262-
258+
art := oci.NewArtifact("repo", true, ftypes.RegistryOptions{}, oci.WithImage(img))
263259
c, err := policy.NewClient(tmpDir, true, "", policy.WithOCIArtifact(art), policy.WithClock(tt.clock))
264260
require.NoError(t, err)
265261

@@ -361,9 +357,7 @@ func TestClient_DownloadBuiltinPolicies(t *testing.T) {
361357
}, nil)
362358

363359
// Mock OCI artifact
364-
art, err := oci.NewArtifact("repo", true, ftypes.RegistryOptions{}, oci.WithImage(img))
365-
require.NoError(t, err)
366-
360+
art := oci.NewArtifact("repo", true, ftypes.RegistryOptions{}, oci.WithImage(img))
367361
c, err := policy.NewClient(tempDir, true, "", policy.WithClock(tt.clock), policy.WithOCIArtifact(art))
368362
require.NoError(t, err)
369363

0 commit comments

Comments
 (0)