2021-02-26 18:12:30 +00:00
|
|
|
package nosql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-02-28 01:05:37 +00:00
|
|
|
"github.com/smallstep/certificates/acme"
|
2021-02-26 18:12:30 +00:00
|
|
|
"github.com/smallstep/nosql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Mutex for locking ordersByAccount index operations.
|
|
|
|
var ordersByAccountMux sync.Mutex
|
|
|
|
|
|
|
|
type dbOrder struct {
|
2021-03-01 06:49:20 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
AccountID string `json:"accountID"`
|
|
|
|
ProvisionerID string `json:"provisionerID"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Expires time.Time `json:"expires,omitempty"`
|
|
|
|
Status acme.Status `json:"status"`
|
|
|
|
Identifiers []acme.Identifier `json:"identifiers"`
|
|
|
|
NotBefore time.Time `json:"notBefore,omitempty"`
|
|
|
|
NotAfter time.Time `json:"notAfter,omitempty"`
|
2021-03-05 07:10:46 +00:00
|
|
|
Error *acme.Error `json:"error,omitempty"`
|
2021-03-01 06:49:20 +00:00
|
|
|
Authorizations []string `json:"authorizations"`
|
2021-03-05 07:10:46 +00:00
|
|
|
CertificateID string `json:"certificate,omitempty"`
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 21:06:43 +00:00
|
|
|
func (a *dbOrder) clone() *dbOrder {
|
|
|
|
b := *a
|
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:12:30 +00:00
|
|
|
// getDBOrder retrieves and unmarshals an ACME Order type from the database.
|
2021-03-06 21:06:43 +00:00
|
|
|
func (db *DB) getDBOrder(ctx context.Context, id string) (*dbOrder, error) {
|
2021-02-26 18:12:30 +00:00
|
|
|
b, err := db.db.Get(orderTable, []byte(id))
|
|
|
|
if nosql.IsErrNotFound(err) {
|
2021-03-05 07:10:46 +00:00
|
|
|
return nil, acme.WrapError(acme.ErrorMalformedType, err, "order %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 order %s", id)
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
o := new(dbOrder)
|
|
|
|
if err := json.Unmarshal(b, &o); err != nil {
|
2021-03-01 06:49:20 +00:00
|
|
|
return nil, errors.Wrap(err, "error unmarshaling order")
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrder retrieves an ACME Order from the database.
|
2021-03-01 06:49:20 +00:00
|
|
|
func (db *DB) GetOrder(ctx context.Context, id string) (*acme.Order, error) {
|
2021-03-06 21:06:43 +00:00
|
|
|
dbo, err := db.getDBOrder(ctx, id)
|
2021-03-05 07:10:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
2021-03-05 07:10:46 +00:00
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
o := &acme.Order{
|
2021-03-05 07:10:46 +00:00
|
|
|
Status: dbo.Status,
|
|
|
|
Expires: dbo.Expires,
|
|
|
|
Identifiers: dbo.Identifiers,
|
|
|
|
NotBefore: dbo.NotBefore,
|
|
|
|
NotAfter: dbo.NotAfter,
|
|
|
|
AuthorizationIDs: dbo.Authorizations,
|
|
|
|
ID: dbo.ID,
|
|
|
|
ProvisionerID: dbo.ProvisionerID,
|
|
|
|
CertificateID: dbo.CertificateID,
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:05:37 +00:00
|
|
|
// CreateOrder creates ACME Order resources and saves them to the DB.
|
|
|
|
func (db *DB) CreateOrder(ctx context.Context, o *acme.Order) error {
|
2021-03-05 07:10:46 +00:00
|
|
|
var err error
|
2021-02-28 01:05:37 +00:00
|
|
|
o.ID, err = randID()
|
2021-02-26 18:12:30 +00:00
|
|
|
if err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return err
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
now := clock.Now()
|
2021-02-26 18:17:18 +00:00
|
|
|
dbo := &dbOrder{
|
2021-02-28 01:05:37 +00:00
|
|
|
ID: o.ID,
|
|
|
|
AccountID: o.AccountID,
|
|
|
|
ProvisionerID: o.ProvisionerID,
|
2021-02-26 18:12:30 +00:00
|
|
|
Created: now,
|
2021-03-05 07:10:46 +00:00
|
|
|
Status: acme.StatusPending,
|
|
|
|
Expires: o.Expires,
|
2021-03-03 23:16:25 +00:00
|
|
|
Identifiers: o.Identifiers,
|
|
|
|
NotBefore: o.NotBefore,
|
|
|
|
NotAfter: o.NotBefore,
|
|
|
|
Authorizations: o.AuthorizationIDs,
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
2021-03-05 07:10:46 +00:00
|
|
|
if err := db.save(ctx, o.ID, dbo, nil, "order", orderTable); err != nil {
|
|
|
|
return err
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 21:06:43 +00:00
|
|
|
_, err = db.updateAddOrderIDs(ctx, o.AccountID, o.ID)
|
2021-02-26 18:12:30 +00:00
|
|
|
if err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return err
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
2021-03-05 07:10:46 +00:00
|
|
|
return nil
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type orderIDsByAccount struct{}
|
|
|
|
|
|
|
|
// addOrderID adds an order ID to a users index of in progress order IDs.
|
|
|
|
// This method will also cull any orders that are no longer in the `pending`
|
|
|
|
// state from the index before returning it.
|
2021-03-06 21:06:43 +00:00
|
|
|
func (db *DB) updateAddOrderIDs(ctx context.Context, accID string, addOids ...string) ([]string, error) {
|
2021-02-26 18:12:30 +00:00
|
|
|
ordersByAccountMux.Lock()
|
|
|
|
defer ordersByAccountMux.Unlock()
|
|
|
|
|
2021-03-06 21:06:43 +00:00
|
|
|
b, err := db.db.Get(ordersByAccountIDTable, []byte(accID))
|
2021-02-26 18:12:30 +00:00
|
|
|
if err != nil {
|
|
|
|
if nosql.IsErrNotFound(err) {
|
|
|
|
return []string{}, nil
|
|
|
|
}
|
2021-03-05 07:10:46 +00:00
|
|
|
return nil, errors.Wrapf(err, "error loading orderIDs for account %s", accID)
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
var oids []string
|
|
|
|
if err := json.Unmarshal(b, &oids); err != nil {
|
2021-03-05 07:10:46 +00:00
|
|
|
return nil, errors.Wrapf(err, "error unmarshaling orderIDs for account %s", accID)
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any order that is not in PENDING state and update the stored list
|
|
|
|
// before returning.
|
|
|
|
//
|
|
|
|
// According to RFC 8555:
|
|
|
|
// The server SHOULD include pending orders and SHOULD NOT include orders
|
|
|
|
// that are invalid in the array of URLs.
|
|
|
|
pendOids := []string{}
|
|
|
|
for _, oid := range oids {
|
2021-03-06 21:06:43 +00:00
|
|
|
o, err := db.GetOrder(ctx, oid)
|
2021-02-26 18:12:30 +00:00
|
|
|
if err != nil {
|
2021-03-06 21:06:43 +00:00
|
|
|
return nil, acme.WrapErrorISE(err, "error loading order %s for account %s", oid, accID)
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
2021-03-06 21:06:43 +00:00
|
|
|
if err = o.UpdateStatus(ctx, db); err != nil {
|
|
|
|
return nil, acme.WrapErrorISE(err, "error updating order %s for account %s", oid, accID)
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
2021-03-05 07:10:46 +00:00
|
|
|
if o.Status == acme.StatusPending {
|
2021-02-26 18:12:30 +00:00
|
|
|
pendOids = append(pendOids, oid)
|
|
|
|
}
|
|
|
|
}
|
2021-03-06 21:06:43 +00:00
|
|
|
pendOids = append(pendOids, addOids...)
|
|
|
|
if len(oids) == 0 {
|
|
|
|
oids = nil
|
|
|
|
}
|
|
|
|
if err = db.save(ctx, accID, pendOids, oids, "orderIDsByAccountID", ordersByAccountIDTable); err != nil {
|
|
|
|
// Delete all orders that may have been previously stored if orderIDsByAccountID update fails.
|
|
|
|
for _, oid := range addOids {
|
|
|
|
db.db.Del(orderTable, []byte(oid))
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
2021-03-06 21:06:43 +00:00
|
|
|
return nil, errors.Wrap(err, "error saving OrderIDsByAccountID index")
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
|
|
|
return pendOids, nil
|
|
|
|
}
|
|
|
|
|
2021-03-06 21:06:43 +00:00
|
|
|
func (db *DB) GetOrdersByAccountID(ctx context.Context, accID string) ([]string, error) {
|
|
|
|
return db.updateAddOrderIDs(ctx, accID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateOrder saves an updated ACME Order to the database.
|
|
|
|
func (db *DB) UpdateOrder(ctx context.Context, o *acme.Order) error {
|
|
|
|
old, err := db.getDBOrder(ctx, o.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|
2021-03-06 21:06:43 +00:00
|
|
|
|
|
|
|
nu := old.clone()
|
|
|
|
|
|
|
|
nu.Status = o.Status
|
|
|
|
nu.Error = o.Error
|
|
|
|
nu.CertificateID = o.CertificateID
|
|
|
|
return db.save(ctx, old.ID, nu, old, "order", orderTable)
|
2021-02-26 18:12:30 +00:00
|
|
|
}
|