Skip to content

Commit c7611c5

Browse files
committed
feat: refactor OpenSearch integration with new connection model
1 parent 031f1c1 commit c7611c5

6 files changed

Lines changed: 73 additions & 28 deletions

File tree

plugins/compliance-orchestrator/client/backend.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ func (c *BackendClient) GetRequest(ctx context.Context, url string) ([]byte, err
123123
}
124124

125125
func (b *BackendClient) IndexEvaluationResult(ctx context.Context, index string, doc any) error {
126-
baseURL := plugins.PluginCfg("org.opensearch", false).Get("opensearch").String()
127-
endpoint := fmt.Sprintf("%s/%s/_doc", baseURL, index)
126+
osCfg := LoadOpenSearchConfig()
127+
client := NewOpenSearchHTTPClient()
128+
129+
endpoint := fmt.Sprintf("%s/%s/_doc", osCfg.URL, index)
128130

129131
jsonBody, err := json.Marshal(doc)
130132
if err != nil {
@@ -137,15 +139,17 @@ func (b *BackendClient) IndexEvaluationResult(ctx context.Context, index string,
137139
}
138140

139141
req.Header.Set("Content-Type", "application/json")
142+
req.SetBasicAuth(osCfg.User, osCfg.Pass)
140143

141-
resp, err := b.httpClient.Do(req)
144+
resp, err := client.Do(req)
142145
if err != nil {
143146
return fmt.Errorf("index request failed: %w", err)
144147
}
145148
defer resp.Body.Close()
146149

147150
if resp.StatusCode >= 300 {
148-
return fmt.Errorf("indexing failed with status %d", resp.StatusCode)
151+
body, _ := io.ReadAll(resp.Body)
152+
return fmt.Errorf("indexing failed with status %d: %s", resp.StatusCode, string(body))
149153
}
150154

151155
return nil

plugins/compliance-orchestrator/client/opensearch.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,32 @@ import (
99

1010
"github.com/threatwinds/go-sdk/catcher"
1111
sdkos "github.com/threatwinds/go-sdk/os"
12-
"github.com/threatwinds/go-sdk/plugins"
1312
)
1413

1514
func ConnectOpenSearch() error {
16-
osUrl := plugins.PluginCfg("org.opensearch", false).Get("opensearch").String()
15+
osCfg := LoadOpenSearchConfig()
1716

18-
err := sdkos.Connect([]string{osUrl}, "", "")
17+
err := sdkos.Connect([]string{osCfg.URL}, osCfg.User, osCfg.Pass)
1918
if err != nil {
2019
return catcher.Error("failed to connect to OpenSearch", err, map[string]any{
21-
"url": osUrl,
20+
"url": osCfg.URL,
21+
"user": osCfg.User,
2222
})
2323
}
2424

2525
catcher.Info("Connected to OpenSearch", map[string]any{
26-
"url": osUrl,
26+
"url": osCfg.URL,
2727
})
28-
2928
return nil
3029
}
3130

32-
type SQLResponse struct {
33-
Schema []any `json:"schema"`
34-
DataRows [][]any `json:"datarows"`
35-
Total int `json:"total"`
36-
}
37-
3831
func (b *BackendClient) ExecuteSQLQuery(ctx context.Context, sql string) (SQLResult, error) {
39-
baseURL := plugins.PluginCfg("org.opensearch", false).Get("opensearch").String()
40-
sqlEndpoint := fmt.Sprintf("%s/_plugins/_sql", baseURL)
32+
osCfg := LoadOpenSearchConfig()
33+
client := NewOpenSearchHTTPClient()
4134

42-
body := map[string]string{
43-
"query": sql,
44-
}
35+
sqlEndpoint := fmt.Sprintf("%s/_plugins/_sql", osCfg.URL)
4536

37+
body := map[string]string{"query": sql}
4638
jsonBody, err := json.Marshal(body)
4739
if err != nil {
4840
return SQLResult{}, fmt.Errorf("failed to marshal SQL body: %w", err)
@@ -54,8 +46,9 @@ func (b *BackendClient) ExecuteSQLQuery(ctx context.Context, sql string) (SQLRes
5446
}
5547

5648
req.Header.Set("Content-Type", "application/json")
49+
req.SetBasicAuth(osCfg.User, osCfg.Pass)
5750

58-
resp, err := b.httpClient.Do(req)
51+
resp, err := client.Do(req)
5952
if err != nil {
6053
return SQLResult{}, fmt.Errorf("SQL request failed: %w", err)
6154
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package client
2+
3+
import (
4+
"crypto/tls"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/threatwinds/go-sdk/plugins"
9+
)
10+
11+
type OpenSearchConfig struct {
12+
Host string
13+
Port string
14+
User string
15+
Pass string
16+
URL string
17+
}
18+
19+
func LoadOpenSearchConfig() OpenSearchConfig {
20+
cfg := plugins.PluginCfg("org.opensearch", false).Get("opensearch")
21+
22+
host := cfg.Get("host").String()
23+
port := cfg.Get("port").String()
24+
user := cfg.Get("user").String()
25+
pass := cfg.Get("password").String()
26+
27+
return OpenSearchConfig{
28+
Host: host,
29+
Port: port,
30+
User: user,
31+
Pass: pass,
32+
URL: fmt.Sprintf("https://%s:%s", host, port),
33+
}
34+
}
35+
36+
func NewOpenSearchHTTPClient() *http.Client {
37+
return &http.Client{
38+
Transport: &http.Transport{
39+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
40+
},
41+
}
42+
}

plugins/compliance-orchestrator/client/opensearch_result.go

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package client
2+
3+
type SQLResult struct {
4+
Rows [][]any
5+
Count int
6+
}
7+
8+
type SQLResponse struct {
9+
Schema []any `json:"schema"`
10+
DataRows [][]any `json:"datarows"`
11+
Total int `json:"total"`
12+
}

plugins/compliance-orchestrator/models/report_config.go renamed to plugins/compliance-orchestrator/models/control_config.go

File renamed without changes.

0 commit comments

Comments
 (0)