Semaphore/api/projects/keys.go

179 lines
4.1 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
2016-04-04 01:10:12 +02:00
import (
"fmt"
"net/http"
"github.com/semaphoreui/semaphore/api/helpers"
"github.com/semaphoreui/semaphore/db"
2017-02-23 00:21:49 +01:00
"github.com/gorilla/context"
2016-04-04 01:10:12 +02:00
)
2016-04-02 14:40:07 +02:00
// KeyMiddleware ensures a key exists and loads it to the context
func KeyMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
keyID, err := helpers.GetIntParam("key_id", w, r)
if err != nil {
return
}
key, err := helpers.Store(r).GetAccessKey(project.ID, keyID)
2020-12-08 08:23:33 +01:00
if err != nil {
helpers.WriteError(w, err)
return
}
context.Set(r, "accessKey", key)
next.ServeHTTP(w, r)
})
2016-04-04 15:44:34 +02:00
}
2022-02-03 08:05:13 +01:00
func GetKeyRefs(w http.ResponseWriter, r *http.Request) {
key := context.Get(r, "accessKey").(db.AccessKey)
refs, err := helpers.Store(r).GetAccessKeyRefs(*key.ProjectID, key.ID)
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, refs)
}
// GetKeys retrieves sorted keys from the database
func GetKeys(w http.ResponseWriter, r *http.Request) {
2020-11-04 20:30:36 +01:00
if key := context.Get(r, "accessKey"); key != nil {
k := key.(db.AccessKey)
helpers.WriteJSON(w, http.StatusOK, k)
2020-11-04 20:30:36 +01:00
return
}
project := context.Get(r, "project").(db.Project)
var keys []db.AccessKey
2021-10-13 16:33:07 +02:00
keys, err := helpers.Store(r).GetAccessKeys(project.ID, helpers.QueryParams(r.URL))
2020-12-08 08:23:33 +01:00
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, keys)
2016-04-02 14:40:07 +02:00
}
// AddKey adds a new key to the database
func AddKey(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
var key db.AccessKey
if !helpers.Bind(w, r, &key) {
return
}
2020-12-08 08:23:33 +01:00
if key.ProjectID == nil || *key.ProjectID != project.ID {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Project ID in body and URL must be the same",
2020-12-08 08:23:33 +01:00
})
return
}
2021-09-01 16:38:28 +02:00
if err := key.Validate(true); err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
return
}
2020-12-08 08:23:33 +01:00
newKey, err := helpers.Store(r).CreateAccessKey(key)
if err != nil {
2020-12-08 08:23:33 +01:00
helpers.WriteError(w, err)
return
}
helpers.EventLog(r, helpers.EventLogCreate, helpers.EventLogItem{
UserID: helpers.UserFromContext(r).ID,
ProjectID: *newKey.ProjectID,
ObjectType: db.EventKey,
ObjectID: newKey.ID,
Description: fmt.Sprintf("Access Key %s created", key.Name),
})
w.WriteHeader(http.StatusNoContent)
2016-04-04 01:10:12 +02:00
}
// UpdateKey updates key in database
// nolint: gocyclo
func UpdateKey(w http.ResponseWriter, r *http.Request) {
var key db.AccessKey
oldKey := context.Get(r, "accessKey").(db.AccessKey)
if !helpers.Bind(w, r, &key) {
return
}
repos, err := helpers.Store(r).GetRepositories(*key.ProjectID, db.RetrieveQueryParams{})
if err != nil {
helpers.WriteError(w, err)
return
}
for _, repo := range repos {
if repo.SSHKeyID != key.ID {
continue
}
2022-01-27 15:16:58 +01:00
err = repo.ClearCache()
if err != nil {
helpers.WriteError(w, err)
return
}
}
err = helpers.Store(r).UpdateAccessKey(key)
if err != nil {
2020-12-08 08:23:33 +01:00
helpers.WriteError(w, err)
return
}
helpers.EventLog(r, helpers.EventLogUpdate, helpers.EventLogItem{
UserID: helpers.UserFromContext(r).ID,
ProjectID: *oldKey.ProjectID,
ObjectType: db.EventKey,
ObjectID: oldKey.ID,
Description: fmt.Sprintf("Access Key %s updated", key.Name),
})
w.WriteHeader(http.StatusNoContent)
2016-04-02 14:40:07 +02:00
}
// RemoveKey deletes a key from the database
func RemoveKey(w http.ResponseWriter, r *http.Request) {
key := context.Get(r, "accessKey").(db.AccessKey)
err := helpers.Store(r).DeleteAccessKey(*key.ProjectID, key.ID)
2022-02-03 08:05:13 +01:00
if err == db.ErrInvalidOperation {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
"error": "Access Key is in use by one or more templates",
"inUse": true,
})
return
}
2020-12-08 08:23:33 +01:00
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.EventLog(r, helpers.EventLogDelete, helpers.EventLogItem{
UserID: helpers.UserFromContext(r).ID,
ProjectID: *key.ProjectID,
ObjectType: db.EventKey,
ObjectID: key.ID,
Description: fmt.Sprintf("Access Key %s deleted", key.Name),
})
w.WriteHeader(http.StatusNoContent)
2016-04-02 14:40:07 +02:00
}