Преглед изворни кода

Refactor ACME EAB queries

The ACME EAB keys are now also indexed by the provisioner. This
solves part of the issue in which too many EAB keys may be in
memory at a given time.
Herman Slatman пре 4 година
родитељ
комит
ef16febf40

+ 7 - 7
acme/account.go

@@ -45,13 +45,13 @@ func KeyToID(jwk *jose.JSONWebKey) (string, error) {
 
 // ExternalAccountKey is an ACME External Account Binding key.
 type ExternalAccountKey struct {
-	ID          string    `json:"id"`
-	Provisioner string    `json:"provisioner"`
-	Reference   string    `json:"reference"`
-	AccountID   string    `json:"-"`
-	KeyBytes    []byte    `json:"-"`
-	CreatedAt   time.Time `json:"createdAt"`
-	BoundAt     time.Time `json:"boundAt,omitempty"`
+	ID            string    `json:"id"`
+	ProvisionerID string    `json:"provisionerID"`
+	Reference     string    `json:"reference"`
+	AccountID     string    `json:"-"`
+	KeyBytes      []byte    `json:"-"`
+	CreatedAt     time.Time `json:"createdAt"`
+	BoundAt       time.Time `json:"boundAt,omitempty"`
 }
 
 // AlreadyBound returns whether this EAK is already bound to

+ 10 - 10
acme/account_test.go

@@ -92,10 +92,10 @@ func TestExternalAccountKey_BindTo(t *testing.T) {
 		{
 			name: "ok",
 			eak: &ExternalAccountKey{
-				ID:          "eakID",
-				Provisioner: "prov",
-				Reference:   "ref",
-				KeyBytes:    []byte{1, 3, 3, 7},
+				ID:            "eakID",
+				ProvisionerID: "provID",
+				Reference:     "ref",
+				KeyBytes:      []byte{1, 3, 3, 7},
 			},
 			acct: &Account{
 				ID: "accountID",
@@ -105,12 +105,12 @@ func TestExternalAccountKey_BindTo(t *testing.T) {
 		{
 			name: "fail/already-bound",
 			eak: &ExternalAccountKey{
-				ID:          "eakID",
-				Provisioner: "prov",
-				Reference:   "ref",
-				KeyBytes:    []byte{1, 3, 3, 7},
-				AccountID:   "someAccountID",
-				BoundAt:     boundAt,
+				ID:            "eakID",
+				ProvisionerID: "provID",
+				Reference:     "ref",
+				KeyBytes:      []byte{1, 3, 3, 7},
+				AccountID:     "someAccountID",
+				BoundAt:       boundAt,
 			},
 			acct: &Account{
 				ID: "accountID",

+ 2 - 2
acme/api/account.go

@@ -144,7 +144,7 @@ func (h *Handler) NewAccount(w http.ResponseWriter, r *http.Request) {
 				api.WriteError(w, err)
 				return
 			}
-			if err := h.db.UpdateExternalAccountKey(ctx, prov.Name, eak); err != nil {
+			if err := h.db.UpdateExternalAccountKey(ctx, prov.ID, eak); err != nil {
 				api.WriteError(w, acme.WrapErrorISE(err, "error updating external account binding key"))
 				return
 			}
@@ -274,7 +274,7 @@ func (h *Handler) validateExternalAccountBinding(ctx context.Context, nar *NewAc
 		return nil, acmeErr
 	}
 
-	externalAccountKey, err := h.db.GetExternalAccountKey(ctx, acmeProv.Name, keyID)
+	externalAccountKey, err := h.db.GetExternalAccountKey(ctx, acmeProv.ID, keyID)
 	if err != nil {
 		if _, ok := err.(*acme.Error); ok {
 			return nil, acme.WrapError(acme.ErrorUnauthorizedType, err, "the field 'kid' references an unknown key")

+ 49 - 47
acme/api/account_test.go

@@ -351,6 +351,7 @@ func TestHandler_NewAccount(t *testing.T) {
 	prov := newProv()
 	escProvName := url.PathEscape(prov.GetName())
 	baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
+	provID := prov.GetID()
 
 	type test struct {
 		db         acme.DB
@@ -554,11 +555,11 @@ func TestHandler_NewAccount(t *testing.T) {
 			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			ctx = context.WithValue(ctx, jwsContextKey, parsedJWS)
 			eak := &acme.ExternalAccountKey{
-				ID:          "eakID",
-				Provisioner: escProvName,
-				Reference:   "testeak",
-				KeyBytes:    []byte{1, 3, 3, 7},
-				CreatedAt:   time.Now(),
+				ID:            "eakID",
+				ProvisionerID: provID,
+				Reference:     "testeak",
+				KeyBytes:      []byte{1, 3, 3, 7},
+				CreatedAt:     time.Now(),
 			}
 			return test{
 				db: &acme.MockDB{
@@ -731,11 +732,11 @@ func TestHandler_NewAccount(t *testing.T) {
 					},
 					MockGetExternalAccountKey: func(ctx context.Context, provisionerName, keyID string) (*acme.ExternalAccountKey, error) {
 						return &acme.ExternalAccountKey{
-							ID:          "eakID",
-							Provisioner: escProvName,
-							Reference:   "testeak",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   time.Now(),
+							ID:            "eakID",
+							ProvisionerID: provID,
+							Reference:     "testeak",
+							KeyBytes:      []byte{1, 3, 3, 7},
+							CreatedAt:     time.Now(),
 						}, nil
 					},
 					MockUpdateExternalAccountKey: func(ctx context.Context, provisionerName string, eak *acme.ExternalAccountKey) error {
@@ -1056,6 +1057,7 @@ func TestHandler_validateExternalAccountBinding(t *testing.T) {
 	acmeProv := newACMEProv(t)
 	escProvName := url.PathEscape(acmeProv.GetName())
 	baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
+	provID := acmeProv.GetID()
 	type test struct {
 		db  acme.DB
 		ctx context.Context
@@ -1128,11 +1130,11 @@ func TestHandler_validateExternalAccountBinding(t *testing.T) {
 				db: &acme.MockDB{
 					MockGetExternalAccountKey: func(ctx context.Context, provisionerName, keyID string) (*acme.ExternalAccountKey, error) {
 						return &acme.ExternalAccountKey{
-							ID:          "eakID",
-							Provisioner: escProvName,
-							Reference:   "testeak",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   createdAt,
+							ID:            "eakID",
+							ProvisionerID: provID,
+							Reference:     "testeak",
+							KeyBytes:      []byte{1, 3, 3, 7},
+							CreatedAt:     createdAt,
 						}, nil
 					},
 				},
@@ -1142,11 +1144,11 @@ func TestHandler_validateExternalAccountBinding(t *testing.T) {
 					ExternalAccountBinding: eab,
 				},
 				eak: &acme.ExternalAccountKey{
-					ID:          "eakID",
-					Provisioner: escProvName,
-					Reference:   "testeak",
-					KeyBytes:    []byte{1, 3, 3, 7},
-					CreatedAt:   createdAt,
+					ID:            "eakID",
+					ProvisionerID: provID,
+					Reference:     "testeak",
+					KeyBytes:      []byte{1, 3, 3, 7},
+					CreatedAt:     createdAt,
 				},
 				err: nil,
 			}
@@ -1492,12 +1494,12 @@ func TestHandler_validateExternalAccountBinding(t *testing.T) {
 				db: &acme.MockDB{
 					MockGetExternalAccountKey: func(ctx context.Context, provisionerName, keyID string) (*acme.ExternalAccountKey, error) {
 						return &acme.ExternalAccountKey{
-							ID:          "eakID",
-							Provisioner: escProvName,
-							Reference:   "testeak",
-							CreatedAt:   createdAt,
-							AccountID:   "some-account-id",
-							BoundAt:     boundAt,
+							ID:            "eakID",
+							ProvisionerID: provID,
+							Reference:     "testeak",
+							CreatedAt:     createdAt,
+							AccountID:     "some-account-id",
+							BoundAt:       boundAt,
 						}, nil
 					},
 				},
@@ -1549,11 +1551,11 @@ func TestHandler_validateExternalAccountBinding(t *testing.T) {
 				db: &acme.MockDB{
 					MockGetExternalAccountKey: func(ctx context.Context, provisionerName, keyID string) (*acme.ExternalAccountKey, error) {
 						return &acme.ExternalAccountKey{
-							ID:          "eakID",
-							Provisioner: escProvName,
-							Reference:   "testeak",
-							KeyBytes:    []byte{1, 2, 3, 4},
-							CreatedAt:   time.Now(),
+							ID:            "eakID",
+							ProvisionerID: provID,
+							Reference:     "testeak",
+							KeyBytes:      []byte{1, 2, 3, 4},
+							CreatedAt:     time.Now(),
 						}, nil
 					},
 				},
@@ -1607,11 +1609,11 @@ func TestHandler_validateExternalAccountBinding(t *testing.T) {
 				db: &acme.MockDB{
 					MockGetExternalAccountKey: func(ctx context.Context, provisionerName, keyID string) (*acme.ExternalAccountKey, error) {
 						return &acme.ExternalAccountKey{
-							ID:          "eakID",
-							Provisioner: escProvName,
-							Reference:   "testeak",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   time.Now(),
+							ID:            "eakID",
+							ProvisionerID: provID,
+							Reference:     "testeak",
+							KeyBytes:      []byte{1, 3, 3, 7},
+							CreatedAt:     time.Now(),
 						}, nil
 					},
 				},
@@ -1662,11 +1664,11 @@ func TestHandler_validateExternalAccountBinding(t *testing.T) {
 				db: &acme.MockDB{
 					MockGetExternalAccountKey: func(ctx context.Context, provisionerName, keyID string) (*acme.ExternalAccountKey, error) {
 						return &acme.ExternalAccountKey{
-							ID:          "eakID",
-							Provisioner: escProvName,
-							Reference:   "testeak",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   time.Now(),
+							ID:            "eakID",
+							ProvisionerID: provID,
+							Reference:     "testeak",
+							KeyBytes:      []byte{1, 3, 3, 7},
+							CreatedAt:     time.Now(),
 						}, nil
 					},
 				},
@@ -1718,11 +1720,11 @@ func TestHandler_validateExternalAccountBinding(t *testing.T) {
 				db: &acme.MockDB{
 					MockGetExternalAccountKey: func(ctx context.Context, provisionerName, keyID string) (*acme.ExternalAccountKey, error) {
 						return &acme.ExternalAccountKey{
-							ID:          "eakID",
-							Provisioner: escProvName,
-							Reference:   "testeak",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   time.Now(),
+							ID:            "eakID",
+							ProvisionerID: provID,
+							Reference:     "testeak",
+							KeyBytes:      []byte{1, 3, 3, 7},
+							CreatedAt:     time.Now(),
 						}, nil
 					},
 				},
@@ -1765,7 +1767,7 @@ func TestHandler_validateExternalAccountBinding(t *testing.T) {
 					assert.NotNil(t, tc.eak)
 					assert.Equals(t, got.ID, tc.eak.ID)
 					assert.Equals(t, got.KeyBytes, tc.eak.KeyBytes)
-					assert.Equals(t, got.Provisioner, tc.eak.Provisioner)
+					assert.Equals(t, got.ProvisionerID, tc.eak.ProvisionerID)
 					assert.Equals(t, got.Reference, tc.eak.Reference)
 					assert.Equals(t, got.CreatedAt, tc.eak.CreatedAt)
 					assert.Equals(t, got.AccountID, tc.eak.AccountID)

+ 24 - 24
acme/db.go

@@ -19,12 +19,12 @@ type DB interface {
 	GetAccountByKeyID(ctx context.Context, kid string) (*Account, error)
 	UpdateAccount(ctx context.Context, acc *Account) error
 
-	CreateExternalAccountKey(ctx context.Context, provisionerName, reference string) (*ExternalAccountKey, error)
-	GetExternalAccountKey(ctx context.Context, provisionerName, keyID string) (*ExternalAccountKey, error)
-	GetExternalAccountKeys(ctx context.Context, provisionerName string) ([]*ExternalAccountKey, error)
-	GetExternalAccountKeyByReference(ctx context.Context, provisionerName, reference string) (*ExternalAccountKey, error)
-	DeleteExternalAccountKey(ctx context.Context, provisionerName, keyID string) error
-	UpdateExternalAccountKey(ctx context.Context, provisionerName string, eak *ExternalAccountKey) error
+	CreateExternalAccountKey(ctx context.Context, provisionerID, reference string) (*ExternalAccountKey, error)
+	GetExternalAccountKey(ctx context.Context, provisionerID, keyID string) (*ExternalAccountKey, error)
+	GetExternalAccountKeys(ctx context.Context, provisionerID string) ([]*ExternalAccountKey, error)
+	GetExternalAccountKeyByReference(ctx context.Context, provisionerID, reference string) (*ExternalAccountKey, error)
+	DeleteExternalAccountKey(ctx context.Context, provisionerID, keyID string) error
+	UpdateExternalAccountKey(ctx context.Context, provisionerID string, eak *ExternalAccountKey) error
 
 	CreateNonce(ctx context.Context) (Nonce, error)
 	DeleteNonce(ctx context.Context, nonce Nonce) error
@@ -56,12 +56,12 @@ type MockDB struct {
 	MockGetAccountByKeyID func(ctx context.Context, kid string) (*Account, error)
 	MockUpdateAccount     func(ctx context.Context, acc *Account) error
 
-	MockCreateExternalAccountKey         func(ctx context.Context, provisionerName, reference string) (*ExternalAccountKey, error)
-	MockGetExternalAccountKey            func(ctx context.Context, provisionerName, keyID string) (*ExternalAccountKey, error)
-	MockGetExternalAccountKeys           func(ctx context.Context, provisionerName string) ([]*ExternalAccountKey, error)
-	MockGetExternalAccountKeyByReference func(ctx context.Context, provisionerName, reference string) (*ExternalAccountKey, error)
-	MockDeleteExternalAccountKey         func(ctx context.Context, provisionerName, keyID string) error
-	MockUpdateExternalAccountKey         func(ctx context.Context, provisionerName string, eak *ExternalAccountKey) error
+	MockCreateExternalAccountKey         func(ctx context.Context, provisionerID, reference string) (*ExternalAccountKey, error)
+	MockGetExternalAccountKey            func(ctx context.Context, provisionerID, keyID string) (*ExternalAccountKey, error)
+	MockGetExternalAccountKeys           func(ctx context.Context, provisionerID string) ([]*ExternalAccountKey, error)
+	MockGetExternalAccountKeyByReference func(ctx context.Context, provisionerID, reference string) (*ExternalAccountKey, error)
+	MockDeleteExternalAccountKey         func(ctx context.Context, provisionerID, keyID string) error
+	MockUpdateExternalAccountKey         func(ctx context.Context, provisionerID string, eak *ExternalAccountKey) error
 
 	MockCreateNonce func(ctx context.Context) (Nonce, error)
 	MockDeleteNonce func(ctx context.Context, nonce Nonce) error
@@ -129,9 +129,9 @@ func (m *MockDB) UpdateAccount(ctx context.Context, acc *Account) error {
 }
 
 // CreateExternalAccountKey mock
-func (m *MockDB) CreateExternalAccountKey(ctx context.Context, provisionerName, reference string) (*ExternalAccountKey, error) {
+func (m *MockDB) CreateExternalAccountKey(ctx context.Context, provisionerID, reference string) (*ExternalAccountKey, error) {
 	if m.MockCreateExternalAccountKey != nil {
-		return m.MockCreateExternalAccountKey(ctx, provisionerName, reference)
+		return m.MockCreateExternalAccountKey(ctx, provisionerID, reference)
 	} else if m.MockError != nil {
 		return nil, m.MockError
 	}
@@ -139,9 +139,9 @@ func (m *MockDB) CreateExternalAccountKey(ctx context.Context, provisionerName,
 }
 
 // GetExternalAccountKey mock
-func (m *MockDB) GetExternalAccountKey(ctx context.Context, provisionerName, keyID string) (*ExternalAccountKey, error) {
+func (m *MockDB) GetExternalAccountKey(ctx context.Context, provisionerID, keyID string) (*ExternalAccountKey, error) {
 	if m.MockGetExternalAccountKey != nil {
-		return m.MockGetExternalAccountKey(ctx, provisionerName, keyID)
+		return m.MockGetExternalAccountKey(ctx, provisionerID, keyID)
 	} else if m.MockError != nil {
 		return nil, m.MockError
 	}
@@ -149,9 +149,9 @@ func (m *MockDB) GetExternalAccountKey(ctx context.Context, provisionerName, key
 }
 
 // GetExternalAccountKeys mock
-func (m *MockDB) GetExternalAccountKeys(ctx context.Context, provisionerName string) ([]*ExternalAccountKey, error) {
+func (m *MockDB) GetExternalAccountKeys(ctx context.Context, provisionerID string) ([]*ExternalAccountKey, error) {
 	if m.MockGetExternalAccountKeys != nil {
-		return m.MockGetExternalAccountKeys(ctx, provisionerName)
+		return m.MockGetExternalAccountKeys(ctx, provisionerID)
 	} else if m.MockError != nil {
 		return nil, m.MockError
 	}
@@ -159,9 +159,9 @@ func (m *MockDB) GetExternalAccountKeys(ctx context.Context, provisionerName str
 }
 
 // GetExternalAccountKeyByReference mock
-func (m *MockDB) GetExternalAccountKeyByReference(ctx context.Context, provisionerName, reference string) (*ExternalAccountKey, error) {
+func (m *MockDB) GetExternalAccountKeyByReference(ctx context.Context, provisionerID, reference string) (*ExternalAccountKey, error) {
 	if m.MockGetExternalAccountKeyByReference != nil {
-		return m.MockGetExternalAccountKeyByReference(ctx, provisionerName, reference)
+		return m.MockGetExternalAccountKeyByReference(ctx, provisionerID, reference)
 	} else if m.MockError != nil {
 		return nil, m.MockError
 	}
@@ -169,9 +169,9 @@ func (m *MockDB) GetExternalAccountKeyByReference(ctx context.Context, provision
 }
 
 // DeleteExternalAccountKey mock
-func (m *MockDB) DeleteExternalAccountKey(ctx context.Context, provisionerName, keyID string) error {
+func (m *MockDB) DeleteExternalAccountKey(ctx context.Context, provisionerID, keyID string) error {
 	if m.MockDeleteExternalAccountKey != nil {
-		return m.MockDeleteExternalAccountKey(ctx, provisionerName, keyID)
+		return m.MockDeleteExternalAccountKey(ctx, provisionerID, keyID)
 	} else if m.MockError != nil {
 		return m.MockError
 	}
@@ -179,9 +179,9 @@ func (m *MockDB) DeleteExternalAccountKey(ctx context.Context, provisionerName,
 }
 
 // UpdateExternalAccountKey mock
-func (m *MockDB) UpdateExternalAccountKey(ctx context.Context, provisionerName string, eak *ExternalAccountKey) error {
+func (m *MockDB) UpdateExternalAccountKey(ctx context.Context, provisionerID string, eak *ExternalAccountKey) error {
 	if m.MockUpdateExternalAccountKey != nil {
-		return m.MockUpdateExternalAccountKey(ctx, provisionerName, eak)
+		return m.MockUpdateExternalAccountKey(ctx, provisionerID, eak)
 	} else if m.MockError != nil {
 		return m.MockError
 	}

+ 203 - 68
acme/db/nosql/account.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"crypto/rand"
 	"encoding/json"
+	"sync"
 	"time"
 
 	"github.com/pkg/errors"
@@ -12,6 +13,9 @@ import (
 	"go.step.sm/crypto/jose"
 )
 
+// Mutex for locking referencesByProvisioner index operations.
+var referencesByProvisionerIndexMux sync.Mutex
+
 // dbAccount represents an ACME account.
 type dbAccount struct {
 	ID            string           `json:"id"`
@@ -28,13 +32,13 @@ func (dba *dbAccount) clone() *dbAccount {
 }
 
 type dbExternalAccountKey struct {
-	ID          string    `json:"id"`
-	Provisioner string    `json:"provisioner"`
-	Reference   string    `json:"reference"`
-	AccountID   string    `json:"accountID,omitempty"`
-	KeyBytes    []byte    `json:"key"`
-	CreatedAt   time.Time `json:"createdAt"`
-	BoundAt     time.Time `json:"boundAt"`
+	ID            string    `json:"id"`
+	ProvisionerID string    `json:"provisionerID"`
+	Reference     string    `json:"reference"`
+	AccountID     string    `json:"accountID,omitempty"`
+	KeyBytes      []byte    `json:"key"`
+	CreatedAt     time.Time `json:"createdAt"`
+	BoundAt       time.Time `json:"boundAt"`
 }
 
 type dbExternalAccountKeyReference struct {
@@ -170,7 +174,7 @@ func (db *DB) UpdateAccount(ctx context.Context, acc *acme.Account) error {
 }
 
 // CreateExternalAccountKey creates a new External Account Binding key with a name
-func (db *DB) CreateExternalAccountKey(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
+func (db *DB) CreateExternalAccountKey(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
 	keyID, err := randID()
 	if err != nil {
 		return nil, err
@@ -183,106 +187,125 @@ func (db *DB) CreateExternalAccountKey(ctx context.Context, provisionerName, ref
 	}
 
 	dbeak := &dbExternalAccountKey{
-		ID:          keyID,
-		Provisioner: provisionerName,
-		Reference:   reference,
-		KeyBytes:    random,
-		CreatedAt:   clock.Now(),
+		ID:            keyID,
+		ProvisionerID: provisionerID,
+		Reference:     reference,
+		KeyBytes:      random,
+		CreatedAt:     clock.Now(),
 	}
 
 	if err := db.save(ctx, keyID, dbeak, nil, "external_account_key", externalAccountKeyTable); err != nil {
 		return nil, err
 	}
 
+	if err := db.addEAKID(ctx, provisionerID, dbeak.ID); err != nil {
+		return nil, err
+	}
+
 	if dbeak.Reference != "" {
 		dbExternalAccountKeyReference := &dbExternalAccountKeyReference{
 			Reference:            dbeak.Reference,
 			ExternalAccountKeyID: dbeak.ID,
 		}
-		if err := db.save(ctx, dbeak.Reference, dbExternalAccountKeyReference, nil, "external_account_key_reference", externalAccountKeysByReferenceTable); err != nil {
+		if err := db.save(ctx, referenceKey(provisionerID, dbeak.Reference), dbExternalAccountKeyReference, nil, "external_account_key_reference", externalAccountKeysByReferenceTable); err != nil {
 			return nil, err
 		}
 	}
 
 	return &acme.ExternalAccountKey{
-		ID:          dbeak.ID,
-		Provisioner: dbeak.Provisioner,
-		Reference:   dbeak.Reference,
-		AccountID:   dbeak.AccountID,
-		KeyBytes:    dbeak.KeyBytes,
-		CreatedAt:   dbeak.CreatedAt,
-		BoundAt:     dbeak.BoundAt,
+		ID:            dbeak.ID,
+		ProvisionerID: dbeak.ProvisionerID,
+		Reference:     dbeak.Reference,
+		AccountID:     dbeak.AccountID,
+		KeyBytes:      dbeak.KeyBytes,
+		CreatedAt:     dbeak.CreatedAt,
+		BoundAt:       dbeak.BoundAt,
 	}, nil
 }
 
 // GetExternalAccountKey retrieves an External Account Binding key by KeyID
-func (db *DB) GetExternalAccountKey(ctx context.Context, provisionerName, keyID string) (*acme.ExternalAccountKey, error) {
+func (db *DB) GetExternalAccountKey(ctx context.Context, provisionerID, keyID string) (*acme.ExternalAccountKey, error) {
 	dbeak, err := db.getDBExternalAccountKey(ctx, keyID)
 	if err != nil {
 		return nil, err
 	}
 
-	if dbeak.Provisioner != provisionerName {
-		return nil, acme.NewError(acme.ErrorUnauthorizedType, "name of provisioner does not match provisioner for which the EAB key was created")
+	if dbeak.ProvisionerID != provisionerID {
+		return nil, acme.NewError(acme.ErrorUnauthorizedType, "provisioner does not match provisioner for which the EAB key was created")
 	}
 
 	return &acme.ExternalAccountKey{
-		ID:          dbeak.ID,
-		Provisioner: dbeak.Provisioner,
-		Reference:   dbeak.Reference,
-		AccountID:   dbeak.AccountID,
-		KeyBytes:    dbeak.KeyBytes,
-		CreatedAt:   dbeak.CreatedAt,
-		BoundAt:     dbeak.BoundAt,
+		ID:            dbeak.ID,
+		ProvisionerID: dbeak.ProvisionerID,
+		Reference:     dbeak.Reference,
+		AccountID:     dbeak.AccountID,
+		KeyBytes:      dbeak.KeyBytes,
+		CreatedAt:     dbeak.CreatedAt,
+		BoundAt:       dbeak.BoundAt,
 	}, nil
 }
 
-func (db *DB) DeleteExternalAccountKey(ctx context.Context, provisionerName, keyID string) error {
+func (db *DB) DeleteExternalAccountKey(ctx context.Context, provisionerID, keyID string) error {
 	dbeak, err := db.getDBExternalAccountKey(ctx, keyID)
 	if err != nil {
 		return errors.Wrapf(err, "error loading ACME EAB Key with Key ID %s", keyID)
 	}
-	if dbeak.Provisioner != provisionerName {
-		return errors.New("name of provisioner does not match provisioner for which the EAB key was created")
+
+	if dbeak.ProvisionerID != provisionerID {
+		return errors.New("provisioner does not match provisioner for which the EAB key was created")
 	}
+
 	if dbeak.Reference != "" {
-		err = db.db.Del(externalAccountKeysByReferenceTable, []byte(dbeak.Reference))
-		if err != nil {
-			return errors.Wrapf(err, "error deleting ACME EAB Key Reference with Key ID %s and reference %s", keyID, dbeak.Reference)
+		if err := db.db.Del(externalAccountKeysByReferenceTable, []byte(referenceKey(provisionerID, dbeak.Reference))); err != nil {
+			return errors.Wrapf(err, "error deleting ACME EAB Key reference with Key ID %s and reference %s", keyID, dbeak.Reference)
 		}
 	}
-	if err = db.db.Del(externalAccountKeyTable, []byte(keyID)); err != nil {
+	if err := db.db.Del(externalAccountKeyTable, []byte(keyID)); err != nil {
 		return errors.Wrapf(err, "error deleting ACME EAB Key with Key ID %s", keyID)
 	}
+	if err := db.deleteEAKID(ctx, provisionerID, keyID); err != nil {
+		return errors.Wrapf(err, "error removing ACME EAB Key ID %s", keyID)
+	}
+
 	return nil
 }
 
 // GetExternalAccountKeys retrieves all External Account Binding keys for a provisioner
-func (db *DB) GetExternalAccountKeys(ctx context.Context, provisionerName string) ([]*acme.ExternalAccountKey, error) {
+func (db *DB) GetExternalAccountKeys(ctx context.Context, provisionerID string) ([]*acme.ExternalAccountKey, error) {
+
+	// TODO: mutex?
 
-	// TODO: lookup by provisioner based on index
-	entries, err := db.db.List(externalAccountKeyTable)
+	var eakIDs []string
+	r, err := db.db.Get(externalAccountKeysByProvisionerIDTable, []byte(provisionerID))
 	if err != nil {
-		return nil, err
+		if !nosqlDB.IsErrNotFound(err) {
+			return nil, errors.Wrapf(err, "error loading ACME EAB Key IDs for provisioner %s", provisionerID)
+		}
+	} else {
+		if err := json.Unmarshal(r, &eakIDs); err != nil {
+			return nil, errors.Wrapf(err, "error unmarshaling ACME EAB Key IDs for provisioner %s", provisionerID)
+		}
 	}
 
 	keys := []*acme.ExternalAccountKey{}
-	for _, entry := range entries { // entries is sorted alphabetically on the key (ID) of the EAK; no need to sort this again.
-		dbeak := new(dbExternalAccountKey)
-		if err = json.Unmarshal(entry.Value, dbeak); err != nil {
-			return nil, errors.Wrapf(err, "error unmarshaling external account key %s into ExternalAccountKey", string(entry.Key))
+	for _, eakID := range eakIDs {
+		if eakID == "" {
+			continue // shouldn't happen; just in case
 		}
-		if dbeak.Provisioner != provisionerName {
-			continue
+		eak, err := db.getDBExternalAccountKey(ctx, eakID)
+		if err != nil {
+			if !nosqlDB.IsErrNotFound(err) {
+				return nil, errors.Wrapf(err, "error retrieving ACME EAB Key for provisioner %s and keyID %s", provisionerID, eakID)
+			}
 		}
 		keys = append(keys, &acme.ExternalAccountKey{
-			ID:          dbeak.ID,
-			KeyBytes:    dbeak.KeyBytes,
-			Provisioner: dbeak.Provisioner,
-			Reference:   dbeak.Reference,
-			AccountID:   dbeak.AccountID,
-			CreatedAt:   dbeak.CreatedAt,
-			BoundAt:     dbeak.BoundAt,
+			ID:            eak.ID,
+			KeyBytes:      eak.KeyBytes,
+			ProvisionerID: eak.ProvisionerID,
+			Reference:     eak.Reference,
+			AccountID:     eak.AccountID,
+			CreatedAt:     eak.CreatedAt,
+			BoundAt:       eak.BoundAt,
 		})
 	}
 
@@ -290,11 +313,12 @@ func (db *DB) GetExternalAccountKeys(ctx context.Context, provisionerName string
 }
 
 // GetExternalAccountKeyByReference retrieves an External Account Binding key with unique reference
-func (db *DB) GetExternalAccountKeyByReference(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
+func (db *DB) GetExternalAccountKeyByReference(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
 	if reference == "" {
 		return nil, nil
 	}
-	k, err := db.db.Get(externalAccountKeysByReferenceTable, []byte(reference))
+
+	k, err := db.db.Get(externalAccountKeysByReferenceTable, []byte(referenceKey(provisionerID, reference)))
 	if nosqlDB.IsErrNotFound(err) {
 		return nil, acme.ErrNotFound
 	} else if err != nil {
@@ -304,28 +328,139 @@ func (db *DB) GetExternalAccountKeyByReference(ctx context.Context, provisionerN
 	if err := json.Unmarshal(k, dbExternalAccountKeyReference); err != nil {
 		return nil, errors.Wrapf(err, "error unmarshaling ACME EAB key for reference %s", reference)
 	}
-	return db.GetExternalAccountKey(ctx, provisionerName, dbExternalAccountKeyReference.ExternalAccountKeyID)
+
+	return db.GetExternalAccountKey(ctx, provisionerID, dbExternalAccountKeyReference.ExternalAccountKeyID)
 }
 
-func (db *DB) UpdateExternalAccountKey(ctx context.Context, provisionerName string, eak *acme.ExternalAccountKey) error {
+func (db *DB) UpdateExternalAccountKey(ctx context.Context, provisionerID string, eak *acme.ExternalAccountKey) error {
 	old, err := db.getDBExternalAccountKey(ctx, eak.ID)
 	if err != nil {
 		return err
 	}
 
-	if old.Provisioner != provisionerName {
-		return errors.New("name of provisioner does not match provisioner for which the EAB key was created")
+	if old.ProvisionerID != provisionerID {
+		return errors.New("provisioner does not match provisioner for which the EAB key was created")
+	}
+
+	if old.ProvisionerID != eak.ProvisionerID {
+		return errors.New("cannot change provisioner for an existing ACME EAB Key")
+	}
+
+	if old.Reference != eak.Reference {
+		return errors.New("cannot change reference for an existing ACME EAB Key")
 	}
 
 	nu := dbExternalAccountKey{
-		ID:          eak.ID,
-		Provisioner: eak.Provisioner,
-		Reference:   eak.Reference,
-		AccountID:   eak.AccountID,
-		KeyBytes:    eak.KeyBytes,
-		CreatedAt:   eak.CreatedAt,
-		BoundAt:     eak.BoundAt,
+		ID:            eak.ID,
+		ProvisionerID: eak.ProvisionerID,
+		Reference:     eak.Reference,
+		AccountID:     eak.AccountID,
+		KeyBytes:      eak.KeyBytes,
+		CreatedAt:     eak.CreatedAt,
+		BoundAt:       eak.BoundAt,
 	}
 
 	return db.save(ctx, nu.ID, nu, old, "external_account_key", externalAccountKeyTable)
 }
+
+func (db *DB) addEAKID(ctx context.Context, provisionerID, eakID string) error {
+	referencesByProvisionerIndexMux.Lock()
+	defer referencesByProvisionerIndexMux.Unlock()
+
+	var eakIDs []string
+	b, err := db.db.Get(externalAccountKeysByProvisionerIDTable, []byte(provisionerID))
+	if err != nil {
+		if !nosqlDB.IsErrNotFound(err) {
+			return errors.Wrapf(err, "error loading eakIDs for provisioner %s", provisionerID)
+		}
+	} else {
+		if err := json.Unmarshal(b, &eakIDs); err != nil {
+			return errors.Wrapf(err, "error unmarshaling eakIDs for provisioner %s", provisionerID)
+		}
+	}
+
+	var newEAKIDs []string
+	newEAKIDs = append(newEAKIDs, eakIDs...)
+	newEAKIDs = append(newEAKIDs, eakID)
+	var (
+		_old interface{} = eakIDs
+		_new interface{} = newEAKIDs
+	)
+
+	if len(eakIDs) == 0 {
+		_old = nil
+	}
+
+	if err = db.save(ctx, provisionerID, _new, _old, "externalAccountKeysByProvisionerID", externalAccountKeysByProvisionerIDTable); err != nil {
+		return errors.Wrapf(err, "error saving eakIDs index for provisioner %s", provisionerID)
+	}
+
+	return nil
+}
+
+func (db *DB) deleteEAKID(ctx context.Context, provisionerID, eakID string) error {
+	referencesByProvisionerIndexMux.Lock()
+	defer referencesByProvisionerIndexMux.Unlock()
+
+	var eakIDs []string
+	b, err := db.db.Get(externalAccountKeysByProvisionerIDTable, []byte(provisionerID))
+	if err != nil {
+		if !nosqlDB.IsErrNotFound(err) {
+			return errors.Wrapf(err, "error loading reference IDs for provisioner %s", provisionerID)
+		}
+	} else {
+		if err := json.Unmarshal(b, &eakIDs); err != nil {
+			return errors.Wrapf(err, "error unmarshaling eakIDs for provisioner %s", provisionerID)
+		}
+	}
+
+	newEAKIDs := removeElement(eakIDs, eakID)
+	var (
+		_old interface{} = eakIDs
+		_new interface{} = newEAKIDs
+	)
+
+	switch {
+	case len(eakIDs) == 0:
+		_old = nil
+	case len(newEAKIDs) == 0:
+		_new = nil
+	}
+
+	if err = db.save(ctx, provisionerID, _new, _old, "externalAccountKeysByProvisionerID", externalAccountKeysByProvisionerIDTable); err != nil {
+		return errors.Wrapf(err, "error saving referenceIDs index for provisioner %s", provisionerID)
+	}
+
+	return nil
+}
+
+// referenceKey returns a unique key for a reference per provisioner
+func referenceKey(provisionerID, reference string) string {
+	return provisionerID + "." + reference
+}
+
+// sliceIndex finds the index of item in slice
+func sliceIndex(slice []string, item string) int {
+	for i := range slice {
+		if slice[i] == item {
+			return i
+		}
+	}
+	return -1
+}
+
+// removeElement deletes the item if it exists in the
+// slice. It returns a new slice, keeping the old one intact.
+func removeElement(slice []string, item string) []string {
+
+	newSlice := make([]string, 0)
+	index := sliceIndex(slice, item)
+	if index < 0 {
+		newSlice = append(newSlice, slice...)
+		return newSlice
+	}
+
+	newSlice = append(newSlice, slice[:index]...)
+
+	return append(newSlice, slice[index+1:]...)
+}

Разлика између датотеке није приказан због своје велике величине
+ 297 - 217
acme/db/nosql/account_test.go


+ 14 - 12
acme/db/nosql/nosql.go

@@ -11,17 +11,18 @@ import (
 )
 
 var (
-	accountTable                        = []byte("acme_accounts")
-	accountByKeyIDTable                 = []byte("acme_keyID_accountID_index")
-	authzTable                          = []byte("acme_authzs")
-	challengeTable                      = []byte("acme_challenges")
-	nonceTable                          = []byte("nonces")
-	orderTable                          = []byte("acme_orders")
-	ordersByAccountIDTable              = []byte("acme_account_orders_index")
-	certTable                           = []byte("acme_certs")
-	certBySerialTable                   = []byte("acme_serial_certs_index")
-	externalAccountKeyTable             = []byte("acme_external_account_keys")
-	externalAccountKeysByReferenceTable = []byte("acme_external_account_key_reference_index")
+	accountTable                            = []byte("acme_accounts")
+	accountByKeyIDTable                     = []byte("acme_keyID_accountID_index")
+	authzTable                              = []byte("acme_authzs")
+	challengeTable                          = []byte("acme_challenges")
+	nonceTable                              = []byte("nonces")
+	orderTable                              = []byte("acme_orders")
+	ordersByAccountIDTable                  = []byte("acme_account_orders_index")
+	certTable                               = []byte("acme_certs")
+	certBySerialTable                       = []byte("acme_serial_certs_index")
+	externalAccountKeyTable                 = []byte("acme_external_account_keys")
+	externalAccountKeysByReferenceTable     = []byte("acme_external_account_key_reference_index")
+	externalAccountKeysByProvisionerIDTable = []byte("acme_external_account_keyID_provisionerID_index")
 )
 
 // DB is a struct that implements the AcmeDB interface.
@@ -33,7 +34,8 @@ type DB struct {
 func New(db nosqlDB.DB) (*DB, error) {
 	tables := [][]byte{accountTable, accountByKeyIDTable, authzTable,
 		challengeTable, nonceTable, orderTable, ordersByAccountIDTable,
-		certTable, certBySerialTable, externalAccountKeyTable, externalAccountKeysByReferenceTable,
+		certTable, certBySerialTable, externalAccountKeyTable,
+		externalAccountKeysByReferenceTable, externalAccountKeysByProvisionerIDTable,
 	}
 	for _, b := range tables {
 		if err := db.CreateTable(b); err != nil {

+ 64 - 24
authority/admin/api/acme.go

@@ -15,6 +15,11 @@ import (
 	"google.golang.org/protobuf/types/known/timestamppb"
 )
 
+const (
+	// provisionerContextKey provisioner key
+	provisionerContextKey = ContextKey("provisioner")
+)
+
 // CreateExternalAccountKeyRequest is the type for POST /admin/acme/eab requests
 type CreateExternalAccountKeyRequest struct {
 	Reference string `json:"reference"`
@@ -37,47 +42,63 @@ type GetExternalAccountKeysResponse struct {
 // before serving requests that act on ACME EAB credentials.
 func (h *Handler) requireEABEnabled(next nextHTTP) nextHTTP {
 	return func(w http.ResponseWriter, r *http.Request) {
-		prov := chi.URLParam(r, "prov")
-		eabEnabled, err := h.provisionerHasEABEnabled(r.Context(), prov)
+		ctx := r.Context()
+		provName := chi.URLParam(r, "prov")
+		eabEnabled, prov, err := h.provisionerHasEABEnabled(ctx, provName)
 		if err != nil {
 			api.WriteError(w, err)
 			return
 		}
 		if !eabEnabled {
-			api.WriteError(w, admin.NewError(admin.ErrorBadRequestType, "ACME EAB not enabled for provisioner %s", prov))
+			api.WriteError(w, admin.NewError(admin.ErrorBadRequestType, "ACME EAB not enabled for provisioner %s", prov.GetName()))
 			return
 		}
-		next(w, r)
+		ctx = context.WithValue(ctx, provisionerContextKey, prov)
+		next(w, r.WithContext(ctx))
 	}
 }
 
 // provisionerHasEABEnabled determines if the "requireEAB" setting for an ACME
 // provisioner is set to true and thus has EAB enabled.
-func (h *Handler) provisionerHasEABEnabled(ctx context.Context, provisionerName string) (bool, error) {
+func (h *Handler) provisionerHasEABEnabled(ctx context.Context, provisionerName string) (bool, *linkedca.Provisioner, error) {
 	var (
 		p   provisioner.Interface
 		err error
 	)
 	if p, err = h.auth.LoadProvisionerByName(provisionerName); err != nil {
-		return false, admin.WrapErrorISE(err, "error loading provisioner %s", provisionerName)
+		return false, nil, admin.WrapErrorISE(err, "error loading provisioner %s", provisionerName)
 	}
 
 	prov, err := h.db.GetProvisioner(ctx, p.GetID())
 	if err != nil {
-		return false, admin.WrapErrorISE(err, "error getting provisioner with ID: %s", p.GetID())
+		return false, nil, admin.WrapErrorISE(err, "error getting provisioner with ID: %s", p.GetID())
 	}
 
 	details := prov.GetDetails()
 	if details == nil {
-		return false, admin.NewErrorISE("error getting details for provisioner with ID: %s", p.GetID())
+		return false, nil, admin.NewErrorISE("error getting details for provisioner with ID: %s", p.GetID())
 	}
 
 	acmeProvisioner := details.GetACME()
 	if acmeProvisioner == nil {
-		return false, admin.NewErrorISE("error getting ACME details for provisioner with ID: %s", p.GetID())
+		return false, nil, admin.NewErrorISE("error getting ACME details for provisioner with ID: %s", p.GetID())
 	}
 
-	return acmeProvisioner.GetRequireEab(), nil
+	return acmeProvisioner.GetRequireEab(), prov, nil
+}
+
+// provisionerFromContext searches the context for a provisioner. Returns the
+// provisioner or an error.
+func provisionerFromContext(ctx context.Context) (*linkedca.Provisioner, error) {
+	val := ctx.Value(provisionerContextKey)
+	if val == nil {
+		return nil, admin.NewErrorISE("provisioner expected in request context")
+	}
+	pval, ok := val.(*linkedca.Provisioner)
+	if !ok || pval == nil {
+		return nil, admin.NewErrorISE("provisioner in context is not a linkedca.Provisioner")
+	}
+	return pval, nil
 }
 
 // CreateExternalAccountKey creates a new External Account Binding key
@@ -93,12 +114,17 @@ func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Reques
 		return
 	}
 
-	prov := chi.URLParam(r, "prov")
-	reference := body.Reference
+	ctx := r.Context()
+	prov, err := provisionerFromContext(ctx)
+	if err != nil {
+		api.WriteError(w, admin.WrapErrorISE(err, "error getting provisioner from context"))
+		return
+	}
 
 	// check if a key with the reference does not exist (only when a reference was in the request)
+	reference := body.Reference
 	if reference != "" {
-		k, err := h.acmeDB.GetExternalAccountKeyByReference(r.Context(), prov, reference)
+		k, err := h.acmeDB.GetExternalAccountKeyByReference(ctx, prov.GetId(), reference)
 		// retrieving an EAB key from DB results in an error if it doesn't exist, which is what we're looking for,
 		// but other errors can also happen. Return early if that happens; continuing if it was acme.ErrNotFound.
 		if shouldWriteError := err != nil && !errors.Is(err, acme.ErrNotFound); shouldWriteError {
@@ -107,7 +133,7 @@ func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Reques
 		}
 		// if a key was found, return HTTP 409 conflict
 		if k != nil {
-			err := admin.NewError(admin.ErrorBadRequestType, "an ACME EAB key for provisioner %s with reference %s already exists", prov, reference)
+			err := admin.NewError(admin.ErrorBadRequestType, "an ACME EAB key for provisioner '%s' with reference '%s' already exists", prov.GetName(), reference)
 			err.Status = 409
 			api.WriteError(w, err)
 			return
@@ -115,9 +141,9 @@ func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Reques
 		// continue execution if no key was found for the reference
 	}
 
-	eak, err := h.acmeDB.CreateExternalAccountKey(r.Context(), prov, reference)
+	eak, err := h.acmeDB.CreateExternalAccountKey(ctx, prov.GetId(), reference)
 	if err != nil {
-		msg := fmt.Sprintf("error creating ACME EAB key for provisioner '%s'", prov)
+		msg := fmt.Sprintf("error creating ACME EAB key for provisioner '%s'", prov.GetName())
 		if reference != "" {
 			msg += fmt.Sprintf(" and reference '%s'", reference)
 		}
@@ -128,7 +154,7 @@ func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Reques
 	response := &linkedca.EABKey{
 		Id:          eak.ID,
 		HmacKey:     eak.KeyBytes,
-		Provisioner: eak.Provisioner,
+		Provisioner: prov.GetName(),
 		Reference:   eak.Reference,
 	}
 
@@ -137,10 +163,17 @@ func (h *Handler) CreateExternalAccountKey(w http.ResponseWriter, r *http.Reques
 
 // DeleteExternalAccountKey deletes an ACME External Account Key.
 func (h *Handler) DeleteExternalAccountKey(w http.ResponseWriter, r *http.Request) {
-	prov := chi.URLParam(r, "prov")
+
 	keyID := chi.URLParam(r, "id")
 
-	if err := h.acmeDB.DeleteExternalAccountKey(r.Context(), prov, keyID); err != nil {
+	ctx := r.Context()
+	prov, err := provisionerFromContext(ctx)
+	if err != nil {
+		api.WriteError(w, admin.WrapErrorISE(err, "error getting provisioner from context"))
+		return
+	}
+
+	if err := h.acmeDB.DeleteExternalAccountKey(ctx, prov.GetId(), keyID); err != nil {
 		api.WriteError(w, admin.WrapErrorISE(err, "error deleting ACME EAB Key '%s'", keyID))
 		return
 	}
@@ -152,8 +185,6 @@ func (h *Handler) DeleteExternalAccountKey(w http.ResponseWriter, r *http.Reques
 // only the ExternalAccountKey with that reference is returned. Otherwise all
 // ExternalAccountKeys in the system for a specific provisioner are returned.
 func (h *Handler) GetExternalAccountKeys(w http.ResponseWriter, r *http.Request) {
-	prov := chi.URLParam(r, "prov")
-	reference := chi.URLParam(r, "ref")
 
 	var (
 		key  *acme.ExternalAccountKey
@@ -161,8 +192,16 @@ func (h *Handler) GetExternalAccountKeys(w http.ResponseWriter, r *http.Request)
 		err  error
 	)
 
+	ctx := r.Context()
+	prov, err := provisionerFromContext(ctx)
+	if err != nil {
+		api.WriteError(w, admin.WrapErrorISE(err, "error getting provisioner from context"))
+		return
+	}
+
+	reference := chi.URLParam(r, "ref")
 	if reference != "" {
-		if key, err = h.acmeDB.GetExternalAccountKeyByReference(r.Context(), prov, reference); err != nil {
+		if key, err = h.acmeDB.GetExternalAccountKeyByReference(ctx, prov.GetId(), reference); err != nil {
 			api.WriteError(w, admin.WrapErrorISE(err, "error retrieving external account key with reference '%s'", reference))
 			return
 		}
@@ -170,18 +209,19 @@ func (h *Handler) GetExternalAccountKeys(w http.ResponseWriter, r *http.Request)
 			keys = []*acme.ExternalAccountKey{key}
 		}
 	} else {
-		if keys, err = h.acmeDB.GetExternalAccountKeys(r.Context(), prov); err != nil {
+		if keys, err = h.acmeDB.GetExternalAccountKeys(ctx, prov.GetId()); err != nil {
 			api.WriteError(w, admin.WrapErrorISE(err, "error retrieving external account keys"))
 			return
 		}
 	}
 
+	provisionerName := prov.GetName()
 	eaks := make([]*linkedca.EABKey, len(keys))
 	for i, k := range keys {
 		eaks[i] = &linkedca.EABKey{
 			Id:          k.ID,
 			HmacKey:     []byte{},
-			Provisioner: k.Provisioner,
+			Provisioner: provisionerName,
 			Reference:   k.Reference,
 			Account:     k.AccountID,
 			CreatedAt:   timestamppb.New(k.CreatedAt),

+ 94 - 140
authority/admin/api/acme_test.go

@@ -368,12 +368,13 @@ func TestHandler_provisionerHasEABEnabled(t *testing.T) {
 				auth:   tc.auth,
 				acmeDB: nil,
 			}
-			got, err := h.provisionerHasEABEnabled(context.TODO(), tc.provisionerName)
+			got, prov, err := h.provisionerHasEABEnabled(context.TODO(), tc.provisionerName)
 			if (err != nil) != (tc.err != nil) {
 				t.Errorf("Handler.provisionerHasEABEnabled() error = %v, want err %v", err, tc.err)
 				return
 			}
 			if tc.err != nil {
+				assert.Type(t, &linkedca.Provisioner{}, prov)
 				assert.Type(t, &admin.Error{}, err)
 				adminError, _ := err.(*admin.Error)
 				assert.Equals(t, tc.err.Type, adminError.Type)
@@ -434,6 +435,10 @@ func TestCreateExternalAccountKeyRequest_Validate(t *testing.T) {
 }
 
 func TestHandler_CreateExternalAccountKey(t *testing.T) {
+	prov := &linkedca.Provisioner{
+		Id:   "provID",
+		Name: "provName",
+	}
 	type test struct {
 		ctx        context.Context
 		db         acme.DB
@@ -487,14 +492,15 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 			chiCtx := chi.NewRouteContext()
 			chiCtx.URLParams.Add("prov", "provName")
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			req := CreateExternalAccountKeyRequest{
 				Reference: "an-external-key-reference",
 			}
 			body, err := json.Marshal(req)
 			assert.FatalError(t, err)
 			db := &acme.MockDB{
-				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "an-external-key-reference", reference)
 					return nil, errors.New("force")
 				},
@@ -517,22 +523,23 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 			chiCtx := chi.NewRouteContext()
 			chiCtx.URLParams.Add("prov", "provName")
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			req := CreateExternalAccountKeyRequest{
 				Reference: "an-external-key-reference",
 			}
 			body, err := json.Marshal(req)
 			assert.FatalError(t, err)
 			db := &acme.MockDB{
-				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "an-external-key-reference", reference)
 					past := time.Now().Add(-24 * time.Hour)
 					return &acme.ExternalAccountKey{
-						ID:          "eakID",
-						Provisioner: "provName",
-						Reference:   "an-external-key-reference",
-						KeyBytes:    []byte{1, 3, 3, 7},
-						CreatedAt:   past,
+						ID:            "eakID",
+						ProvisionerID: "provID",
+						Reference:     "an-external-key-reference",
+						KeyBytes:      []byte{1, 3, 3, 7},
+						CreatedAt:     past,
 					}, nil
 				},
 			}
@@ -546,7 +553,7 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 					Type:    admin.ErrorBadRequestType.String(),
 					Status:  409,
 					Detail:  "bad request",
-					Message: "an ACME EAB key for provisioner provName with reference an-external-key-reference already exists",
+					Message: "an ACME EAB key for provisioner 'provName' with reference 'an-external-key-reference' already exists",
 				},
 			}
 		},
@@ -554,14 +561,15 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 			chiCtx := chi.NewRouteContext()
 			chiCtx.URLParams.Add("prov", "provName")
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			req := CreateExternalAccountKeyRequest{
 				Reference: "",
 			}
 			body, err := json.Marshal(req)
 			assert.FatalError(t, err)
 			db := &acme.MockDB{
-				MockCreateExternalAccountKey: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockCreateExternalAccountKey: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "", reference)
 					return nil, errors.New("force")
 				},
@@ -583,19 +591,20 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 			chiCtx := chi.NewRouteContext()
 			chiCtx.URLParams.Add("prov", "provName")
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			req := CreateExternalAccountKeyRequest{
 				Reference: "an-external-key-reference",
 			}
 			body, err := json.Marshal(req)
 			assert.FatalError(t, err)
 			db := &acme.MockDB{
-				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "an-external-key-reference", reference)
 					return nil, acme.ErrNotFound // simulating not found; skipping 409 conflict
 				},
-				MockCreateExternalAccountKey: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockCreateExternalAccountKey: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "an-external-key-reference", reference)
 					return nil, errors.New("force")
 				},
@@ -617,6 +626,7 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 			chiCtx := chi.NewRouteContext()
 			chiCtx.URLParams.Add("prov", "provName")
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			req := CreateExternalAccountKeyRequest{
 				Reference: "",
 			}
@@ -624,15 +634,15 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 			assert.FatalError(t, err)
 			now := time.Now()
 			db := &acme.MockDB{
-				MockCreateExternalAccountKey: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockCreateExternalAccountKey: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "", reference)
 					return &acme.ExternalAccountKey{
-						ID:          "eakID",
-						Provisioner: "provName",
-						Reference:   "",
-						KeyBytes:    []byte{1, 3, 3, 7},
-						CreatedAt:   now,
+						ID:            "eakID",
+						ProvisionerID: "provID",
+						Reference:     "",
+						KeyBytes:      []byte{1, 3, 3, 7},
+						CreatedAt:     now,
 					}, nil
 				},
 			}
@@ -653,6 +663,7 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 			chiCtx := chi.NewRouteContext()
 			chiCtx.URLParams.Add("prov", "provName")
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			req := CreateExternalAccountKeyRequest{
 				Reference: "an-external-key-reference",
 			}
@@ -660,20 +671,20 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 			assert.FatalError(t, err)
 			now := time.Now()
 			db := &acme.MockDB{
-				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "an-external-key-reference", reference)
 					return nil, acme.ErrNotFound // simulating not found; skipping 409 conflict
 				},
-				MockCreateExternalAccountKey: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockCreateExternalAccountKey: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "an-external-key-reference", reference)
 					return &acme.ExternalAccountKey{
-						ID:          "eakID",
-						Provisioner: "provName",
-						Reference:   "an-external-key-reference",
-						KeyBytes:    []byte{1, 3, 3, 7},
-						CreatedAt:   now,
+						ID:            "eakID",
+						ProvisionerID: "provID",
+						Reference:     "an-external-key-reference",
+						KeyBytes:      []byte{1, 3, 3, 7},
+						CreatedAt:     now,
 					}, nil
 				},
 			}
@@ -737,6 +748,10 @@ func TestHandler_CreateExternalAccountKey(t *testing.T) {
 }
 
 func TestHandler_DeleteExternalAccountKey(t *testing.T) {
+	prov := &linkedca.Provisioner{
+		Id:   "provID",
+		Name: "provName",
+	}
 	type test struct {
 		ctx        context.Context
 		db         acme.DB
@@ -749,9 +764,10 @@ func TestHandler_DeleteExternalAccountKey(t *testing.T) {
 			chiCtx.URLParams.Add("prov", "provName")
 			chiCtx.URLParams.Add("id", "keyID")
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			db := &acme.MockDB{
-				MockDeleteExternalAccountKey: func(ctx context.Context, provisionerName, keyID string) error {
-					assert.Equals(t, "provName", provisionerName)
+				MockDeleteExternalAccountKey: func(ctx context.Context, provisionerID, keyID string) error {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "keyID", keyID)
 					return errors.New("force")
 				},
@@ -773,9 +789,10 @@ func TestHandler_DeleteExternalAccountKey(t *testing.T) {
 			chiCtx.URLParams.Add("prov", "provName")
 			chiCtx.URLParams.Add("id", "keyID")
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			db := &acme.MockDB{
-				MockDeleteExternalAccountKey: func(ctx context.Context, provisionerName, keyID string) error {
-					assert.Equals(t, "provName", provisionerName)
+				MockDeleteExternalAccountKey: func(ctx context.Context, provisionerID, keyID string) error {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "keyID", keyID)
 					return nil
 				},
@@ -831,6 +848,10 @@ func TestHandler_DeleteExternalAccountKey(t *testing.T) {
 }
 
 func TestHandler_GetExternalAccountKeys(t *testing.T) {
+	prov := &linkedca.Provisioner{
+		Id:   "provID",
+		Name: "provName",
+	}
 	type test struct {
 		ctx        context.Context
 		db         acme.DB
@@ -846,9 +867,10 @@ func TestHandler_GetExternalAccountKeys(t *testing.T) {
 			chiCtx.URLParams.Add("ref", "an-external-key-reference")
 			req := httptest.NewRequest("GET", "/foo", nil)
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			db := &acme.MockDB{
-				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "an-external-key-reference", reference)
 					return nil, errors.New("force")
 				},
@@ -871,9 +893,10 @@ func TestHandler_GetExternalAccountKeys(t *testing.T) {
 			chiCtx.URLParams.Add("prov", "provName")
 			req := httptest.NewRequest("GET", "/foo", nil)
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			db := &acme.MockDB{
-				MockGetExternalAccountKeys: func(ctx context.Context, provisionerName string) ([]*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockGetExternalAccountKeys: func(ctx context.Context, provisionerID string) ([]*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					return nil, errors.New("force")
 				},
 			}
@@ -896,9 +919,10 @@ func TestHandler_GetExternalAccountKeys(t *testing.T) {
 			chiCtx.URLParams.Add("ref", "an-external-key-reference")
 			req := httptest.NewRequest("GET", "/foo", nil)
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			db := &acme.MockDB{
-				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "an-external-key-reference", reference)
 					return nil, nil // returning nil; no key found
 				},
@@ -920,17 +944,18 @@ func TestHandler_GetExternalAccountKeys(t *testing.T) {
 			chiCtx.URLParams.Add("ref", "an-external-key-reference")
 			req := httptest.NewRequest("GET", "/foo", nil)
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			createdAt := time.Now().Add(-24 * time.Hour)
 			var boundAt time.Time
 			db := &acme.MockDB{
-				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerName, reference string) (*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					assert.Equals(t, "an-external-key-reference", reference)
 					return &acme.ExternalAccountKey{
-						ID:          "eakID",
-						Provisioner: "provName",
-						Reference:   "an-external-key-reference",
-						CreatedAt:   createdAt,
+						ID:            "eakID",
+						ProvisionerID: "provID",
+						Reference:     "an-external-key-reference",
+						CreatedAt:     createdAt,
 					}, nil
 				},
 			}
@@ -958,107 +983,36 @@ func TestHandler_GetExternalAccountKeys(t *testing.T) {
 			chiCtx.URLParams.Add("prov", "provName")
 			req := httptest.NewRequest("GET", "/foo", nil)
 			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
+			ctx = context.WithValue(ctx, provisionerContextKey, prov)
 			createdAt := time.Now().Add(-24 * time.Hour)
 			var boundAt time.Time
 			boundAtSet := time.Now().Add(-12 * time.Hour)
 			db := &acme.MockDB{
-				MockGetExternalAccountKeys: func(ctx context.Context, provisionerName string) ([]*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
-					return []*acme.ExternalAccountKey{
-						{
-							ID:          "eakID1",
-							Provisioner: "provName",
-							Reference:   "some-external-key-reference",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   createdAt,
-						},
-						{
-							ID:          "eakID2",
-							Provisioner: "provName",
-							Reference:   "some-other-external-key-reference",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   createdAt.Add(1 * time.Hour),
-						},
-						{
-							ID:          "eakID3",
-							Provisioner: "provName",
-							Reference:   "another-external-key-reference",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   createdAt,
-							BoundAt:     boundAtSet,
-							AccountID:   "accountID",
-						},
-					}, nil
-				},
-			}
-			return test{
-				ctx:        ctx,
-				statusCode: 200,
-				req:        req,
-				resp: GetExternalAccountKeysResponse{
-					EAKs: []*linkedca.EABKey{
-						{
-							Id:          "eakID1",
-							Provisioner: "provName",
-							Reference:   "some-external-key-reference",
-							CreatedAt:   timestamppb.New(createdAt),
-							BoundAt:     timestamppb.New(boundAt),
-						},
-						{
-							Id:          "eakID2",
-							Provisioner: "provName",
-							Reference:   "some-other-external-key-reference",
-							CreatedAt:   timestamppb.New(createdAt.Add(1 * time.Hour)),
-							BoundAt:     timestamppb.New(boundAt),
-						},
-						{
-							Id:          "eakID3",
-							Provisioner: "provName",
-							Reference:   "another-external-key-reference",
-							CreatedAt:   timestamppb.New(createdAt),
-							BoundAt:     timestamppb.New(boundAtSet),
-							Account:     "accountID",
-						},
-					},
-				},
-				db:  db,
-				err: nil,
-			}
-		},
-		"ok/multiple-keys-with-cursor-and-limit": func(t *testing.T) test {
-			chiCtx := chi.NewRouteContext()
-			chiCtx.URLParams.Add("prov", "provName")
-			req := httptest.NewRequest("GET", "/foo?cursor=eakID1&limit=10", nil)
-			ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
-			createdAt := time.Now().Add(-24 * time.Hour)
-			var boundAt time.Time
-			boundAtSet := time.Now().Add(-12 * time.Hour)
-			db := &acme.MockDB{
-				MockGetExternalAccountKeys: func(ctx context.Context, provisionerName string) ([]*acme.ExternalAccountKey, error) {
-					assert.Equals(t, "provName", provisionerName)
+				MockGetExternalAccountKeys: func(ctx context.Context, provisionerID string) ([]*acme.ExternalAccountKey, error) {
+					assert.Equals(t, "provID", provisionerID)
 					return []*acme.ExternalAccountKey{
 						{
-							ID:          "eakID1",
-							Provisioner: "provName",
-							Reference:   "some-external-key-reference",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   createdAt,
+							ID:            "eakID1",
+							ProvisionerID: "provID",
+							Reference:     "some-external-key-reference",
+							KeyBytes:      []byte{1, 3, 3, 7},
+							CreatedAt:     createdAt,
 						},
 						{
-							ID:          "eakID2",
-							Provisioner: "provName",
-							Reference:   "some-other-external-key-reference",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   createdAt.Add(1 * time.Hour),
+							ID:            "eakID2",
+							ProvisionerID: "provID",
+							Reference:     "some-other-external-key-reference",
+							KeyBytes:      []byte{1, 3, 3, 7},
+							CreatedAt:     createdAt.Add(1 * time.Hour),
 						},
 						{
-							ID:          "eakID3",
-							Provisioner: "provName",
-							Reference:   "another-external-key-reference",
-							KeyBytes:    []byte{1, 3, 3, 7},
-							CreatedAt:   createdAt,
-							BoundAt:     boundAtSet,
-							AccountID:   "accountID",
+							ID:            "eakID3",
+							ProvisionerID: "provID",
+							Reference:     "another-external-key-reference",
+							KeyBytes:      []byte{1, 3, 3, 7},
+							CreatedAt:     createdAt,
+							BoundAt:       boundAtSet,
+							AccountID:     "accountID",
 						},
 					}, nil
 				},

Неке датотеке нису приказане због велике количине промена