mirror of
https://github.com/gravitational/teleport.git
synced 2026-10-12 02:15:01 +00:00
Remove github.com/elastic/go-elasticsearch dependency (#55319)
The library was largely used for tests which were easily updated to leverage the http library directly. The only other use of the library was for `types.ErrorClause` which was removed in favor of vendoring the subset of the type needed by the elastic engine.
This commit is contained in:
@@ -101,7 +101,6 @@ require (
|
||||
github.com/docker/cli v27.5.0+incompatible
|
||||
github.com/docker/docker v28.1.1+incompatible
|
||||
github.com/dustin/go-humanize v1.0.1
|
||||
github.com/elastic/go-elasticsearch/v8 v8.18.0
|
||||
github.com/elimity-com/scim v0.0.0-20240320110924-172bf2aee9c8
|
||||
github.com/envoyproxy/go-control-plane/envoy v1.32.4
|
||||
github.com/evanphx/json-patch v5.9.11+incompatible
|
||||
@@ -348,7 +347,6 @@ require (
|
||||
github.com/docker/go-units v0.5.0 // indirect
|
||||
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
|
||||
github.com/ebitengine/purego v0.8.3 // indirect
|
||||
github.com/elastic/elastic-transport-go/v8 v8.7.0 // indirect
|
||||
github.com/emicklei/go-restful/v3 v3.11.3 // indirect
|
||||
github.com/envoyproxy/protoc-gen-validate v1.2.1 // indirect
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
||||
|
||||
@@ -1184,10 +1184,6 @@ github.com/dvsekhvalnov/jose2go v1.6.0 h1:Y9gnSnP4qEI0+/uQkHvFXeD2PLPJeXEL+ySMEA
|
||||
github.com/dvsekhvalnov/jose2go v1.6.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
|
||||
github.com/ebitengine/purego v0.8.3 h1:K+0AjQp63JEZTEMZiwsI9g0+hAMNohwUOtY0RPGexmc=
|
||||
github.com/ebitengine/purego v0.8.3/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||
github.com/elastic/elastic-transport-go/v8 v8.7.0 h1:OgTneVuXP2uip4BA658Xi6Hfw+PeIOod2rY3GVMGoVE=
|
||||
github.com/elastic/elastic-transport-go/v8 v8.7.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk=
|
||||
github.com/elastic/go-elasticsearch/v8 v8.18.0 h1:ANNq1h7DEiPUaALb8+5w3baQzaS08WfHV0DNzp0VG4M=
|
||||
github.com/elastic/go-elasticsearch/v8 v8.18.0/go.mod h1:WLqwXsJmQoYkoA9JBFeEwPkQhCfAZuUvfpdU/NvSSf0=
|
||||
github.com/elimity-com/scim v0.0.0-20240320110924-172bf2aee9c8 h1:0+BTyxIYgiVAry/P5s8R4dYuLkhB9Nhso8ogFWNr4IQ=
|
||||
github.com/elimity-com/scim v0.0.0-20240320110924-172bf2aee9c8/go.mod h1:JkjcmqbLW+khwt2fmBPJFBhx2zGZ8XobRZ+O0VhlwWo=
|
||||
github.com/emicklei/go-restful/v3 v3.11.3 h1:yagOQz/38xJmcNeZJtrUcKjkHRltIaIFXKWeG1SkWGE=
|
||||
|
||||
@@ -449,10 +449,6 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/ebitengine/purego v0.8.3 h1:K+0AjQp63JEZTEMZiwsI9g0+hAMNohwUOtY0RPGexmc=
|
||||
github.com/ebitengine/purego v0.8.3/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||
github.com/elastic/elastic-transport-go/v8 v8.7.0 h1:OgTneVuXP2uip4BA658Xi6Hfw+PeIOod2rY3GVMGoVE=
|
||||
github.com/elastic/elastic-transport-go/v8 v8.7.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk=
|
||||
github.com/elastic/go-elasticsearch/v8 v8.18.0 h1:ANNq1h7DEiPUaALb8+5w3baQzaS08WfHV0DNzp0VG4M=
|
||||
github.com/elastic/go-elasticsearch/v8 v8.18.0/go.mod h1:WLqwXsJmQoYkoA9JBFeEwPkQhCfAZuUvfpdU/NvSSf0=
|
||||
github.com/elimity-com/scim v0.0.0-20240320110924-172bf2aee9c8 h1:0+BTyxIYgiVAry/P5s8R4dYuLkhB9Nhso8ogFWNr4IQ=
|
||||
github.com/elimity-com/scim v0.0.0-20240320110924-172bf2aee9c8/go.mod h1:JkjcmqbLW+khwt2fmBPJFBhx2zGZ8XobRZ+O0VhlwWo=
|
||||
github.com/emicklei/go-restful/v3 v3.11.3 h1:yagOQz/38xJmcNeZJtrUcKjkHRltIaIFXKWeG1SkWGE=
|
||||
|
||||
@@ -35,7 +35,6 @@ import (
|
||||
|
||||
"github.com/ClickHouse/ch-go"
|
||||
cqlclient "github.com/datastax/go-cassandra-native-protocol/client"
|
||||
elastic "github.com/elastic/go-elasticsearch/v8"
|
||||
mysqlclient "github.com/go-mysql-org/go-mysql/client"
|
||||
mysqllib "github.com/go-mysql-org/go-mysql/mysql"
|
||||
"github.com/google/uuid"
|
||||
@@ -2069,7 +2068,7 @@ func (c *testContext) snowflakeClient(ctx context.Context, teleportUser, dbServi
|
||||
}
|
||||
|
||||
// elasticsearchClient returns an Elasticsearch test DB client.
|
||||
func (c *testContext) elasticsearchClient(ctx context.Context, teleportUser, dbService, dbUser string) (*elastic.Client, *alpnproxy.LocalProxy, error) {
|
||||
func (c *testContext) elasticsearchClient(ctx context.Context, teleportUser, dbService, dbUser string) (*elasticsearch.TestClient, *alpnproxy.LocalProxy, error) {
|
||||
route := tlsca.RouteToDatabase{
|
||||
ServiceName: dbService,
|
||||
Protocol: defaults.ProtocolElasticsearch,
|
||||
|
||||
@@ -31,7 +31,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
elastic "github.com/elastic/go-elasticsearch/v8/typedapi/types"
|
||||
"github.com/gravitational/trace"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
@@ -74,8 +73,18 @@ func (e *Engine) SendError(err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// ErrorCause type.
|
||||
//
|
||||
// https://github.com/elastic/elasticsearch-specification/blob/f6a370d0fba975752c644fc730f7c45610e28f36/specification/_types/Errors.ts#L25-L50
|
||||
type ErrorCause struct {
|
||||
// Reason A human-readable explanation of the error, in English.
|
||||
Reason *string `json:"reason,omitempty"`
|
||||
// Type The type of error
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
reason := err.Error()
|
||||
cause := elastic.ErrorCause{
|
||||
cause := ErrorCause{
|
||||
Reason: &reason,
|
||||
Type: "internal_server_error_exception",
|
||||
}
|
||||
|
||||
@@ -24,9 +24,10 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/elastic/go-elasticsearch/v8"
|
||||
"github.com/gravitational/trace"
|
||||
|
||||
"github.com/gravitational/teleport/lib/srv/db/common"
|
||||
@@ -114,18 +115,44 @@ func (s *TestServer) Close() error {
|
||||
return s.listener.Close()
|
||||
}
|
||||
|
||||
// MakeTestClient returns Redis client connection according to the provided
|
||||
// parameters.
|
||||
func MakeTestClient(_ context.Context, config common.TestClientConfig) (*elasticsearch.Client, error) {
|
||||
es, err := elasticsearch.NewClient(elasticsearch.Config{
|
||||
Addresses: []string{"http://" + config.Address},
|
||||
})
|
||||
type TestClient struct {
|
||||
addr string
|
||||
}
|
||||
|
||||
func (t TestClient) Ping() (*http.Response, error) {
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: t.addr,
|
||||
Path: "/",
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodHead, u.String(), http.NoBody)
|
||||
if err != nil {
|
||||
return nil, trace.Wrap(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return es, nil
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
func (t TestClient) Query(q string) (*http.Response, error) {
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: t.addr,
|
||||
Path: "/_sql",
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, u.String(), strings.NewReader(q))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return http.DefaultClient.Do(req)
|
||||
}
|
||||
|
||||
// MakeTestClient returns Redis client connection according to the provided
|
||||
// parameters.
|
||||
func MakeTestClient(_ context.Context, config common.TestClientConfig) (*TestClient, error) {
|
||||
return &TestClient{
|
||||
addr: config.Address,
|
||||
}, nil
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -21,10 +21,12 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/gravitational/teleport/api/types"
|
||||
@@ -48,6 +50,7 @@ func newTestElasticsearchEngine(ec common.EngineConfig) common.Engine {
|
||||
}
|
||||
|
||||
func TestAccessElasticsearch(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
testCtx := setupTestContext(ctx, t, withElasticsearch("Elasticsearch"))
|
||||
go testCtx.startHandlingConnections()
|
||||
@@ -116,26 +119,27 @@ func TestAccessElasticsearch(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Execute a query.
|
||||
result, err := dbConn.SQL.Query(strings.NewReader(`{ "query": "SELECT 42" }`))
|
||||
result, err := dbConn.Query(`{ "query": "SELECT 42" }`)
|
||||
require.NoError(t, err)
|
||||
|
||||
if test.err {
|
||||
t.Logf("result: %v", result)
|
||||
require.True(t, result.IsError())
|
||||
require.Equal(t, 401, result.StatusCode)
|
||||
assert.NoError(t, result.Body.Close())
|
||||
require.Equal(t, http.StatusUnauthorized, result.StatusCode)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.False(t, result.IsError())
|
||||
require.False(t, result.HasWarnings())
|
||||
require.Equal(t, `[200 OK] {"columns":[{"name":"42","type":"integer"}],"rows":[[42]]}`, result.String())
|
||||
|
||||
require.NoError(t, result.Body.Close())
|
||||
out, err := io.ReadAll(result.Body)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, result.Body.Close())
|
||||
require.Equal(t, http.StatusOK, result.StatusCode)
|
||||
require.Equal(t, `{"columns":[{"name":"42","type":"integer"}],"rows":[[42]]}`, string(out))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditElasticsearch(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
testCtx := setupTestContext(ctx, t, withElasticsearch("Elasticsearch"))
|
||||
go testCtx.startHandlingConnections()
|
||||
@@ -148,9 +152,9 @@ func TestAuditElasticsearch(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := dbConn.Ping()
|
||||
|
||||
require.NoError(t, err)
|
||||
require.True(t, resp.IsError())
|
||||
assert.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||
|
||||
waitForEvent(t, testCtx, libevents.DatabaseSessionStartFailureCode)
|
||||
proxy.Close()
|
||||
@@ -169,15 +173,19 @@ func TestAuditElasticsearch(t *testing.T) {
|
||||
// Connect should trigger successful session start event.
|
||||
resp, err := dbConn.Ping()
|
||||
require.NoError(t, err)
|
||||
require.False(t, resp.IsError())
|
||||
assert.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
waitForEvent(t, testCtx, libevents.DatabaseSessionStartCode)
|
||||
})
|
||||
|
||||
t.Run("command sends", func(t *testing.T) {
|
||||
// should trigger Query event.
|
||||
result, err := dbConn.SQL.Query(strings.NewReader(`{ "query": "SELECT 42" }`))
|
||||
result, err := dbConn.Query(`{ "query": "SELECT 42" }`)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, `[200 OK] {"columns":[{"name":"42","type":"integer"}],"rows":[[42]]}`, result.String())
|
||||
out, err := io.ReadAll(result.Body)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, result.Body.Close())
|
||||
assert.Equal(t, `{"columns":[{"name":"42","type":"integer"}],"rows":[[42]]}`, string(out))
|
||||
|
||||
_ = waitForEvent(t, testCtx, libevents.ElasticsearchRequestCode)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user