From e3d5b5c30608aae7b7d5429a095cc8b2b8e62648 Mon Sep 17 00:00:00 2001 From: kostas-kou Date: Fri, 27 Sep 2024 16:00:25 +0200 Subject: [PATCH] Endpoint for adding the key in the database - Add the structure - Add the database functions for inserting the key and its description in the encryption_keys table - Add the endpoint and the function for calling the db functions --- sda/cmd/api/api.go | 28 ++++++++++++++++++++--- sda/internal/database/db_functions.go | 33 +++++++++++++++++++++++++++ sda/internal/schema/schema.go | 5 ++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/sda/cmd/api/api.go b/sda/cmd/api/api.go index 901047cb4..b85c53082 100644 --- a/sda/cmd/api/api.go +++ b/sda/cmd/api/api.go @@ -23,9 +23,11 @@ import ( log "github.com/sirupsen/logrus" ) -var Conf *config.Config -var err error -var auth *userauth.ValidateFromToken +var ( + Conf *config.Config + err error + auth *userauth.ValidateFromToken +) func main() { Conf, err = config.NewConfig("api") @@ -80,6 +82,7 @@ func setup(config *config.Config) *http.Server { r.POST("/file/accession", isAdmin(), setAccession) // assign accession ID to a file r.POST("/dataset/create", isAdmin(), createDataset) // maps a set of files to a dataset r.POST("/dataset/release/*dataset", isAdmin(), releaseDataset) // Releases a dataset to be accessible + r.POST("/key/hashed", isAdmin(), addHashedKey) // Adds a hashed key to the database r.GET("/users", isAdmin(), listActiveUsers) // Lists all users r.GET("/users/:username/files", isAdmin(), listUserFiles) // Lists all unmapped files for a user } @@ -389,3 +392,22 @@ func listUserFiles(c *gin.Context) { c.Writer.Header().Set("Content-Type", "application/json") c.JSON(200, files) } + +// addHashedKey function adds a hashed public key and its description to the database +func addHashedKey(c *gin.Context) { + var keyhash schema.KeyhashInsertion + err = Conf.API.DB.AddKeyHash(keyhash.Hash, keyhash.Description) + if err := c.BindJSON(&keyhash); err != nil { + c.AbortWithStatusJSON( + http.StatusBadRequest, + gin.H{ + "error": "json decoding : " + err.Error(), + "status": http.StatusBadRequest, + }, + ) + + return + } + + c.Status(http.StatusOK) +} diff --git a/sda/internal/database/db_functions.go b/sda/internal/database/db_functions.go index f3179e1b2..f09d37cef 100644 --- a/sda/internal/database/db_functions.go +++ b/sda/internal/database/db_functions.go @@ -720,3 +720,36 @@ func (dbs *SDAdb) ListActiveUsers() ([]string, error) { return users, nil } + +// AddKeyHash adds a key hash and key description in the encryption_keys table +func (dbs *SDAdb) AddKeyHash(keyHash, keyDescription string) error { + var err error + // 2, 4, 8, 16, 32 seconds between each retry event. + for count := 1; count <= RetryTimes; count++ { + err = dbs.addKeyHash(keyHash, keyDescription) + if err == nil { + break + } + time.Sleep(time.Duration(math.Pow(2, float64(count))) * time.Second) + } + + return err +} + +func (dbs *SDAdb) addKeyHash(keyHash, keyDescription string) error { + dbs.checkAndReconnectIfNeeded() + db := dbs.DB + + const query = "INSERT INTO sda.encryption_keys(key_hash, description) VALUES($1, $2) ON CONFLICT DO NOTHING;" + + result, err := db.Exec(query, keyHash, keyDescription) + if err != nil { + return err + } + + if rowsAffected, _ := result.RowsAffected(); rowsAffected == 0 { + return errors.New("something went wrong with the query zero rows were changed") + } + + return nil +} diff --git a/sda/internal/schema/schema.go b/sda/internal/schema/schema.go index c01440469..f8d309422 100644 --- a/sda/internal/schema/schema.go +++ b/sda/internal/schema/schema.go @@ -179,3 +179,8 @@ type SyncMetadata struct { type Metadata struct { Metadata interface{} } + +type KeyhashInsertion struct { + Hash string `json:"hash"` + Description string `json:"description"` +}