-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyserver.go
338 lines (284 loc) · 9.22 KB
/
keyserver.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2017 Tanck. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"net/http"
"fmt"
"os"
"time"
"net"
"log"
"io/ioutil"
"crypto/tls"
"strings"
"encoding/json"
"github.com/spf13/pflag"
"github.com/bitly/go-simplejson"
)
var (
argKeyStone = pflag.IP("keystone-server", net.IPv4(192, 168, 17, 39), "The keystone server address.")
argAuthPort = pflag.Int("auth-port", 35357, "The authentication port.")
argBindAddress = pflag.IP("bind-address", net.IPv4(0, 0, 0, 0), "The address auth server bind to.")
argListenPort = pflag.Int("listen-port", 5012, "The listen port of auth server.")
argIngress = pflag.Bool("user-ingress", false, "Expose ingress or not.")
argDNS = pflag.String("dns-host", "", "DNS host.")
argDBImg = pflag.String("db-img", "", "dashboard images.")
argCharServer = pflag.String("helm-chart-server", "", "helm-chart-server address")
)
const SERVICE_NAME = "dashboard-larryck"
type TreeSubitem struct {
Name string `json:"name"`
State string `json:"state"`
}
type TreeItem struct {
State string `json:"state"`
Item []TreeSubitem `json:"item"`
}
var (
K8S_ADMIN_TREE = map[string]TreeItem{
"Cluster":{"cluster", []TreeSubitem{{"Namespaces", "namespace.list"}, {"Nodes", "node.list"}, {"Persistent Volumes", "persistentvolume.list"}, {"Roles", "role.list"}, {"Storage Classes", "storageclass.list"}}},
"Namespace":{},
"Workloads":{"workload", []TreeSubitem{{"Daemon Sets", "daemonset.list"}, {"Deployments", "deployment.list"}, {"Jobs", "job.list"}, {"Pods", "pod.list"}, {"Replica Sets", "replicaset.list"}, {"Replication Controllers", "replicationcontroller.list"}, {"Stateful Sets", "statefulset.list"}}},
"Discovery and Load Balancing":{"discovery", []TreeSubitem{{"Ingresses", "ingress.list"}, {"Services", "service.list"}}},
"Config and Storage":{"config", []TreeSubitem{{"Config Maps", "configmap.list"}, {"Persistent Volume Claims", "persistentvolumeclaim.list"}, {"Secrets", "secret.list"}}},
"About":{},
"APP Store":{"appStore", []TreeSubitem{}},
"repositories":{"repositories", []TreeSubitem{}},
"release":{"release", []TreeSubitem{}},
"home":{"home", []TreeSubitem{}},}
K8S_USER_TREE = map[string]TreeItem{
"Workloads":{"workload", []TreeSubitem{{"Deployments", "deployment.list"}, {"Pods", "pod.list"}, {"Replica Sets", "replicaset.list"}}},
"Discovery and Load Balancing":{"discovery", []TreeSubitem{{"Services", "service.list"}}},
"Config and Storage":{"config", []TreeSubitem{{"Config Maps", "configmap.list"}, {"Secrets", "secret.list"}}},
"APP Store":{"appStore", []TreeSubitem{}},
"repositories":{"repositories", []TreeSubitem{}},
"release":{"release", []TreeSubitem{}},
"home":{"home", []TreeSubitem{}},}
)
func buildTree(role string) map[string]TreeItem {
switch(role) {
case "k8s-user":
return K8S_USER_TREE
case "k8s-admin":
return K8S_ADMIN_TREE
}
return nil
}
func auth(proj string, user string, passwd string) (role string, id string) {
keystoneUrl := fmt.Sprintf("https://%s:%d/v2.0/tokens", *argKeyStone, *argAuthPort)
header := "application/json"
data := fmt.Sprintf("{\"auth\": {\"tenantName\":\"%s\", \"passwordCredentials\": {\"username\": \"%s\",\"password\": \"%s\"}}}", proj, user, passwd)
fmt.Println(keystoneUrl)
fmt.Println(data)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Post(keystoneUrl, header, strings.NewReader(data))
if err != nil {
fmt.Println(err)
return "", ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil || resp.StatusCode!=http.StatusOK {
fmt.Println(err)
fmt.Println(string(body))
return "", ""
}
fmt.Println(string(body))
//extract role from json with bitly go-simplejson
js,err := simplejson.NewJson(body)
if err!=nil {
fmt.Println(err)
return "", ""
}
user_id := js.Get("access").Get("user").Get("id").MustString()
roles,_ := js.Get("access").Get("user").Get("roles").Array()
if len(roles)==0 {
return "", ""
}
roleMap := roles[0].(map[string]interface {})
for _,v := range roleMap {
fmt.Println(v.(string))
return v.(string), user_id
}
return "", ""
}
type KeyserverResp struct {
Status string `json:"status"`
Tree map[string]TreeItem `json:"tree"`
URL string `json:"url"`
}
func pushTree(tree map[string]TreeItem, url string, w http.ResponseWriter) {
ksr := &KeyserverResp {"OK", tree, url}
jn,_ := json.Marshal(ksr)
fmt.Println(string(jn))
w.Write(jn)
}
type LogoutResp struct {
Status string
}
func handleLogout(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// cross domain post
w.Header().Set("Access-Control-Allow-Origin", "*")
loR := LogoutResp{"Error"}
name := r.Form["name"][0]
namespace := r.Form["namespace"][0]
if !(len(name)>0 && len(namespace)>0) {
// return 403 error
w.WriteHeader(403)
fmt.Println("Get Name/Namespace error")
jn,_ := json.Marshal(loR)
w.Write(jn)
return
}
err := DeleteDashBoard(*argIngress, name, namespace)
if err!=nil {
w.WriteHeader(403)
fmt.Println(err)
jn,_ := json.Marshal(loR)
w.Write(jn)
return
}
loR.Status = "OK"
jn,_ := json.Marshal(loR)
w.Write(jn)
}
func handleAuth(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// cross domain post
w.Header().Set("Access-Control-Allow-Origin", "*")
ksr := KeyserverResp {"ERROR", nil, ""}
proj := r.PostFormValue("project")
user := r.PostFormValue("userName")
passwd := r.PostFormValue("userPassword")
if !(len(proj)>0 && len(user)>0 && len(passwd)>0) {
// return 403 error
w.WriteHeader(403)
jn,_ := json.Marshal(ksr)
w.Write(jn)
return
}
role, id := auth(proj, user, passwd)
if len(role)==0 {
// return 401, unauthorized
w.WriteHeader(401)
jn,_ := json.Marshal(ksr)
w.Write(jn)
return
}
tree := buildTree(role)
if tree==nil {
// return 401, unauthorized
w.WriteHeader(401)
fmt.Fprintf(w, "Unauthorized\n")
jn,_ := json.Marshal(ksr)
w.Write(jn)
return
}
js_tree,_ := json.Marshal(tree)
// must have DBImg
if len(*argDBImg)==0 || *argIngress==true && len(*argDNS)==0 {
// return 403, unauthorized
w.WriteHeader(403)
fmt.Fprintf(w, "Parameters error\n")
jn,_ := json.Marshal(ksr)
w.Write(jn)
return
}
// deploy dashboard and service
ip, err := DeployDashboard(*argCharServer, *argDBImg, *argIngress, *argDNS, SERVICE_NAME, id, "authedDashboard", user, role, string(js_tree))
if err!= nil {
// return 403 error
w.WriteHeader(403)
jn,_ := json.Marshal(ksr)
w.Write(jn)
return
}
url := genURL(ip, id)
// add request service check
serviceAvailableCheck(*argIngress, url)
fmt.Println("Dashboard service available")
pushTree(tree, url, w)
}
func serviceAvailableCheck(use_ingress bool, url string) {
for i:=0; i<10; i++ {
ch := make(chan bool)
go dialService(use_ingress, url, ch)
if status:= <- ch; status {
return
}
}
}
func dialService(use_ingress bool, url string, chChan chan bool) {
c := &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 1 * time.Second,
}).Dial,
DisableKeepAlives: true,
},
}
resp, err := c.Get(url)
if err != nil {
fmt.Println(err)
chChan <- false
}else {
if use_ingress {
_, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
chChan <- false
} else if resp.StatusCode!=200 {
fmt.Println(resp.StatusCode)
time.Sleep(1 * time.Second)
chChan <- false
}else {
fmt.Println(resp.StatusCode)
fmt.Println("Get good request")
resp.Body.Close()
chChan <- true
}
}else{
fmt.Println("Get good request")
resp.Body.Close()
chChan <- true
}
}
}
func genURL(ip string, namespace string) string {
return "http://"+ip+"/#!/deployment?namespace="+namespace
}
func CreateAuthHandler() http.Handler {
return http.HandlerFunc(handleAuth)
}
func LogoutHandler() http.Handler {
return http.HandlerFunc(handleLogout)
}
func main() {
// Set logging output to standard console out
log.SetOutput(os.Stdout)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
log.Printf("Listen on port %d", *argListenPort)
log.Printf("Using authentication address: https://%s:%d", *argKeyStone, *argAuthPort)
http.Handle("/auth", CreateAuthHandler())
http.Handle("/logout", LogoutHandler())
addr := fmt.Sprintf("%s:%d", *argBindAddress, *argListenPort)
go log.Fatal(http.ListenAndServe(addr, nil))
select {}
}