Skip to content

Commit

Permalink
Endpoint for adding the key in the database
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
kostas-kou committed Sep 27, 2024
1 parent de86093 commit e3d5b5c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
28 changes: 25 additions & 3 deletions sda/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
33 changes: 33 additions & 0 deletions sda/internal/database/db_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions sda/internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,8 @@ type SyncMetadata struct {
type Metadata struct {
Metadata interface{}
}

type KeyhashInsertion struct {
Hash string `json:"hash"`
Description string `json:"description"`
}

0 comments on commit e3d5b5c

Please sign in to comment.