Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: es v7 (#14373)

ecmadao4472c81b6e40
diff --git a/backend/plugin/db/elasticsearch/elasticsearch.go b/backend/plugin/db/elasticsearch/elasticsearch.go
index 120e7af33a..e962529e5c 100644
--- a/backend/plugin/db/elasticsearch/elasticsearch.go
+++ b/backend/plugin/db/elasticsearch/elasticsearch.go
@@ -11,10 +11,11 @@ import (
"fmt"
"io"
"net/http"
+ "net/url"
"strings"
"time"
- "github.com/elastic/go-elasticsearch/v8"
+ "github.com/elastic/go-elasticsearch/v7"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/durationpb"
@@ -36,7 +37,7 @@ var (
)
type Driver struct {
- typedClient *elasticsearch.TypedClient
+ typedClient *elasticsearch.Client
basicAuthClient *BasicAuthClient
config db.ConnectionConfig
}
@@ -73,51 +74,62 @@ func (scheduler *AddressScheduler) GetNewAddress() string {
}
func (*Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionConfig) (db.Driver, error) {
- // addresses.
- addresses := []string{}
- protocol := "http"
- if config.TLSConfig.SslCert != "" {
- protocol = "https"
+ addresse := fmt.Sprintf("%s:%s", config.Host, config.Port)
+ u, err := url.Parse(addresse)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to parse addresse: %v", addresse)
}
- if len(config.MultiHosts) == 0 {
- addresses = append(addresses, fmt.Sprintf("%s://%s:%s", protocol, config.Host, config.Port))
- } else {
- for idx := range config.MultiHosts {
- addresses = append(addresses, fmt.Sprintf("%s://%s:%s", protocol, config.MultiHosts[idx], config.MultiPorts[idx]))
+ if u.Scheme == "" {
+ protocol := "http"
+ if config.TLSConfig.UseSSL {
+ protocol = "https"
}
+ addresse = fmt.Sprintf("%s://%s", protocol, addresse)
}
esConfig := elasticsearch.Config{
Username: config.Username,
Password: config.Password,
- Addresses: addresses,
- }
- if protocol == "https" {
- esConfig.CACert = []byte(config.TLSConfig.SslCert)
+ Addresses: []string{addresse},
+ Transport: &http.Transport{
+ MaxIdleConnsPerHost: 10,
+ ResponseHeaderTimeout: time.Second,
+ TLSClientConfig: &tls.Config{
+ MinVersion: tls.VersionTLS12,
+ InsecureSkipVerify: true,
+ },
+ },
}
-
- // typed elasticsearch client.
- typedClient, err := elasticsearch.NewTypedClient(esConfig)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to create elasticsearch client")
+ // default http client.
+ httpClient := &http.Client{
+ Transport: &http.Transport{
+ TLSClientConfig: &tls.Config{
+ MinVersion: tls.VersionTLS12,
+ InsecureSkipVerify: true,
+ },
+ },
}
- // default http client.
- var httpClient *http.Client
if config.TLSConfig.SslCert != "" {
certPool := x509.NewCertPool()
if ok := certPool.AppendCertsFromPEM([]byte(config.TLSConfig.SslCert)); !ok {
return nil, errors.New("cannot add CA cert to pool")
}
- httpClient = &http.Client{
- Transport: &http.Transport{
- TLSClientConfig: &tls.Config{
- RootCAs: certPool,
- },
+ esConfig.CACert = []byte(config.TLSConfig.SslCert)
+ esConfig.Transport = &http.Transport{
+ MaxIdleConnsPerHost: 10,
+ ResponseHeaderTimeout: time.Second,
+ TLSClientConfig: &tls.Config{
+ MinVersion: tls.VersionTLS12,
},
}
- } else {
- httpClient = http.DefaultClient
+ httpClient.Transport = esConfig.Transport
+ }
+
+ // typed elasticsearch client.
+ typedClient, err := elasticsearch.NewClient(esConfig)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to create elasticsearch client")
}
// generate basic authentication string for http client.
@@ -129,7 +141,7 @@ func (*Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionCon
basicAuthClient: &BasicAuthClient{
httpClient: httpClient,
addrScheduler: &AddressScheduler{
- addresses: addresses,
+ addresses: []string{addresse},
count: 0,
},
basicAuthString: basicAuthString,
@@ -143,9 +155,8 @@ func (*Driver) Close(_ context.Context) error {
return nil
}
-func (d *Driver) Ping(ctx context.Context) error {
- _, err := d.typedClient.Ping().Do(ctx)
- if err != nil {
+func (d *Driver) Ping(_ context.Context) error {
+ if _, err := d.typedClient.Ping(); err != nil {
return errors.Wrapf(err, "failed to ping db")
}
return nil
diff --git a/backend/plugin/db/elasticsearch/role.go b/backend/plugin/db/elasticsearch/role.go
index 61fbb4ae24..738e3ae5ee 100644
--- a/backend/plugin/db/elasticsearch/role.go
+++ b/backend/plugin/db/elasticsearch/role.go
@@ -4,11 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
+ "log/slog"
"net/http"
"strings"
- "github.com/elastic/go-elasticsearch/v8/esapi"
+ "github.com/elastic/go-elasticsearch/v7/esapi"
+ "github.com/bytebase/bytebase/backend/common/log"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -27,12 +29,13 @@ func (d *Driver) getInstanceRoles() ([]*storepb.InstanceRole, error) {
return nil, err
}
+ var instanceRoles []*storepb.InstanceRole
results := map[string]UserResult{}
if err := json.Unmarshal(bytes, &results); err != nil {
- return nil, err
+ slog.Error("failed list es roles", log.BBError(err))
+ return instanceRoles, nil
}
- var instanceRoles []*storepb.InstanceRole
for name, userResult := range results {
privileges, err := d.getUserPrivileges(name)
if err != nil {
diff --git a/backend/plugin/db/elasticsearch/sync.go b/backend/plugin/db/elasticsearch/sync.go
index 1b447c8f62..9f86cd30bb 100644
--- a/backend/plugin/db/elasticsearch/sync.go
+++ b/backend/plugin/db/elasticsearch/sync.go
@@ -11,7 +11,7 @@ import (
"net/http"
"time"
- "github.com/elastic/go-elasticsearch/v8/esapi"
+ "github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/db"
diff --git a/go.mod b/go.mod
index eb5c3b4b79..ad9a8b17f1 100644
--- a/go.mod
+++ b/go.mod
@@ -33,6 +33,7 @@ require (
github.com/cockroachdb/cockroachdb-parser v0.23.2
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/databricks/databricks-sdk-go v0.50.0
+ github.com/elastic/go-elasticsearch/v7 v7.17.10
github.com/epiclabs-io/diff3 v0.0.0-20240325112732-ba77e92bf0e4
github.com/github/gh-ost v1.1.6
github.com/go-ego/gse v0.80.3
@@ -140,7 +141,6 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.7.0 // indirect
- github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/getsentry/sentry-go v0.12.0 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect
@@ -255,7 +255,6 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
- github.com/elastic/go-elasticsearch/v8 v8.15.0
github.com/envoyproxy/go-control-plane v0.13.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
diff --git a/go.sum b/go.sum
index 9294fe95f7..bb2544a8ff 100644
--- a/go.sum
+++ b/go.sum
@@ -974,10 +974,8 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
-github.com/elastic/elastic-transport-go/v8 v8.6.0 h1:Y2S/FBjx1LlCv5m6pWAF2kDJAHoSjSRSJCApolgfthA=
-github.com/elastic/elastic-transport-go/v8 v8.6.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk=
-github.com/elastic/go-elasticsearch/v8 v8.15.0 h1:IZyJhe7t7WI3NEFdcHnf6IJXqpRf+8S8QWLtZYYyBYk=
-github.com/elastic/go-elasticsearch/v8 v8.15.0/go.mod h1:HCON3zj4btpqs2N1jjsAy4a/fiAul+YBP00mBH4xik8=
+github.com/elastic/go-elasticsearch/v7 v7.17.10 h1:TCQ8i4PmIJuBunvBS6bwT2ybzVFxxUhhltAs3Gyu1yo=
+github.com/elastic/go-elasticsearch/v7 v7.17.10/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=