Skip to content

Commit bf21319

Browse files
committed
Fix PR comments and issue with empty string slices
1 parent fd9845e commit bf21319

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

Diff for: acme/db/nosql/eab.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (db *DB) CreateExternalAccountKey(ctx context.Context, provisionerID, refer
8989
Reference: dbeak.Reference,
9090
ExternalAccountKeyID: dbeak.ID,
9191
}
92-
if err := db.save(ctx, referenceKey(provisionerID, dbeak.Reference), dbExternalAccountKeyReference, nil, "external_account_key_reference", externalAccountKeysByReferenceTable); err != nil {
92+
if err := db.save(ctx, referenceKey(provisionerID, dbeak.Reference), dbExternalAccountKeyReference, nil, "external_account_key_reference", externalAccountKeyIDsByReferenceTable); err != nil {
9393
return nil, err
9494
}
9595
}
@@ -144,7 +144,7 @@ func (db *DB) DeleteExternalAccountKey(ctx context.Context, provisionerID, keyID
144144
}
145145

146146
if dbeak.Reference != "" {
147-
if err := db.db.Del(externalAccountKeysByReferenceTable, []byte(referenceKey(provisionerID, dbeak.Reference))); err != nil {
147+
if err := db.db.Del(externalAccountKeyIDsByReferenceTable, []byte(referenceKey(provisionerID, dbeak.Reference))); err != nil {
148148
return errors.Wrapf(err, "error deleting ACME EAB Key reference with Key ID %s and reference %s", keyID, dbeak.Reference)
149149
}
150150
}
@@ -212,7 +212,7 @@ func (db *DB) GetExternalAccountKeyByReference(ctx context.Context, provisionerI
212212
return nil, nil
213213
}
214214

215-
k, err := db.db.Get(externalAccountKeysByReferenceTable, []byte(referenceKey(provisionerID, reference)))
215+
k, err := db.db.Get(externalAccountKeyIDsByReferenceTable, []byte(referenceKey(provisionerID, reference)))
216216
if nosqlDB.IsErrNotFound(err) {
217217
return nil, acme.ErrNotFound
218218
} else if err != nil {
@@ -291,11 +291,19 @@ func (db *DB) addEAKID(ctx context.Context, provisionerID, eakID string) error {
291291
var newEAKIDs []string
292292
newEAKIDs = append(newEAKIDs, eakIDs...)
293293
newEAKIDs = append(newEAKIDs, eakID)
294+
294295
var (
295296
_old interface{} = eakIDs
296297
_new interface{} = newEAKIDs
297298
)
298299

300+
// ensure that the DB gets the expected value when the slice is empty; otherwise
301+
// it'll return with an error that indicates that the DBs view of the data is
302+
// different from the last read (i.e. _old is different from what the DB has).
303+
if len(eakIDs) == 0 {
304+
_old = nil
305+
}
306+
299307
if err = db.save(ctx, provisionerID, _new, _old, "externalAccountKeyIDsByProvisionerID", externalAccountKeyIDsByProvisionerIDTable); err != nil {
300308
return errors.Wrapf(err, "error saving eakIDs index for provisioner %s", provisionerID)
301309
}
@@ -326,6 +334,13 @@ func (db *DB) deleteEAKID(ctx context.Context, provisionerID, eakID string) erro
326334
_new interface{} = newEAKIDs
327335
)
328336

337+
// ensure that the DB gets the expected value when the slice is empty; otherwise
338+
// it'll return with an error that indicates that the DBs view of the data is
339+
// different from the last read (i.e. _old is different from what the DB has).
340+
if len(eakIDs) == 0 {
341+
_old = nil
342+
}
343+
329344
if err = db.save(ctx, provisionerID, _new, _old, "externalAccountKeyIDsByProvisionerID", externalAccountKeyIDsByProvisionerIDTable); err != nil {
330345
return errors.Wrapf(err, "error saving eakIDs index for provisioner %s", provisionerID)
331346
}

Diff for: acme/db/nosql/eab_test.go

+22-23
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func TestDB_GetExternalAccountKeyByReference(t *testing.T) {
271271
db: &certdb.MockNoSQLDB{
272272
MGet: func(bucket, key []byte) ([]byte, error) {
273273
switch string(bucket) {
274-
case string(externalAccountKeysByReferenceTable):
274+
case string(externalAccountKeyIDsByReferenceTable):
275275
assert.Equals(t, string(key), provID+"."+ref)
276276
return dbrefBytes, nil
277277
case string(externalAccountKeyTable):
@@ -306,7 +306,7 @@ func TestDB_GetExternalAccountKeyByReference(t *testing.T) {
306306
ref: ref,
307307
db: &certdb.MockNoSQLDB{
308308
MGet: func(bucket, key []byte) ([]byte, error) {
309-
assert.Equals(t, string(bucket), string(externalAccountKeysByReferenceTable))
309+
assert.Equals(t, string(bucket), string(externalAccountKeyIDsByReferenceTable))
310310
assert.Equals(t, string(key), provID+"."+ref)
311311
return nil, nosqldb.ErrNotFound
312312
},
@@ -319,7 +319,7 @@ func TestDB_GetExternalAccountKeyByReference(t *testing.T) {
319319
ref: ref,
320320
db: &certdb.MockNoSQLDB{
321321
MGet: func(bucket, key []byte) ([]byte, error) {
322-
assert.Equals(t, string(bucket), string(externalAccountKeysByReferenceTable))
322+
assert.Equals(t, string(bucket), string(externalAccountKeyIDsByReferenceTable))
323323
assert.Equals(t, string(key), provID+"."+ref)
324324
return nil, errors.New("force")
325325
},
@@ -332,7 +332,7 @@ func TestDB_GetExternalAccountKeyByReference(t *testing.T) {
332332
ref: ref,
333333
db: &certdb.MockNoSQLDB{
334334
MGet: func(bucket, key []byte) ([]byte, error) {
335-
assert.Equals(t, string(bucket), string(externalAccountKeysByReferenceTable))
335+
assert.Equals(t, string(bucket), string(externalAccountKeyIDsByReferenceTable))
336336
assert.Equals(t, string(key), provID+"."+ref)
337337
return []byte{0}, nil
338338
},
@@ -352,7 +352,7 @@ func TestDB_GetExternalAccountKeyByReference(t *testing.T) {
352352
db: &certdb.MockNoSQLDB{
353353
MGet: func(bucket, key []byte) ([]byte, error) {
354354
switch string(bucket) {
355-
case string(externalAccountKeysByReferenceTable):
355+
case string(externalAccountKeyIDsByReferenceTable):
356356
assert.Equals(t, string(key), provID+"."+ref)
357357
return dbrefBytes, nil
358358
case string(externalAccountKeyTable):
@@ -642,7 +642,7 @@ func TestDB_DeleteExternalAccountKey(t *testing.T) {
642642
db: &certdb.MockNoSQLDB{
643643
MGet: func(bucket, key []byte) ([]byte, error) {
644644
switch string(bucket) {
645-
case string(externalAccountKeysByReferenceTable):
645+
case string(externalAccountKeyIDsByReferenceTable):
646646
assert.Equals(t, string(key), provID+"."+ref)
647647
return dbrefBytes, nil
648648
case string(externalAccountKeyTable):
@@ -660,7 +660,7 @@ func TestDB_DeleteExternalAccountKey(t *testing.T) {
660660
},
661661
MDel: func(bucket, key []byte) error {
662662
switch string(bucket) {
663-
case string(externalAccountKeysByReferenceTable):
663+
case string(externalAccountKeyIDsByReferenceTable):
664664
assert.Equals(t, string(key), provID+"."+ref)
665665
return nil
666666
case string(externalAccountKeyTable):
@@ -674,7 +674,7 @@ func TestDB_DeleteExternalAccountKey(t *testing.T) {
674674
MCmpAndSwap: func(bucket, key, old, new []byte) ([]byte, bool, error) {
675675
fmt.Println(string(bucket))
676676
switch string(bucket) {
677-
case string(externalAccountKeysByReferenceTable):
677+
case string(externalAccountKeyIDsByReferenceTable):
678678
assert.Equals(t, provID+"."+ref, string(key))
679679
return nil, true, nil
680680
case string(externalAccountKeyIDsByProvisionerIDTable):
@@ -745,7 +745,7 @@ func TestDB_DeleteExternalAccountKey(t *testing.T) {
745745
db: &certdb.MockNoSQLDB{
746746
MGet: func(bucket, key []byte) ([]byte, error) {
747747
switch string(bucket) {
748-
case string(externalAccountKeysByReferenceTable):
748+
case string(externalAccountKeyIDsByReferenceTable):
749749
assert.Equals(t, string(key), ref)
750750
return dbrefBytes, nil
751751
case string(externalAccountKeyTable):
@@ -758,7 +758,7 @@ func TestDB_DeleteExternalAccountKey(t *testing.T) {
758758
},
759759
MDel: func(bucket, key []byte) error {
760760
switch string(bucket) {
761-
case string(externalAccountKeysByReferenceTable):
761+
case string(externalAccountKeyIDsByReferenceTable):
762762
assert.Equals(t, string(key), provID+"."+ref)
763763
return errors.New("force")
764764
case string(externalAccountKeyTable):
@@ -795,7 +795,7 @@ func TestDB_DeleteExternalAccountKey(t *testing.T) {
795795
db: &certdb.MockNoSQLDB{
796796
MGet: func(bucket, key []byte) ([]byte, error) {
797797
switch string(bucket) {
798-
case string(externalAccountKeysByReferenceTable):
798+
case string(externalAccountKeyIDsByReferenceTable):
799799
assert.Equals(t, string(key), ref)
800800
return dbrefBytes, nil
801801
case string(externalAccountKeyTable):
@@ -808,7 +808,7 @@ func TestDB_DeleteExternalAccountKey(t *testing.T) {
808808
},
809809
MDel: func(bucket, key []byte) error {
810810
switch string(bucket) {
811-
case string(externalAccountKeysByReferenceTable):
811+
case string(externalAccountKeyIDsByReferenceTable):
812812
assert.Equals(t, string(key), provID+"."+ref)
813813
return nil
814814
case string(externalAccountKeyTable):
@@ -845,7 +845,7 @@ func TestDB_DeleteExternalAccountKey(t *testing.T) {
845845
db: &certdb.MockNoSQLDB{
846846
MGet: func(bucket, key []byte) ([]byte, error) {
847847
switch string(bucket) {
848-
case string(externalAccountKeysByReferenceTable):
848+
case string(externalAccountKeyIDsByReferenceTable):
849849
assert.Equals(t, string(key), ref)
850850
return dbrefBytes, nil
851851
case string(externalAccountKeyTable):
@@ -860,7 +860,7 @@ func TestDB_DeleteExternalAccountKey(t *testing.T) {
860860
},
861861
MDel: func(bucket, key []byte) error {
862862
switch string(bucket) {
863-
case string(externalAccountKeysByReferenceTable):
863+
case string(externalAccountKeyIDsByReferenceTable):
864864
assert.Equals(t, string(key), provID+"."+ref)
865865
return nil
866866
case string(externalAccountKeyTable):
@@ -939,7 +939,7 @@ func TestDB_CreateExternalAccountKey(t *testing.T) {
939939
case string(externalAccountKeyIDsByProvisionerIDTable):
940940
assert.Equals(t, provID, string(key))
941941
return nu, true, nil
942-
case string(externalAccountKeysByReferenceTable):
942+
case string(externalAccountKeyIDsByReferenceTable):
943943
assert.Equals(t, provID+"."+ref, string(key))
944944
assert.Equals(t, nil, old)
945945
return nu, true, nil
@@ -973,7 +973,7 @@ func TestDB_CreateExternalAccountKey(t *testing.T) {
973973
db: &certdb.MockNoSQLDB{
974974
MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
975975
switch string(bucket) {
976-
case string(externalAccountKeysByReferenceTable):
976+
case string(externalAccountKeyIDsByReferenceTable):
977977
assert.Equals(t, string(key), ref)
978978
assert.Equals(t, old, nil)
979979
return nu, true, nil
@@ -999,7 +999,7 @@ func TestDB_CreateExternalAccountKey(t *testing.T) {
999999
},
10001000
MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
10011001
switch string(bucket) {
1002-
case string(externalAccountKeysByReferenceTable):
1002+
case string(externalAccountKeyIDsByReferenceTable):
10031003
assert.Equals(t, string(key), ref)
10041004
assert.Equals(t, old, nil)
10051005
return nu, true, nil
@@ -1029,7 +1029,7 @@ func TestDB_CreateExternalAccountKey(t *testing.T) {
10291029
case string(externalAccountKeyIDsByProvisionerIDTable):
10301030
assert.Equals(t, provID, string(key))
10311031
return nu, true, nil
1032-
case string(externalAccountKeysByReferenceTable):
1032+
case string(externalAccountKeyIDsByReferenceTable):
10331033
assert.Equals(t, provID+"."+ref, string(key))
10341034
assert.Equals(t, old, nil)
10351035
return nu, true, errors.New("force")
@@ -1348,7 +1348,7 @@ func TestDB_addEAKID(t *testing.T) {
13481348
MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
13491349
assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
13501350
assert.Equals(t, string(key), provID)
1351-
assert.Equals(t, old, []byte{110, 117, 108, 108})
1351+
assert.Equals(t, old, nil)
13521352
b, _ := json.Marshal([]string{eakID})
13531353
assert.Equals(t, nu, b)
13541354
return b, true, nil
@@ -1482,7 +1482,7 @@ func TestDB_deleteEAKID(t *testing.T) {
14821482
MCmpAndSwap: func(bucket, key, old, nu []byte) ([]byte, bool, error) {
14831483
assert.Equals(t, bucket, externalAccountKeyIDsByProvisionerIDTable)
14841484
assert.Equals(t, string(key), provID)
1485-
assert.Equals(t, old, []byte{110, 117, 108, 108})
1485+
assert.Equals(t, old, nil)
14861486
b, _ := json.Marshal([]string{})
14871487
assert.Equals(t, nu, b)
14881488
return b, true, nil
@@ -1579,7 +1579,7 @@ func TestDB_addAndDeleteEAKID(t *testing.T) {
15791579
assert.Equals(t, string(key), provID)
15801580
switch callCounter {
15811581
case 0:
1582-
assert.Equals(t, old, []byte{110, 117, 108, 108})
1582+
assert.Equals(t, old, nil)
15831583
newB, _ := json.Marshal([]string{"eakID"})
15841584
assert.Equals(t, nu, newB)
15851585
return newB, true, nil
@@ -1589,8 +1589,7 @@ func TestDB_addAndDeleteEAKID(t *testing.T) {
15891589
newB, _ := json.Marshal([]string{})
15901590
return newB, true, nil
15911591
case 2:
1592-
oldB, _ := json.Marshal([]string{})
1593-
assert.Equals(t, old, oldB)
1592+
assert.Equals(t, old, nil)
15941593
newB, _ := json.Marshal([]string{"eakID1"})
15951594
assert.Equals(t, nu, newB)
15961595
return newB, true, nil

Diff for: acme/db/nosql/nosql.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121
certTable = []byte("acme_certs")
2222
certBySerialTable = []byte("acme_serial_certs_index")
2323
externalAccountKeyTable = []byte("acme_external_account_keys")
24-
externalAccountKeysByReferenceTable = []byte("acme_external_account_key_reference_index")
24+
externalAccountKeyIDsByReferenceTable = []byte("acme_external_account_keyID_reference_index")
2525
externalAccountKeyIDsByProvisionerIDTable = []byte("acme_external_account_keyID_provisionerID_index")
2626
)
2727

@@ -35,7 +35,7 @@ func New(db nosqlDB.DB) (*DB, error) {
3535
tables := [][]byte{accountTable, accountByKeyIDTable, authzTable,
3636
challengeTable, nonceTable, orderTable, ordersByAccountIDTable,
3737
certTable, certBySerialTable, externalAccountKeyTable,
38-
externalAccountKeysByReferenceTable, externalAccountKeyIDsByProvisionerIDTable,
38+
externalAccountKeyIDsByReferenceTable, externalAccountKeyIDsByProvisionerIDTable,
3939
}
4040
for _, b := range tables {
4141
if err := db.CreateTable(b); err != nil {

0 commit comments

Comments
 (0)