|
| 1 | +package kiali |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + |
| 12 | + internalk8s "github.com/containers/kubernetes-mcp-server/pkg/kubernetes" |
| 13 | + "k8s.io/klog/v2" |
| 14 | +) |
| 15 | + |
| 16 | +type Kiali struct { |
| 17 | + manager *Manager |
| 18 | +} |
| 19 | + |
| 20 | +func (m *Manager) GetKiali() *Kiali { |
| 21 | + return &Kiali{manager: m} |
| 22 | +} |
| 23 | + |
| 24 | +func (k *Kiali) GetKiali() *Kiali { |
| 25 | + return k |
| 26 | +} |
| 27 | + |
| 28 | +// validateAndGetURL validates the Kiali client configuration and returns the full URL |
| 29 | +// by safely concatenating the base URL with the provided endpoint, avoiding duplicate |
| 30 | +// or missing slashes regardless of trailing/leading slashes. |
| 31 | +func (k *Kiali) validateAndGetURL(endpoint string) (string, error) { |
| 32 | + if k == nil || k.manager == nil || k.manager.KialiURL == "" { |
| 33 | + return "", fmt.Errorf("kiali client not initialized") |
| 34 | + } |
| 35 | + baseStr := strings.TrimSpace(k.manager.KialiURL) |
| 36 | + if baseStr == "" { |
| 37 | + return "", fmt.Errorf("kiali server URL not configured") |
| 38 | + } |
| 39 | + baseURL, err := url.Parse(baseStr) |
| 40 | + if err != nil { |
| 41 | + return "", fmt.Errorf("invalid kiali base URL: %w", err) |
| 42 | + } |
| 43 | + if endpoint == "" { |
| 44 | + return baseURL.String(), nil |
| 45 | + } |
| 46 | + ref, err := url.Parse(endpoint) |
| 47 | + if err != nil { |
| 48 | + return "", fmt.Errorf("invalid endpoint path: %w", err) |
| 49 | + } |
| 50 | + return baseURL.ResolveReference(ref).String(), nil |
| 51 | +} |
| 52 | + |
| 53 | +func (k *Kiali) createHTTPClient() *http.Client { |
| 54 | + return &http.Client{ |
| 55 | + Transport: &http.Transport{ |
| 56 | + TLSClientConfig: &tls.Config{ |
| 57 | + InsecureSkipVerify: k.manager.KialiInsecure, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// CurrentAuthorizationHeader returns the Authorization header value that the |
| 64 | +// Kiali client is currently configured to use (Bearer <token>), or empty |
| 65 | +// if no bearer token is configured. |
| 66 | +func (k *Kiali) CurrentAuthorizationHeader(ctx context.Context) string { |
| 67 | + token, _ := ctx.Value(internalk8s.OAuthAuthorizationHeader).(string) |
| 68 | + token = strings.TrimSpace(token) |
| 69 | + |
| 70 | + if token == "" { |
| 71 | + // Fall back to using the same token that the Kubernetes client is using |
| 72 | + if k == nil || k.manager == nil || k.manager.BearerToken == "" { |
| 73 | + return "" |
| 74 | + } |
| 75 | + token = strings.TrimSpace(k.manager.BearerToken) |
| 76 | + if token == "" { |
| 77 | + return "" |
| 78 | + } |
| 79 | + } |
| 80 | + // Normalize to exactly "Bearer <token>" without double prefix |
| 81 | + lower := strings.ToLower(token) |
| 82 | + if strings.HasPrefix(lower, "bearer ") { |
| 83 | + return "Bearer " + strings.TrimSpace(token[7:]) |
| 84 | + } |
| 85 | + return "Bearer " + token |
| 86 | +} |
| 87 | + |
| 88 | +// executeRequest executes an HTTP request and handles common error scenarios. |
| 89 | +func (k *Kiali) executeRequest(ctx context.Context, endpoint string) (string, error) { |
| 90 | + ApiCallURL, err := k.validateAndGetURL(endpoint) |
| 91 | + if err != nil { |
| 92 | + return "", err |
| 93 | + } |
| 94 | + |
| 95 | + klog.V(0).Infof("Kiali Call URL: %s", ApiCallURL) |
| 96 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, ApiCallURL, nil) |
| 97 | + if err != nil { |
| 98 | + return "", err |
| 99 | + } |
| 100 | + authHeader := k.CurrentAuthorizationHeader(ctx) |
| 101 | + if authHeader != "" { |
| 102 | + req.Header.Set("Authorization", authHeader) |
| 103 | + } |
| 104 | + client := k.createHTTPClient() |
| 105 | + resp, err := client.Do(req) |
| 106 | + if err != nil { |
| 107 | + return "", err |
| 108 | + } |
| 109 | + defer func() { _ = resp.Body.Close() }() |
| 110 | + body, _ := io.ReadAll(resp.Body) |
| 111 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 112 | + if len(body) > 0 { |
| 113 | + return "", fmt.Errorf("kiali API error: %s", strings.TrimSpace(string(body))) |
| 114 | + } |
| 115 | + return "", fmt.Errorf("kiali API error: status %d", resp.StatusCode) |
| 116 | + } |
| 117 | + return string(body), nil |
| 118 | +} |
0 commit comments