Skip to content

Commit

Permalink
tmp api/key/hashed sends 200 if key is already there
Browse files Browse the repository at this point in the history
  • Loading branch information
MalinAhlberg committed Oct 4, 2024
1 parent 0a40903 commit ae47c17
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions sda/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions sda/internal/database/db_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit ae47c17

Please sign in to comment.