From 49f8f03a71cd1521b54db25aac74489c275af17d Mon Sep 17 00:00:00 2001 From: Jason Lui Date: Sat, 8 Mar 2025 00:50:22 +0800 Subject: [PATCH] feat: pass dynamic credential provider to transport --- _benchmarks/benchmarks/go.mod | 2 +- _examples/bulk/kafka/go.mod | 3 ++- _examples/bulk/kafka/go.sum | 1 + _examples/cloudfunction/go.mod | 2 +- _examples/encoding/go.mod | 2 +- _examples/extension/go.mod | 2 +- _examples/fasthttp/go.mod | 2 +- _examples/instrumentation/go.mod | 2 +- _examples/logging/go.mod | 2 +- _examples/security/go.mod | 2 +- _examples/xkcdsearch/go.mod | 2 +- elasticsearch.go | 32 +++++++++++++++++++------------- esapi/test/go.mod | 2 +- 13 files changed, 32 insertions(+), 24 deletions(-) diff --git a/_benchmarks/benchmarks/go.mod b/_benchmarks/benchmarks/go.mod index d87265c368..352f2487d9 100644 --- a/_benchmarks/benchmarks/go.mod +++ b/_benchmarks/benchmarks/go.mod @@ -7,7 +7,7 @@ toolchain go1.22.0 replace github.com/elastic/go-elasticsearch/v8 => ../../ require ( - github.com/elastic/elastic-transport-go/v8 v8.6.0 + github.com/elastic/elastic-transport-go/v8 v8.6.1 github.com/elastic/go-elasticsearch/v8 v8.0.0-20200408073057-6f36a473b19f github.com/fatih/color v1.7.0 github.com/montanaflynn/stats v0.6.3 diff --git a/_examples/bulk/kafka/go.mod b/_examples/bulk/kafka/go.mod index ecc89764a2..d4e9494411 100644 --- a/_examples/bulk/kafka/go.mod +++ b/_examples/bulk/kafka/go.mod @@ -1,6 +1,7 @@ module github.com/elastic/go-elasticsearch/v8/_examples/bulk/kafka go 1.22 + toolchain go1.22.0 replace github.com/elastic/go-elasticsearch/v8 => ../../.. @@ -13,7 +14,7 @@ require ( require ( github.com/armon/go-radix v1.0.0 // indirect - github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect + github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect github.com/elastic/go-licenser v0.3.1 // indirect github.com/elastic/go-sysinfo v1.1.1 // indirect github.com/elastic/go-windows v1.0.0 // indirect diff --git a/_examples/bulk/kafka/go.sum b/_examples/bulk/kafka/go.sum index f1be7e8b0d..0b5f141175 100644 --- a/_examples/bulk/kafka/go.sum +++ b/_examples/bulk/kafka/go.sum @@ -7,6 +7,7 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 h1:YEetp8 github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= 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/elastic-transport-go/v8 v8.6.1/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk= github.com/elastic/go-licenser v0.3.1 h1:RmRukU/JUmts+rpexAw0Fvt2ly7VVu6mw8z4HrEzObU= github.com/elastic/go-licenser v0.3.1/go.mod h1:D8eNQk70FOCVBl3smCGQt/lv7meBeQno2eI1S5apiHQ= github.com/elastic/go-sysinfo v1.1.1 h1:ZVlaLDyhVkDfjwPGU55CQRCRolNpc7P0BbyhhQZQmMI= diff --git a/_examples/cloudfunction/go.mod b/_examples/cloudfunction/go.mod index afe8b83b38..279db98966 100644 --- a/_examples/cloudfunction/go.mod +++ b/_examples/cloudfunction/go.mod @@ -9,7 +9,7 @@ replace github.com/elastic/go-elasticsearch/v8 => ../.. require github.com/elastic/go-elasticsearch/v8 v8.0.0-20210817150010-57d659deaca7 require ( - github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect + github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect go.opentelemetry.io/otel v1.28.0 // indirect diff --git a/_examples/encoding/go.mod b/_examples/encoding/go.mod index 9d49969481..e819475cda 100644 --- a/_examples/encoding/go.mod +++ b/_examples/encoding/go.mod @@ -14,7 +14,7 @@ require ( ) require ( - github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect + github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/josharian/intern v1.0.0 // indirect diff --git a/_examples/extension/go.mod b/_examples/extension/go.mod index a709f366f8..120abf10af 100644 --- a/_examples/extension/go.mod +++ b/_examples/extension/go.mod @@ -7,7 +7,7 @@ toolchain go1.22.0 replace github.com/elastic/go-elasticsearch/v8 => ../.. require ( - github.com/elastic/elastic-transport-go/v8 v8.6.0 + github.com/elastic/elastic-transport-go/v8 v8.6.1 github.com/elastic/go-elasticsearch/v8 v8.13.1 ) diff --git a/_examples/fasthttp/go.mod b/_examples/fasthttp/go.mod index 4e37a24618..d4b0424e9c 100644 --- a/_examples/fasthttp/go.mod +++ b/_examples/fasthttp/go.mod @@ -13,7 +13,7 @@ require ( require ( github.com/andybalholm/brotli v1.0.4 // indirect - github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect + github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/klauspost/compress v1.15.0 // indirect diff --git a/_examples/instrumentation/go.mod b/_examples/instrumentation/go.mod index b3b40843ca..cc4695fced 100644 --- a/_examples/instrumentation/go.mod +++ b/_examples/instrumentation/go.mod @@ -7,7 +7,7 @@ toolchain go1.22.0 replace github.com/elastic/go-elasticsearch/v8 => ../.. require ( - github.com/elastic/elastic-transport-go/v8 v8.6.0 + github.com/elastic/elastic-transport-go/v8 v8.6.1 github.com/elastic/go-elasticsearch/v8 v8.0.0-20191002063538-b491ce54d752 github.com/fatih/color v1.7.0 go.elastic.co/apm v1.11.0 diff --git a/_examples/logging/go.mod b/_examples/logging/go.mod index 34600ff313..699aea2671 100644 --- a/_examples/logging/go.mod +++ b/_examples/logging/go.mod @@ -7,7 +7,7 @@ toolchain go1.22.0 replace github.com/elastic/go-elasticsearch/v8 => ../.. require ( - github.com/elastic/elastic-transport-go/v8 v8.6.0 + github.com/elastic/elastic-transport-go/v8 v8.6.1 github.com/elastic/go-elasticsearch/v8 v8.0.0-00010101000000-000000000000 github.com/rs/zerolog v1.32.0 ) diff --git a/_examples/security/go.mod b/_examples/security/go.mod index 45ea67de2f..5a15d4de37 100644 --- a/_examples/security/go.mod +++ b/_examples/security/go.mod @@ -9,7 +9,7 @@ replace github.com/elastic/go-elasticsearch/v8 => ../.. require github.com/elastic/go-elasticsearch/v8 v8.0.0-00010101000000-000000000000 require ( - github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect + github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect go.opentelemetry.io/otel v1.28.0 // indirect diff --git a/_examples/xkcdsearch/go.mod b/_examples/xkcdsearch/go.mod index 62c3141fd8..973a39fa62 100644 --- a/_examples/xkcdsearch/go.mod +++ b/_examples/xkcdsearch/go.mod @@ -14,7 +14,7 @@ require ( ) require ( - github.com/elastic/elastic-transport-go/v8 v8.6.0 // indirect + github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect diff --git a/elasticsearch.go b/elasticsearch.go index 8434bd3f52..48d58886de 100644 --- a/elasticsearch.go +++ b/elasticsearch.go @@ -18,10 +18,10 @@ package elasticsearch import ( + "context" "encoding/base64" "errors" "fmt" - "go.opentelemetry.io/otel/trace" "net/http" "net/url" "os" @@ -32,13 +32,13 @@ import ( "sync" "time" - "github.com/elastic/go-elasticsearch/v8/typedapi" + "github.com/elastic/elastic-transport-go/v8/elastictransport" + tpversion "github.com/elastic/elastic-transport-go/v8/elastictransport/version" + "go.opentelemetry.io/otel/trace" "github.com/elastic/go-elasticsearch/v8/esapi" "github.com/elastic/go-elasticsearch/v8/internal/version" - - "github.com/elastic/elastic-transport-go/v8/elastictransport" - tpversion "github.com/elastic/elastic-transport-go/v8/elastictransport/version" + "github.com/elastic/go-elasticsearch/v8/typedapi" ) const ( @@ -69,13 +69,16 @@ func init() { // Config represents the client configuration. type Config struct { Addresses []string // A list of Elasticsearch nodes to use. - Username string // Username for HTTP Basic Authentication. - Password string // Password for HTTP Basic Authentication. - CloudID string // Endpoint for the Elastic Service (https://elastic.co/cloud). - APIKey string // Base64-encoded token for authorization; if set, overrides username/password and service token. - ServiceToken string // Service token for authorization; if set, overrides username/password. - CertificateFingerprint string // SHA256 hex fingerprint given by Elasticsearch on first launch. + CloudID string // Endpoint for the Elastic Service (https://elastic.co/cloud). + APIKeyProvider func(context.Context) (string, error) // If set, the Client shall call this func to obtain an API Key, overriding APIKey. + APIKey string // Base64-encoded token for authorization; if set, overrides username/password and service token. + ServiceTokenProvider func(context.Context) (string, error) // If set, the Client shall call this func to obtain a service token, overriding ServiceToken. + ServiceToken string // Service token for authorization; if set, overrides username/password. + BasicAuthProvider func(context.Context) (string, string, error) // If set, the Client shall call this func to obtain a username and password, overriding Username and Password. + Username string // Username for HTTP Basic Authentication. + Password string // Password for HTTP Basic Authentication. + CertificateFingerprint string // SHA256 hex fingerprint given by Elasticsearch on first launch. Header http.Header // Global HTTP request header. @@ -278,10 +281,13 @@ func newTransport(cfg Config) (*elastictransport.Client, error) { UserAgent: userAgent, URLs: urls, - Username: cfg.Username, - Password: cfg.Password, + APIKeyProvider: cfg.APIKeyProvider, APIKey: cfg.APIKey, + ServiceTokenProvider: cfg.ServiceTokenProvider, ServiceToken: cfg.ServiceToken, + BasicAuthProvider: cfg.BasicAuthProvider, + Username: cfg.Username, + Password: cfg.Password, CertificateFingerprint: cfg.CertificateFingerprint, Header: cfg.Header, diff --git a/esapi/test/go.mod b/esapi/test/go.mod index a12b3bc039..8b569f2176 100644 --- a/esapi/test/go.mod +++ b/esapi/test/go.mod @@ -7,7 +7,7 @@ toolchain go1.22.0 replace github.com/elastic/go-elasticsearch/v8 => ../../ require ( - github.com/elastic/elastic-transport-go/v8 v8.6.0 + github.com/elastic/elastic-transport-go/v8 v8.6.1 github.com/elastic/go-elasticsearch/v8 v8.0.0-20210817150010-57d659deaca7 gopkg.in/yaml.v2 v2.4.0 )