-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
260 lines (225 loc) · 7.53 KB
/
auth.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package routers
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
b64 "encoding/base64"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/kubernetes"
clientcmd "k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// SetupKubeconfig godoc
// @Summary setup kubeconfig authentication
// @Description setup kubernetes client
func setupKubeconfigFromFile() *kubernetes.Clientset {
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
catchError(err)
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
catchError(err)
return clientset
}
// postAuthConfig godoc
func postAuthConfig(authConfig *AuthConfig) (*version.Info, *AuthError) {
var (
serverName = authConfig.ServerName
server = authConfig.Server
token = authConfig.Token
serverCADataString = authConfig.ServerCADataString
clientset = &kubernetes.Clientset{}
authError = &AuthError{}
serverVersion = &version.Info{}
)
kubeconfigPath := homeDir() + "/.kubestorm/" + hash(server)
serverHashPath := homeDir() + "/.kubestorm/" + serverName
if isFileExist(kubeconfigPath) && !isFileExist(serverHashPath) {
authError.Message = fmt.Sprintf("%v already exists", server)
authError.Code = 409
authError.Error = errors.New("")
} else {
if serverName != "" && server != "" && token != "" && serverCADataString != "" {
serverCAData, _ := b64.StdEncoding.DecodeString(serverCADataString)
kubeconfig := &clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
serverName: {Token: token}},
Clusters: map[string]*clientcmdapi.Cluster{
serverName: {Server: server, CertificateAuthorityData: serverCAData, InsecureSkipTLSVerify: false}},
Contexts: map[string]*clientcmdapi.Context{
serverName: {AuthInfo: serverName, Cluster: serverName}},
CurrentContext: serverName,
}
clientcmd.WriteToFile(*kubeconfig, "/tmp/"+serverName+"-kubeconfig")
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags(server, "/tmp/"+serverName+"-kubeconfig")
if err != nil {
authError.Message = "Not able to create clientcmdapi config"
authError.Error = err
authError.Code = 500
return serverVersion, authError
}
// create the clientset
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
authError.Message = "Not able to create clientset"
authError.Error = err
authError.Code = 500
return serverVersion, authError
}
// test the clientset
serverVersion, err = clientset.ServerVersion()
if err != nil {
authError.Message = "Not able to retrieve server version. Something went wrong!"
authError.Error = err
authError.Code = 401
return serverVersion, authError
}
clientcmd.WriteToFile(*kubeconfig, kubeconfigPath)
if err := writeToFile([]byte(hash(server)), serverHashPath); err != nil {
panic(err.Error())
}
} else {
authError.Message = "Invalid Request"
authError.Code = 400
authError.Error = errors.New("Missing parameter")
}
}
return serverVersion, authError
}
// getAuthConfig
func getAuthConfig(serverName string) (*AuthConfig, *AuthError) {
var (
authError = &AuthError{}
authConfig = &AuthConfig{}
)
hashID, err := getHashIDFromServerName(serverName)
if err != nil {
authError.Message = fmt.Sprintf("Not able to get config file for %v", serverName)
authError.Code = 400
authError.Error = err
return authConfig, authError
}
kubeconfigPath := getKubeconfigPathFromHash(hashID)
kubeconfig, err := clientcmd.LoadFromFile(kubeconfigPath)
if err != nil {
authError.Message = fmt.Sprintf("Not able to load kubeconfig for %v", serverName)
authError.Code = 400
authError.Error = err
return authConfig, authError
}
authConfig.Server = kubeconfig.Clusters[serverName].Server
authConfig.ServerName = serverName
authConfig.Token = kubeconfig.AuthInfos[serverName].Token
authConfig.ServerCADataString = b64.StdEncoding.EncodeToString(kubeconfig.Clusters[serverName].CertificateAuthorityData)
return authConfig, authError
}
// deleteAuthConfig
func deleteAuthConfig(serverName string) *AuthError {
var (
authError = &AuthError{}
)
serverHashPath := homeDir() + "/.kubestorm/" + serverName
if _, err := os.Stat(serverHashPath); err == nil {
hashID, err := getHashIDFromServerName(serverName)
if err != nil {
authError.Message = fmt.Sprintf("Not able to read %v", serverHashPath)
authError.Code = 400
authError.Error = err
return authError
}
kubeconfigPath := getKubeconfigPathFromHash(hashID)
err = os.Remove(kubeconfigPath)
if err != nil {
authError.Message = fmt.Sprintf("Not able to remove %v", kubeconfigPath)
authError.Code = 400
authError.Error = err
return authError
}
err = os.Remove(serverHashPath)
if err != nil {
authError.Message = fmt.Sprintf("Not able to remove %v", serverHashPath)
authError.Code = 400
authError.Error = err
return authError
}
} else {
authError.Message = fmt.Sprintf("%v does not exists!", serverHashPath)
authError.Code = 404
authError.Error = err
}
return authError
}
// getHashIDFromServerName godoc
func getHashIDFromServerName(serverName string) ([]byte, error) {
return ioutil.ReadFile(homeDir() + "/.kubestorm/" + serverName)
}
// getKubeconfigPathFromHash godoc
func getKubeconfigPathFromHash(hashID []byte) string {
return homeDir() + "/.kubestorm/" + string(hashID)
}
// getClientSet godoc
func getClientSet(serverName string) (*kubernetes.Clientset, *AuthError) {
var (
authError = &AuthError{}
clientset = &kubernetes.Clientset{}
)
hashID, err := getHashIDFromServerName(serverName)
if err != nil {
authError.Message = fmt.Sprintf("Not able to get config for %v", serverName)
authError.Code = 400
authError.Error = err
return clientset, authError
}
kubeconfigPath := getKubeconfigPathFromHash(hashID)
kubeconfig, err := clientcmd.LoadFromFile(kubeconfigPath)
if err != nil {
authError.Message = "Not able to load config from file"
authError.Error = err
authError.Code = 500
return clientset, authError
}
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags(kubeconfig.Clusters[serverName].Server, kubeconfigPath)
if err != nil {
authError.Message = "Not able to create clientcmdapi config"
authError.Error = err
authError.Code = 500
return clientset, authError
}
// create the clientset
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
authError.Message = fmt.Sprintf("Not able to create clientset for %v", serverName)
authError.Code = 400
authError.Error = err
}
return clientset, authError
}
// generateKubeconfigAwsCli godoc
func generateKubeconfigAwsCli(clusterName string, roleArn string, awsRegion string) {
// Split the entire command up using ' ' as the delimeter
parts := strings.Split("aws eks --region "+awsRegion+" update-kubeconfig --name "+strings.TrimSpace(clusterName)+" --role-arn "+strings.TrimSpace(roleArn), " ")
bin := parts[0]
args := parts[1:len(parts)]
cmd := exec.Command(bin, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
}