From ee383ced9c7339aa416590665549ea45f8c9dc27 Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Fri, 24 Jul 2015 14:35:14 -0700 Subject: [PATCH] adding db queries Signed-off-by: David Lawrence (github: endophage) --- server/storage/database.go | 32 +++++++++++++++++++++++++++++++- server/storage/interface.go | 5 +++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/server/storage/database.go b/server/storage/database.go index 9748f099fb..2caf2e0be9 100644 --- a/server/storage/database.go +++ b/server/storage/database.go @@ -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"` +} diff --git a/server/storage/interface.go b/server/storage/interface.go index 1d3daa8335..0bc69a62c9 100644 --- a/server/storage/interface.go +++ b/server/storage/interface.go @@ -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 +}