@@ -10,7 +10,6 @@ import (
10
10
"github.com/google/uuid"
11
11
"github.com/kataras/iris/v12"
12
12
"github.com/kataras/iris/v12/context"
13
- "k8s.io/client-go/rest"
14
13
"k8s.io/client-go/tools/clientcmd"
15
14
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
16
15
)
@@ -31,15 +30,15 @@ func NewHandler() *Handler {
31
30
32
31
func (h * Handler ) GetConfigFile () iris.Handler {
33
32
return func (ctx * context.Context ) {
34
- sessionId := ctx .URLParam ("session " )
33
+ sessionId := ctx .URLParam ("token " )
35
34
requireLen := len (uuid .New ().String ())
36
35
if len (sessionId ) != requireLen {
37
36
ctx .StatusCode (iris .StatusBadRequest )
38
37
ctx .Values ().Set ("message" , fmt .Sprintf ("sessionId length must be %d" , requireLen ))
39
38
return
40
39
}
41
- cfg := h .sessionCache .Get (sessionId )
42
- if cfg != nil {
40
+ sess := h .sessionCache .Get (sessionId )
41
+ if sess != nil {
43
42
h .sessionCache .Delete (sessionId )
44
43
} else {
45
44
ctx .StatusCode (iris .StatusInternalServerError )
@@ -51,7 +50,7 @@ func (h *Handler) GetConfigFile() iris.Handler {
51
50
ctx .Header ("Content-Disposition" , "attachment;filename=config" )
52
51
ctx .Header ("Content-Transfer-Encoding" , "binary" )
53
52
54
- cc := toCmdConfig (* cfg . config )
53
+ cc := toCmdConfig (sess )
55
54
bs , err := clientcmd .Write (* cc )
56
55
if err != nil {
57
56
ctx .StatusCode (iris .StatusInternalServerError )
@@ -62,20 +61,23 @@ func (h *Handler) GetConfigFile() iris.Handler {
62
61
}
63
62
}
64
63
65
- func toCmdConfig (rc rest. Config ) * clientcmdapi.Config {
64
+ func toCmdConfig (sess * Session ) * clientcmdapi.Config {
66
65
cc := clientcmdapi .NewConfig ()
67
- cc .Clusters ["default" ] = & clientcmdapi.Cluster {
68
- Server : rc .Host ,
66
+ cc .Clusters [sess .Cluster ] = & clientcmdapi.Cluster {
67
+ Server : sess .config .Host ,
68
+ InsecureSkipTLSVerify : true ,
69
69
}
70
- cc .AuthInfos ["default" ] = & clientcmdapi.AuthInfo {
71
- ClientCertificateData : rc . CAData ,
72
- ClientKeyData : rc .KeyData ,
73
- Token : rc .BearerToken ,
70
+ cc .AuthInfos [sess . User ] = & clientcmdapi.AuthInfo {
71
+ ClientCertificateData : sess . config . CertData ,
72
+ ClientKeyData : sess . config .KeyData ,
73
+ Token : sess . config .BearerToken ,
74
74
}
75
- cc .Contexts ["default" ] = & clientcmdapi.Context {
76
- Cluster : "default" ,
77
- AuthInfo : "default" ,
75
+ contextName := fmt .Sprintf ("%s@%s" , sess .Cluster , sess .User )
76
+ cc .Contexts [contextName ] = & clientcmdapi.Context {
77
+ Cluster : sess .Cluster ,
78
+ AuthInfo : sess .User ,
78
79
}
80
+ cc .CurrentContext = contextName
79
81
return cc
80
82
}
81
83
@@ -113,15 +115,15 @@ func (h *Handler) CreateSession() iris.Handler {
113
115
cfg .CertData = rb .Certificate
114
116
}
115
117
sess .config = cfg
118
+ sess .User = profile .Name
116
119
sessionId := uuid .New ().String ()
117
120
h .sessionCache .Put (sessionId , & sess )
118
- ctx .Values ().Set ("data" , & SessionResponse {SessionId : sessionId })
121
+ ctx .Values ().Set ("data" , & SessionResponse {Token : sessionId })
119
122
}
120
123
}
121
124
122
- func Install (parent iris.Party ) {
125
+ func Install (authParent , noAuthParty iris.Party ) {
123
126
handler := NewHandler ()
124
- sp := parent .Party ("/webkubectl" )
125
- sp .Get ("/session" , handler .GetConfigFile ())
126
- sp .Post ("/session" , handler .CreateSession ())
127
+ authParent .Post ("/webkubectl/session" , handler .CreateSession ())
128
+ noAuthParty .Get ("/webkubectl/session" , handler .GetConfigFile ())
127
129
}
0 commit comments