local-server/cmd/dkl-local-server/ws.go

159 lines
4.1 KiB
Go
Raw Normal View History

2019-02-01 07:28:08 +00:00
package main
import (
"fmt"
2019-02-01 07:28:08 +00:00
"log"
"net"
2019-02-01 07:35:50 +00:00
"net/http"
2019-02-01 07:28:08 +00:00
"strings"
"text/template"
2019-02-01 07:28:08 +00:00
"github.com/emicklei/go-restful"
2019-02-04 04:06:02 +00:00
"novit.nc/direktil/local-server/pkg/mime"
2019-02-04 02:56:43 +00:00
"novit.nc/direktil/pkg/localconfig"
2019-02-01 07:28:08 +00:00
)
2019-04-15 17:56:31 +00:00
func registerWS(rest *restful.Container) {
2019-04-19 16:07:22 +00:00
// Admin-level APIs
ws := &restful.WebService{}
2019-04-15 17:56:31 +00:00
ws.Filter(adminAuth).
HeaderParameter("Authorization", "Admin bearer token")
2019-02-01 07:28:08 +00:00
2019-04-15 17:56:31 +00:00
// - configs API
ws.Route(ws.POST("/configs").To(wsUploadConfig).
2019-02-04 02:56:43 +00:00
Doc("Upload a new current configuration, archiving the previous one"))
2019-04-15 17:56:31 +00:00
// - clusters API
ws.Route(ws.GET("/clusters").To(wsListClusters).
2019-02-04 02:56:43 +00:00
Doc("List clusters"))
2019-02-04 04:46:03 +00:00
2019-04-15 17:56:31 +00:00
ws.Route(ws.GET("/clusters/{cluster-name}").To(wsCluster).
2019-02-04 02:56:43 +00:00
Doc("Get cluster details"))
2019-02-04 04:46:03 +00:00
2019-04-15 17:56:31 +00:00
ws.Route(ws.GET("/clusters/{cluster-name}/addons").To(wsClusterAddons).
2019-02-04 04:06:02 +00:00
Produces(mime.YAML).
2019-02-04 02:56:43 +00:00
Doc("Get cluster addons").
Returns(http.StatusOK, "OK", nil).
Returns(http.StatusNotFound, "The cluster does not exists or does not have addons defined", nil))
2019-10-09 06:45:19 +00:00
ws.Route(ws.GET("/clusters/{cluster-name}/bootstrap-pods").To(wsClusterBootstrapPods).
Produces(mime.YAML).
Doc("Get cluster bootstrap pods YAML definitions").
Returns(http.StatusOK, "OK", nil).
Returns(http.StatusNotFound, "The cluster does not exists or does not have bootstrap pods defined", nil))
2019-04-15 17:56:31 +00:00
ws.Route(ws.GET("/clusters/{cluster-name}/passwords").To(wsClusterPasswords).
2019-04-13 09:36:58 +00:00
Doc("List cluster's passwords"))
2019-04-15 17:56:31 +00:00
ws.Route(ws.GET("/clusters/{cluster-name}/passwords/{password-name}").To(wsClusterPassword).
2019-04-13 09:36:58 +00:00
Doc("Get cluster's password"))
2019-04-15 17:56:31 +00:00
ws.Route(ws.PUT("/clusters/{cluster-name}/passwords/{password-name}").To(wsClusterSetPassword).
2019-04-13 09:36:58 +00:00
Doc("Set cluster's password"))
2020-04-22 15:36:04 +00:00
ws.Route(ws.GET("/clusters/{cluster-name}/ca/{ca-name}/certificate").To(wsClusterCACert).
Produces(mime.CACERT).
Doc("Get cluster CA's certificate"))
ws.Route(ws.GET("/clusters/{cluster-name}/ca/{ca-name}/signed").To(wsClusterSignedCert).
Produces(mime.CERT).
Param(ws.QueryParameter("name", "signed reference name").Required(true)).
Doc("Get cluster's certificate signed by the CA"))
2019-12-16 07:00:57 +00:00
ws.Route(ws.GET("/clusters/{cluster-name}/tokens/{token-name}").To(wsClusterToken).
Doc("Get cluster's token"))
ws.Route(ws.GET("/hosts").To(wsListHosts).
Doc("List hosts"))
2019-04-15 17:56:31 +00:00
(&wsHost{
prefix: "/hosts/{host-name}",
hostDoc: "given host",
getHost: func(req *restful.Request) string {
return req.PathParameter("host-name")
},
}).register(ws, func(rb *restful.RouteBuilder) {
2019-04-15 17:56:31 +00:00
})
rest.Add(ws)
2019-04-15 17:56:31 +00:00
// Hosts API
ws = &restful.WebService{}
ws.Path("/me")
2019-04-15 17:56:31 +00:00
ws.Filter(hostsAuth).
HeaderParameter("Authorization", "Host or admin bearer token")
2019-02-01 07:28:08 +00:00
(&wsHost{
2019-02-04 04:46:03 +00:00
hostDoc: "detected host",
2019-02-01 07:28:08 +00:00
getHost: detectHost,
2019-02-04 02:56:43 +00:00
}).register(ws, func(rb *restful.RouteBuilder) {
rb.Notes("In this case, the host is detected from the remote IP")
})
2019-02-01 07:28:08 +00:00
rest.Add(ws)
2019-02-01 07:28:08 +00:00
}
func detectHost(req *restful.Request) string {
r := req.Request
remoteAddr := r.RemoteAddr
if *trustXFF {
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
remoteAddr = strings.Split(xff, ",")[0]
}
}
hostIP, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
hostIP = remoteAddr
}
cfg, err := readConfig()
if err != nil {
return ""
}
host := cfg.HostByIP(hostIP)
if host == nil {
log.Print("no host found for IP ", hostIP)
return ""
}
return host.Name
}
2019-02-04 02:56:43 +00:00
func wsReadConfig(resp *restful.Response) *localconfig.Config {
cfg, err := readConfig()
2019-02-01 07:35:50 +00:00
if err != nil {
2019-02-04 02:56:43 +00:00
log.Print("failed to read config: ", err)
resp.WriteErrorString(http.StatusServiceUnavailable, "failed to read config")
return nil
2019-02-01 07:35:50 +00:00
}
2019-02-04 02:56:43 +00:00
return cfg
}
2019-02-01 07:35:50 +00:00
2019-02-04 02:56:43 +00:00
func wsNotFound(req *restful.Request, resp *restful.Response) {
http.NotFound(resp.ResponseWriter, req.Request)
}
2019-02-01 07:35:50 +00:00
2019-02-04 02:56:43 +00:00
func wsError(resp *restful.Response, err error) {
log.Output(2, fmt.Sprint("request failed: ", err))
2019-02-04 02:56:43 +00:00
resp.WriteErrorString(
http.StatusInternalServerError,
http.StatusText(http.StatusInternalServerError))
2019-02-01 07:28:08 +00:00
}
func wsRender(resp *restful.Response, tmplStr string, value interface{}) {
tmpl, err := template.New("wsRender").Funcs(templateFuncs).Parse(tmplStr)
if err != nil {
wsError(resp, err)
return
}
err = tmpl.Execute(resp, value)
if err != nil {
wsError(resp, err)
return
}
}