Skip to content

Commit

Permalink
[acme db interface] unit tests compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
dopey committed Mar 25, 2021
1 parent f20fcae commit bb8d54e
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 579 deletions.
22 changes: 8 additions & 14 deletions acme/account_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
package acme

import (
"context"
"encoding/json"
"fmt"
"net/url"
"testing"
"time"

"github.com/pkg/errors"
"github.com/smallstep/assert"
"github.com/smallstep/certificates/authority/provisioner"
"github.com/smallstep/certificates/db"
"github.com/smallstep/nosql"
"github.com/smallstep/nosql/database"
"go.step.sm/crypto/jose"
)

var (
Expand All @@ -39,7 +29,8 @@ func newProv() Provisioner {
return p
}

func newAcc() (*account, error) {
/*
func newAcc() (*Account, error) {
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
if err != nil {
return nil, err
Expand All @@ -53,12 +44,14 @@ func newAcc() (*account, error) {
Key: jwk, Contact: []string{"foo", "bar"},
})
}
*/

/*
func TestGetAccountByID(t *testing.T) {
type test struct {
id string
db nosql.DB
acc *account
acc *Account
err *Error
}
tests := map[string]func(t *testing.T) test{
Expand All @@ -73,7 +66,7 @@ func TestGetAccountByID(t *testing.T) {
return nil, database.ErrNotFound
},
},
err: MalformedErr(errors.Errorf("account %s not found: not found", acc.ID)),
err: NewError(ErrorMalformedType, "account %s not found: not found", acc.ID),
}
},
"fail/db-error": func(t *testing.T) test {
Expand All @@ -87,7 +80,7 @@ func TestGetAccountByID(t *testing.T) {
return nil, errors.New("force")
},
},
err: ServerInternalErr(errors.Errorf("error loading account %s: force", acc.ID)),
err: NewErrorISE("error loading account %s: force", acc.ID),
}
},
"fail/unmarshal-error": func(t *testing.T) test {
Expand Down Expand Up @@ -768,3 +761,4 @@ func TestNewAccount(t *testing.T) {
})
}
}
*/
2 changes: 1 addition & 1 deletion acme/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Handler struct {
db acme.DB
backdate provisioner.Duration
ca acme.CertificateAuthority
linker *Linker
linker Linker
}

// HandlerOptions required to create a new ACME API request handler.
Expand Down
1 change: 0 additions & 1 deletion acme/api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ func TestHandlerGetChallenge(t *testing.T) {
ch := ch()
ch.Status = "valid"
ch.Validated = time.Now().UTC().Format(time.RFC3339)
count := 0
return test{
db: &acme.MockDB{
MockGetChallenge: func(ctx context.Context, chID, azID string) (*acme.Challenge, error) {
Expand Down
120 changes: 105 additions & 15 deletions acme/api/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@ import (
)

// NewLinker returns a new Directory type.
func NewLinker(dns, prefix string) *Linker {
return &Linker{Prefix: prefix, DNS: dns}
func NewLinker(dns, prefix string) Linker {
return &linker{prefix: prefix, dns: dns}
}

// Linker generates ACME links.
type Linker struct {
Prefix string
DNS string
// Linker interface for generating links for ACME resources.
type Linker interface {
GetLink(ctx context.Context, typ LinkType, abs bool, inputs ...string) string
GetLinkExplicit(typ LinkType, provName string, abs bool, baseURL *url.URL, inputs ...string) string

LinkOrder(ctx context.Context, o *acme.Order)
LinkAccount(ctx context.Context, o *acme.Account)
LinkChallenge(ctx context.Context, o *acme.Challenge)
LinkAuthorization(ctx context.Context, o *acme.Authorization)
LinkOrdersByAccountID(ctx context.Context, orders []string)
}

// linker generates ACME links.
type linker struct {
prefix string
dns string
}

// GetLink is a helper for GetLinkExplicit
func (l *Linker) GetLink(ctx context.Context, typ LinkType, abs bool, inputs ...string) string {
func (l *linker) GetLink(ctx context.Context, typ LinkType, abs bool, inputs ...string) string {
var provName string
if p, err := provisionerFromContext(ctx); err == nil && p != nil {
provName = p.GetName()
Expand All @@ -31,7 +43,7 @@ func (l *Linker) GetLink(ctx context.Context, typ LinkType, abs bool, inputs ...
// GetLinkExplicit returns an absolute or partial path to the given resource and a base
// URL dynamically obtained from the request for which the link is being
// calculated.
func (l *Linker) GetLinkExplicit(typ LinkType, provisionerName string, abs bool, baseURL *url.URL, inputs ...string) string {
func (l *linker) GetLinkExplicit(typ LinkType, provisionerName string, abs bool, baseURL *url.URL, inputs ...string) string {
var link string
switch typ {
case NewNonceLinkType, NewAccountLinkType, NewOrderLinkType, NewAuthzLinkType, DirectoryLinkType, KeyChangeLinkType, RevokeCertLinkType:
Expand Down Expand Up @@ -60,10 +72,10 @@ func (l *Linker) GetLinkExplicit(typ LinkType, provisionerName string, abs bool,

// If no Host is set, then use the default (first DNS attr in the ca.json).
if u.Host == "" {
u.Host = l.DNS
u.Host = l.dns
}

u.Path = l.Prefix + link
u.Path = l.prefix + link
return u.String()
}
return link
Expand Down Expand Up @@ -135,7 +147,7 @@ func (l LinkType) String() string {
}

// LinkOrder sets the ACME links required by an ACME order.
func (l *Linker) LinkOrder(ctx context.Context, o *acme.Order) {
func (l *linker) LinkOrder(ctx context.Context, o *acme.Order) {
o.AuthorizationURLs = make([]string, len(o.AuthorizationIDs))
for i, azID := range o.AuthorizationIDs {
o.AuthorizationURLs[i] = l.GetLink(ctx, AuthzLinkType, true, azID)
Expand All @@ -147,25 +159,103 @@ func (l *Linker) LinkOrder(ctx context.Context, o *acme.Order) {
}

// LinkAccount sets the ACME links required by an ACME account.
func (l *Linker) LinkAccount(ctx context.Context, acc *acme.Account) {
func (l *linker) LinkAccount(ctx context.Context, acc *acme.Account) {
acc.Orders = l.GetLink(ctx, OrdersByAccountLinkType, true, acc.ID)
}

// LinkChallenge sets the ACME links required by an ACME challenge.
func (l *Linker) LinkChallenge(ctx context.Context, ch *acme.Challenge) {
func (l *linker) LinkChallenge(ctx context.Context, ch *acme.Challenge) {
ch.URL = l.GetLink(ctx, ChallengeLinkType, true, ch.AuthzID, ch.ID)
}

// LinkAuthorization sets the ACME links required by an ACME authorization.
func (l *Linker) LinkAuthorization(ctx context.Context, az *acme.Authorization) {
func (l *linker) LinkAuthorization(ctx context.Context, az *acme.Authorization) {
for _, ch := range az.Challenges {
l.LinkChallenge(ctx, ch)
}
}

// LinkOrdersByAccountID converts each order ID to an ACME link.
func (l *Linker) LinkOrdersByAccountID(ctx context.Context, orders []string) {
func (l *linker) LinkOrdersByAccountID(ctx context.Context, orders []string) {
for i, id := range orders {
orders[i] = l.GetLink(ctx, OrderLinkType, true, id)
}
}

// MockLinker implements the Linker interface. Only used for testing.
type MockLinker struct {
MockGetLink func(ctx context.Context, typ LinkType, abs bool, inputs ...string) string
MockGetLinkExplicit func(typ LinkType, provName string, abs bool, baseURL *url.URL, inputs ...string) string

MockLinkOrder func(ctx context.Context, o *acme.Order)
MockLinkAccount func(ctx context.Context, o *acme.Account)
MockLinkChallenge func(ctx context.Context, o *acme.Challenge)
MockLinkAuthorization func(ctx context.Context, o *acme.Authorization)
MockLinkOrdersByAccountID func(ctx context.Context, orders []string)

MockError error
MockRet1 interface{}
}

// GetLink mock.
func (m *MockLinker) GetLink(ctx context.Context, typ LinkType, abs bool, inputs ...string) string {
if m.MockGetLink != nil {
return m.MockGetLink(ctx, typ, abs, inputs...)
}

return m.MockRet1.(string)
}

// GetLinkExplicit mock.
func (m *MockLinker) GetLinkExplicit(typ LinkType, provName string, abs bool, baseURL *url.URL, inputs ...string) string {
if m.MockGetLinkExplicit != nil {
return m.MockGetLinkExplicit(typ, provName, abs, baseURL, inputs...)
}

return m.MockRet1.(string)
}

// LinkOrder mock.
func (m *MockLinker) LinkOrder(ctx context.Context, o *acme.Order) {
if m.MockLinkOrder != nil {
m.MockLinkOrder(ctx, o)
return
}
return
}

// LinkAccount mock.
func (m *MockLinker) LinkAccount(ctx context.Context, o *acme.Account) {
if m.MockLinkAccount != nil {
m.MockLinkAccount(ctx, o)
return
}
return
}

// LinkChallenge mock.
func (m *MockLinker) LinkChallenge(ctx context.Context, o *acme.Challenge) {
if m.MockLinkChallenge != nil {
m.MockLinkChallenge(ctx, o)
return
}
return
}

// LinkAuthorization mock.
func (m *MockLinker) LinkAuthorization(ctx context.Context, o *acme.Authorization) {
if m.MockLinkAuthorization != nil {
m.MockLinkAuthorization(ctx, o)
return
}
return
}

// LinkOrderAccountsByID mock.
func (m *MockLinker) LinkOrderAccountsByID(ctx context.Context, orders []string) {
if m.MockLinkOrdersByAccountID != nil {
m.MockLinkOrdersByAccountID(ctx, orders)
return
}
return
}

0 comments on commit bb8d54e

Please sign in to comment.