renaming TimestampKey and ErrTimestampKeyExists to just Key and ErrKeyExists

Signed-off-by: David Lawrence <david.lawrence@docker.com> (github: endophage)
This commit is contained in:
David Lawrence 2015-12-02 17:19:57 -08:00
parent 76caa3d76a
commit e20773f2b1
7 changed files with 34 additions and 33 deletions

View File

@ -140,8 +140,8 @@ func (db *SQLStorage) Delete(gun string) error {
func (db *SQLStorage) GetKey(gun, role string) (algorithm string, public []byte, err error) {
logrus.Debugf("retrieving timestamp key for %s:%s", gun, role)
var row TimestampKey
query := db.Select("cipher, public").Where(&TimestampKey{Gun: gun, Role: role}).Find(&row)
var row Key
query := db.Select("cipher, public").Where(&Key{Gun: gun, Role: role}).Find(&row)
if query.RecordNotFound() {
return "", nil, &ErrNoKey{gun: gun}
@ -155,26 +155,26 @@ func (db *SQLStorage) GetKey(gun, role string) (algorithm string, public []byte,
// SetKey attempts to write a key and returns an error if it already exists for the gun and role
func (db *SQLStorage) SetKey(gun, role, algorithm string, public []byte) error {
entry := TimestampKey{
entry := Key{
Gun: gun,
Role: role,
Cipher: string(algorithm),
Public: public,
}
if !db.Where(&entry).First(&TimestampKey{}).RecordNotFound() {
return &ErrTimestampKeyExists{gun: gun}
if !db.Where(&entry).First(&Key{}).RecordNotFound() {
return &ErrKeyExists{gun: gun, role: role}
}
return translateOldVersionError(
db.FirstOrCreate(&TimestampKey{}, &entry).Error)
db.FirstOrCreate(&Key{}, &entry).Error)
}
// CheckHealth asserts that both required tables are present
func (db *SQLStorage) CheckHealth() error {
interfaces := []interface {
TableName() string
}{&TUFFile{}, &TimestampKey{}}
}{&TUFFile{}, &Key{}}
for _, model := range interfaces {
tableOk := db.HasTable(model)

View File

@ -39,12 +39,12 @@ func SetUpSQLite(t *testing.T, dbDir string) (*gorm.DB, *SQLStorage) {
err = CreateTUFTable(dbStore.DB)
assert.NoError(t, err)
err = CreateTimestampTable(dbStore.DB)
err = CreateKeyTable(dbStore.DB)
assert.NoError(t, err)
// verify that the tables are empty
var count int
for _, model := range [2]interface{}{&TUFFile{}, &TimestampKey{}} {
for _, model := range [2]interface{}{&TUFFile{}, &Key{}} {
query := dbStore.DB.Model(model).Count(&count)
assert.NoError(t, query.Error)
assert.Equal(t, 0, count)
@ -269,7 +269,7 @@ func TestSQLGetKeyNoKey(t *testing.T) {
assert.IsType(t, &ErrNoKey{}, err,
"Expected ErrNoKey from GetKey")
query := gormDB.Create(&TimestampKey{
query := gormDB.Create(&Key{
Gun: "testGUN",
Role: data.CanonicalTimestampRole,
Cipher: "testCipher",
@ -294,18 +294,18 @@ func TestSQLSetKeyExists(t *testing.T) {
err = dbStore.SetKey("testGUN", data.CanonicalTimestampRole, "testCipher", []byte("1"))
assert.Error(t, err)
assert.IsType(t, &ErrTimestampKeyExists{}, err,
"Expected ErrTimestampKeyExists from SetKey")
assert.IsType(t, &ErrKeyExists{}, err,
"Expected ErrKeyExists from SetKey")
var rows []TimestampKey
var rows []Key
query := gormDB.Select("ID, Gun, Cipher, Public").Find(&rows)
assert.NoError(t, query.Error)
expected := TimestampKey{Gun: "testGUN", Cipher: "testCipher",
expected := Key{Gun: "testGUN", Cipher: "testCipher",
Public: []byte("1")}
expected.Model = gorm.Model{ID: 1}
assert.Equal(t, []TimestampKey{expected}, rows)
assert.Equal(t, []Key{expected}, rows)
dbStore.DB.Close()
}
@ -318,7 +318,7 @@ func TestDBCheckHealthTableMissing(t *testing.T) {
defer os.RemoveAll(tempBaseDir)
dbStore.DropTable(&TUFFile{})
dbStore.DropTable(&TimestampKey{})
dbStore.DropTable(&Key{})
// No tables, health check fails
err = dbStore.CheckHealth()
@ -330,7 +330,7 @@ func TestDBCheckHealthTableMissing(t *testing.T) {
assert.Error(t, err, "Cannot access table:")
dbStore.DropTable(&TUFFile{})
CreateTimestampTable(dbStore.DB)
CreateKeyTable(dbStore.DB)
err = dbStore.CheckHealth()
assert.Error(t, err, "Cannot access table:")
}

View File

@ -20,14 +20,15 @@ func (err ErrNotFound) Error() string {
return fmt.Sprintf("No record found")
}
// ErrTimestampKeyExists is returned when a timestamp key already exists
type ErrTimestampKeyExists struct {
gun string
// ErrKeyExists is returned when a key already exists
type ErrKeyExists struct {
gun string
role string
}
// ErrTimestampKeyExists is returned when a timestamp key already exists
func (err ErrTimestampKeyExists) Error() string {
return fmt.Sprintf("Error, timestamp key already exists for %s", err.gun)
// ErrKeyExists is returned when a key already exists
func (err ErrKeyExists) Error() string {
return fmt.Sprintf("Error, timestamp key already exists for %s:%s", err.gun, err.role)
}
// ErrNoKey is returned when no timestamp key is found

View File

@ -106,7 +106,7 @@ func (st *MemStorage) SetKey(gun, role, algorithm string, public []byte) error {
// between checking and setting
_, _, err := st.GetKey(gun, role)
if _, ok := err.(*ErrNoKey); !ok {
return &ErrTimestampKeyExists{gun: gun}
return &ErrKeyExists{gun: gun, role: role}
}
_, ok := st.keys[gun]
if !ok {

View File

@ -57,7 +57,7 @@ func TestSetKey(t *testing.T) {
s.SetKey("gun", data.CanonicalTimestampRole, data.RSAKey, []byte("test"))
err := s.SetKey("gun", data.CanonicalTimestampRole, data.RSAKey, []byte("test2"))
assert.IsType(t, &ErrTimestampKeyExists{}, err, "Expected err to be ErrTimestampKeyExists")
assert.IsType(t, &ErrKeyExists{}, err, "Expected err to be ErrKeyExists")
k := s.keys["gun"][data.CanonicalTimestampRole]
assert.Equal(t, data.RSAKey, k.algorithm, "Expected algorithm to be rsa, received %s", k.algorithm)

View File

@ -16,8 +16,8 @@ func (g TUFFile) TableName() string {
return "tuf_files"
}
// TimestampKey represents a single timestamp key in the database
type TimestampKey struct {
// Key represents a single timestamp key in the database
type Key struct {
gorm.Model
Gun string `sql:"type:varchar(255);not null"`
Role string `sql:"type:varchar(255);not null"`
@ -26,7 +26,7 @@ type TimestampKey struct {
}
// TableName sets a specific table name for our TimestampKey
func (g TimestampKey) TableName() string {
func (g Key) TableName() string {
return "timestamp_keys"
}
@ -45,13 +45,13 @@ func CreateTUFTable(db gorm.DB) error {
return nil
}
// CreateTimestampTable creates the DB table for TUFFile
func CreateTimestampTable(db gorm.DB) error {
query := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&TimestampKey{})
// CreateKeyTable creates the DB table for TUFFile
func CreateKeyTable(db gorm.DB) error {
query := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&Key{})
if query.Error != nil {
return query.Error
}
query = db.Model(&TimestampKey{}).AddUniqueIndex(
query = db.Model(&Key{}).AddUniqueIndex(
"idx_gun_role", "gun", "role")
if query.Error != nil {
return query.Error

View File

@ -33,7 +33,7 @@ func GetOrCreateTimestampKey(gun string, store storage.MetaStore, crypto signed.
return key, nil
}
if _, ok := err.(*storage.ErrTimestampKeyExists); ok {
if _, ok := err.(*storage.ErrKeyExists); ok {
keyAlgorithm, public, err = store.GetKey(gun, data.CanonicalTimestampRole)
if err != nil {
return nil, err