Skip to content

Commit

Permalink
feat(server): add version endpoint (#4869)
Browse files Browse the repository at this point in the history
* feat(server): add version endpoint

* fix panic and test

* move version.go

* move version variable

* add docs about endpoints

* move testdata

* refactor

* update build command

* refactor
  • Loading branch information
nikpivkin authored Aug 20, 2023
1 parent 63cd41d commit d442176
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 141 deletions.
6 changes: 1 addition & 5 deletions cmd/trivy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import (
_ "modernc.org/sqlite" // sqlite driver for RPM DB and Java DB
)

var (
version = "dev"
)

func main() {
if err := run(); err != nil {
log.Fatal(err)
Expand All @@ -35,7 +31,7 @@ func run() error {
return nil
}

app := commands.NewApp(version)
app := commands.NewApp()
if err := app.Execute(); err != nil {
return err
}
Expand Down
42 changes: 42 additions & 0 deletions docs/docs/references/modes/client-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,48 @@ $ trivy server --listen localhost:8080 --token dummy
$ trivy image --server http://localhost:8080 --token dummy alpine:3.10
```

## Endpoints

### Health
Checks whether the Trivy server is running. Authentication is not required.

Example request:
```bash
curl -s 0.0.0.0:8080/healthz
ok
```

Returns the `200 OK` status if the request was successful.
### Version

Returns the version of the Trivy and all components (db, policy). Authentication is not required.

Example request:
```bash
curl -s 0.0.0.0:8080/version | jq
{
"Version": "dev",
"VulnerabilityDB": {
"Version": 2,
"NextUpdate": "2023-07-25T14:15:29.876639806Z",
"UpdatedAt": "2023-07-25T08:15:29.876640206Z",
"DownloadedAt": "2023-07-25T09:36:25.599004Z"
},
"JavaDB": {
"Version": 1,
"NextUpdate": "2023-07-28T01:03:52.169192565Z",
"UpdatedAt": "2023-07-25T01:03:52.169192765Z",
"DownloadedAt": "2023-07-25T09:37:48.906152Z"
},
"PolicyBundle": {
"Digest": "sha256:829832357626da2677955e3b427191212978ba20012b6eaa03229ca28569ae43",
"DownloadedAt": "2023-07-23T11:40:33.122462Z"
}
}
```

Returns the `200 OK` status if the request was successful.

## Architecture

![architecture](../../../imgs/client-server.png)
Expand Down
2 changes: 1 addition & 1 deletion goreleaser-canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ builds:
ldflags:
- -s -w
- "-extldflags '-static'"
- -X main.version={{.Version}}
- -X github.com/aquasecurity/trivy/pkg/version.ver={{.Version}}
env:
- CGO_ENABLED=0
goos:
Expand Down
8 changes: 4 additions & 4 deletions goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ builds:
ldflags:
- -s -w
- "-extldflags '-static'"
- -X main.version={{.Version}}
- -X github.com/aquasecurity/trivy/pkg/version.ver={{.Version}}
env:
- CGO_ENABLED=0
goos:
Expand All @@ -26,7 +26,7 @@ builds:
ldflags:
- -s -w
- "-extldflags '-static'"
- -X main.version={{.Version}}
- -X github.com/aquasecurity/trivy/pkg/version.ver={{.Version}}
env:
- CGO_ENABLED=0
goos:
Expand All @@ -41,7 +41,7 @@ builds:
ldflags:
- -s -w
- "-extldflags '-static'"
- -X main.version={{.Version}}
- -X github.com/aquasecurity/trivy/pkg/version.ver={{.Version}}
env:
- CGO_ENABLED=0
goos:
Expand All @@ -57,7 +57,7 @@ builds:
ldflags:
- -s -w
- "-extldflags '-static'"
- -X main.version={{.Version}}
- -X github.com/aquasecurity/trivy/pkg/version.ver={{.Version}}
env:
- CGO_ENABLED=0
goos:
Expand Down
2 changes: 1 addition & 1 deletion integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func readSpdxJson(t *testing.T, filePath string) *spdx.Document {

func execute(osArgs []string) error {
// Setup CLI App
app := commands.NewApp("dev")
app := commands.NewApp()
app.SetOut(io.Discard)

// Run Trivy
Expand Down
8 changes: 2 additions & 6 deletions magefiles/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ import (

// Generate CLI references
func main() {
ver, err := version()
if err != nil {
log.Fatal(err)
}
// Set a dummy path for the documents
flag.CacheDirFlag.Default = "/path/to/cache"
flag.ModuleDirFlag.Default = "$HOME/.trivy/modules"

cmd := commands.NewApp(ver)
cmd := commands.NewApp()
cmd.DisableAutoGenTag = true
if err = doc.GenMarkdownTree(cmd, "./docs/docs/references/configuration/cli"); err != nil {
if err := doc.GenMarkdownTree(cmd, "./docs/docs/references/configuration/cli"); err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func buildLdflags() (string, error) {
if err != nil {
return "", err
}
return fmt.Sprintf("-s -w -X=main.version=%s", ver), nil
return fmt.Sprintf("-s -w -X=github.com/aquasecurity/trivy/pkg/version.ver=%s", ver), nil
}

type Tool mg.Namespace
Expand Down
Loading

0 comments on commit d442176

Please sign in to comment.