(w http.ResponseWriter, r *http.Request)
| 268 | } |
| 269 | |
| 270 | func (server *Server) handleKubeConfigApi(w http.ResponseWriter, r *http.Request) { |
| 271 | result := ApiResponse{ |
| 272 | Success: false, |
| 273 | } |
| 274 | w.Header().Set("Content-Type", "application/json;charset=utf-8") |
| 275 | if !strings.EqualFold(r.Method, "POST") { |
| 276 | result.Message = "Method Not Allowed" |
| 277 | json.NewEncoder(w).Encode(result) |
| 278 | w.WriteHeader(405) |
| 279 | return |
| 280 | } |
| 281 | body, err := io.ReadAll(r.Body) |
| 282 | if err != nil { |
| 283 | fmt.Printf("read body err, %v\n", err) |
| 284 | result.Message = "Invalid Request Body" |
| 285 | json.NewEncoder(w).Encode(result) |
| 286 | w.WriteHeader(406) |
| 287 | return |
| 288 | } |
| 289 | var request KubeConfigRequest |
| 290 | if err = json.Unmarshal(body, &request); err != nil { |
| 291 | fmt.Printf("Unmarshal err, %v\n", err) |
| 292 | result.Message = "Invalid Request Body" |
| 293 | json.NewEncoder(w).Encode(result) |
| 294 | w.WriteHeader(406) |
| 295 | return |
| 296 | } |
| 297 | if len(request.KubeConfig) < 10 { |
| 298 | result.Message = "Invalid Kube Config" |
| 299 | json.NewEncoder(w).Encode(result) |
| 300 | w.WriteHeader(406) |
| 301 | return |
| 302 | } |
| 303 | if _, base64err := base64.StdEncoding.DecodeString(request.KubeConfig); base64err != nil { |
| 304 | result.Message = "Invalid Base64 Content" |
| 305 | json.NewEncoder(w).Encode(result) |
| 306 | w.WriteHeader(406) |
| 307 | return |
| 308 | } |
| 309 | |
| 310 | //fmt.Printf("%+v", requst) |
| 311 | token := randomstring.Generate(20) |
| 312 | ttyParameter := cache.TtyParameter{ |
| 313 | Title: request.Name, |
| 314 | Arg: strings.Replace(request.KubeConfig, " ", "", -1), |
| 315 | } |
| 316 | if err := server.cache.Add(token, &ttyParameter, time.Duration(server.options.TokenExpiresDuration)*time.Second); err != nil { |
| 317 | log.Printf("save token and ttyParam err:%s", err.Error()) |
| 318 | result.Success = false |
| 319 | result.Message = err.Error() |
| 320 | json.NewEncoder(w).Encode(result) |
| 321 | return |
| 322 | } |
| 323 | result.Success = true |
| 324 | result.Token = token |
| 325 | json.NewEncoder(w).Encode(result) |
| 326 | } |
| 327 |
nothing calls this directly
no test coverage detected