Skip to content

Commit

Permalink
Implement /api/v1/version for rekor-server (#569)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

Signed-off-by: Morten Linderud <[email protected]>

* Generated files

Signed-off-by: Morten Linderud <[email protected]>
  • Loading branch information
Foxboron authored Dec 27, 2021
1 parent 12d1a47 commit 3f4129b
Show file tree
Hide file tree
Showing 19 changed files with 1,079 additions and 48 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ export KO_DOCKER_REPO=$(KO_PREFIX)
SWAGGER := $(TOOLS_BIN_DIR)/swagger
GO-FUZZ-BUILD := $(TOOLS_BIN_DIR)/go-fuzz-build

CLI_PKG=github.com/sigstore/rekor/cmd/rekor-cli/app
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)
FLAG_PKG=github.com/sigstore/rekor/pkg/api
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)

CLI_LDFLAGS=$(REKOR_LDFLAGS)
SERVER_LDFLAGS=$(REKOR_LDFLAGS)

SERVER_PKG=github.com/sigstore/rekor/cmd/rekor-server/app
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)

$(GENSRC): $(SWAGGER) $(OPENAPIDEPS)
$(SWAGGER) generate client -f openapi.yaml -q -r COPYRIGHT.txt -t pkg/generated --default-consumes application/json\;q=1 --additional-initialism=TUF
Expand Down
5 changes: 3 additions & 2 deletions cmd/rekor-cli/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/sigstore/rekor/pkg/api"
"github.com/sigstore/rekor/pkg/log"

// these imports are to call the packages' init methods
Expand Down Expand Up @@ -72,7 +73,7 @@ func init() {
}

// look for the default version and replace it, if found, from runtime build info
if GitVersion != "devel" {
if api.GitVersion != "devel" {
return
}

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

func initConfig(cmd *cobra.Command) error {
Expand Down
25 changes: 5 additions & 20 deletions cmd/rekor-cli/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,10 @@ import (
"text/tabwriter"

"github.com/pkg/errors"
"github.com/sigstore/rekor/pkg/api"
"github.com/spf13/cobra"
)

// Base version information.
//
// This is the fallback data used when version information from git is not
// provided via go ldflags (e.g. via Makefile).
var (
// Output of "git describe". The prerequisite is that the branch should be
// tagged using the correct versioning strategy.
GitVersion string = "devel"
// SHA1 from git, output of $(git rev-parse HEAD)
gitCommit = "unknown"
// State of git tree, either "clean" or "dirty"
gitTreeState = "unknown"
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
buildDate = "unknown"
)

type versionOptions struct {
json bool
}
Expand Down Expand Up @@ -93,10 +78,10 @@ func VersionInfo() Info {
// These variables typically come from -ldflags settings and in
// their absence fallback to the global defaults set above.
return Info{
GitVersion: GitVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GitVersion: api.GitVersion,
GitCommit: api.GitCommit,
GitTreeState: api.GitTreeState,
BuildDate: api.BuildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
Expand Down
5 changes: 3 additions & 2 deletions cmd/rekor-server/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/sigstore/rekor/pkg/api"
"github.com/sigstore/rekor/pkg/log"
)

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

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

// initConfig reads in config file and ENV variables if set.
Expand Down
25 changes: 5 additions & 20 deletions cmd/rekor-server/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,10 @@ import (
"text/tabwriter"

"github.com/pkg/errors"
"github.com/sigstore/rekor/pkg/api"
"github.com/spf13/cobra"
)

// Base version information.
//
// This is the fallback data used when version information from git is not
// provided via go ldflags (e.g. via Makefile).
var (
// Output of "git describe". The prerequisite is that the branch should be
// tagged using the correct versioning strategy.
GitVersion string = "devel"
// SHA1 from git, output of $(git rev-parse HEAD)
gitCommit = "unknown"
// State of git tree, either "clean" or "dirty"
gitTreeState = "unknown"
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
buildDate = "unknown"
)

type versionOptions struct {
json bool
}
Expand Down Expand Up @@ -93,10 +78,10 @@ func VersionInfo() Info {
// These variables typically come from -ldflags settings and in
// their absence fallback to the global defaults set above.
return Info{
GitVersion: GitVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GitVersion: api.GitVersion,
GitCommit: api.GitCommit,
GitTreeState: api.GitTreeState,
BuildDate: api.BuildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
Expand Down
31 changes: 31 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ produces:
- application/yaml

paths:
/api/v1/version:
get:
summary: Get the current version of the rekor server
operationId: getRekorVersion
tags:
- server
responses:
200:
description: A JSON object with the running rekor version
schema:
$ref: '#/definitions/RekorVersion'
default:
$ref: '#/responses/InternalServerError'

/api/v1/index/retrieve:
post:
summary: Searches index by entry metadata
Expand Down Expand Up @@ -490,6 +504,23 @@ definitions:
- "body"
- "integratedTime"

RekorVersion:
type: object
properties:
version:
type: string
commit:
type: string
treestate:
type: string
builddate:
type: string
required:
- version
- commit
- treestate
- builddate

SearchIndex:
type: object
properties:
Expand Down
48 changes: 48 additions & 0 deletions pkg/api/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"github.com/go-openapi/runtime/middleware"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/generated/restapi/operations/server"
)

// Base version information.
//
// This is the fallback data used when version information from git is not
// provided via go ldflags (e.g. via Makefile).
var (
// Output of "git describe". The prerequisite is that the branch should be
// tagged using the correct versioning strategy.
GitVersion string = "devel"
// SHA1 from git, output of $(git rev-parse HEAD)
GitCommit = "unknown"
// State of git tree, either "clean" or "dirty"
GitTreeState = "unknown"
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
BuildDate = "unknown"
)

func GetRekorVersionHandler(params server.GetRekorVersionParams) middleware.Responder {
ver := &models.RekorVersion{
Version: &GitVersion,
Commit: &GitCommit,
Treestate: &GitTreeState,
Builddate: &BuildDate,
}
return server.NewGetRekorVersionOK().WithPayload(ver)
}
5 changes: 5 additions & 0 deletions pkg/generated/client/rekor_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f4129b

Please sign in to comment.