Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move GetRekorClient into util directory #349

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/rekor-cli/app/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/spf13/viper"

"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
"github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/rekor/pkg/generated/client/entries"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/log"
Expand Down Expand Up @@ -70,7 +71,7 @@ var getCmd = &cobra.Command{
}
},
Run: format.WrapCmd(func(args []string) (interface{}, error) {
rekorClient, err := GetRekorClient(viper.GetString("rekor_server"))
rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"))
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/rekor-cli/app/log_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
"github.com/sigstore/rekor/cmd/rekor-cli/app/state"
"github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/rekor/pkg/generated/client/tlog"
"github.com/sigstore/rekor/pkg/log"
"github.com/sigstore/rekor/pkg/util"
Expand Down Expand Up @@ -59,7 +60,7 @@ var logInfoCmd = &cobra.Command{
Long: `Prints info about the transparency log`,
Run: format.WrapCmd(func(args []string) (interface{}, error) {
serverURL := viper.GetString("rekor_server")
rekorClient, err := GetRekorClient(serverURL)
rekorClient, err := client.GetRekorClient(serverURL)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/rekor-cli/app/log_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/viper"

"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
"github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/rekor/pkg/generated/client/tlog"
)

Expand Down Expand Up @@ -71,7 +72,7 @@ var logProofCmd = &cobra.Command{
return nil
},
Run: format.WrapCmd(func(args []string) (interface{}, error) {
rekorClient, err := GetRekorClient(viper.GetString("rekor_server"))
rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"))
if err != nil {
return nil, err
}
Expand Down
29 changes: 0 additions & 29 deletions cmd/rekor-cli/app/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@ import (
"os"
"strings"

"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/util"
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -114,29 +108,6 @@ func initConfig(cmd *cobra.Command) error {
return nil
}

func GetRekorClient(rekorServerURL string) (*client.Rekor, error) {
url, err := url.Parse(rekorServerURL)
if err != nil {
return nil, err
}

rt := httptransport.New(url.Host, client.DefaultBasePath, []string{url.Scheme})
rt.Consumers["application/yaml"] = util.YamlConsumer()
rt.Consumers["application/x-pem-file"] = runtime.TextConsumer()
rt.Consumers["application/pem-certificate-chain"] = runtime.TextConsumer()
rt.Producers["application/yaml"] = util.YamlProducer()
rt.Producers["application/timestamp-query"] = runtime.ByteStreamProducer()
rt.Consumers["application/timestamp-reply"] = runtime.ByteStreamConsumer()

if viper.GetString("api-key") != "" {
rt.DefaultAuthentication = httptransport.APIKeyAuth("apiKey", "query", viper.GetString("api-key"))
}

registry := strfmt.Default
registry.Add("signedCheckpoint", &util.SignedCheckpoint{}, util.SignedCheckpointValidator)
return client.New(rt, registry), nil
}

type urlFlag struct {
url string
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/rekor-cli/app/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/spf13/viper"

"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
"github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/rekor/pkg/generated/client/index"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/log"
Expand Down Expand Up @@ -70,7 +71,7 @@ var searchCmd = &cobra.Command{
},
Run: format.WrapCmd(func(args []string) (interface{}, error) {
log := log.Logger
rekorClient, err := GetRekorClient(viper.GetString("rekor_server"))
rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"))
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/rekor-cli/app/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/sassoftware/relic/lib/pkcs9"
"github.com/sassoftware/relic/lib/x509tools"
"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
"github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/rekor/pkg/generated/client/timestamp"
"github.com/sigstore/rekor/pkg/log"
"github.com/sigstore/rekor/pkg/util"
Expand Down Expand Up @@ -202,7 +203,7 @@ var timestampCmd = &cobra.Command{
return nil
},
Run: format.WrapCmd(func(args []string) (interface{}, error) {
rekorClient, err := GetRekorClient(viper.GetString("rekor_server"))
rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"))
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/rekor-cli/app/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (

"github.com/pkg/errors"
"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/client"
genclient "github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/generated/client/entries"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/log"
Expand Down Expand Up @@ -67,7 +68,7 @@ var uploadCmd = &cobra.Command{
Long: `This command takes the public key, signature and URL of the release artifact and uploads it to the rekor server.`,
Run: format.WrapCmd(func(args []string) (interface{}, error) {
ctx := context.Background()
rekorClient, err := GetRekorClient(viper.GetString("rekor_server"))
rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -143,7 +144,7 @@ var uploadCmd = &cobra.Command{
}),
}

func verifyLogEntry(ctx context.Context, rekorClient *client.Rekor, logEntry models.LogEntryAnon) (bool, error) {
func verifyLogEntry(ctx context.Context, rekorClient *genclient.Rekor, logEntry models.LogEntryAnon) (bool, error) {
if logEntry.Verification == nil {
return false, nil
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/rekor-cli/app/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/spf13/viper"

"github.com/sigstore/rekor/cmd/rekor-cli/app/format"
"github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/rekor/pkg/generated/client/entries"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/log"
Expand Down Expand Up @@ -84,7 +85,7 @@ var verifyCmd = &cobra.Command{
return nil
},
Run: format.WrapCmd(func(args []string) (interface{}, error) {
rekorClient, err := GetRekorClient(viper.GetString("rekor_server"))
rekorClient, err := client.GetRekorClient(viper.GetString("rekor_server"))
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/rekor-server/app/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (
"github.com/spf13/viper"
"gocloud.dev/blob"

"github.com/sigstore/rekor/cmd/rekor-cli/app"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/client"
genclient "github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/log"
"github.com/sigstore/rekor/pkg/util"
)
Expand Down Expand Up @@ -66,7 +66,7 @@ var watchCmd = &cobra.Command{
port := viper.GetUint("rekor_server.port")
interval := viper.GetDuration("interval")
url := fmt.Sprintf("http://%s:%d", host, port)
c, err := app.GetRekorClient(url)
c, err := client.GetRekorClient(url)
if err != nil {
return err
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func init() {
rootCmd.AddCommand(watchCmd)
}

func doCheck(c *client.Rekor, pub crypto.PublicKey) (*SignedAndUnsignedLogRoot, error) {
func doCheck(c *genclient.Rekor, pub crypto.PublicKey) (*SignedAndUnsignedLogRoot, error) {
li, err := c.Tlog.GetLogInfo(nil)
if err != nil {
return nil, errors.Wrap(err, "getting log info")
Expand Down
49 changes: 49 additions & 0 deletions pkg/client/rekor_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 client

import (
"net/url"

"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/util"
"github.com/spf13/viper"
)

func GetRekorClient(rekorServerURL string) (*client.Rekor, error) {
url, err := url.Parse(rekorServerURL)
if err != nil {
return nil, err
}

rt := httptransport.New(url.Host, client.DefaultBasePath, []string{url.Scheme})
rt.Consumers["application/yaml"] = YamlConsumer()
rt.Consumers["application/x-pem-file"] = runtime.TextConsumer()
rt.Consumers["application/pem-certificate-chain"] = runtime.TextConsumer()
rt.Producers["application/yaml"] = YamlProducer()
rt.Producers["application/timestamp-query"] = runtime.ByteStreamProducer()
rt.Consumers["application/timestamp-reply"] = runtime.ByteStreamConsumer()

if viper.GetString("api-key") != "" {
rt.DefaultAuthentication = httptransport.APIKeyAuth("apiKey", "query", viper.GetString("api-key"))
}

registry := strfmt.Default
registry.Add("signedCheckpoint", &util.SignedCheckpoint{}, util.SignedCheckpointValidator)
return client.New(rt, registry), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package app
package client

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/yaml.go → pkg/client/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package util
package client

import (
"bytes"
Expand Down
5 changes: 3 additions & 2 deletions pkg/generated/restapi/configure_rekor_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/spf13/viper"

pkgapi "github.com/sigstore/rekor/pkg/api"
"github.com/sigstore/rekor/pkg/client"
"github.com/sigstore/rekor/pkg/generated/restapi/operations"
"github.com/sigstore/rekor/pkg/generated/restapi/operations/entries"
"github.com/sigstore/rekor/pkg/generated/restapi/operations/index"
Expand Down Expand Up @@ -67,8 +68,8 @@ func configureAPI(api *operations.RekorServerAPI) http.Handler {
api.JSONConsumer = runtime.JSONConsumer()
api.JSONProducer = runtime.JSONProducer()

api.YamlConsumer = util.YamlConsumer()
api.YamlProducer = util.YamlProducer()
api.YamlConsumer = client.YamlConsumer()
api.YamlProducer = client.YamlProducer()

api.ApplicationXPemFileProducer = runtime.TextProducer()
api.ApplicationPemCertificateChainProducer = runtime.TextProducer()
Expand Down
10 changes: 5 additions & 5 deletions tests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/in-toto/in-toto-golang/in_toto"
"github.com/in-toto/in-toto-golang/pkg/ssl"
"github.com/sigstore/rekor/cmd/rekor-cli/app"
"github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/client"
genclient "github.com/sigstore/rekor/pkg/generated/client"
"github.com/sigstore/rekor/pkg/generated/client/entries"
"github.com/sigstore/rekor/pkg/generated/client/timestamp"
"github.com/sigstore/rekor/pkg/generated/models"
Expand Down Expand Up @@ -568,7 +568,7 @@ func TestSignedEntryTimestamp(t *testing.T) {
})

// submit our newly signed payload to rekor
rekorClient, err := app.GetRekorClient("http://localhost:3000")
rekorClient, err := client.GetRekorClient("http://localhost:3000")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -639,7 +639,7 @@ func TestTimestampResponseCLI(t *testing.T) {
out := runCli(t, "timestamp", "--artifact", filePath, "--out", responsePath)
outputContains(t, out, "Wrote timestamp response to")

rekorClient, err := app.GetRekorClient("http://localhost:3000")
rekorClient, err := client.GetRekorClient("http://localhost:3000")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -696,7 +696,7 @@ func TestGetNonExistantUUID(t *testing.T) {
outputContains(t, out, "404")
}

func rekorTimestampCertChain(t *testing.T, ctx context.Context, c *client.Rekor) []*x509.Certificate {
func rekorTimestampCertChain(t *testing.T, ctx context.Context, c *genclient.Rekor) []*x509.Certificate {
resp, err := c.Timestamp.GetTimestampCertChain(&timestamp.GetTimestampCertChainParams{Context: ctx})
if err != nil {
t.Fatal(err)
Expand Down