Skip to content

Commit ca0d972

Browse files
authored
feat(image): add registry options (#3906)
1 parent 0336555 commit ca0d972

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+840
-476
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
- name: Lint
4242
uses: golangci/[email protected]
4343
with:
44-
version: v1.49
44+
version: v1.52
4545
args: --deadline=30m
4646
skip-cache: true # https://github.com/golangci/golangci-lint-action/issues/244#issuecomment-1052197778
4747
if: matrix.operating-system == 'ubuntu-latest'

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ $(GOBIN)/crane:
2828
go install github.com/google/go-containerregistry/cmd/[email protected]
2929

3030
$(GOBIN)/golangci-lint:
31-
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOBIN) v1.49.0
31+
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOBIN) v1.52.2
3232

3333
$(GOBIN)/labeler:
3434
go install github.com/knqyf263/labeler@latest
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
Docker Hub needs `TRIVY_USERNAME` and `TRIVY_PASSWORD`.
2-
You don't need to set ENV vars when download from public repository.
3-
4-
```bash
5-
export TRIVY_USERNAME={DOCKERHUB_USERNAME}
6-
export TRIVY_PASSWORD={DOCKERHUB_PASSWORD}
7-
```
1+
See [here](./index.md) for the detail.
2+
You don't need to provide a credential when download from public repository.
Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
1-
Trivy can download images from a private registry, without installing `Docker` or any other 3rd party tools.
2-
That's because it's easy to run in a CI process.
1+
Trivy can download images from a private registry without the need for installing Docker or any other 3rd party tools.
2+
This makes it easy to run within a CI process.
33

4-
All you have to do is install `Trivy` and set ENV vars.
4+
## Credential
5+
To use Trivy with private images, simply install it and provide your credentials:
6+
7+
```shell
8+
$ TRIVY_USERNAME=YOUR_USERNAME TRIVY_PASSWORD=YOUR_PASSWORD trivy image YOUR_PRIVATE_IMAGE
9+
```
10+
11+
Trivy also supports providing credentials through CLI flags:
12+
13+
```shell
14+
$ TRIVY_PASSWORD=YOUR_PASSWORD trivy image --username YOUR_USERNAME YOUR_PRIVATE_IMAGE
15+
```
16+
17+
!!! warning
18+
The CLI flag `--password` is available, but its use is not recommended for security reasons.
19+
20+
You can also store your credentials in `trivy.yaml`.
21+
For more information, please refer to [the documentation](../../references/customization/config-file.md).
22+
23+
It can handle multiple sets of credentials as well:
24+
25+
```shell
26+
$ export TRIVY_USERNAME=USERNAME1,USERNAME2
27+
$ export TRIVY_PASSWORD=PASSWORD1,PASSWORD2
28+
$ trivy image YOUR_PRIVATE_IMAGE
29+
```
30+
31+
In the example above, Trivy attempts to use two pairs of credentials:
32+
33+
- USERNAME1/PASSWORD1
34+
- USERNAME2/PASSWORD2
35+
36+
Please note that the number of usernames and passwords must be the same.
37+
38+
## docker login
39+
If you have Docker configured locally and have set up the credentials, Trivy can access them.
40+
41+
```shell
42+
$ docker login ghcr.io
43+
Username:
44+
Password:
45+
$ trivy image ghcr.io/your/private_image
46+
```
47+
48+
!!! note
49+
`docker login` can be used with any container runtime, such as Podman.

docs/docs/references/customization/config-file.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,23 @@ db:
163163
java-repository: ghcr.io/aquasecurity/trivy-java-db
164164
```
165165
166+
## Registry Options
167+
168+
```yaml
169+
registry:
170+
# Same as '--username'
171+
# Default is empty
172+
username:
173+
174+
# Same as '--password'
175+
# Default is empty
176+
password:
177+
178+
# Same as '--registry-token'
179+
# Default is empty
180+
registry-token:
181+
```
182+
166183
## Image Options
167184
Available with container image scanning
168185

docs/docs/target/container_image.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ $ trivy image --compliance docker-cis [YOUR_IMAGE_NAME]
437437
!!! note
438438
The `Issues` column represent the total number of failed checks for this control.
439439

440+
## Authentication
441+
Please reference [this page](../advanced/private-registries/index.md).
442+
440443
## Options
441444
### Scan Image on a specific Architecture and OS
442445
By default, Trivy loads an image on a "linux/amd64" machine.

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ require (
3232
github.com/aws/aws-sdk-go-v2/config v1.18.15
3333
github.com/aws/aws-sdk-go-v2/service/ec2 v1.89.1
3434
github.com/aws/aws-sdk-go-v2/service/sts v1.18.5
35-
github.com/caarlos0/env/v6 v6.10.1
3635
github.com/cenkalti/backoff v2.2.1+incompatible
3736
github.com/cheggaaa/pb/v3 v3.1.2
3837
github.com/containerd/containerd v1.6.19

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,6 @@ github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0 h1:nvj0OLI3YqYXe
513513
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
514514
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
515515
github.com/bytecodealliance/wasmtime-go v1.0.0 h1:9u9gqaUiaJeN5IoD1L7egD8atOnTGyJcNp8BhkL9cUU=
516-
github.com/caarlos0/env/v6 v6.10.1 h1:t1mPSxNpei6M5yAeu1qtRdPAK29Nbcf/n3G7x+b3/II=
517-
github.com/caarlos0/env/v6 v6.10.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
518516
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
519517
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
520518
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=

pkg/commands/app.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ func NewImageCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
228228
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
229229
ModuleFlagGroup: flag.NewModuleFlagGroup(),
230230
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
231+
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
231232
RegoFlagGroup: flag.NewRegoFlagGroup(),
232233
ReportFlagGroup: reportFlagGroup,
233234
ScanFlagGroup: flag.NewScanFlagGroup(),
@@ -304,6 +305,7 @@ func NewFilesystemCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
304305
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
305306
ModuleFlagGroup: flag.NewModuleFlagGroup(),
306307
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
308+
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
307309
RegoFlagGroup: flag.NewRegoFlagGroup(),
308310
ReportFlagGroup: reportFlagGroup,
309311
ScanFlagGroup: flag.NewScanFlagGroup(),
@@ -359,6 +361,7 @@ func NewRootfsCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
359361
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
360362
ModuleFlagGroup: flag.NewModuleFlagGroup(),
361363
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
364+
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
362365
RegoFlagGroup: flag.NewRegoFlagGroup(),
363366
ReportFlagGroup: reportFlagGroup,
364367
ScanFlagGroup: flag.NewScanFlagGroup(),
@@ -415,6 +418,7 @@ func NewRepositoryCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
415418
LicenseFlagGroup: flag.NewLicenseFlagGroup(),
416419
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
417420
ModuleFlagGroup: flag.NewModuleFlagGroup(),
421+
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
418422
RegoFlagGroup: flag.NewRegoFlagGroup(),
419423
RemoteFlagGroup: flag.NewClientFlags(), // for client/server mode
420424
ReportFlagGroup: reportFlagGroup,
@@ -472,6 +476,7 @@ func NewClientCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
472476
CacheFlagGroup: flag.NewCacheFlagGroup(),
473477
DBFlagGroup: flag.NewDBFlagGroup(),
474478
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
479+
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
475480
RegoFlagGroup: flag.NewRegoFlagGroup(),
476481
RemoteFlagGroup: remoteFlags,
477482
ReportFlagGroup: flag.NewReportFlagGroup(),
@@ -567,10 +572,11 @@ func NewConfigCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
567572
}
568573

569574
configFlags := &flag.Flags{
570-
CacheFlagGroup: flag.NewCacheFlagGroup(),
571-
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
572-
ModuleFlagGroup: flag.NewModuleFlagGroup(),
573-
RegoFlagGroup: flag.NewRegoFlagGroup(),
575+
CacheFlagGroup: flag.NewCacheFlagGroup(),
576+
MisconfFlagGroup: flag.NewMisconfFlagGroup(),
577+
ModuleFlagGroup: flag.NewModuleFlagGroup(),
578+
RegistryFlagGroup: flag.NewRegistryFlagGroup(),
579+
RegoFlagGroup: flag.NewRegoFlagGroup(),
574580
K8sFlagGroup: &flag.K8sFlagGroup{
575581
// disable unneeded flags
576582
K8sVersion: &flag.K8sVersionFlag,

pkg/commands/artifact/inject.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
// initializeDockerScanner is for container image scanning in standalone mode
2323
// e.g. dockerd, container registry, podman, etc.
2424
func initializeDockerScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache,
25-
localArtifactCache cache.LocalArtifactCache, dockerOpt types.DockerOption, artifactOption artifact.Option) (
25+
localArtifactCache cache.LocalArtifactCache, remoteOpt types.RemoteOptions, artifactOption artifact.Option) (
2626
scanner.Scanner, func(), error) {
2727
wire.Build(scanner.StandaloneDockerSet)
2828
return scanner.Scanner{}, nil, nil
@@ -69,7 +69,7 @@ func initializeVMScanner(ctx context.Context, filePath string, artifactCache cac
6969
// initializeRemoteDockerScanner is for container image scanning in client/server mode
7070
// e.g. dockerd, container registry, podman, etc.
7171
func initializeRemoteDockerScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache,
72-
remoteScanOptions client.ScannerOption, dockerOpt types.DockerOption, artifactOption artifact.Option) (
72+
remoteScanOptions client.ScannerOption, remoteOpt types.RemoteOptions, artifactOption artifact.Option) (
7373
scanner.Scanner, func(), error) {
7474
wire.Build(scanner.RemoteDockerSet)
7575
return scanner.Scanner{}, nil, nil

0 commit comments

Comments
 (0)