2016-05-24 11:55:48 +02:00
|
|
|
package api
|
2016-01-05 00:32:53 +01:00
|
|
|
|
|
|
|
import (
|
2024-02-26 12:03:38 +01:00
|
|
|
"bytes"
|
|
|
|
"embed"
|
2019-07-09 18:14:06 +02:00
|
|
|
"fmt"
|
2017-02-23 00:21:49 +01:00
|
|
|
"net/http"
|
2019-09-08 09:19:46 +02:00
|
|
|
"os"
|
2024-02-26 12:03:38 +01:00
|
|
|
"path"
|
2016-01-06 12:20:07 +01:00
|
|
|
"strings"
|
2024-02-26 12:03:38 +01:00
|
|
|
"time"
|
2016-03-16 22:49:43 +01:00
|
|
|
|
2024-01-11 14:03:09 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/runners"
|
|
|
|
|
2023-08-16 16:04:49 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
2016-05-24 11:55:48 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/projects"
|
|
|
|
"github.com/ansible-semaphore/semaphore/api/sockets"
|
2023-08-16 16:04:49 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2016-03-16 22:49:43 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/gorilla/mux"
|
2016-01-05 00:32:53 +01:00
|
|
|
)
|
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
var startTime = time.Now().UTC()
|
|
|
|
|
|
|
|
//go:embed public/*
|
|
|
|
var publicAssets embed.FS
|
2018-03-05 19:59:58 +01:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
// StoreMiddleware WTF?
|
2022-11-09 17:30:35 +01:00
|
|
|
func StoreMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
store := helpers.Store(r)
|
2023-08-29 00:51:04 +02:00
|
|
|
//var url = r.URL.String()
|
2022-11-09 17:30:35 +01:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
db.StoreSession(store, util.RandString(12), func() {
|
2022-11-18 23:23:30 +01:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2022-11-09 17:30:35 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONMiddleware ensures that all the routes respond with Json, this is added by default to all routes
|
2019-07-09 09:21:49 +02:00
|
|
|
func JSONMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("content-type", "application/json")
|
2019-07-09 18:11:01 +02:00
|
|
|
next.ServeHTTP(w, r)
|
2019-07-09 09:21:49 +02:00
|
|
|
})
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
|
|
|
|
2022-11-09 17:30:35 +01:00
|
|
|
// plainTextMiddleware resets headers to Plain Text if needed
|
2019-07-09 18:14:06 +02:00
|
|
|
func plainTextMiddleware(next http.Handler) http.Handler {
|
2019-07-09 09:21:49 +02:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("content-type", "text/plain; charset=utf-8")
|
2019-07-09 18:11:01 +02:00
|
|
|
next.ServeHTTP(w, r)
|
2019-07-09 09:21:49 +02:00
|
|
|
})
|
2018-04-11 20:05:38 +02:00
|
|
|
}
|
2018-10-23 05:22:49 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
func pongHandler(w http.ResponseWriter, r *http.Request) {
|
2020-02-09 14:48:24 +01:00
|
|
|
//nolint: errcheck
|
2019-07-09 18:14:06 +02:00
|
|
|
w.Write([]byte("pong"))
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// Route declares all routes
|
2019-07-09 18:14:06 +02:00
|
|
|
func Route() *mux.Router {
|
2020-11-26 23:35:49 +01:00
|
|
|
r := mux.NewRouter()
|
2019-07-09 18:11:01 +02:00
|
|
|
r.NotFoundHandler = http.HandlerFunc(servePublic)
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2018-10-24 07:04:55 +02:00
|
|
|
webPath := "/"
|
2020-11-24 13:26:02 +01:00
|
|
|
if util.WebHostURL != nil {
|
2019-09-08 09:19:46 +02:00
|
|
|
webPath = util.WebHostURL.Path
|
2020-11-24 13:26:02 +01:00
|
|
|
if !strings.HasSuffix(webPath, "/") {
|
|
|
|
webPath += "/"
|
|
|
|
}
|
2018-10-24 07:04:55 +02:00
|
|
|
}
|
|
|
|
|
2019-09-12 20:35:44 +02:00
|
|
|
r.Use(mux.CORSMethodMiddleware(r))
|
2019-09-08 09:19:46 +02:00
|
|
|
|
2019-09-12 20:35:44 +02:00
|
|
|
pingRouter := r.Path(webPath + "api/ping").Subrouter()
|
2019-09-08 09:19:46 +02:00
|
|
|
pingRouter.Use(plainTextMiddleware)
|
|
|
|
pingRouter.Methods("GET", "HEAD").HandlerFunc(pongHandler)
|
|
|
|
|
2019-09-12 20:35:44 +02:00
|
|
|
publicAPIRouter := r.PathPrefix(webPath + "api").Subrouter()
|
2022-11-09 17:30:35 +01:00
|
|
|
publicAPIRouter.Use(StoreMiddleware, JSONMiddleware)
|
2019-09-08 09:19:46 +02:00
|
|
|
|
2023-04-16 23:57:56 +02:00
|
|
|
publicAPIRouter.HandleFunc("/auth/login", login).Methods("GET", "POST")
|
2019-09-12 20:35:44 +02:00
|
|
|
publicAPIRouter.HandleFunc("/auth/logout", logout).Methods("POST")
|
2023-04-16 23:57:56 +02:00
|
|
|
publicAPIRouter.HandleFunc("/auth/oidc/{provider}/login", oidcLogin).Methods("GET")
|
|
|
|
publicAPIRouter.HandleFunc("/auth/oidc/{provider}/redirect", oidcRedirect).Methods("GET")
|
2024-03-11 18:25:19 +01:00
|
|
|
publicAPIRouter.HandleFunc("/auth/oidc/{provider}/redirect/{redirect_path:.*}", oidcRedirect).Methods("GET")
|
2019-09-12 20:35:44 +02:00
|
|
|
|
2024-09-28 17:51:58 +02:00
|
|
|
internalAPI := r.PathPrefix(webPath + "internal").Subrouter()
|
2024-09-28 16:05:26 +02:00
|
|
|
|
2024-09-28 17:51:58 +02:00
|
|
|
internalAPI.Use(StoreMiddleware, JSONMiddleware)
|
|
|
|
internalAPI.HandleFunc("/runners", runners.RegisterRunner).Methods("POST")
|
2024-09-28 16:05:26 +02:00
|
|
|
|
2024-09-28 17:51:58 +02:00
|
|
|
runnersAPI := internalAPI.PathPrefix("/runners").Subrouter()
|
2024-09-28 16:05:26 +02:00
|
|
|
runnersAPI.Use(runners.RunnerMiddleware)
|
2024-09-29 11:57:02 +02:00
|
|
|
runnersAPI.Path("/").HandlerFunc(runners.GetRunner).Methods("GET", "HEAD")
|
|
|
|
runnersAPI.Path("/").HandlerFunc(runners.UpdateRunner).Methods("PUT")
|
|
|
|
runnersAPI.Path("/").HandlerFunc(runners.UnregisterRunner).Methods("DELETE")
|
2023-08-29 00:51:04 +02:00
|
|
|
|
2024-03-21 00:23:55 +01:00
|
|
|
publicWebHookRouter := r.PathPrefix(webPath + "api").Subrouter()
|
2024-03-20 14:01:52 +01:00
|
|
|
publicWebHookRouter.Use(StoreMiddleware, JSONMiddleware)
|
2024-03-21 00:23:55 +01:00
|
|
|
publicWebHookRouter.Path("/integrations/{integration_alias}").HandlerFunc(ReceiveIntegration).Methods("POST", "GET", "OPTIONS")
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2022-11-09 17:30:35 +01:00
|
|
|
authenticatedWS := r.PathPrefix(webPath + "api").Subrouter()
|
|
|
|
authenticatedWS.Use(JSONMiddleware, authenticationWithStore)
|
|
|
|
authenticatedWS.Path("/ws").HandlerFunc(sockets.Handler).Methods("GET", "HEAD")
|
|
|
|
|
2019-09-12 20:35:44 +02:00
|
|
|
authenticatedAPI := r.PathPrefix(webPath + "api").Subrouter()
|
2022-11-09 17:30:35 +01:00
|
|
|
authenticatedAPI.Use(StoreMiddleware, JSONMiddleware, authentication)
|
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
authenticatedAPI.Path("/info").HandlerFunc(getSystemInfo).Methods("GET", "HEAD")
|
|
|
|
|
|
|
|
authenticatedAPI.Path("/projects").HandlerFunc(projects.GetProjects).Methods("GET", "HEAD")
|
|
|
|
authenticatedAPI.Path("/projects").HandlerFunc(projects.AddProject).Methods("POST")
|
2024-02-04 13:46:27 +01:00
|
|
|
authenticatedAPI.Path("/projects/restore").HandlerFunc(projects.Restore).Methods("POST")
|
2019-09-08 09:19:46 +02:00
|
|
|
authenticatedAPI.Path("/events").HandlerFunc(getAllEvents).Methods("GET", "HEAD")
|
|
|
|
authenticatedAPI.HandleFunc("/events/last", getLastEvents).Methods("GET", "HEAD")
|
|
|
|
|
|
|
|
authenticatedAPI.Path("/users").HandlerFunc(getUsers).Methods("GET", "HEAD")
|
|
|
|
authenticatedAPI.Path("/users").HandlerFunc(addUser).Methods("POST")
|
2020-11-26 23:35:49 +01:00
|
|
|
authenticatedAPI.Path("/user").HandlerFunc(getUser).Methods("GET", "HEAD")
|
2019-09-08 09:19:46 +02:00
|
|
|
|
2024-07-08 13:15:04 +02:00
|
|
|
authenticatedAPI.Path("/apps").HandlerFunc(getApps).Methods("GET", "HEAD")
|
|
|
|
|
2020-11-26 23:35:49 +01:00
|
|
|
tokenAPI := authenticatedAPI.PathPrefix("/user").Subrouter()
|
2019-09-08 09:19:46 +02:00
|
|
|
tokenAPI.Path("/tokens").HandlerFunc(getAPITokens).Methods("GET", "HEAD")
|
|
|
|
tokenAPI.Path("/tokens").HandlerFunc(createAPIToken).Methods("POST")
|
|
|
|
tokenAPI.HandleFunc("/tokens/{token_id}", expireAPIToken).Methods("DELETE")
|
|
|
|
|
2024-07-08 13:15:04 +02:00
|
|
|
adminAPI := authenticatedAPI.NewRoute().Subrouter()
|
|
|
|
adminAPI.Use(adminMiddleware)
|
|
|
|
adminAPI.Path("/options").HandlerFunc(getOptions).Methods("GET", "HEAD")
|
2024-07-09 10:57:33 +02:00
|
|
|
adminAPI.Path("/options").HandlerFunc(setOption).Methods("POST")
|
2024-07-08 13:15:04 +02:00
|
|
|
|
2024-09-26 14:52:31 +02:00
|
|
|
adminAPI.Path("/runners").HandlerFunc(getGlobalRunners).Methods("GET", "HEAD")
|
|
|
|
adminAPI.Path("/runners").HandlerFunc(addGlobalRunner).Methods("POST", "HEAD")
|
2024-09-26 12:54:03 +02:00
|
|
|
|
2024-09-26 14:52:31 +02:00
|
|
|
globalRunnersAPI := adminAPI.PathPrefix("/runners").Subrouter()
|
2024-09-26 12:54:03 +02:00
|
|
|
globalRunnersAPI.Use(globalRunnerMiddleware)
|
|
|
|
globalRunnersAPI.Path("/{runner_id}").HandlerFunc(getGlobalRunner).Methods("GET", "HEAD")
|
|
|
|
globalRunnersAPI.Path("/{runner_id}").HandlerFunc(updateGlobalRunner).Methods("PUT", "POST")
|
|
|
|
globalRunnersAPI.Path("/{runner_id}/active").HandlerFunc(setGlobalRunnerActive).Methods("POST")
|
|
|
|
globalRunnersAPI.Path("/{runner_id}").HandlerFunc(deleteGlobalRunner).Methods("DELETE")
|
|
|
|
|
2024-07-08 20:56:59 +02:00
|
|
|
appsAPI := adminAPI.PathPrefix("/apps").Subrouter()
|
2024-07-08 13:15:04 +02:00
|
|
|
appsAPI.Use(appMiddleware)
|
|
|
|
appsAPI.Path("/{app_id}").HandlerFunc(getApp).Methods("GET", "HEAD")
|
2024-07-09 12:37:47 +02:00
|
|
|
appsAPI.Path("/{app_id}").HandlerFunc(setApp).Methods("PUT", "POST")
|
2024-07-09 10:57:33 +02:00
|
|
|
appsAPI.Path("/{app_id}/active").HandlerFunc(setAppActive).Methods("POST")
|
|
|
|
appsAPI.Path("/{app_id}").HandlerFunc(deleteApp).Methods("DELETE")
|
2024-07-07 19:12:21 +02:00
|
|
|
|
2020-12-01 17:34:48 +01:00
|
|
|
userAPI := authenticatedAPI.Path("/users/{user_id}").Subrouter()
|
2019-09-08 09:19:46 +02:00
|
|
|
userAPI.Use(getUserMiddleware)
|
|
|
|
|
2020-11-26 23:35:49 +01:00
|
|
|
userAPI.Methods("GET", "HEAD").HandlerFunc(getUser)
|
|
|
|
userAPI.Methods("PUT").HandlerFunc(updateUser)
|
|
|
|
userAPI.Methods("DELETE").HandlerFunc(deleteUser)
|
|
|
|
|
|
|
|
userPasswordAPI := authenticatedAPI.PathPrefix("/users/{user_id}").Subrouter()
|
|
|
|
userPasswordAPI.Use(getUserMiddleware)
|
|
|
|
userPasswordAPI.Path("/password").HandlerFunc(updateUserPassword).Methods("POST")
|
|
|
|
|
|
|
|
projectGet := authenticatedAPI.Path("/project/{project_id}").Subrouter()
|
|
|
|
projectGet.Use(projects.ProjectMiddleware)
|
|
|
|
projectGet.Methods("GET", "HEAD").HandlerFunc(projects.GetProject)
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2023-07-08 23:35:39 +02:00
|
|
|
//
|
|
|
|
// Start and Stop tasks
|
|
|
|
projectTaskStart := authenticatedAPI.PathPrefix("/project/{project_id}").Subrouter()
|
2023-08-26 20:43:42 +02:00
|
|
|
projectTaskStart.Use(projects.ProjectMiddleware, projects.GetMustCanMiddleware(db.CanRunProjectTasks))
|
2023-07-08 23:35:39 +02:00
|
|
|
projectTaskStart.Path("/tasks").HandlerFunc(projects.AddTask).Methods("POST")
|
|
|
|
|
2023-08-27 00:10:02 +02:00
|
|
|
projectTaskStop := authenticatedAPI.PathPrefix("/project/{project_id}").Subrouter()
|
2023-08-26 20:43:42 +02:00
|
|
|
projectTaskStop.Use(projects.ProjectMiddleware, projects.GetTaskMiddleware, projects.GetMustCanMiddleware(db.CanRunProjectTasks))
|
2023-08-27 00:10:02 +02:00
|
|
|
projectTaskStop.HandleFunc("/tasks/{task_id}/stop", projects.StopTask).Methods("POST")
|
2024-03-12 01:44:04 +01:00
|
|
|
projectTaskStop.HandleFunc("/tasks/{task_id}/confirm", projects.ConfirmTask).Methods("POST")
|
2023-07-08 23:35:39 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Project resources CRUD
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserAPI := authenticatedAPI.PathPrefix("/project/{project_id}").Subrouter()
|
2023-08-26 20:43:42 +02:00
|
|
|
projectUserAPI.Use(projects.ProjectMiddleware, projects.GetMustCanMiddleware(db.CanManageProjectResources))
|
|
|
|
|
|
|
|
projectUserAPI.Path("/role").HandlerFunc(projects.GetUserRole).Methods("GET", "HEAD")
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserAPI.Path("/events").HandlerFunc(getAllEvents).Methods("GET", "HEAD")
|
|
|
|
projectUserAPI.HandleFunc("/events/last", getLastEvents).Methods("GET", "HEAD")
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserAPI.Path("/users").HandlerFunc(projects.GetUsers).Methods("GET", "HEAD")
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserAPI.Path("/keys").HandlerFunc(projects.GetKeys).Methods("GET", "HEAD")
|
|
|
|
projectUserAPI.Path("/keys").HandlerFunc(projects.AddKey).Methods("POST")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserAPI.Path("/repositories").HandlerFunc(projects.GetRepositories).Methods("GET", "HEAD")
|
|
|
|
projectUserAPI.Path("/repositories").HandlerFunc(projects.AddRepository).Methods("POST")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserAPI.Path("/inventory").HandlerFunc(projects.GetInventory).Methods("GET", "HEAD")
|
|
|
|
projectUserAPI.Path("/inventory").HandlerFunc(projects.AddInventory).Methods("POST")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserAPI.Path("/environment").HandlerFunc(projects.GetEnvironment).Methods("GET", "HEAD")
|
|
|
|
projectUserAPI.Path("/environment").HandlerFunc(projects.AddEnvironment).Methods("POST")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
projectUserAPI.Path("/tasks").HandlerFunc(projects.GetAllTasks).Methods("GET", "HEAD")
|
|
|
|
projectUserAPI.HandleFunc("/tasks/last", projects.GetLastTasks).Methods("GET", "HEAD")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserAPI.Path("/templates").HandlerFunc(projects.GetTemplates).Methods("GET", "HEAD")
|
|
|
|
projectUserAPI.Path("/templates").HandlerFunc(projects.AddTemplate).Methods("POST")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2024-06-23 19:24:22 +02:00
|
|
|
projectUserAPI.Path("/schedules").HandlerFunc(projects.GetProjectSchedules).Methods("GET", "HEAD")
|
2021-09-06 16:12:57 +02:00
|
|
|
projectUserAPI.Path("/schedules").HandlerFunc(projects.AddSchedule).Methods("POST")
|
2021-09-06 18:36:25 +02:00
|
|
|
projectUserAPI.Path("/schedules/validate").HandlerFunc(projects.ValidateScheduleCronFormat).Methods("POST")
|
2021-09-06 16:12:57 +02:00
|
|
|
|
2021-10-27 13:43:04 +02:00
|
|
|
projectUserAPI.Path("/views").HandlerFunc(projects.GetViews).Methods("GET", "HEAD")
|
|
|
|
projectUserAPI.Path("/views").HandlerFunc(projects.AddView).Methods("POST")
|
2021-10-27 20:05:54 +02:00
|
|
|
projectUserAPI.Path("/views/positions").HandlerFunc(projects.SetViewPositions).Methods("POST")
|
2021-10-26 20:19:12 +02:00
|
|
|
|
2024-02-11 20:52:14 +01:00
|
|
|
projectUserAPI.Path("/integrations").HandlerFunc(projects.GetIntegrations).Methods("GET", "HEAD")
|
|
|
|
projectUserAPI.Path("/integrations").HandlerFunc(projects.AddIntegration).Methods("POST")
|
2024-01-20 23:46:43 +01:00
|
|
|
projectUserAPI.Path("/backup").HandlerFunc(projects.GetBackup).Methods("GET", "HEAD")
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2023-07-08 23:35:39 +02:00
|
|
|
//
|
|
|
|
// Updating and deleting project
|
2020-12-01 17:34:48 +01:00
|
|
|
projectAdminAPI := authenticatedAPI.Path("/project/{project_id}").Subrouter()
|
2023-08-26 20:43:42 +02:00
|
|
|
projectAdminAPI.Use(projects.ProjectMiddleware, projects.GetMustCanMiddleware(db.CanUpdateProject))
|
2020-11-26 23:35:49 +01:00
|
|
|
projectAdminAPI.Methods("PUT").HandlerFunc(projects.UpdateProject)
|
|
|
|
projectAdminAPI.Methods("DELETE").HandlerFunc(projects.DeleteProject)
|
|
|
|
|
2023-09-18 21:43:13 +02:00
|
|
|
meAPI := authenticatedAPI.Path("/project/{project_id}/me").Subrouter()
|
|
|
|
meAPI.Use(projects.ProjectMiddleware)
|
|
|
|
meAPI.HandleFunc("", projects.LeftProject).Methods("DELETE")
|
|
|
|
|
2023-07-08 23:35:39 +02:00
|
|
|
//
|
|
|
|
// Manage project users
|
2020-11-26 23:35:49 +01:00
|
|
|
projectAdminUsersAPI := authenticatedAPI.PathPrefix("/project/{project_id}").Subrouter()
|
2023-09-18 21:43:13 +02:00
|
|
|
|
2023-08-26 20:43:42 +02:00
|
|
|
projectAdminUsersAPI.Use(projects.ProjectMiddleware, projects.GetMustCanMiddleware(db.CanManageProjectUsers))
|
2020-11-26 23:35:49 +01:00
|
|
|
projectAdminUsersAPI.Path("/users").HandlerFunc(projects.AddUser).Methods("POST")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-12-01 18:16:29 +01:00
|
|
|
projectUserManagement := projectAdminUsersAPI.PathPrefix("/users").Subrouter()
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserManagement.Use(projects.UserMiddleware)
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-11-03 21:56:22 +01:00
|
|
|
projectUserManagement.HandleFunc("/{user_id}", projects.GetUsers).Methods("GET", "HEAD")
|
2023-07-08 13:42:50 +02:00
|
|
|
projectUserManagement.HandleFunc("/{user_id}", projects.UpdateUser).Methods("PUT")
|
2019-09-08 09:19:46 +02:00
|
|
|
projectUserManagement.HandleFunc("/{user_id}", projects.RemoveUser).Methods("DELETE")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2023-07-08 23:35:39 +02:00
|
|
|
//
|
|
|
|
// Project resources CRUD (continue)
|
2022-01-18 23:07:53 +01:00
|
|
|
projectKeyManagement := projectUserAPI.PathPrefix("/keys").Subrouter()
|
2019-09-08 09:19:46 +02:00
|
|
|
projectKeyManagement.Use(projects.KeyMiddleware)
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-11-04 20:30:36 +01:00
|
|
|
projectKeyManagement.HandleFunc("/{key_id}", projects.GetKeys).Methods("GET", "HEAD")
|
2022-02-03 08:05:13 +01:00
|
|
|
projectKeyManagement.HandleFunc("/{key_id}/refs", projects.GetKeyRefs).Methods("GET", "HEAD")
|
2020-02-08 08:44:33 +01:00
|
|
|
projectKeyManagement.HandleFunc("/{key_id}", projects.UpdateKey).Methods("PUT")
|
|
|
|
projectKeyManagement.HandleFunc("/{key_id}", projects.RemoveKey).Methods("DELETE")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
projectRepoManagement := projectUserAPI.PathPrefix("/repositories").Subrouter()
|
|
|
|
projectRepoManagement.Use(projects.RepositoryMiddleware)
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-11-04 20:30:36 +01:00
|
|
|
projectRepoManagement.HandleFunc("/{repository_id}", projects.GetRepositories).Methods("GET", "HEAD")
|
2022-02-03 08:05:13 +01:00
|
|
|
projectRepoManagement.HandleFunc("/{repository_id}/refs", projects.GetRepositoryRefs).Methods("GET", "HEAD")
|
2020-02-08 08:44:33 +01:00
|
|
|
projectRepoManagement.HandleFunc("/{repository_id}", projects.UpdateRepository).Methods("PUT")
|
|
|
|
projectRepoManagement.HandleFunc("/{repository_id}", projects.RemoveRepository).Methods("DELETE")
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
projectInventoryManagement := projectUserAPI.PathPrefix("/inventory").Subrouter()
|
|
|
|
projectInventoryManagement.Use(projects.InventoryMiddleware)
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-11-03 20:32:24 +01:00
|
|
|
projectInventoryManagement.HandleFunc("/{inventory_id}", projects.GetInventory).Methods("GET", "HEAD")
|
2022-02-03 08:05:13 +01:00
|
|
|
projectInventoryManagement.HandleFunc("/{inventory_id}/refs", projects.GetInventoryRefs).Methods("GET", "HEAD")
|
2020-02-08 08:44:33 +01:00
|
|
|
projectInventoryManagement.HandleFunc("/{inventory_id}", projects.UpdateInventory).Methods("PUT")
|
|
|
|
projectInventoryManagement.HandleFunc("/{inventory_id}", projects.RemoveInventory).Methods("DELETE")
|
2019-09-08 09:19:46 +02:00
|
|
|
|
|
|
|
projectEnvManagement := projectUserAPI.PathPrefix("/environment").Subrouter()
|
|
|
|
projectEnvManagement.Use(projects.EnvironmentMiddleware)
|
|
|
|
|
2020-11-03 20:32:24 +01:00
|
|
|
projectEnvManagement.HandleFunc("/{environment_id}", projects.GetEnvironment).Methods("GET", "HEAD")
|
2022-02-03 08:05:13 +01:00
|
|
|
projectEnvManagement.HandleFunc("/{environment_id}/refs", projects.GetEnvironmentRefs).Methods("GET", "HEAD")
|
2020-02-08 08:44:33 +01:00
|
|
|
projectEnvManagement.HandleFunc("/{environment_id}", projects.UpdateEnvironment).Methods("PUT")
|
|
|
|
projectEnvManagement.HandleFunc("/{environment_id}", projects.RemoveEnvironment).Methods("DELETE")
|
2019-09-08 09:19:46 +02:00
|
|
|
|
|
|
|
projectTmplManagement := projectUserAPI.PathPrefix("/templates").Subrouter()
|
|
|
|
projectTmplManagement.Use(projects.TemplatesMiddleware)
|
|
|
|
|
2020-02-08 08:44:33 +01:00
|
|
|
projectTmplManagement.HandleFunc("/{template_id}", projects.UpdateTemplate).Methods("PUT")
|
|
|
|
projectTmplManagement.HandleFunc("/{template_id}", projects.RemoveTemplate).Methods("DELETE")
|
2020-10-05 00:29:02 +02:00
|
|
|
projectTmplManagement.HandleFunc("/{template_id}", projects.GetTemplate).Methods("GET")
|
2022-02-04 18:35:08 +01:00
|
|
|
projectTmplManagement.HandleFunc("/{template_id}/refs", projects.GetTemplateRefs).Methods("GET", "HEAD")
|
2022-01-29 19:00:21 +01:00
|
|
|
projectTmplManagement.HandleFunc("/{template_id}/tasks", projects.GetAllTasks).Methods("GET")
|
|
|
|
projectTmplManagement.HandleFunc("/{template_id}/tasks/last", projects.GetLastTasks).Methods("GET")
|
2021-09-06 16:12:57 +02:00
|
|
|
projectTmplManagement.HandleFunc("/{template_id}/schedules", projects.GetTemplateSchedules).Methods("GET")
|
2019-09-08 09:19:46 +02:00
|
|
|
|
|
|
|
projectTaskManagement := projectUserAPI.PathPrefix("/tasks").Subrouter()
|
2022-01-29 19:00:21 +01:00
|
|
|
projectTaskManagement.Use(projects.GetTaskMiddleware)
|
2019-09-08 09:19:46 +02:00
|
|
|
|
2022-01-29 19:00:21 +01:00
|
|
|
projectTaskManagement.HandleFunc("/{task_id}/output", projects.GetTaskOutput).Methods("GET", "HEAD")
|
|
|
|
projectTaskManagement.HandleFunc("/{task_id}", projects.GetTask).Methods("GET", "HEAD")
|
|
|
|
projectTaskManagement.HandleFunc("/{task_id}", projects.RemoveTask).Methods("DELETE")
|
2021-09-06 13:05:10 +02:00
|
|
|
|
|
|
|
projectScheduleManagement := projectUserAPI.PathPrefix("/schedules").Subrouter()
|
|
|
|
projectScheduleManagement.Use(projects.SchedulesMiddleware)
|
|
|
|
projectScheduleManagement.HandleFunc("/{schedule_id}", projects.GetSchedule).Methods("GET", "HEAD")
|
|
|
|
projectScheduleManagement.HandleFunc("/{schedule_id}", projects.UpdateSchedule).Methods("PUT")
|
2024-06-30 23:12:49 +02:00
|
|
|
projectScheduleManagement.HandleFunc("/{schedule_id}/active", projects.SetScheduleActive).Methods("PUT")
|
2021-09-06 13:05:10 +02:00
|
|
|
projectScheduleManagement.HandleFunc("/{schedule_id}", projects.RemoveSchedule).Methods("DELETE")
|
2019-09-08 09:19:46 +02:00
|
|
|
|
2021-10-27 18:22:52 +02:00
|
|
|
projectViewManagement := projectUserAPI.PathPrefix("/views").Subrouter()
|
2021-10-26 20:19:12 +02:00
|
|
|
projectViewManagement.Use(projects.ViewMiddleware)
|
|
|
|
projectViewManagement.HandleFunc("/{view_id}", projects.GetViews).Methods("GET", "HEAD")
|
|
|
|
projectViewManagement.HandleFunc("/{view_id}", projects.UpdateView).Methods("PUT")
|
|
|
|
projectViewManagement.HandleFunc("/{view_id}", projects.RemoveView).Methods("DELETE")
|
2021-10-27 21:48:51 +02:00
|
|
|
projectViewManagement.HandleFunc("/{view_id}/templates", projects.GetViewTemplates).Methods("GET", "HEAD")
|
2021-10-26 20:19:12 +02:00
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
projectIntegrationsAliasAPI := projectUserAPI.PathPrefix("/integrations").Subrouter()
|
|
|
|
projectIntegrationsAliasAPI.Use(projects.ProjectMiddleware)
|
|
|
|
projectIntegrationsAliasAPI.HandleFunc("/aliases", projects.GetIntegrationAlias).Methods("GET", "HEAD")
|
|
|
|
projectIntegrationsAliasAPI.HandleFunc("/aliases", projects.AddIntegrationAlias).Methods("POST")
|
|
|
|
projectIntegrationsAliasAPI.HandleFunc("/aliases/{alias_id}", projects.RemoveIntegrationAlias).Methods("DELETE")
|
2024-02-11 20:52:14 +01:00
|
|
|
|
2024-03-20 22:35:47 +01:00
|
|
|
projectIntegrationsAPI := projectUserAPI.PathPrefix("/integrations").Subrouter()
|
2024-02-11 20:52:14 +01:00
|
|
|
projectIntegrationsAPI.Use(projects.ProjectMiddleware, projects.IntegrationMiddleware)
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}", projects.UpdateIntegration).Methods("PUT")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}", projects.DeleteIntegration).Methods("DELETE")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}", projects.GetIntegration).Methods("GET")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/refs", projects.GetIntegrationRefs).Methods("GET", "HEAD")
|
2024-03-06 14:31:58 +01:00
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/matchers", projects.GetIntegrationMatchers).Methods("GET", "HEAD")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/matchers", projects.AddIntegrationMatcher).Methods("POST")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/values", projects.GetIntegrationExtractValues).Methods("GET", "HEAD")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/values", projects.AddIntegrationExtractValue).Methods("POST")
|
2024-03-20 22:35:47 +01:00
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/aliases", projects.GetIntegrationAlias).Methods("GET", "HEAD")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/aliases", projects.AddIntegrationAlias).Methods("POST")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/aliases/{alias_id}", projects.RemoveIntegrationAlias).Methods("DELETE")
|
2024-03-06 14:31:58 +01:00
|
|
|
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/matchers/{matcher_id}", projects.GetIntegrationMatcher).Methods("GET", "HEAD")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/matchers/{matcher_id}", projects.UpdateIntegrationMatcher).Methods("PUT")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/matchers/{matcher_id}", projects.DeleteIntegrationMatcher).Methods("DELETE")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/matchers/{matcher_id}/refs", projects.GetIntegrationMatcherRefs).Methods("GET", "HEAD")
|
|
|
|
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/values/{value_id}", projects.GetIntegrationExtractValue).Methods("GET", "HEAD")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/values/{value_id}", projects.UpdateIntegrationExtractValue).Methods("PUT")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/values/{value_id}", projects.DeleteIntegrationExtractValue).Methods("DELETE")
|
|
|
|
projectIntegrationsAPI.HandleFunc("/{integration_id}/values/{value_id}/refs", projects.GetIntegrationExtractValueRefs).Methods("GET")
|
2024-02-10 18:18:56 +01:00
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
if os.Getenv("DEBUG") == "1" {
|
|
|
|
defer debugPrintRoutes(r)
|
|
|
|
}
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2017-02-23 00:21:49 +01:00
|
|
|
return r
|
2016-01-05 00:32:53 +01:00
|
|
|
}
|
|
|
|
|
2019-09-08 09:19:46 +02:00
|
|
|
func debugPrintRoutes(r *mux.Router) {
|
|
|
|
err := r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
|
|
|
|
pathTemplate, err := route.GetPathTemplate()
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("ROUTE:", pathTemplate)
|
|
|
|
}
|
|
|
|
pathRegexp, err := route.GetPathRegexp()
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Path regexp:", pathRegexp)
|
|
|
|
}
|
|
|
|
queriesTemplates, err := route.GetQueriesTemplates()
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Queries templates:", strings.Join(queriesTemplates, ","))
|
|
|
|
}
|
|
|
|
queriesRegexps, err := route.GetQueriesRegexp()
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Queries regexps:", strings.Join(queriesRegexps, ","))
|
|
|
|
}
|
|
|
|
methods, err := route.GetMethods()
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Methods:", strings.Join(methods, ","))
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
func servePublic(w http.ResponseWriter, r *http.Request) {
|
2020-11-26 23:35:49 +01:00
|
|
|
webPath := "/"
|
|
|
|
if util.WebHostURL != nil {
|
2024-02-26 12:03:38 +01:00
|
|
|
webPath = util.WebHostURL.Path
|
|
|
|
if !strings.HasSuffix(webPath, "/") {
|
|
|
|
webPath += "/"
|
|
|
|
}
|
2020-11-26 23:35:49 +01:00
|
|
|
}
|
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
reqPath := r.URL.Path
|
|
|
|
apiPath := path.Join(webPath, "api")
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
if reqPath == apiPath || strings.HasPrefix(reqPath, apiPath) {
|
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2020-11-26 23:35:49 +01:00
|
|
|
return
|
2020-11-05 09:59:14 +01:00
|
|
|
}
|
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
if !strings.Contains(reqPath, ".") {
|
|
|
|
serveFile(w, r, "index.html")
|
|
|
|
return
|
2020-11-05 09:59:14 +01:00
|
|
|
}
|
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
newPath := strings.Replace(
|
|
|
|
reqPath,
|
|
|
|
webPath,
|
|
|
|
"",
|
|
|
|
1,
|
|
|
|
)
|
2016-01-05 00:32:53 +01:00
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
serveFile(w, r, newPath)
|
|
|
|
}
|
2020-11-23 21:57:03 +01:00
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
func serveFile(w http.ResponseWriter, r *http.Request, name string) {
|
|
|
|
res, err := publicAssets.ReadFile(
|
|
|
|
fmt.Sprintf("public/%s", name),
|
|
|
|
)
|
2020-11-23 21:57:03 +01:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
if err != nil {
|
2024-02-26 12:03:38 +01:00
|
|
|
http.Error(
|
|
|
|
w,
|
|
|
|
http.StatusText(http.StatusNotFound),
|
|
|
|
http.StatusNotFound,
|
|
|
|
)
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2019-07-09 14:56:03 +02:00
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
if util.WebHostURL != nil && name == "index.html" {
|
2020-11-24 13:26:02 +01:00
|
|
|
baseURL := util.WebHostURL.String()
|
2024-02-26 12:03:38 +01:00
|
|
|
|
2020-11-24 13:26:02 +01:00
|
|
|
if !strings.HasSuffix(baseURL, "/") {
|
|
|
|
baseURL += "/"
|
|
|
|
}
|
2024-02-26 12:03:38 +01:00
|
|
|
|
|
|
|
res = []byte(
|
|
|
|
strings.Replace(
|
|
|
|
string(res),
|
|
|
|
`<base href="/">`,
|
|
|
|
fmt.Sprintf(`<base href="%s">`, baseURL),
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2019-07-09 14:56:03 +02:00
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
if !strings.HasSuffix(name, ".html") {
|
|
|
|
w.Header().Add(
|
|
|
|
"Cache-Control",
|
|
|
|
fmt.Sprintf("max-age=%d, public, must-revalidate, proxy-revalidate", 24*time.Hour),
|
|
|
|
)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2024-02-26 12:03:38 +01:00
|
|
|
http.ServeContent(
|
|
|
|
w,
|
|
|
|
r,
|
|
|
|
name,
|
|
|
|
startTime,
|
|
|
|
bytes.NewReader(
|
|
|
|
res,
|
|
|
|
),
|
|
|
|
)
|
2016-01-05 00:32:53 +01:00
|
|
|
}
|
2016-05-17 17:18:26 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
func getSystemInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body := map[string]interface{}{
|
2024-09-28 17:17:52 +02:00
|
|
|
"version": util.Version(),
|
|
|
|
"ansible": util.AnsibleVersion(),
|
|
|
|
"web_host": util.WebHostURL.String(),
|
2019-07-09 18:14:06 +02:00
|
|
|
}
|
2016-05-17 17:18:26 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, body)
|
2021-09-22 06:01:53 +02:00
|
|
|
}
|