-
Notifications
You must be signed in to change notification settings - Fork 359
/
k3s_connect.go
66 lines (58 loc) · 1.62 KB
/
k3s_connect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package filters
import (
"net/http"
"net/url"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/loft-sh/vcluster/pkg/util/websocketproxy"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/client-go/transport"
)
const (
K3sConnectPath = "/v1-k3s/connect"
)
func WithK3sConnect(h http.Handler) http.Handler {
serverURL := &url.URL{
Scheme: "wss",
Host: "127.0.0.1:6443",
}
transportCfg := &transport.Config{
TLS: transport.TLSConfig{
CAFile: "/data/agent/server-ca.crt",
CertFile: "/data/agent/client-kubelet.crt",
KeyFile: "/data/agent/client-kubelet.key",
},
}
tlsCfg, err := transport.TLSConfigFor(transportCfg)
if err != nil {
// ignore if errors if certificate files are not present
// this is most likely different flavor, not k3s
return h
}
proxy := websocketproxy.NewProxy(serverURL)
proxy.Dialer = &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
TLSClientConfig: tlsCfg,
}
proxy.Backend = func(r *http.Request) *url.URL {
u := *serverURL
u.Path = K3sConnectPath
return &u
}
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if strings.HasSuffix(req.URL.Path, K3sConnectPath) {
// check implementation is based on
// https://github.com/k3s-io/k3s/blob/a5414bb1fc904e3d44a6a66ccffa819340e712a8/pkg/daemons/control/tunnel.go#L47-L58
user, ok := request.UserFrom(req.Context())
if !ok || !strings.HasPrefix(user.GetName(), "system:node:") {
http.Error(w, "Access denied", http.StatusForbidden)
return
}
proxy.ServeHTTP(w, req)
return
}
h.ServeHTTP(w, req)
})
}