Skip to content

Commit 21b68e1

Browse files
authored
fix: respect GITHUB_TOKEN to download artifacts from GHCR (#7580)
Signed-off-by: knqyf263 <[email protected]>
1 parent 71391a5 commit 21b68e1

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

docs/docs/references/troubleshooting.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,25 @@ $ TRIVY_INSECURE=true trivy image [YOUR_IMAGE]
7979
```
8080

8181
### GitHub Rate limiting
82+
Trivy uses GitHub API for [VEX repositories](../supply-chain/vex/repo.md).
8283

8384
!!! error
8485
``` bash
85-
$ trivy image ...
86+
$ trivy image --vex repo ...
8687
...
8788
API rate limit exceeded for xxx.xxx.xxx.xxx.
8889
```
8990

90-
Specify GITHUB_TOKEN for authentication
91-
https://developer.github.com/v3/#rate-limiting
91+
Specify GITHUB_TOKEN for [authentication](https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28)
9292

9393
```
94-
$ GITHUB_TOKEN=XXXXXXXXXX trivy alpine:3.10
94+
$ GITHUB_TOKEN=XXXXXXXXXX trivy image --vex repo [YOUR_IMAGE]
9595
```
9696

97+
!!! note
98+
`GITHUB_TOKEN` doesn't help with the rate limit for the vulnerability database and other assets.
99+
See https://github.com/aquasecurity/trivy/discussions/8009
100+
97101
### Unable to open JAR files
98102

99103
!!! error
@@ -217,6 +221,11 @@ Please remove the token and try downloading the DB again.
217221
docker logout ghcr.io
218222
```
219223

224+
or
225+
226+
```shell
227+
unset GITHUB_TOKEN
228+
```
220229

221230
## Homebrew
222231
### Scope error

magefiles/fixture.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"path/filepath"
88
"strings"
99

10+
"github.com/google/go-containerregistry/pkg/authn"
11+
"github.com/google/go-containerregistry/pkg/authn/github"
1012
"github.com/google/go-containerregistry/pkg/crane"
1113
v1 "github.com/google/go-containerregistry/pkg/v1"
1214
"github.com/magefile/mage/sh"
@@ -16,13 +18,15 @@ import (
1618

1719
const dir = "integration/testdata/fixtures/images/"
1820

21+
var auth = crane.WithAuthFromKeychain(authn.NewMultiKeychain(authn.DefaultKeychain, github.Keychain))
22+
1923
func fixtureContainerImages() error {
2024
var testImages = testutil.ImageName("", "", "")
2125

2226
if err := os.MkdirAll(dir, 0750); err != nil {
2327
return err
2428
}
25-
tags, err := crane.ListTags(testImages)
29+
tags, err := crane.ListTags(testImages, auth)
2630
if err != nil {
2731
return err
2832
}
@@ -53,7 +57,7 @@ func saveImage(subpath, tag string) error {
5357
}
5458
fmt.Printf("Downloading %s...\n", imgName)
5559

56-
img, err := crane.Pull(imgName)
60+
img, err := crane.Pull(imgName, auth)
5761
if err != nil {
5862
return err
5963
}
@@ -64,7 +68,6 @@ func saveImage(subpath, tag string) error {
6468
if err = sh.Run("gzip", tarPath); err != nil {
6569
return err
6670
}
67-
6871
return nil
6972
}
7073

@@ -77,12 +80,12 @@ func fixtureVMImages() error {
7780
if err := os.MkdirAll(dir, 0750); err != nil {
7881
return err
7982
}
80-
tags, err := crane.ListTags(testVMImages)
83+
tags, err := crane.ListTags(testVMImages, auth)
8184
if err != nil {
8285
return err
8386
}
8487
for _, tag := range tags {
85-
img, err := crane.Pull(fmt.Sprintf("%s:%s", testVMImages, tag))
88+
img, err := crane.Pull(fmt.Sprintf("%s:%s", testVMImages, tag), auth)
8689
if err != nil {
8790
return err
8891
}

pkg/remote/remote.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/google/go-containerregistry/pkg/authn"
12+
"github.com/google/go-containerregistry/pkg/authn/github"
1213
"github.com/google/go-containerregistry/pkg/name"
1314
v1 "github.com/google/go-containerregistry/pkg/v1"
1415
"github.com/google/go-containerregistry/pkg/v1/remote"
@@ -166,7 +167,7 @@ func authOptions(ctx context.Context, ref name.Reference, option types.RegistryO
166167
return []remote.Option{remote.WithAuth(&bearer)}
167168
default:
168169
// Use the keychain anyway at the end
169-
opts = append(opts, remote.WithAuthFromKeychain(authn.DefaultKeychain))
170+
opts = append(opts, remote.WithAuthFromKeychain(authn.NewMultiKeychain(authn.DefaultKeychain, github.Keychain)))
170171
return opts
171172
}
172173
}

0 commit comments

Comments
 (0)