|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/threatwinds/go-sdk/catcher" |
| 13 | + "github.com/threatwinds/go-sdk/plugins" |
| 14 | + "github.com/utmstack/UTMStack/plugins/compliance-orchestrator/models" |
| 15 | +) |
| 16 | + |
| 17 | +type BackendClient struct { |
| 18 | + baseURL string |
| 19 | + internalKey string |
| 20 | + httpClient *http.Client |
| 21 | +} |
| 22 | + |
| 23 | +func NewBackendClient() *BackendClient { |
| 24 | + raw := plugins.PluginCfg("com.utmstack", false).Get("backend").String() |
| 25 | + |
| 26 | + if !strings.HasPrefix(raw, "http://") && !strings.HasPrefix(raw, "https://") { |
| 27 | + raw = "http://" + raw |
| 28 | + } |
| 29 | + |
| 30 | + return &BackendClient{ |
| 31 | + baseURL: raw, |
| 32 | + internalKey: plugins.PluginCfg("com.utmstack", false).Get("internalKey").String(), |
| 33 | + httpClient: &http.Client{ |
| 34 | + Timeout: 30 * time.Second, |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (c *BackendClient) HealthCheck(ctx context.Context) error { |
| 40 | + url := fmt.Sprintf("%s/api/ping", c.baseURL) |
| 41 | + |
| 42 | + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 43 | + if err != nil { |
| 44 | + return catcher.Error("failed to create backend ping request", err, nil) |
| 45 | + } |
| 46 | + |
| 47 | + resp, err := c.httpClient.Do(req) |
| 48 | + if err != nil { |
| 49 | + return catcher.Error("backend ping request failed", err, nil) |
| 50 | + } |
| 51 | + |
| 52 | + defer func(Body io.ReadCloser) { |
| 53 | + err := Body.Close() |
| 54 | + if err != nil { |
| 55 | + catcher.Error("failed to close backend ping response body", err, nil) |
| 56 | + } |
| 57 | + }(resp.Body) |
| 58 | + |
| 59 | + if resp.StatusCode != http.StatusOK { |
| 60 | + return catcher.Error("backend ping returned non-200", nil, map[string]any{ |
| 61 | + "status": resp.StatusCode, |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func (c *BackendClient) GetReportConfigs(ctx context.Context) ([]models.ReportConfig, error) { |
| 69 | + url := fmt.Sprintf("%s/api/compliance/report-config?page=0&size=1000&sort=id,asc", c.baseURL) |
| 70 | + var reports []models.ReportConfig |
| 71 | + |
| 72 | + var body, err = c.GetRequest(ctx, url) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + if err := json.Unmarshal(body, &reports); err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + return reports, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (c *BackendClient) GetActiveIndexPatterns(ctx context.Context) ([]models.IndexPattern, error) { |
| 84 | + url := fmt.Sprintf("%s/api/utm-index-patterns?page=0&size=1000&sort=id,asc&isActive.equals=true", c.baseURL) |
| 85 | + var activeIndex []models.IndexPattern |
| 86 | + |
| 87 | + var body, err = c.GetRequest(ctx, url) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + if err := json.Unmarshal(body, &activeIndex); err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + return activeIndex, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (c *BackendClient) GetRequest(ctx context.Context, url string) ([]byte, error) { |
| 99 | + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + req.Header.Set("utm-internal-key", c.internalKey) |
| 105 | + |
| 106 | + resp, err := c.httpClient.Do(req) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + defer resp.Body.Close() |
| 111 | + |
| 112 | + if resp.StatusCode != http.StatusOK { |
| 113 | + return nil, fmt.Errorf("backend returned %d", resp.StatusCode) |
| 114 | + } |
| 115 | + |
| 116 | + body, err := io.ReadAll(resp.Body) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + return body, nil |
| 122 | +} |
0 commit comments