Skip to content

Commit

Permalink
Refactor rekor client code
Browse files Browse the repository at this point in the history
Push the rekor client stuff into a package
  • Loading branch information
nsmith5 committed Jan 5, 2022
1 parent 169610c commit c4d5eec
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
10 changes: 6 additions & 4 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import (
"errors"
"fmt"
"time"

"github.com/nsmith5/rekor-sidekick/rekor"
)

type agent struct {
rc *rekorClient
rc *rekor.Client

quit chan struct{}
}

// newAgent constructs an agent from config or bails
func newAgent(c config) (*agent, error) {
rc, err := newRekorClient(c.RekorServerURL)
rc, err := rekor.NewClient(c.RekorServerURL)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -46,9 +48,9 @@ func (a *agent) run() error {
return nil

default:
entry, err := a.rc.getNextLogEntry()
entry, err := a.rc.GetNextLogEntry()
if err != nil {
if err == ErrEntryDoesntExist {
if err == rekor.ErrEntryDoesntExist {
// Log doesn't exist yet, lets just wait 10 seconds and try again
time.Sleep(10 * time.Second)

Expand Down
32 changes: 18 additions & 14 deletions rekorclient.go β†’ rekor/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package rekor

import (
"encoding/base64"
Expand All @@ -14,31 +14,33 @@ var (
ErrEntryDoesntExist = errors.New(`Rekor entry doesn't exist yet`)
)

// rekorEntry is unstructured, we create this type simply
// to know what we're talking about passing it around
type rekorLogEntry struct {
// LogEntry is a Rekor log entry
type LogEntry struct {
Kind string
APIVersion string `json:"apiVersion"`
Spec interface{}
}

// rekorTreeState represents the current state of the transparency log (size
// treeState represents the current state of the transparency log (size
// etc)
type rekorTreeState struct {
type treeState struct {
RootHash string
SignedTreeHead string
TreeSize uint
}

type rekorClient struct {
// Client is a Rekor api client
type Client struct {
baseURL string
currentIndex uint

*http.Client
}

func newRekorClient(baseURL string) (*rekorClient, error) {
rc := rekorClient{
// NewClient returns a Rekor client or fails if the baseURL
// is misconfigured.
func NewClient(baseURL string) (*Client, error) {
rc := Client{
baseURL: baseURL,
currentIndex: 0,
Client: new(http.Client),
Expand All @@ -58,7 +60,7 @@ func newRekorClient(baseURL string) (*rekorClient, error) {
return &rc, nil
}

func (rc *rekorClient) getLogEntry(index uint) (*rekorLogEntry, error) {
func (rc *Client) getLogEntry(index uint) (*LogEntry, error) {
url := fmt.Sprintf("%s/api/v1/log/entries?logIndex=%d", rc.baseURL, index)

req, err := http.NewRequest(`GET`, url, nil)
Expand Down Expand Up @@ -97,7 +99,7 @@ func (rc *rekorClient) getLogEntry(index uint) (*rekorLogEntry, error) {
break
}

var entry rekorLogEntry
var entry LogEntry
err = json.NewDecoder(
base64.NewDecoder(base64.URLEncoding, strings.NewReader(body)),
).Decode(&entry)
Expand All @@ -108,7 +110,9 @@ func (rc *rekorClient) getLogEntry(index uint) (*rekorLogEntry, error) {
return &entry, nil
}

func (rc *rekorClient) getNextLogEntry() (*rekorLogEntry, error) {
// GetNextLogEntry pulls the next entry in the Rekor log. If the
// next log doesn't exist yet ErrEntryDoesntExist is returned.
func (rc *Client) GetNextLogEntry() (*LogEntry, error) {
entry, err := rc.getLogEntry(rc.currentIndex)
if err != nil {
return nil, err
Expand All @@ -117,7 +121,7 @@ func (rc *rekorClient) getNextLogEntry() (*rekorLogEntry, error) {
return entry, nil
}

func (rc *rekorClient) getTreeState() (*rekorTreeState, error) {
func (rc *Client) getTreeState() (*treeState, error) {
url := fmt.Sprintf("%s/api/v1/log", rc.baseURL)

req, err := http.NewRequest(`GET`, url, nil)
Expand All @@ -131,7 +135,7 @@ func (rc *rekorClient) getTreeState() (*rekorTreeState, error) {
}
defer resp.Body.Close()

var state rekorTreeState
var state treeState
err = json.NewDecoder(resp.Body).Decode(&state)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions rekorclient_test.go β†’ rekor/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package rekor

import (
"io"
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestGetLogEntry(t *testing.T) {
`/api/v1/log/entries`: `testdata/rekor-api-log-entry.json`,
})

rc, err := newRekorClient(ts.URL)
rc, err := NewClient(ts.URL)
if err != nil {
t.Fatal(err)
}
Expand All @@ -62,7 +62,7 @@ func TestGetTreeState(t *testing.T) {
`/api/v1/log`: `testdata/rekor-api-log.json`,
})

rc, err := newRekorClient(ts.URL)
rc, err := NewClient(ts.URL)
if err != nil {
t.Fatal(err)
}
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit c4d5eec

Please sign in to comment.