-
Notifications
You must be signed in to change notification settings - Fork 315
/
webkubectl.go
131 lines (121 loc) · 3.95 KB
/
webkubectl.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package webkubectl
import (
"encoding/pem"
"fmt"
"github.com/KubeOperator/kubepi/internal/api/v1/session"
"github.com/KubeOperator/kubepi/internal/service/v1/cluster"
"github.com/KubeOperator/kubepi/internal/service/v1/clusterbinding"
"github.com/KubeOperator/kubepi/internal/service/v1/common"
"github.com/KubeOperator/kubepi/pkg/kubernetes"
"github.com/google/uuid"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
type Handler struct {
clusterBindingService clusterbinding.Service
clusterService cluster.Service
sessionCache *TerminalSessions
}
func NewHandler() *Handler {
return &Handler{
clusterBindingService: clusterbinding.NewService(),
clusterService: cluster.NewService(),
sessionCache: NewTerminalSessions(),
}
}
func (h *Handler) GetConfigFile() iris.Handler {
return func(ctx *context.Context) {
sessionId := ctx.URLParam("token")
requireLen := len(uuid.New().String())
if len(sessionId) != requireLen {
ctx.StatusCode(iris.StatusBadRequest)
ctx.Values().Set("message", fmt.Sprintf("sessionId length must be %d", requireLen))
return
}
sess := h.sessionCache.Get(sessionId)
if sess != nil {
h.sessionCache.Delete(sessionId)
} else {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", fmt.Sprintf("can not find sessionId: %s in memory", sessionId))
return
}
ctx.Header("Content-Type", "application/download")
ctx.Header("Content-Disposition", "attachment;filename=config")
ctx.Header("Content-Transfer-Encoding", "binary")
cc := toCmdConfig(sess)
bs, err := clientcmd.Write(*cc)
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", "can not generate config file")
return
}
_, _ = ctx.Write(bs)
}
}
func toCmdConfig(sess *Session) *clientcmdapi.Config {
cc := clientcmdapi.NewConfig()
cc.Clusters[sess.Cluster] = &clientcmdapi.Cluster{
Server: sess.config.Host,
InsecureSkipTLSVerify: true,
}
cc.AuthInfos[sess.User] = &clientcmdapi.AuthInfo{
ClientCertificateData: sess.config.CertData,
ClientKeyData: sess.config.KeyData,
Token: sess.config.BearerToken,
}
contextName := fmt.Sprintf("%s@%s", sess.Cluster, sess.User)
cc.Contexts[contextName] = &clientcmdapi.Context{
Cluster: sess.Cluster,
AuthInfo: sess.User,
}
cc.CurrentContext = contextName
return cc
}
func (h *Handler) CreateSession() iris.Handler {
return func(ctx *context.Context) {
var sess Session
if err := ctx.ReadJSON(&sess); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.Values().Set("message", err.Error())
return
}
u := ctx.Values().Get("profile")
profile := u.(session.UserProfile)
c, err := h.clusterService.Get(sess.Cluster, common.DBOptions{})
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err.Error())
return
}
k := kubernetes.NewKubernetes(c)
cfg, err := k.Config()
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err.Error())
return
}
if !profile.IsAdministrator {
rb, err := h.clusterBindingService.GetBindingByClusterNameAndUserName(sess.Cluster, profile.Name, common.DBOptions{})
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Values().Set("message", err.Error())
return
}
cfg.CertData = rb.Certificate
cfg.KeyData =pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: c.PrivateKey})
}
sess.config = cfg
sess.User = profile.Name
sessionId := uuid.New().String()
h.sessionCache.Put(sessionId, &sess)
ctx.Values().Set("data", &SessionResponse{Token: sessionId})
}
}
func Install(authParent, noAuthParty iris.Party) {
handler := NewHandler()
authParent.Post("/webkubectl/session", handler.CreateSession())
noAuthParty.Get("/webkubectl/session", handler.GetConfigFile())
}