2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
2016-04-04 15:44:34 +02:00
|
|
|
import (
|
2024-04-02 23:10:42 +02:00
|
|
|
"errors"
|
2024-04-12 09:23:13 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
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"
|
2016-04-13 18:09:44 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/gorilla/context"
|
2016-04-04 15:44:34 +02:00
|
|
|
)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// InventoryMiddleware ensures an inventory exists and loads it to the context
|
2019-07-09 18:14:06 +02:00
|
|
|
func InventoryMiddleware(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
|
|
|
inventoryID, err := helpers.GetIntParam("inventory_id", w, r)
|
2019-07-09 18:14:06 +02:00
|
|
|
if err != nil {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
2019-07-09 14:56:03 +02:00
|
|
|
}
|
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
inventory, err := helpers.Store(r).GetInventory(project.ID, inventoryID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
2020-12-08 08:23:33 +01:00
|
|
|
return
|
2019-07-09 18:14:06 +02:00
|
|
|
}
|
2019-07-09 14:56:03 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
context.Set(r, "inventory", inventory)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
func GetInventoryRefs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
inventory := context.Get(r, "inventory").(db.Inventory)
|
|
|
|
refs, err := helpers.Store(r).GetInventoryRefs(inventory.ProjectID, inventory.ID)
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, refs)
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetInventory returns an inventory from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetInventory(w http.ResponseWriter, r *http.Request) {
|
2020-11-03 19:09:09 +01:00
|
|
|
if inventory := context.Get(r, "inventory"); inventory != nil {
|
2020-12-04 23:41:26 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, inventory.(db.Inventory))
|
2020-11-03 19:09:09 +01:00
|
|
|
return
|
|
|
|
}
|
2020-11-22 01:32:49 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2020-11-03 19:09:09 +01:00
|
|
|
|
2021-10-13 16:33:07 +02:00
|
|
|
inventories, err := helpers.Store(r).GetInventories(project.ID, helpers.QueryParams(r.URL))
|
2017-03-16 15:55:50 +01:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, inventories)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddInventory creates an inventory in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddInventory(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2020-12-07 13:13:59 +01:00
|
|
|
|
|
|
|
var inventory db.Inventory
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &inventory) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
if inventory.ProjectID != project.ID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Project ID in body and URL must be the same",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
switch inventory.Type {
|
2024-06-12 21:53:00 +02:00
|
|
|
case db.InventoryStatic, db.InventoryStaticYaml, db.InventoryFile, db.InventoryTerraformWorkspace:
|
2019-07-09 18:11:01 +02:00
|
|
|
break
|
|
|
|
default:
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Not supported inventory type",
|
|
|
|
})
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-17 02:20:23 +02:00
|
|
|
|
2024-03-18 15:33:40 +01:00
|
|
|
err := db.ValidateInventory(helpers.Store(r), &inventory)
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
newInventory, err := helpers.Store(r).CreateInventory(inventory)
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
if err != nil {
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2024-04-12 09:23:13 +02:00
|
|
|
helpers.EventLog(r, helpers.EventLogCreate, helpers.EventLogItem{
|
|
|
|
UserID: helpers.UserFromContext(r).ID,
|
|
|
|
ProjectID: project.ID,
|
|
|
|
ObjectType: db.EventInventory,
|
|
|
|
ObjectID: newInventory.ID,
|
|
|
|
Description: fmt.Sprintf("Inventory %s created", inventory.Name),
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusCreated, newInventory)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// IsValidInventoryPath tests a path to ensure it is below the cwd
|
2018-03-08 12:37:38 +01:00
|
|
|
func IsValidInventoryPath(path string) bool {
|
2018-03-27 22:12:47 +02:00
|
|
|
|
|
|
|
currentPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
2018-03-08 10:04:34 +01:00
|
|
|
return false
|
2018-03-27 22:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
absPath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
2018-03-08 10:04:34 +01:00
|
|
|
return false
|
2018-03-27 22:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
relPath, err := filepath.Rel(currentPath, absPath)
|
|
|
|
if err != nil {
|
2018-03-08 10:04:34 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-03-27 22:12:47 +02:00
|
|
|
|
|
|
|
return !strings.HasPrefix(relPath, "..")
|
2018-03-08 10:04:34 +01:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// UpdateInventory writes updated values to an existing inventory item in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func UpdateInventory(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
oldInventory := context.Get(r, "inventory").(db.Inventory)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
var inventory db.Inventory
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &inventory) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2020-12-07 13:13:59 +01:00
|
|
|
if inventory.ID != oldInventory.ID {
|
2024-04-02 23:10:42 +02:00
|
|
|
helpers.WriteErrorStatus(w,
|
|
|
|
"Inventory ID in body and URL must be the same",
|
|
|
|
http.StatusBadRequest)
|
2020-12-07 13:13:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if inventory.ProjectID != oldInventory.ProjectID {
|
2024-04-02 23:10:42 +02:00
|
|
|
helpers.WriteErrorStatus(w,
|
|
|
|
"project ID in body and URL must be the same",
|
|
|
|
http.StatusBadRequest)
|
2020-12-07 13:13:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
switch inventory.Type {
|
2022-05-24 17:54:33 +02:00
|
|
|
case db.InventoryStatic, db.InventoryStaticYaml:
|
2019-07-09 18:11:01 +02:00
|
|
|
break
|
2021-09-12 00:18:26 +02:00
|
|
|
case db.InventoryFile:
|
2019-07-09 18:11:01 +02:00
|
|
|
if !IsValidInventoryPath(inventory.Inventory) {
|
2024-05-22 19:00:28 +02:00
|
|
|
helpers.WriteErrorStatus(w, "Invalid inventory file pathname. Must be: path/to/inventory.", http.StatusBadRequest)
|
2020-11-22 01:32:49 +01:00
|
|
|
return
|
2019-07-09 14:56:03 +02:00
|
|
|
}
|
2024-06-12 21:53:00 +02:00
|
|
|
case db.InventoryTerraformWorkspace:
|
|
|
|
break
|
2019-07-09 18:11:01 +02:00
|
|
|
default:
|
2024-04-02 23:10:42 +02:00
|
|
|
helpers.WriteErrorStatus(w,
|
2024-04-27 09:00:15 +02:00
|
|
|
"unknown inventory type: "+string(inventory.Type),
|
2024-04-02 23:10:42 +02:00
|
|
|
http.StatusBadRequest)
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-16 21:42:57 +02:00
|
|
|
|
2024-03-18 15:33:40 +01:00
|
|
|
if err := db.ValidateInventory(helpers.Store(r), &inventory); err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2024-03-18 15:33:40 +01:00
|
|
|
if err := helpers.Store(r).UpdateInventory(inventory); err != nil {
|
2020-12-07 13:13:59 +01:00
|
|
|
helpers.WriteError(w, err)
|
2020-12-01 20:06:49 +01:00
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2019-07-09 14:56:03 +02:00
|
|
|
|
2024-04-12 09:23:13 +02:00
|
|
|
helpers.EventLog(r, helpers.EventLogUpdate, helpers.EventLogItem{
|
|
|
|
UserID: helpers.UserFromContext(r).ID,
|
|
|
|
ProjectID: oldInventory.ProjectID,
|
|
|
|
ObjectType: db.EventInventory,
|
|
|
|
ObjectID: oldInventory.ID,
|
|
|
|
Description: fmt.Sprintf("Inventory %s updated", inventory.Name),
|
|
|
|
})
|
|
|
|
|
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
|
|
|
// RemoveInventory deletes an inventory from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func RemoveInventory(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
inventory := context.Get(r, "inventory").(db.Inventory)
|
2020-12-07 19:50:41 +01:00
|
|
|
var err error
|
2016-04-13 18:09:44 +02:00
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
err = helpers.Store(r).DeleteInventory(inventory.ProjectID, inventory.ID)
|
2024-04-02 23:10:42 +02:00
|
|
|
if errors.Is(err, db.ErrInvalidOperation) {
|
2022-02-03 08:05:13 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
|
|
|
|
"error": "Inventory is in use by one or more templates",
|
|
|
|
"inUse": true,
|
|
|
|
})
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2020-12-07 19:50:41 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-12 09:23:13 +02:00
|
|
|
helpers.EventLog(r, helpers.EventLogDelete, helpers.EventLogItem{
|
|
|
|
UserID: helpers.UserFromContext(r).ID,
|
|
|
|
ProjectID: inventory.ProjectID,
|
|
|
|
ObjectType: db.EventInventory,
|
|
|
|
ObjectID: inventory.ID,
|
|
|
|
Description: fmt.Sprintf("Inventory %s deleted", inventory.Name),
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|