@@ -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
@@ -90,7 +92,7 @@ func (c *Configuration) NewClient(logger *zap.Logger, metricsFactory metrics.Fac
90
92
if len (c .Servers ) < 1 {
91
93
return nil , errors .New ("No servers specified" )
92
94
}
93
- options , err := c .getConfigOptions ()
95
+ options , err := c .getConfigOptions (logger )
94
96
if err != nil {
95
97
return nil , err
96
98
}
@@ -247,8 +249,13 @@ func (c *Configuration) IsEnabled() bool {
247
249
}
248
250
249
251
// getConfigOptions wraps the configs to feed to the ElasticSearch client init
250
- func (c * Configuration ) getConfigOptions () ([]elastic.ClientOptionFunc , error ) {
251
- options := []elastic.ClientOptionFunc {elastic .SetURL (c .Servers ... ), elastic .SetSniff (c .Sniffer )}
252
+ func (c * Configuration ) getConfigOptions (logger * zap.Logger ) ([]elastic.ClientOptionFunc , error ) {
253
+
254
+ options := []elastic.ClientOptionFunc {elastic .SetURL (c .Servers ... ), elastic .SetSniff (c .Sniffer ),
255
+ // Disable health check when token from context is allowed, this is because at this time
256
+ // we don' have a valid token to do the check ad if we don't disable the check the service that
257
+ // uses this won't start.
258
+ elastic .SetHealthcheck (! c .AllowTokenFromContext )}
252
259
httpClient := & http.Client {
253
260
Timeout : c .Timeout ,
254
261
}
@@ -271,14 +278,24 @@ func (c *Configuration) getConfigOptions() ([]elastic.ClientOptionFunc, error) {
271
278
}
272
279
httpTransport .TLSClientConfig = & tls.Config {RootCAs : ca }
273
280
}
281
+
282
+ token := ""
274
283
if c .TokenFilePath != "" {
275
- token , err := loadToken (c .TokenFilePath )
284
+ if c .AllowTokenFromContext {
285
+ logger .Warn ("Token file and token propagation are both enabled, token from file won't be used" )
286
+ }
287
+ tokenFromFile , err := loadToken (c .TokenFilePath )
276
288
if err != nil {
277
289
return nil , err
278
290
}
291
+ token = tokenFromFile
292
+ }
293
+
294
+ if token != "" || c .AllowTokenFromContext {
279
295
httpClient .Transport = & tokenAuthTransport {
280
- token : token ,
281
- wrapped : httpTransport ,
296
+ token : token ,
297
+ allowOverrideFromCtx : c .AllowTokenFromContext ,
298
+ wrapped : httpTransport ,
282
299
}
283
300
} else {
284
301
httpClient .Transport = httpTransport
@@ -329,12 +346,20 @@ func (tlsConfig *TLSConfig) loadPrivateKey() (*tls.Certificate, error) {
329
346
330
347
// TokenAuthTransport
331
348
type tokenAuthTransport struct {
332
- token string
333
- wrapped * http.Transport
349
+ token string
350
+ allowOverrideFromCtx bool
351
+ wrapped * http.Transport
334
352
}
335
353
336
354
func (tr * tokenAuthTransport ) RoundTrip (r * http.Request ) (* http.Response , error ) {
337
- r .Header .Set ("Authorization" , "Bearer " + tr .token )
355
+ token := tr .token
356
+ if tr .allowOverrideFromCtx {
357
+ headerToken , _ := spanstore .GetBearerToken (r .Context ())
358
+ if headerToken != "" {
359
+ token = headerToken
360
+ }
361
+ }
362
+ r .Header .Set ("Authorization" , "Bearer " + token )
338
363
return tr .wrapped .RoundTrip (r )
339
364
}
340
365
0 commit comments