Semaphore/api/router.go

322 lines
11 KiB
Go
Raw Normal View History

2016-05-24 11:55:48 +02:00
package api
2016-01-05 00:32:53 +01:00
import (
"fmt"
2017-02-23 00:21:49 +01:00
"net/http"
2019-09-08 09:19:46 +02:00
"os"
2016-01-06 12:20:07 +01:00
"strings"
2016-03-16 22:49:43 +01:00
2016-05-24 11:55:48 +02:00
"github.com/ansible-semaphore/semaphore/api/projects"
"github.com/ansible-semaphore/semaphore/api/sockets"
"github.com/ansible-semaphore/semaphore/api/tasks"
2016-03-16 22:49:43 +01:00
"github.com/ansible-semaphore/semaphore/util"
"github.com/gobuffalo/packr"
2017-02-23 00:21:49 +01:00
"github.com/gorilla/mux"
2016-05-24 11:55:48 +02:00
"github.com/russross/blackfriday"
2016-01-05 00:32:53 +01:00
)
var publicAssets = packr.NewBox("../web/public")
//JSONMiddleware ensures that all the routes respond with Json, this is added by default to all routes
func JSONMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
next.ServeHTTP(w, r)
})
}
//plainTextMiddleware resets headers to Plain Text if needed
func plainTextMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/plain; charset=utf-8")
next.ServeHTTP(w, r)
})
}
2018-10-23 05:22:49 +02:00
func pongHandler(w http.ResponseWriter, r *http.Request) {
2020-02-09 14:48:24 +01:00
//nolint: errcheck
w.Write([]byte("pong"))
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.WriteHeader(http.StatusNotFound)
2020-02-09 14:48:24 +01:00
//nolint: errcheck
w.Write([]byte("404 not found"))
fmt.Println(r.Method, ":", r.URL.String(), "--> 404 Not Found")
}
// Route declares all routes
func Route() *mux.Router {
2019-09-08 09:19:46 +02:00
r := mux.NewRouter().StrictSlash(true)
r.NotFoundHandler = http.HandlerFunc(servePublic)
2016-01-05 00:32:53 +01:00
webPath := "/"
if util.WebHostURL != nil && util.WebHostURL.Hostname() != "0.0.0.0" {
2019-09-08 09:19:46 +02:00
r.Host(util.WebHostURL.Hostname())
webPath = util.WebHostURL.Path
}
r.Use(mux.CORSMethodMiddleware(r))
2019-09-08 09:19:46 +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)
publicAPIRouter := r.PathPrefix(webPath + "api").Subrouter()
publicAPIRouter.Use(JSONMiddleware)
2019-09-08 09:19:46 +02:00
publicAPIRouter.HandleFunc("/auth/login", login).Methods("POST")
publicAPIRouter.HandleFunc("/auth/logout", logout).Methods("POST")
authenticatedAPI := r.PathPrefix(webPath + "api").Subrouter()
authenticatedAPI.Use(JSONMiddleware, authentication)
2019-09-08 09:19:46 +02:00
authenticatedAPI.Path("/ws").HandlerFunc(sockets.Handler).Methods("GET", "HEAD")
authenticatedAPI.Path("/info").HandlerFunc(getSystemInfo).Methods("GET", "HEAD")
authenticatedAPI.Path("/upgrade").HandlerFunc(checkUpgrade).Methods("GET", "HEAD")
authenticatedAPI.Path("/upgrade").HandlerFunc(doUpgrade).Methods("POST")
authenticatedAPI.Path("/projects").HandlerFunc(projects.GetProjects).Methods("GET", "HEAD")
authenticatedAPI.Path("/projects").HandlerFunc(projects.AddProject).Methods("POST")
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")
tokenAPI := authenticatedAPI.PathPrefix("/user").Subrouter()
tokenAPI.Path("/").HandlerFunc(getUser).Methods("GET", "HEAD")
tokenAPI.Path("/tokens").HandlerFunc(getAPITokens).Methods("GET", "HEAD")
tokenAPI.Path("/tokens").HandlerFunc(createAPIToken).Methods("POST")
tokenAPI.HandleFunc("/tokens/{token_id}", expireAPIToken).Methods("DELETE")
userAPI := authenticatedAPI.PathPrefix("/users/{user_id}").Subrouter()
userAPI.Use(getUserMiddleware)
userAPI.Path("/").HandlerFunc(getUser).Methods("GET", "HEAD")
userAPI.Path("/").HandlerFunc(updateUser).Methods("PUT")
userAPI.Path("/").HandlerFunc(deleteUser).Methods("DELETE")
userAPI.Path("/password").HandlerFunc(updateUserPassword).Methods("POST")
2016-01-05 00:32:53 +01:00
2019-09-08 09:19:46 +02:00
projectUserAPI := authenticatedAPI.PathPrefix("/project/{project_id}").Subrouter()
projectUserAPI.Use(projects.ProjectMiddleware)
2019-09-08 09:19:46 +02:00
projectUserAPI.Path("/").HandlerFunc(projects.GetProject).Methods("GET", "HEAD")
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-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-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-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-09-08 09:19:46 +02:00
projectUserAPI.Path("/tasks").HandlerFunc(tasks.GetAllTasks).Methods("GET", "HEAD")
projectUserAPI.HandleFunc("/tasks/last", tasks.GetLastTasks).Methods("GET", "HEAD")
projectUserAPI.Path("/tasks").HandlerFunc(tasks.AddTask).Methods("POST")
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-09-08 09:19:46 +02:00
projectAdminAPI := authenticatedAPI.PathPrefix("/project/{project_id}").Subrouter()
projectAdminAPI.Use(projects.ProjectMiddleware, projects.MustBeAdmin)
2019-09-08 09:19:46 +02:00
projectAdminAPI.Path("/").HandlerFunc(projects.UpdateProject).Methods("PUT")
projectAdminAPI.Path("/").HandlerFunc(projects.DeleteProject).Methods("DELETE")
projectAdminAPI.Path("/users").HandlerFunc(projects.AddUser).Methods("POST")
2019-09-08 09:19:46 +02:00
projectUserManagement := projectAdminAPI.PathPrefix("/users").Subrouter()
projectUserManagement.Use(projects.UserMiddleware)
2019-09-08 09:19:46 +02:00
projectUserManagement.HandleFunc("/{user_id}/admin", projects.MakeUserAdmin).Methods("POST")
projectUserManagement.HandleFunc("/{user_id}/admin", projects.MakeUserAdmin).Methods("DELETE")
projectUserManagement.HandleFunc("/{user_id}", projects.RemoveUser).Methods("DELETE")
2020-02-08 08:44:33 +01:00
projectKeyManagement := projectAdminAPI.PathPrefix("/keys").Subrouter()
2019-09-08 09:19:46 +02:00
projectKeyManagement.Use(projects.KeyMiddleware)
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-09-08 09:19:46 +02:00
projectRepoManagement := projectUserAPI.PathPrefix("/repositories").Subrouter()
projectRepoManagement.Use(projects.RepositoryMiddleware)
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-09-08 09:19:46 +02:00
projectInventoryManagement := projectUserAPI.PathPrefix("/inventory").Subrouter()
projectInventoryManagement.Use(projects.InventoryMiddleware)
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-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")
2019-09-08 09:19:46 +02:00
projectTaskManagement := projectUserAPI.PathPrefix("/tasks").Subrouter()
projectTaskManagement.Use(tasks.GetTaskMiddleware)
2020-02-08 08:44:33 +01:00
projectTaskManagement.HandleFunc("/{task_id}/output", tasks.GetTaskOutput).Methods("GET", "HEAD")
projectTaskManagement.HandleFunc("/{task_id}", tasks.GetTask).Methods("GET", "HEAD")
projectTaskManagement.HandleFunc("/{task_id}", tasks.RemoveTask).Methods("DELETE")
2019-09-08 09:19:46 +02:00
if os.Getenv("DEBUG") == "1" {
defer debugPrintRoutes(r)
}
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)
}
}
//nolint: gocyclo
func servePublic(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
2016-04-04 15:44:34 +02:00
webPath := "/"
if util.WebHostURL != nil {
webPath = util.WebHostURL.RequestURI()
}
2016-01-06 12:20:07 +01:00
if !strings.HasPrefix(path, webPath+"public") {
if len(strings.Split(path, ".")) > 1 {
w.WriteHeader(http.StatusNotFound)
return
}
2016-01-06 12:20:07 +01:00
path = "/html/index.html"
}
2017-05-20 16:14:36 +02:00
path = strings.Replace(path, webPath+"public/", "", 1)
split := strings.Split(path, ".")
suffix := split[len(split)-1]
2016-01-05 00:32:53 +01:00
res, err := publicAssets.MustBytes(path)
if err != nil {
notFoundHandler(w, r)
return
}
// replace base path
if util.WebHostURL != nil && path == "/html/index.html" {
res = []byte(strings.Replace(string(res),
"<base href=\"/\">",
"<base href=\""+util.WebHostURL.String()+"\">",
1))
}
contentType := "text/plain"
switch suffix {
case "png":
contentType = "image/png"
case "jpg", "jpeg":
contentType = "image/jpeg"
case "gif":
contentType = "image/gif"
case "js":
contentType = "application/javascript"
case "css":
contentType = "text/css"
case "woff":
contentType = "application/x-font-woff"
case "ttf":
contentType = "application/x-font-ttf"
case "otf":
contentType = "application/x-font-otf"
case "html":
contentType = "text/html"
}
w.Header().Set("content-type", contentType)
_, err = w.Write(res)
util.LogWarning(err)
2016-01-05 00:32:53 +01:00
}
2016-05-17 17:18:26 +02:00
func getSystemInfo(w http.ResponseWriter, r *http.Request) {
body := map[string]interface{}{
"version": util.Version,
"update": util.UpdateAvailable,
"config": map[string]string{
"dbHost": util.Config.MySQL.Hostname,
"dbName": util.Config.MySQL.DbName,
"dbUser": util.Config.MySQL.Username,
"path": util.Config.TmpPath,
"cmdPath": util.FindSemaphore(),
},
}
2016-05-17 17:18:26 +02:00
if util.UpdateAvailable != nil {
body["updateBody"] = string(blackfriday.MarkdownCommon([]byte(*util.UpdateAvailable.Body)))
}
2016-05-17 17:18:26 +02:00
util.WriteJSON(w, http.StatusOK, body)
2016-05-17 17:18:26 +02:00
}
func checkUpgrade(w http.ResponseWriter, r *http.Request) {
if err := util.CheckUpdate(util.Version); err != nil {
util.WriteJSON(w, 500, err)
return
}
2016-05-17 17:18:26 +02:00
if util.UpdateAvailable != nil {
getSystemInfo(w, r)
return
}
2016-05-17 17:18:26 +02:00
w.WriteHeader(http.StatusNoContent)
2016-05-17 17:18:26 +02:00
}
func doUpgrade(w http.ResponseWriter, r *http.Request) {
util.LogError(util.DoUpgrade(util.Version))
2016-05-17 17:18:26 +02:00
}