Skip to content

Commit

Permalink
signatory tests got refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
e-asphyx committed Jul 26, 2021
1 parent 9998b2e commit 355f8fa
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 362 deletions.
4 changes: 2 additions & 2 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func NewRootCommand(c *Context, name string) *cobra.Command {
Policy: pol,
Vaults: conf.Vaults,
Interceptor: metrics.Interceptor,
Watermark: signatory.NewInMemoryWatermark(),
Watermark: &signatory.InMemoryWatermark{},
}

sig, err := signatory.NewSignatory(c.Context, &sigConf)
sig, err := signatory.New(c.Context, &sigConf)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/signatory-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
_ "github.com/ecadlabs/signatory/pkg/vault/cloudkms"
_ "github.com/ecadlabs/signatory/pkg/vault/file"
_ "github.com/ecadlabs/signatory/pkg/vault/ledger"
_ "github.com/ecadlabs/signatory/pkg/vault/mem"
_ "github.com/ecadlabs/signatory/pkg/vault/yubi"
)

Expand Down
1 change: 1 addition & 0 deletions cmd/signatory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
_ "github.com/ecadlabs/signatory/pkg/vault/cloudkms"
_ "github.com/ecadlabs/signatory/pkg/vault/file"
_ "github.com/ecadlabs/signatory/pkg/vault/ledger"
_ "github.com/ecadlabs/signatory/pkg/vault/mem"
_ "github.com/ecadlabs/signatory/pkg/vault/yubi"
)

Expand Down
33 changes: 11 additions & 22 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func TestSign(t *testing.T) {

handler, err := srv.Handler()
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

s := httptest.NewServer(handler)
Expand All @@ -106,21 +105,18 @@ func TestSign(t *testing.T) {

req, err := http.NewRequest("POST", s.URL+"/keys/03123453", body)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}
resp, err := s.Client().Do(req)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

require.Equal(t, c.StatusCode, resp.StatusCode)

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

require.Equal(t, c.Expected, string(b))
Expand Down Expand Up @@ -165,30 +161,26 @@ func TestGetPublicKey(t *testing.T) {

handler, err := srv.Handler()
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

s := httptest.NewServer(handler)
defer s.Close()

req, err := http.NewRequest("GET", s.URL+"/keys/03123453", nil)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}
resp, err := s.Client().Do(req)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

require.Equal(t, c.StatusCode, resp.StatusCode)

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

require.Equal(t, c.Expected, string(b))
Expand Down Expand Up @@ -233,8 +225,7 @@ func TestSignedRequest(t *testing.T) {

handler, err := srv.Handler()
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

s := httptest.NewServer(handler)
Expand All @@ -250,13 +241,11 @@ func TestSignedRequest(t *testing.T) {

req, err := http.NewRequest("POST", u.String(), body)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}
resp, err := s.Client().Do(req)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

require.Equal(t, c.StatusCode, resp.StatusCode)
Expand Down
8 changes: 4 additions & 4 deletions pkg/signatory/signatory.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ func (s *Signatory) Sign(ctx context.Context, req *SignRequest) (string, error)
}
l.WithField("raw", hex.EncodeToString(req.Message)).Log(level, "About to sign raw bytes")

if !s.config.Watermark.IsSafeToSign(req.PublicKeyHash, msg) {
err = ErrNotSafeToSign
if err = s.config.Watermark.IsSafeToSign(req.PublicKeyHash, msg); err != nil {
err = errors.Wrap(err, http.StatusForbidden)
l.Error(err)
return "", err
}
Expand Down Expand Up @@ -417,8 +417,8 @@ type Config struct {
VaultFactory vault.Factory
}

// NewSignatory returns Signatory instance
func NewSignatory(ctx context.Context, c *Config) (*Signatory, error) {
// New returns Signatory instance
func New(ctx context.Context, c *Config) (*Signatory, error) {
s := &Signatory{
config: *c,
vaults: make(map[string]vault.Vault, len(c.Vaults)),
Expand Down
Loading

0 comments on commit 355f8fa

Please sign in to comment.