@@ -32,32 +32,34 @@ import (
32
32
33
33
"github.com/jaegertracing/jaeger/pkg/es"
34
34
"github.com/jaegertracing/jaeger/pkg/es/wrapper"
35
+ "github.com/jaegertracing/jaeger/storage/spanstore"
35
36
storageMetrics "github.com/jaegertracing/jaeger/storage/spanstore/metrics"
36
37
)
37
38
38
39
// Configuration describes the configuration properties needed to connect to an ElasticSearch cluster
39
40
type Configuration struct {
40
- Servers []string
41
- Username string
42
- Password string
43
- TokenFilePath string
44
- Sniffer bool // https://github.com/olivere/elastic/wiki/Sniffing
45
- MaxNumSpans int // defines maximum number of spans to fetch from storage per query
46
- MaxSpanAge time.Duration `yaml:"max_span_age"` // configures the maximum lookback on span reads
47
- NumShards int64 `yaml:"shards"`
48
- NumReplicas int64 `yaml:"replicas"`
49
- Timeout time.Duration `validate:"min=500"`
50
- BulkSize int
51
- BulkWorkers int
52
- BulkActions int
53
- BulkFlushInterval time.Duration
54
- IndexPrefix string
55
- TagsFilePath string
56
- AllTagsAsFields bool
57
- TagDotReplacement string
58
- Enabled bool
59
- TLS TLSConfig
60
- UseReadWriteAliases bool
41
+ Servers []string
42
+ Username string
43
+ Password string
44
+ TokenFilePath string
45
+ AllowTokenFromContext bool
46
+ Sniffer bool // https://github.com/olivere/elastic/wiki/Sniffing
47
+ MaxNumSpans int // defines maximum number of spans to fetch from storage per query
48
+ MaxSpanAge time.Duration `yaml:"max_span_age"` // configures the maximum lookback on span reads
49
+ NumShards int64 `yaml:"shards"`
50
+ NumReplicas int64 `yaml:"replicas"`
51
+ Timeout time.Duration `validate:"min=500"`
52
+ BulkSize int
53
+ BulkWorkers int
54
+ BulkActions int
55
+ BulkFlushInterval time.Duration
56
+ IndexPrefix string
57
+ TagsFilePath string
58
+ AllTagsAsFields bool
59
+ TagDotReplacement string
60
+ Enabled bool
61
+ TLS TLSConfig
62
+ UseReadWriteAliases bool
61
63
}
62
64
63
65
// TLSConfig describes the configuration properties to connect tls enabled ElasticSearch cluster
@@ -248,7 +250,8 @@ func (c *Configuration) IsEnabled() bool {
248
250
249
251
// getConfigOptions wraps the configs to feed to the ElasticSearch client init
250
252
func (c * Configuration ) getConfigOptions () ([]elastic.ClientOptionFunc , error ) {
251
- options := []elastic.ClientOptionFunc {elastic .SetURL (c .Servers ... ), elastic .SetSniff (c .Sniffer )}
253
+ options := []elastic.ClientOptionFunc {elastic .SetURL (c .Servers ... ), elastic .SetSniff (c .Sniffer ),
254
+ elastic .SetHealthcheck (! c .AllowTokenFromContext )}
252
255
httpClient := & http.Client {
253
256
Timeout : c .Timeout ,
254
257
}
@@ -271,14 +274,21 @@ func (c *Configuration) getConfigOptions() ([]elastic.ClientOptionFunc, error) {
271
274
}
272
275
httpTransport .TLSClientConfig = & tls.Config {RootCAs : ca }
273
276
}
277
+
278
+ token := ""
274
279
if c .TokenFilePath != "" {
275
- token , err := loadToken (c .TokenFilePath )
280
+ tokenFromFile , err := loadToken (c .TokenFilePath )
276
281
if err != nil {
277
282
return nil , err
278
283
}
284
+ token = tokenFromFile
285
+ }
286
+
287
+ if token != "" || c .AllowTokenFromContext {
279
288
httpClient .Transport = & tokenAuthTransport {
280
- token : token ,
281
- wrapped : httpTransport ,
289
+ token : token ,
290
+ allowOverrideFromCtx : c .AllowTokenFromContext ,
291
+ wrapped : httpTransport ,
282
292
}
283
293
} else {
284
294
httpClient .Transport = httpTransport
@@ -329,12 +339,17 @@ func (tlsConfig *TLSConfig) loadPrivateKey() (*tls.Certificate, error) {
329
339
330
340
// TokenAuthTransport
331
341
type tokenAuthTransport struct {
332
- token string
333
- wrapped * http.Transport
342
+ token string
343
+ allowOverrideFromCtx bool
344
+ wrapped * http.Transport
334
345
}
335
346
336
347
func (tr * tokenAuthTransport ) RoundTrip (r * http.Request ) (* http.Response , error ) {
337
- r .Header .Set ("Authorization" , "Bearer " + tr .token )
348
+ token := tr .token
349
+ if tr .allowOverrideFromCtx {
350
+ token , _ = spanstore .GetBearerToken (r .Context ())
351
+ }
352
+ r .Header .Set ("Authorization" , "Bearer " + token )
338
353
return tr .wrapped .RoundTrip (r )
339
354
}
340
355
0 commit comments