Skip to content

Commit 3f4129b

Browse files
authored
Implement /api/v1/version for rekor-server (sigstore#569)
* rekor-server: Implement /api/v1/version This implements a version endpoint for rekor. This helps figure out the version the server is currently running. It could later be used to implement version compatibility with CLI utilities. Example: λ rekor main» curl -s localhost:3000/api/v1/version | jq { "builddate": "'2021-12-27T13:20:32Z'", "commit": "12d1a47c7ac986932a2734cb855c642ac01ffde4", "treestate": "dirty", "version": "v0.4.0-15-g12d1a47-dirty" } This removes some duplication of the build flags and inserts them into /pkg/api which is then reused across the utilities. Signed-off-by: Morten Linderud <morten@linderud.pw> Signed-off-by: Morten Linderud <morten@linderud.pw> * Generated files Signed-off-by: Morten Linderud <morten@linderud.pw>
1 parent 12d1a47 commit 3f4129b

19 files changed

+1079
-48
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ export KO_DOCKER_REPO=$(KO_PREFIX)
4949
SWAGGER := $(TOOLS_BIN_DIR)/swagger
5050
GO-FUZZ-BUILD := $(TOOLS_BIN_DIR)/go-fuzz-build
5151

52-
CLI_PKG=github.com/sigstore/rekor/cmd/rekor-cli/app
53-
CLI_LDFLAGS=-X $(CLI_PKG).GitVersion=$(GIT_VERSION) -X $(CLI_PKG).gitCommit=$(GIT_HASH) -X $(CLI_PKG).gitTreeState=$(GIT_TREESTATE) -X $(CLI_PKG).buildDate=$(BUILD_DATE)
52+
FLAG_PKG=github.com/sigstore/rekor/pkg/api
53+
REKOR_LDFLAGS=-X $(FLAG_PKG).GitVersion=$(GIT_VERSION) -X $(FLAG_PKG).GitCommit=$(GIT_HASH) -X $(FLAG_PKG).GitTreeState=$(GIT_TREESTATE) -X $(FLAG_PKG).BuildDate=$(BUILD_DATE)
54+
55+
CLI_LDFLAGS=$(REKOR_LDFLAGS)
56+
SERVER_LDFLAGS=$(REKOR_LDFLAGS)
5457

55-
SERVER_PKG=github.com/sigstore/rekor/cmd/rekor-server/app
56-
SERVER_LDFLAGS=-X $(SERVER_PKG).GitVersion=$(GIT_VERSION) -X $(SERVER_PKG).gitCommit=$(GIT_HASH) -X $(SERVER_PKG).gitTreeState=$(GIT_TREESTATE) -X $(SERVER_PKG).buildDate=$(BUILD_DATE)
5758

5859
$(GENSRC): $(SWAGGER) $(OPENAPIDEPS)
5960
$(SWAGGER) generate client -f openapi.yaml -q -r COPYRIGHT.txt -t pkg/generated --default-consumes application/json\;q=1 --additional-initialism=TUF

cmd/rekor-cli/app/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/spf13/pflag"
2626
"github.com/spf13/viper"
2727

28+
"github.com/sigstore/rekor/pkg/api"
2829
"github.com/sigstore/rekor/pkg/log"
2930

3031
// these imports are to call the packages' init methods
@@ -72,7 +73,7 @@ func init() {
7273
}
7374

7475
// look for the default version and replace it, if found, from runtime build info
75-
if GitVersion != "devel" {
76+
if api.GitVersion != "devel" {
7677
return
7778
}
7879

@@ -82,7 +83,7 @@ func init() {
8283
}
8384
// Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-cli/app.GitVersion=1.2.3
8485
// Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-cli
85-
GitVersion = bi.Main.Version
86+
api.GitVersion = bi.Main.Version
8687
}
8788

8889
func initConfig(cmd *cobra.Command) error {

cmd/rekor-cli/app/version.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,10 @@ import (
2323
"text/tabwriter"
2424

2525
"github.com/pkg/errors"
26+
"github.com/sigstore/rekor/pkg/api"
2627
"github.com/spf13/cobra"
2728
)
2829

29-
// Base version information.
30-
//
31-
// This is the fallback data used when version information from git is not
32-
// provided via go ldflags (e.g. via Makefile).
33-
var (
34-
// Output of "git describe". The prerequisite is that the branch should be
35-
// tagged using the correct versioning strategy.
36-
GitVersion string = "devel"
37-
// SHA1 from git, output of $(git rev-parse HEAD)
38-
gitCommit = "unknown"
39-
// State of git tree, either "clean" or "dirty"
40-
gitTreeState = "unknown"
41-
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
42-
buildDate = "unknown"
43-
)
44-
4530
type versionOptions struct {
4631
json bool
4732
}
@@ -93,10 +78,10 @@ func VersionInfo() Info {
9378
// These variables typically come from -ldflags settings and in
9479
// their absence fallback to the global defaults set above.
9580
return Info{
96-
GitVersion: GitVersion,
97-
GitCommit: gitCommit,
98-
GitTreeState: gitTreeState,
99-
BuildDate: buildDate,
81+
GitVersion: api.GitVersion,
82+
GitCommit: api.GitCommit,
83+
GitTreeState: api.GitTreeState,
84+
BuildDate: api.BuildDate,
10085
GoVersion: runtime.Version(),
10186
Compiler: runtime.Compiler,
10287
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),

cmd/rekor-server/app/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/spf13/cobra"
2727
"github.com/spf13/viper"
2828

29+
"github.com/sigstore/rekor/pkg/api"
2930
"github.com/sigstore/rekor/pkg/log"
3031
)
3132

@@ -91,7 +92,7 @@ func init() {
9192
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
9293

9394
// look for the default version and replace it, if found, from runtime build info
94-
if GitVersion != "devel" {
95+
if api.GitVersion != "devel" {
9596
return
9697
}
9798
log.Logger.Debugf("pprof enabled %v", enablePprof)
@@ -116,7 +117,7 @@ func init() {
116117
}
117118
// Version is set in artifacts built with -X github.com/sigstore/rekor/cmd/rekor-server/app.GitVersion=1.2.3
118119
// Ensure version is also set when installed via go install github.com/sigstore/rekor/cmd/rekor-server
119-
GitVersion = bi.Main.Version
120+
api.GitVersion = bi.Main.Version
120121
}
121122

122123
// initConfig reads in config file and ENV variables if set.

cmd/rekor-server/app/version.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,10 @@ import (
2323
"text/tabwriter"
2424

2525
"github.com/pkg/errors"
26+
"github.com/sigstore/rekor/pkg/api"
2627
"github.com/spf13/cobra"
2728
)
2829

29-
// Base version information.
30-
//
31-
// This is the fallback data used when version information from git is not
32-
// provided via go ldflags (e.g. via Makefile).
33-
var (
34-
// Output of "git describe". The prerequisite is that the branch should be
35-
// tagged using the correct versioning strategy.
36-
GitVersion string = "devel"
37-
// SHA1 from git, output of $(git rev-parse HEAD)
38-
gitCommit = "unknown"
39-
// State of git tree, either "clean" or "dirty"
40-
gitTreeState = "unknown"
41-
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
42-
buildDate = "unknown"
43-
)
44-
4530
type versionOptions struct {
4631
json bool
4732
}
@@ -93,10 +78,10 @@ func VersionInfo() Info {
9378
// These variables typically come from -ldflags settings and in
9479
// their absence fallback to the global defaults set above.
9580
return Info{
96-
GitVersion: GitVersion,
97-
GitCommit: gitCommit,
98-
GitTreeState: gitTreeState,
99-
BuildDate: buildDate,
81+
GitVersion: api.GitVersion,
82+
GitCommit: api.GitCommit,
83+
GitTreeState: api.GitTreeState,
84+
BuildDate: api.BuildDate,
10085
GoVersion: runtime.Version(),
10186
Compiler: runtime.Compiler,
10287
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),

openapi.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ produces:
3131
- application/yaml
3232

3333
paths:
34+
/api/v1/version:
35+
get:
36+
summary: Get the current version of the rekor server
37+
operationId: getRekorVersion
38+
tags:
39+
- server
40+
responses:
41+
200:
42+
description: A JSON object with the running rekor version
43+
schema:
44+
$ref: '#/definitions/RekorVersion'
45+
default:
46+
$ref: '#/responses/InternalServerError'
47+
3448
/api/v1/index/retrieve:
3549
post:
3650
summary: Searches index by entry metadata
@@ -490,6 +504,23 @@ definitions:
490504
- "body"
491505
- "integratedTime"
492506

507+
RekorVersion:
508+
type: object
509+
properties:
510+
version:
511+
type: string
512+
commit:
513+
type: string
514+
treestate:
515+
type: string
516+
builddate:
517+
type: string
518+
required:
519+
- version
520+
- commit
521+
- treestate
522+
- builddate
523+
493524
SearchIndex:
494525
type: object
495526
properties:

pkg/api/version.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Copyright 2021 The Sigstore Authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package api
17+
18+
import (
19+
"github.com/go-openapi/runtime/middleware"
20+
"github.com/sigstore/rekor/pkg/generated/models"
21+
"github.com/sigstore/rekor/pkg/generated/restapi/operations/server"
22+
)
23+
24+
// Base version information.
25+
//
26+
// This is the fallback data used when version information from git is not
27+
// provided via go ldflags (e.g. via Makefile).
28+
var (
29+
// Output of "git describe". The prerequisite is that the branch should be
30+
// tagged using the correct versioning strategy.
31+
GitVersion string = "devel"
32+
// SHA1 from git, output of $(git rev-parse HEAD)
33+
GitCommit = "unknown"
34+
// State of git tree, either "clean" or "dirty"
35+
GitTreeState = "unknown"
36+
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
37+
BuildDate = "unknown"
38+
)
39+
40+
func GetRekorVersionHandler(params server.GetRekorVersionParams) middleware.Responder {
41+
ver := &models.RekorVersion{
42+
Version: &GitVersion,
43+
Commit: &GitCommit,
44+
Treestate: &GitTreeState,
45+
Builddate: &BuildDate,
46+
}
47+
return server.NewGetRekorVersionOK().WithPayload(ver)
48+
}

pkg/generated/client/rekor_client.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)