diff --git a/sda/cmd/api/api.go b/sda/cmd/api/api.go index 0dece461c..ba19c8af0 100644 --- a/sda/cmd/api/api.go +++ b/sda/cmd/api/api.go @@ -409,10 +409,13 @@ 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 d50d53fce..b30168d00 100644 --- a/sda/internal/database/db_functions.go +++ b/sda/internal/database/db_functions.go @@ -747,10 +747,20 @@ 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 err != nil { + return err + } + if exists { + return nil + } if rowsAffected, _ := result.RowsAffected(); rowsAffected == 0 { + return errors.New("something went wrong with the query zero rows were changed") } + return nil }