certificates/acme/db/nosql/challenge.go

106 lines
2.9 KiB
Go
Raw Normal View History

package nosql
import (
"context"
"encoding/json"
"time"
"github.com/pkg/errors"
2023-01-26 12:41:01 +00:00
"github.com/smallstep/nosql"
2023-01-26 12:41:01 +00:00
"github.com/smallstep/certificates/acme"
)
type dbChallenge struct {
ID string `json:"id"`
AccountID string `json:"accountID"`
Type acme.ChallengeType `json:"type"`
Status acme.Status `json:"status"`
Token string `json:"token"`
Value string `json:"value"`
ValidatedAt string `json:"validatedAt"`
CreatedAt time.Time `json:"createdAt"`
Error *acme.Error `json:"error"` // TODO(hs): a bit dangerous; should become db-specific type
}
func (dbc *dbChallenge) clone() *dbChallenge {
2021-03-01 06:49:20 +00:00
u := *dbc
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) {
return nil, acme.NewError(acme.ErrorMalformedType, "challenge %s not found", id)
} else if err != nil {
return nil, errors.Wrapf(err, "error loading acme challenge %s", id)
}
2021-03-01 06:49:20 +00:00
dbch := new(dbChallenge)
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-03-01 06:49:20 +00:00
return dbch, nil
}
// 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 {
var err error
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")
}
dbch := &dbChallenge{
ID: ch.ID,
AccountID: ch.AccountID,
Value: ch.Value,
2021-03-01 06:49:20 +00:00
Status: acme.StatusPending,
Token: ch.Token,
CreatedAt: clock.Now(),
Type: ch.Type,
}
2021-03-01 06:49:20 +00:00
return db.save(ctx, ch.ID, dbch, nil, "challenge", challengeTable)
}
// 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) {
dbch, err := db.getDBChallenge(ctx, id)
if err != nil {
2021-03-01 06:49:20 +00:00
return nil, err
}
2021-03-01 06:49:20 +00:00
ch := &acme.Challenge{
ID: dbch.ID,
AccountID: dbch.AccountID,
Type: dbch.Type,
Value: dbch.Value,
Status: dbch.Status,
Token: dbch.Token,
Error: dbch.Error,
ValidatedAt: dbch.ValidatedAt,
}
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
old, err := db.getDBChallenge(ctx, ch.ID)
if err != nil {
return err
}
nu := old.clone()
// These should be the only values changing in an Update request.
nu.Status = ch.Status
nu.Error = ch.Error
nu.ValidatedAt = ch.ValidatedAt
2021-02-28 01:05:37 +00:00
return db.save(ctx, old.ID, nu, old, "challenge", challengeTable)
}