adding db queries

Signed-off-by: David Lawrence <david.lawrence@docker.com> (github: endophage)
This commit is contained in:
David Lawrence 2015-07-24 14:35:14 -07:00
parent b4b364df5f
commit ee383ced9c
2 changed files with 36 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/Sirupsen/logrus"
"github.com/endophage/gotuf/data"
"github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
)
// MySQLStorage implements a versioned store using a relational database.
@ -27,12 +28,14 @@ import (
// ) DEFAULT CHARSET=utf8;
type MySQLStorage struct {
sql.DB
gdb *gorm.DB
}
// NewMySQLStorage is a convenience method to create a MySQLStorage
func NewMySQLStorage(db *sql.DB) *MySQLStorage {
return &MySQLStorage{
DB: *db,
DB: *db,
gdb: gorm.Open("mysql", db.DB),
}
}
@ -175,3 +178,30 @@ func (db *MySQLStorage) SetTimestampKey(gun string, algorithm data.KeyAlgorithm,
}
return nil
}
func (db *MySQLStorage) GetPrivateKey(keyID string) (algorithm data.KeyAlgorithm, encryption string, public, private []byte, err error) {
privateKey := PrivateKey{}
if db.gdb.Where("keyId = ?", keyID).First(&privateKey).RecordNotFound() {
return "", nil, nil, ErrNoKey{}
}
return privateKey.Algorithm, privateKey.Encryption, privateKey.Public, privateKey.Private, nil
}
func (db *MySQLStorage) SetPrivateKey(keyID string, algorithm data.KeyAlgorithm, encryption string, public, private []byte) error {
privateKey := PrivateKey{
KeyID: keyID,
Encryption: encryption,
Algorithm: algorithm.String(),
Public: public,
Private: private,
}
}
type PrivateKey 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 []byte `gorm:"not null"`
}

View File

@ -11,3 +11,8 @@ type MetaStore interface {
GetTimestampKey(gun string) (algorithm data.KeyAlgorithm, public []byte, err error)
SetTimestampKey(gun string, algorithm data.KeyAlgorithm, public []byte) error
}
type PrivateKeyStore interface {
GetPrivateKey(keyID string) (algorithm data.KeyAlgorithm, public []byte, private []byte, err error)
SetPrivateKey(keyID string, algorithm data.KeyAlgorithm, public, private []byte) error
}