k3s/pkg/server/router.go

113 lines
3.1 KiB
Go
Raw Normal View History

2019-01-01 08:23:01 +00:00
package server
import (
"net/http"
2019-01-26 04:56:12 +00:00
"strconv"
2019-01-01 08:23:01 +00:00
"github.com/gorilla/mux"
2019-01-09 16:54:15 +00:00
"github.com/rancher/k3s/pkg/daemons/config"
2019-01-26 04:56:12 +00:00
"github.com/rancher/k3s/pkg/openapi"
2019-01-09 16:54:15 +00:00
"k8s.io/apimachinery/pkg/util/json"
2019-01-01 08:23:01 +00:00
)
2019-01-26 04:56:12 +00:00
const (
jsonMediaType = "application/json"
binaryMediaType = "application/octet-stream"
pbMediaType = "application/com.github.proto-openapi.spec.v2@v1.0+protobuf"
openapiPrefix = "openapi."
)
2019-01-01 08:23:01 +00:00
type CACertsGetter func() (string, error)
func router(serverConfig *config.Control, tunnel http.Handler, cacertsGetter CACertsGetter) http.Handler {
authed := mux.NewRouter()
authed.Use(authMiddleware(serverConfig))
authed.NotFoundHandler = serverConfig.Runtime.Handler
authed.Path("/v1-k3s/connect").Handler(tunnel)
authed.Path("/v1-k3s/node.crt").Handler(nodeCrt(serverConfig))
authed.Path("/v1-k3s/node.key").Handler(nodeKey(serverConfig))
authed.Path("/v1-k3s/config").Handler(configHandler(serverConfig))
router := mux.NewRouter()
router.NotFoundHandler = authed
router.Path("/cacerts").Handler(cacerts(cacertsGetter))
2019-01-26 04:56:12 +00:00
router.Path("/openapi/v2").Handler(serveOpenapi())
2019-01-31 22:42:27 +00:00
router.Path("/ping").Handler(ping())
2019-01-01 08:23:01 +00:00
return router
}
func cacerts(getter CACertsGetter) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
content, err := getter()
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
resp.Write([]byte(err.Error()))
}
resp.Header().Set("content-type", "text/plain")
resp.Write([]byte(content))
})
}
func nodeCrt(server *config.Control) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if req.TLS == nil {
resp.WriteHeader(http.StatusNotFound)
return
}
http.ServeFile(resp, req, server.Runtime.NodeCert)
})
}
func nodeKey(server *config.Control) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if req.TLS == nil {
resp.WriteHeader(http.StatusNotFound)
return
}
http.ServeFile(resp, req, server.Runtime.NodeKey)
})
}
func configHandler(server *config.Control) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if req.TLS == nil {
resp.WriteHeader(http.StatusNotFound)
return
}
resp.Header().Set("content-type", "application/json")
json.NewEncoder(resp).Encode(server)
})
}
2019-01-26 04:56:12 +00:00
func serveOpenapi() http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
suffix := "json"
contentType := jsonMediaType
if req.Header.Get("Accept") == pbMediaType {
suffix = "pb"
contentType = binaryMediaType
}
data, err := openapi.Asset(openapiPrefix + suffix)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
resp.Write([]byte(err.Error()))
return
}
resp.Header().Set("Content-Type", contentType)
resp.Header().Set("Content-Length", strconv.Itoa(len(data)))
resp.Write(data)
})
}
2019-01-31 22:42:27 +00:00
func ping() http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
data := []byte("pong")
resp.Header().Set("Content-Type", "text/plain")
resp.Header().Set("Content-Length", strconv.Itoa(len(data)))
resp.Write(data)
})
}