From 578548404c16f50777eeb8455ecab14b087e1c8a Mon Sep 17 00:00:00 2001 From: MalinAhlberg Date: Fri, 4 Oct 2024 11:05:34 +0200 Subject: [PATCH] tmp api/key/hashed sends 200 if key is already there --- sda/cmd/api/api.go | 10 ++++++---- sda/internal/database/db_functions.go | 5 +++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/sda/cmd/api/api.go b/sda/cmd/api/api.go index 0dece461c..c317f24b7 100644 --- a/sda/cmd/api/api.go +++ b/sda/cmd/api/api.go @@ -409,10 +409,12 @@ func addHashedKey(c *gin.Context) { } err = Conf.API.DB.AddKeyHash(keyhash.Hash, keyhash.Description) - if err != nil { - c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) - return - } + if err != nil { + log.Warnf("aborting! not happy %v", err) + c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) + return + } + log.Warnf("Is happy") c.Status(http.StatusOK) } diff --git a/sda/internal/database/db_functions.go b/sda/internal/database/db_functions.go index 9d3e1525f..986143838 100644 --- a/sda/internal/database/db_functions.go +++ b/sda/internal/database/db_functions.go @@ -747,6 +747,11 @@ func (dbs *SDAdb) addKeyHash(keyHash, keyDescription string) error { if err != nil { return err } + var exists bool + err = db.QueryRow("SELECT EXISTS(SELECT 1 FROM sda.encryption_keys WHERE key_hash=$1 AND description=$2)", keyHash, keyDescription).Scan(&exists) + if exists { + return nil + } if rowsAffected, _ := result.RowsAffected(); rowsAffected == 0 { return errors.New("something went wrong with the query zero rows were changed")