2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
2016-04-04 01:10:12 +02:00
|
|
|
import (
|
2020-12-01 20:06:49 +01:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2020-12-03 14:51:15 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
2020-12-04 23:41:26 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2020-12-01 20:06:49 +01:00
|
|
|
"net/http"
|
2019-07-09 19:45:27 +02:00
|
|
|
|
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
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// KeyMiddleware ensures a key exists and loads it to the context
|
2019-07-09 18:14:06 +02:00
|
|
|
func KeyMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2020-12-03 14:51:15 +01:00
|
|
|
keyID, err := helpers.GetIntParam("key_id", w, r)
|
2019-07-09 18:14:06 +02:00
|
|
|
if err != nil {
|
2016-04-09 21:09:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
key, err := helpers.Store(r).GetAccessKey(project.ID, keyID)
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:14:06 +02:00
|
|
|
}
|
2016-04-09 21:09:57 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
context.Set(r, "accessKey", key)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetKeys retrieves sorted keys from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetKeys(w http.ResponseWriter, r *http.Request) {
|
2020-11-04 20:30:36 +01:00
|
|
|
if key := context.Get(r, "accessKey"); key != nil {
|
2021-08-30 22:48:53 +02:00
|
|
|
k := key.(db.AccessKey)
|
|
|
|
k.Secret = nil
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, k)
|
2020-11-04 20:30:36 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
var keys []db.AccessKey
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
params := db.RetrieveQueryParams{
|
|
|
|
SortBy: r.URL.Query().Get("sort"),
|
|
|
|
SortInverted: r.URL.Query().Get("order") == desc,
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
keys, err := helpers.Store(r).GetAccessKeys(project.ID, params)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2021-08-30 22:48:53 +02:00
|
|
|
for _, k := range keys {
|
|
|
|
k.Secret = nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, keys)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddKey adds a new key to the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddKey(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
var key db.AccessKey
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &key) {
|
2019-07-09 18:11:01 +02:00
|
|
|
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{
|
2021-04-22 12:36:23 +02:00
|
|
|
"error": "Project ID in body and URL must be the same",
|
2020-12-08 08:23:33 +01:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
switch key.Type {
|
2021-08-30 16:24:20 +02:00
|
|
|
case db.AccessKeyNone:
|
2019-07-09 18:11:01 +02:00
|
|
|
break
|
2021-08-30 16:24:20 +02:00
|
|
|
case db.AccessKeySSH:
|
2019-07-09 18:11:01 +02:00
|
|
|
if key.Secret == nil || len(*key.Secret) == 0 {
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
2019-07-09 18:11:01 +02:00
|
|
|
"error": "SSH Secret empty",
|
2016-06-17 22:16:46 +02:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2019-07-09 18:11:01 +02:00
|
|
|
default:
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
2019-07-09 18:11:01 +02:00
|
|
|
"error": "Invalid key type",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:24:20 +02:00
|
|
|
if key.Secret != nil {
|
|
|
|
*key.Secret += "\n"
|
|
|
|
}
|
2020-12-08 08:23:33 +01:00
|
|
|
|
2021-08-31 07:47:19 +02:00
|
|
|
err := key.EncryptSecret()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
newKey, err := helpers.Store(r).CreateAccessKey(key)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-08 08:23:33 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 08:28:50 +02:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
objType := "key"
|
|
|
|
|
|
|
|
desc := "Access Key " + key.Name + " created"
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID: &user.ID,
|
2020-12-08 08:23:33 +01:00
|
|
|
ProjectID: newKey.ProjectID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ObjectType: &objType,
|
2020-12-08 08:23:33 +01:00
|
|
|
ObjectID: &newKey.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
Description: &desc,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-04 01:10:12 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// UpdateKey updates key in database
|
|
|
|
// nolint: gocyclo
|
2019-07-09 18:11:01 +02:00
|
|
|
func UpdateKey(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
var key db.AccessKey
|
|
|
|
oldKey := context.Get(r, "accessKey").(db.AccessKey)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &key) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch key.Type {
|
2021-08-30 16:24:20 +02:00
|
|
|
case db.AccessKeyNone:
|
|
|
|
case db.AccessKeySSH:
|
2019-07-09 18:11:01 +02:00
|
|
|
default:
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
2019-07-09 18:11:01 +02:00
|
|
|
"error": "Invalid key type",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:48:53 +02:00
|
|
|
if key.Type == db.AccessKeyNone {
|
|
|
|
key.Secret = nil
|
|
|
|
} else if key.Secret == nil || len(*key.Secret) == 0 {
|
2019-07-09 18:11:01 +02:00
|
|
|
// override secret
|
|
|
|
key.Secret = oldKey.Secret
|
|
|
|
} else {
|
2021-08-31 01:02:41 +02:00
|
|
|
*key.Secret += "\n"
|
|
|
|
|
|
|
|
err := key.EncryptSecret()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
if err := helpers.Store(r).UpdateAccessKey(key); err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 08:28:50 +02:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
desc := "Access Key " + key.Name + " updated"
|
|
|
|
objType := "key"
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err := helpers.Store(r).CreateEvent(db.Event{
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID: &user.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ProjectID: oldKey.ProjectID,
|
|
|
|
Description: &desc,
|
|
|
|
ObjectID: &oldKey.ID,
|
|
|
|
ObjectType: &objType,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// RemoveKey deletes a key from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func RemoveKey(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
key := context.Get(r, "accessKey").(db.AccessKey)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
var err error
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2021-05-16 23:44:42 +02:00
|
|
|
softDeletion := r.URL.Query().Get("setRemoved") == "1"
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
if softDeletion {
|
|
|
|
err = helpers.Store(r).DeleteAccessKeySoft(*key.ProjectID, key.ID)
|
|
|
|
} else {
|
|
|
|
err = helpers.Store(r).DeleteAccessKey(*key.ProjectID, key.ID)
|
|
|
|
if err == db.ErrInvalidOperation {
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
|
2020-12-08 08:23:33 +01:00
|
|
|
"error": "Inventory is in use by one or more templates",
|
2019-07-09 18:11:01 +02:00
|
|
|
"inUse": true,
|
|
|
|
})
|
2016-06-17 22:16:46 +02:00
|
|
|
return
|
|
|
|
}
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 08:23:33 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 08:28:50 +02:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
desc := "Access Key " + key.Name + " deleted"
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID: &user.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ProjectID: key.ProjectID,
|
|
|
|
Description: &desc,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|