2021-02-26 18:12:30 +00:00
|
|
|
package nosql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-03-01 06:49:20 +00:00
|
|
|
"github.com/smallstep/certificates/acme"
|
2021-02-26 18:12:30 +00:00
|
|
|
"github.com/smallstep/nosql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// dbChallenge is the base Challenge type that others build from.
|
|
|
|
type dbChallenge struct {
|
2021-03-01 06:49:20 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
AccountID string `json:"accountID"`
|
|
|
|
AuthzID string `json:"authzID"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Status acme.Status `json:"status"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
Validated string `json:"validated"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Error *AError `json:"error"`
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dbc *dbChallenge) clone() *dbChallenge {
|
2021-03-01 06:49:20 +00:00
|
|
|
u := *dbc
|
2021-02-26 18:12:30 +00:00
|
|
|
return &u
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) getDBChallenge(ctx context.Context, id string) (*dbChallenge, error) {
|
|
|
|
data, err := db.db.Get(challengeTable, []byte(id))
|
|
|
|
if nosql.IsErrNotFound(err) {
|
2021-03-01 06:49:20 +00:00
|
|
|
return nil, errors.Wrapf(err, "challenge %s not found", id)
|
2021-02-26 18:12:30 +00:00
|
|
|
} else if err != nil {
|
2021-03-01 06:49:20 +00:00
|
|
|
return nil, errors.Wrapf(err, "error loading challenge %s", id)
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
dbch := new(dbChallenge)
|
2021-02-26 18:12:30 +00:00
|
|
|
if err := json.Unmarshal(data, dbch); err != nil {
|
2021-03-01 06:49:20 +00:00
|
|
|
return nil, errors.Wrap(err, "error unmarshaling dbChallenge")
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
2021-03-01 06:49:20 +00:00
|
|
|
return dbch, nil
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateChallenge creates a new ACME challenge data structure in the database.
|
|
|
|
// Implements acme.DB.CreateChallenge interface.
|
2021-03-01 06:49:20 +00:00
|
|
|
func (db *DB) CreateChallenge(ctx context.Context, ch *acme.Challenge) error {
|
2021-02-26 18:12:30 +00:00
|
|
|
if len(ch.AuthzID) == 0 {
|
2021-03-01 06:49:20 +00:00
|
|
|
return errors.New("AuthzID cannot be empty")
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
if len(ch.AccountID) == 0 {
|
2021-03-01 06:49:20 +00:00
|
|
|
return errors.New("AccountID cannot be empty")
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
if len(ch.Value) == 0 {
|
2021-03-01 06:49:20 +00:00
|
|
|
return errors.New("AccountID cannot be empty")
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
// TODO: verify that challenge type is set and is one of expected types.
|
|
|
|
if len(ch.Type) == 0 {
|
2021-03-01 06:49:20 +00:00
|
|
|
return errors.New("Type cannot be empty")
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
var err error
|
2021-02-26 18:12:30 +00:00
|
|
|
ch.ID, err = randID()
|
|
|
|
if err != nil {
|
2021-03-01 06:49:20 +00:00
|
|
|
return errors.Wrap(err, "error generating random id for ACME challenge")
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
ch.Token, err = randID()
|
|
|
|
if err != nil {
|
2021-03-01 06:49:20 +00:00
|
|
|
return errors.Wrap(err, "error generating token for ACME challenge")
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbch := &dbChallenge{
|
|
|
|
ID: ch.ID,
|
|
|
|
AuthzID: ch.AuthzID,
|
|
|
|
AccountID: ch.AccountID,
|
|
|
|
Value: ch.Value,
|
2021-03-01 06:49:20 +00:00
|
|
|
Status: acme.StatusPending,
|
2021-02-26 18:12:30 +00:00
|
|
|
Token: ch.Token,
|
|
|
|
Created: clock.Now(),
|
|
|
|
Type: ch.Type,
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
return db.save(ctx, ch.ID, dbch, nil, "challenge", challengeTable)
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetChallenge retrieves and unmarshals an ACME challenge type from the database.
|
|
|
|
// Implements the acme.DB GetChallenge interface.
|
2021-03-01 06:49:20 +00:00
|
|
|
func (db *DB) GetChallenge(ctx context.Context, id, authzID string) (*acme.Challenge, error) {
|
2021-02-26 18:12:30 +00:00
|
|
|
dbch, err := db.getDBChallenge(ctx, id)
|
|
|
|
if err != nil {
|
2021-03-01 06:49:20 +00:00
|
|
|
return nil, err
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
ch := &acme.Challenge{
|
|
|
|
Type: dbch.Type,
|
|
|
|
Status: dbch.Status,
|
|
|
|
Token: dbch.Token,
|
|
|
|
URL: dir.getLink(ctx, ChallengeLink, true, dbch.ID),
|
|
|
|
ID: dbch.ID,
|
|
|
|
AuthzID: dbch.AuthzID,
|
|
|
|
Error: dbch.Error,
|
|
|
|
Validated: dbch.Validated,
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateChallenge updates an ACME challenge type in the database.
|
2021-03-01 06:49:20 +00:00
|
|
|
func (db *DB) UpdateChallenge(ctx context.Context, ch *acme.Challenge) error {
|
2021-02-28 01:05:37 +00:00
|
|
|
if len(ch.ID) == 0 {
|
2021-03-01 06:49:20 +00:00
|
|
|
return errors.New("id cannot be empty")
|
2021-02-28 01:05:37 +00:00
|
|
|
}
|
|
|
|
old, err := db.getDBChallenge(ctx, ch.ID)
|
2021-02-26 18:12:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nu := old.clone()
|
|
|
|
|
|
|
|
// These should be the only values chaning in an Update request.
|
|
|
|
nu.Status = ch.Status
|
|
|
|
nu.Error = ch.Error
|
2021-03-01 06:49:20 +00:00
|
|
|
nu.Validated = ch.Validated
|
2021-02-26 18:12:30 +00:00
|
|
|
|
2021-02-28 01:05:37 +00:00
|
|
|
return db.save(ctx, old.ID, nu, old, "challenge", challengeTable)
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|