From c7e421a501ee4d5d74b7e45c0d533077b95423e3 Mon Sep 17 00:00:00 2001 From: Diogo Monica Date: Fri, 24 Jul 2015 12:36:17 -0700 Subject: [PATCH] Fixing unique key_id entry enforcement Signed-off-by: Diogo Monica --- trustmanager/keydbstore.go | 10 +++++----- trustmanager/keydbstore_test.go | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/trustmanager/keydbstore.go b/trustmanager/keydbstore.go index e53250a9d4..6a4b97a44d 100644 --- a/trustmanager/keydbstore.go +++ b/trustmanager/keydbstore.go @@ -23,11 +23,11 @@ type KeyDBStore struct { // GormPrivateKey represents a PrivateKey in the database type GormPrivateKey struct { gorm.Model - KeyID string `gorm:"not null;unique_index"` - Encryption string `gorm:"type:varchar(50);not null"` - Algorithm string `gorm:"not null"` - Public []byte `gorm:"not null"` - Private string `gorm:"not null"` + KeyID string `sql:"not null;unique"` + Encryption string `sql:"not null"` + Algorithm string `sql:"not null"` + Public []byte `sql:"not null"` + Private string `sql:"not null"` } // TableName sets a specific table name for our GormPrivateKey diff --git a/trustmanager/keydbstore_test.go b/trustmanager/keydbstore_test.go index 39a8745b3b..796ecc8d6b 100644 --- a/trustmanager/keydbstore_test.go +++ b/trustmanager/keydbstore_test.go @@ -79,6 +79,10 @@ func TestDoubleCreate(t *testing.T) { err = dbStore.AddKey("", "", testKey) assert.NoError(t, err) + // Test writing the same key in the database. Should fail. + err = dbStore.AddKey("", "", testKey) + assert.Error(t, err, "failed to add private key to database:") + // Test writing new key succeeds err = dbStore.AddKey("", "", anotherTestKey) assert.NoError(t, err)