| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- package nosql
- import (
- "context"
- "encoding/json"
- "testing"
- "time"
- "github.com/pkg/errors"
- "github.com/smallstep/assert"
- "github.com/smallstep/certificates/acme"
- "github.com/smallstep/certificates/db"
- "github.com/smallstep/nosql"
- nosqldb "github.com/smallstep/nosql/database"
- )
- func TestDB_getDBChallenge(t *testing.T) {
- chID := "chID"
- type test struct {
- db nosql.DB
- err error
- acmeErr *acme.Error
- dbc *dbChallenge
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/not-found": func(t *testing.T) test {
- return test{
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return nil, nosqldb.ErrNotFound
- },
- },
- acmeErr: acme.NewError(acme.ErrorMalformedType, "challenge chID not found"),
- }
- },
- "fail/db.Get-error": func(t *testing.T) test {
- return test{
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return nil, errors.New("force")
- },
- },
- err: errors.New("error loading acme challenge chID: force"),
- }
- },
- "fail/unmarshal-error": func(t *testing.T) test {
- return test{
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return []byte("foo"), nil
- },
- },
- err: errors.New("error unmarshaling dbChallenge"),
- }
- },
- "ok": func(t *testing.T) test {
- dbc := &dbChallenge{
- ID: chID,
- AccountID: "accountID",
- Type: "dns-01",
- Status: acme.StatusPending,
- Token: "token",
- Value: "test.ca.smallstep.com",
- CreatedAt: clock.Now(),
- ValidatedAt: "foobar",
- Error: acme.NewErrorISE("The server experienced an internal error"),
- }
- b, err := json.Marshal(dbc)
- assert.FatalError(t, err)
- return test{
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return b, nil
- },
- },
- dbc: dbc,
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- d := DB{db: tc.db}
- if ch, err := d.getDBChallenge(context.Background(), chID); err != nil {
- var ae *acme.Error
- if errors.As(err, &ae) {
- if assert.NotNil(t, tc.acmeErr) {
- assert.Equals(t, ae.Type, tc.acmeErr.Type)
- assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
- assert.Equals(t, ae.Status, tc.acmeErr.Status)
- assert.Equals(t, ae.Err.Error(), tc.acmeErr.Err.Error())
- assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
- }
- } else {
- if assert.NotNil(t, tc.err) {
- assert.HasPrefix(t, err.Error(), tc.err.Error())
- }
- }
- } else if assert.Nil(t, tc.err) {
- assert.Equals(t, ch.ID, tc.dbc.ID)
- assert.Equals(t, ch.AccountID, tc.dbc.AccountID)
- assert.Equals(t, ch.Type, tc.dbc.Type)
- assert.Equals(t, ch.Status, tc.dbc.Status)
- assert.Equals(t, ch.Token, tc.dbc.Token)
- assert.Equals(t, ch.Value, tc.dbc.Value)
- assert.Equals(t, ch.ValidatedAt, tc.dbc.ValidatedAt)
- assert.Equals(t, ch.Error.Error(), tc.dbc.Error.Error())
- }
- })
- }
- }
- func TestDB_CreateChallenge(t *testing.T) {
- type test struct {
- db nosql.DB
- ch *acme.Challenge
- err error
- _id *string
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/cmpAndSwap-error": func(t *testing.T) test {
- ch := &acme.Challenge{
- AccountID: "accountID",
- Type: "dns-01",
- Status: acme.StatusPending,
- Token: "token",
- Value: "test.ca.smallstep.com",
- }
- return test{
- db: &db.MockNoSQLDB{
- MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), ch.ID)
- assert.Equals(t, old, nil)
- dbc := new(dbChallenge)
- assert.FatalError(t, json.Unmarshal(nu, dbc))
- assert.Equals(t, dbc.ID, string(key))
- assert.Equals(t, dbc.AccountID, ch.AccountID)
- assert.Equals(t, dbc.Type, ch.Type)
- assert.Equals(t, dbc.Status, ch.Status)
- assert.Equals(t, dbc.Token, ch.Token)
- assert.Equals(t, dbc.Value, ch.Value)
- assert.True(t, clock.Now().Add(-time.Minute).Before(dbc.CreatedAt))
- assert.True(t, clock.Now().Add(time.Minute).After(dbc.CreatedAt))
- return nil, false, errors.New("force")
- },
- },
- ch: ch,
- err: errors.New("error saving acme challenge: force"),
- }
- },
- "ok": func(t *testing.T) test {
- var (
- id string
- idPtr = &id
- ch = &acme.Challenge{
- AccountID: "accountID",
- Type: "dns-01",
- Status: acme.StatusPending,
- Token: "token",
- Value: "test.ca.smallstep.com",
- }
- )
- return test{
- ch: ch,
- db: &db.MockNoSQLDB{
- MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
- *idPtr = string(key)
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), ch.ID)
- assert.Equals(t, old, nil)
- dbc := new(dbChallenge)
- assert.FatalError(t, json.Unmarshal(nu, dbc))
- assert.Equals(t, dbc.ID, string(key))
- assert.Equals(t, dbc.AccountID, ch.AccountID)
- assert.Equals(t, dbc.Type, ch.Type)
- assert.Equals(t, dbc.Status, ch.Status)
- assert.Equals(t, dbc.Token, ch.Token)
- assert.Equals(t, dbc.Value, ch.Value)
- assert.True(t, clock.Now().Add(-time.Minute).Before(dbc.CreatedAt))
- assert.True(t, clock.Now().Add(time.Minute).After(dbc.CreatedAt))
- return nil, true, nil
- },
- },
- _id: idPtr,
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- d := DB{db: tc.db}
- if err := d.CreateChallenge(context.Background(), tc.ch); err != nil {
- if assert.NotNil(t, tc.err) {
- assert.HasPrefix(t, err.Error(), tc.err.Error())
- }
- } else {
- if assert.Nil(t, tc.err) {
- assert.Equals(t, tc.ch.ID, *tc._id)
- }
- }
- })
- }
- }
- func TestDB_GetChallenge(t *testing.T) {
- chID := "chID"
- azID := "azID"
- type test struct {
- db nosql.DB
- err error
- acmeErr *acme.Error
- dbc *dbChallenge
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/db.Get-error": func(t *testing.T) test {
- return test{
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return nil, errors.New("force")
- },
- },
- err: errors.New("error loading acme challenge chID: force"),
- }
- },
- "fail/forward-acme-error": func(t *testing.T) test {
- return test{
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return nil, nosqldb.ErrNotFound
- },
- },
- acmeErr: acme.NewError(acme.ErrorMalformedType, "challenge chID not found"),
- }
- },
- "ok": func(t *testing.T) test {
- dbc := &dbChallenge{
- ID: chID,
- AccountID: "accountID",
- Type: "dns-01",
- Status: acme.StatusPending,
- Token: "token",
- Value: "test.ca.smallstep.com",
- CreatedAt: clock.Now(),
- ValidatedAt: "foobar",
- Error: acme.NewErrorISE("The server experienced an internal error"),
- }
- b, err := json.Marshal(dbc)
- assert.FatalError(t, err)
- return test{
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return b, nil
- },
- },
- dbc: dbc,
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- d := DB{db: tc.db}
- if ch, err := d.GetChallenge(context.Background(), chID, azID); err != nil {
- var ae *acme.Error
- if errors.As(err, &ae) {
- if assert.NotNil(t, tc.acmeErr) {
- assert.Equals(t, ae.Type, tc.acmeErr.Type)
- assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
- assert.Equals(t, ae.Status, tc.acmeErr.Status)
- assert.Equals(t, ae.Err.Error(), tc.acmeErr.Err.Error())
- assert.Equals(t, ae.Detail, tc.acmeErr.Detail)
- }
- } else {
- if assert.NotNil(t, tc.err) {
- assert.HasPrefix(t, err.Error(), tc.err.Error())
- }
- }
- } else if assert.Nil(t, tc.err) {
- assert.Equals(t, ch.ID, tc.dbc.ID)
- assert.Equals(t, ch.AccountID, tc.dbc.AccountID)
- assert.Equals(t, ch.Type, tc.dbc.Type)
- assert.Equals(t, ch.Status, tc.dbc.Status)
- assert.Equals(t, ch.Token, tc.dbc.Token)
- assert.Equals(t, ch.Value, tc.dbc.Value)
- assert.Equals(t, ch.ValidatedAt, tc.dbc.ValidatedAt)
- assert.Equals(t, ch.Error.Error(), tc.dbc.Error.Error())
- }
- })
- }
- }
- func TestDB_UpdateChallenge(t *testing.T) {
- chID := "chID"
- dbc := &dbChallenge{
- ID: chID,
- AccountID: "accountID",
- Type: "dns-01",
- Status: acme.StatusPending,
- Token: "token",
- Value: "test.ca.smallstep.com",
- CreatedAt: clock.Now(),
- }
- b, err := json.Marshal(dbc)
- assert.FatalError(t, err)
- type test struct {
- db nosql.DB
- ch *acme.Challenge
- err error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/db.Get-error": func(t *testing.T) test {
- return test{
- ch: &acme.Challenge{
- ID: chID,
- },
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return nil, errors.New("force")
- },
- },
- err: errors.New("error loading acme challenge chID: force"),
- }
- },
- "fail/db.CmpAndSwap-error": func(t *testing.T) test {
- updCh := &acme.Challenge{
- ID: chID,
- Status: acme.StatusValid,
- ValidatedAt: "foobar",
- Error: acme.NewError(acme.ErrorMalformedType, "The request message was malformed"),
- }
- return test{
- ch: updCh,
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return b, nil
- },
- MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, old, b)
- dbOld := new(dbChallenge)
- assert.FatalError(t, json.Unmarshal(old, dbOld))
- assert.Equals(t, dbc, dbOld)
- dbNew := new(dbChallenge)
- assert.FatalError(t, json.Unmarshal(nu, dbNew))
- assert.Equals(t, dbNew.ID, dbc.ID)
- assert.Equals(t, dbNew.AccountID, dbc.AccountID)
- assert.Equals(t, dbNew.Type, dbc.Type)
- assert.Equals(t, dbNew.Status, updCh.Status)
- assert.Equals(t, dbNew.Token, dbc.Token)
- assert.Equals(t, dbNew.Value, dbc.Value)
- assert.Equals(t, dbNew.Error.Error(), updCh.Error.Error())
- assert.Equals(t, dbNew.CreatedAt, dbc.CreatedAt)
- assert.Equals(t, dbNew.ValidatedAt, updCh.ValidatedAt)
- return nil, false, errors.New("force")
- },
- },
- err: errors.New("error saving acme challenge: force"),
- }
- },
- "ok": func(t *testing.T) test {
- updCh := &acme.Challenge{
- ID: dbc.ID,
- AccountID: dbc.AccountID,
- Type: dbc.Type,
- Token: dbc.Token,
- Value: dbc.Value,
- Status: acme.StatusValid,
- ValidatedAt: "foobar",
- Error: acme.NewError(acme.ErrorMalformedType, "malformed"),
- }
- return test{
- ch: updCh,
- db: &db.MockNoSQLDB{
- MGet: func(bucket, key []byte) ([]byte, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, string(key), chID)
- return b, nil
- },
- MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
- assert.Equals(t, bucket, challengeTable)
- assert.Equals(t, old, b)
- dbOld := new(dbChallenge)
- assert.FatalError(t, json.Unmarshal(old, dbOld))
- assert.Equals(t, dbc, dbOld)
- dbNew := new(dbChallenge)
- assert.FatalError(t, json.Unmarshal(nu, dbNew))
- assert.Equals(t, dbNew.ID, dbc.ID)
- assert.Equals(t, dbNew.AccountID, dbc.AccountID)
- assert.Equals(t, dbNew.Type, dbc.Type)
- assert.Equals(t, dbNew.Token, dbc.Token)
- assert.Equals(t, dbNew.Value, dbc.Value)
- assert.Equals(t, dbNew.CreatedAt, dbc.CreatedAt)
- assert.Equals(t, dbNew.Status, acme.StatusValid)
- assert.Equals(t, dbNew.ValidatedAt, "foobar")
- assert.Equals(t, dbNew.Error.Error(), acme.NewError(acme.ErrorMalformedType, "The request message was malformed").Error())
- return nu, true, nil
- },
- },
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- d := DB{db: tc.db}
- if err := d.UpdateChallenge(context.Background(), tc.ch); err != nil {
- if assert.NotNil(t, tc.err) {
- assert.HasPrefix(t, err.Error(), tc.err.Error())
- }
- } else {
- if assert.Nil(t, tc.err) {
- assert.Equals(t, tc.ch.ID, dbc.ID)
- assert.Equals(t, tc.ch.AccountID, dbc.AccountID)
- assert.Equals(t, tc.ch.Type, dbc.Type)
- assert.Equals(t, tc.ch.Token, dbc.Token)
- assert.Equals(t, tc.ch.Value, dbc.Value)
- assert.Equals(t, tc.ch.ValidatedAt, "foobar")
- assert.Equals(t, tc.ch.Status, acme.StatusValid)
- assert.Equals(t, tc.ch.Error.Error(), acme.NewError(acme.ErrorMalformedType, "malformed").Error())
- }
- }
- })
- }
- }
|