Skip to content

Commit

Permalink
fix invalid keyReference migration objects (#3504)
Browse files Browse the repository at this point in the history
  • Loading branch information
woutslakhorst authored Oct 22, 2024
1 parent e5cfc99 commit 0695b67
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (client *Crypto) Migrate() error {
for _, keyNameVersion := range keys {
var keyRef orm.KeyReference
// find existing record, if it exists do nothing
err := tx.WithContext(ctx).Model(&orm.KeyReference{}).Where("key_name = ? and version = ?", keyNameVersion.KeyName, keyNameVersion.KeyName).First(&keyRef).Error
err := tx.WithContext(ctx).Model(&orm.KeyReference{}).Where("key_name = ? and version = ?", keyNameVersion.KeyName, keyNameVersion.Version).First(&keyRef).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
// create a new key reference
Expand Down
26 changes: 21 additions & 5 deletions crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ func TestCrypto_Exists(t *testing.T) {
}

func TestCrypto_Migrate(t *testing.T) {
backend := NewMemoryStorage()
db := orm.NewTestDatabase(t)
client := &Crypto{backend: backend, db: db}

keypair, _ := spi.GenerateKeyPair()
t.Run("ok - 1 key migrated", func(t *testing.T) {
keypair, _ := spi.GenerateKeyPair()
backend := NewMemoryStorage()
db := orm.NewTestDatabase(t)
client := &Crypto{backend: backend, db: db}
err := backend.SavePrivateKey(context.Background(), "test", keypair)
require.NoError(t, err)

Expand All @@ -80,9 +79,26 @@ func TestCrypto_Migrate(t *testing.T) {

t.Run("ok - already exists", func(t *testing.T) {
err = client.Migrate()

assert.NoError(t, err)
})
})
t.Run("don't migrate new keys", func(t *testing.T) {
backend := NewMemoryStorage()
db := orm.NewTestDatabase(t)
client := &Crypto{backend: backend, db: db}
err := backend.SavePrivateKey(context.Background(), "some-uuid", keypair)
require.NoError(t, err)

err = db.Save(&orm.KeyReference{KID: "vm-id", KeyName: "some-uuid", Version: "1"}).Error
require.NoError(t, err)

err = client.Migrate()
require.NoError(t, err)

keys := client.List(context.Background())
require.Len(t, keys, 1)
})
}

func TestCrypto_New(t *testing.T) {
Expand Down

0 comments on commit 0695b67

Please sign in to comment.