@@ -21,6 +21,7 @@ import (
21
21
"github.com/asaskevich/govalidator"
22
22
esV8 "github.com/elastic/go-elasticsearch/v9"
23
23
"github.com/olivere/elastic/v7"
24
+ "go.opentelemetry.io/collector/config/configoptional"
24
25
"go.opentelemetry.io/collector/config/configtls"
25
26
"go.uber.org/zap"
26
27
"go.uber.org/zap/zapcore"
@@ -193,8 +194,8 @@ type BulkProcessing struct {
193
194
}
194
195
195
196
type Authentication struct {
196
- BasicAuthentication BasicAuthentication `mapstructure:"basic"`
197
- BearerTokenAuthentication BearerTokenAuthentication `mapstructure:"bearer_token"`
197
+ BasicAuthentication configoptional. Optional [ BasicAuthentication ] `mapstructure:"basic"`
198
+ BearerTokenAuthentication configoptional. Optional [ BearerTokenAuthentication ] `mapstructure:"bearer_token"`
198
199
}
199
200
200
201
type BasicAuthentication struct {
@@ -340,8 +341,11 @@ func (bcb *bulkCallback) invoke(id int64, requests []elastic.BulkableRequest, re
340
341
func newElasticsearchV8 (ctx context.Context , c * Configuration , logger * zap.Logger ) (* esV8.Client , error ) {
341
342
var options esV8.Config
342
343
options .Addresses = c .Servers
343
- options .Username = c .Authentication .BasicAuthentication .Username
344
- options .Password = c .Authentication .BasicAuthentication .Password
344
+ if c .Authentication .BasicAuthentication .HasValue () {
345
+ basicAuth := c .Authentication .BasicAuthentication .Get ()
346
+ options .Username = basicAuth .Username
347
+ options .Password = basicAuth .Password
348
+ }
345
349
options .DiscoverNodesOnStart = c .Sniffing .Enabled
346
350
options .CompressRequestBody = c .HTTPCompression
347
351
transport , err := GetHTTPRoundTripper (ctx , c , logger )
@@ -379,11 +383,33 @@ func (c *Configuration) ApplyDefaults(source *Configuration) {
379
383
if len (c .RemoteReadClusters ) == 0 {
380
384
c .RemoteReadClusters = source .RemoteReadClusters
381
385
}
382
- if c .Authentication .BasicAuthentication .Username == "" {
383
- c .Authentication .BasicAuthentication .Username = source .Authentication .BasicAuthentication .Username
384
- }
385
- if c .Authentication .BasicAuthentication .Password == "" {
386
- c .Authentication .BasicAuthentication .Password = source .Authentication .BasicAuthentication .Password
386
+ // Handle BasicAuthentication defaults
387
+ sourceHasBasicAuth := source .Authentication .BasicAuthentication .HasValue ()
388
+ targetHasBasicAuth := c .Authentication .BasicAuthentication .HasValue ()
389
+ if sourceHasBasicAuth {
390
+ // If target doesn't have BasicAuth, copy it from source
391
+ if ! targetHasBasicAuth {
392
+ c .Authentication .BasicAuthentication = source .Authentication .BasicAuthentication
393
+ } else {
394
+ // Target has BasicAuth, apply field-level defaults
395
+ sourceBasicAuth := source .Authentication .BasicAuthentication .Get ()
396
+ // Make a copy of target BasicAuth
397
+ basicAuth := * c .Authentication .BasicAuthentication .Get ()
398
+
399
+ // Apply defaults for username if not set
400
+ if basicAuth .Username == "" && sourceBasicAuth .Username != "" {
401
+ basicAuth .Username = sourceBasicAuth .Username
402
+ }
403
+ // Apply defaults for password if not set
404
+ if basicAuth .Password == "" && sourceBasicAuth .Password != "" {
405
+ basicAuth .Password = sourceBasicAuth .Password
406
+ }
407
+
408
+ // Only update BasicAuthentication if we have values to set
409
+ if basicAuth .Username != "" || basicAuth .Password != "" {
410
+ c .Authentication .BasicAuthentication = configoptional .Some (basicAuth )
411
+ }
412
+ }
387
413
}
388
414
if ! c .Sniffing .Enabled {
389
415
c .Sniffing .Enabled = source .Sniffing .Enabled
@@ -504,8 +530,14 @@ func (c *Configuration) getConfigOptions(ctx context.Context, logger *zap.Logger
504
530
// 1. When health check is explicitly disabled
505
531
// 2. When tokens are EXCLUSIVELY available from context (not from file)
506
532
// because at startup we don't have a valid token to do the health check
507
- disableHealthCheck := c .DisableHealthCheck ||
508
- (c .Authentication .BearerTokenAuthentication .AllowFromContext && c .Authentication .BearerTokenAuthentication .FilePath == "" )
533
+ disableHealthCheck := c .DisableHealthCheck
534
+
535
+ // Check if we have bearer token or API key authentication that only allows from context
536
+ if c .Authentication .BearerTokenAuthentication .HasValue () {
537
+ bearerAuth := c .Authentication .BearerTokenAuthentication .Get ()
538
+ disableHealthCheck = disableHealthCheck || (bearerAuth .AllowFromContext && bearerAuth .FilePath == "" )
539
+ }
540
+
509
541
// Get base Elasticsearch options using the helper function
510
542
options := c .getESOptions (disableHealthCheck )
511
543
// Configure HTTP transport with TLS and authentication
@@ -521,19 +553,28 @@ func (c *Configuration) getConfigOptions(ctx context.Context, logger *zap.Logger
521
553
}
522
554
523
555
options = append (options , elastic .SetHttpClient (httpClient ))
556
+
524
557
// Basic authentication setup
525
- if c .Authentication .BasicAuthentication .Password != "" && c .Authentication .BasicAuthentication .PasswordFilePath != "" {
526
- return nil , errors .New ("both Password and PasswordFilePath are set" )
527
- }
528
- if c .Authentication .BasicAuthentication .PasswordFilePath != "" {
529
- passwordFromFile , err := loadTokenFromFile (c .Authentication .BasicAuthentication .PasswordFilePath )
530
- if err != nil {
531
- return nil , fmt .Errorf ("failed to load password from file: %w" , err )
558
+ if c .Authentication .BasicAuthentication .HasValue () {
559
+ basicAuth := c .Authentication .BasicAuthentication .Get ()
560
+
561
+ password := basicAuth .Password
562
+ passwordFilePath := basicAuth .PasswordFilePath
563
+
564
+ if password != "" && passwordFilePath != "" {
565
+ return nil , errors .New ("both Password and PasswordFilePath are set" )
566
+ }
567
+ if passwordFilePath != "" {
568
+ passwordFromFile , err := loadTokenFromFile (passwordFilePath )
569
+ if err != nil {
570
+ return nil , fmt .Errorf ("failed to load password from file: %w" , err )
571
+ }
572
+ password = passwordFromFile
532
573
}
533
- c .Authentication .BasicAuthentication .Password = passwordFromFile
534
- }
535
574
536
- options = append (options , elastic .SetBasicAuth (c .Authentication .BasicAuthentication .Username , c .Authentication .BasicAuthentication .Password ))
575
+ username := basicAuth .Username
576
+ options = append (options , elastic .SetBasicAuth (username , password ))
577
+ }
537
578
538
579
// Add logging configuration
539
580
options , err = addLoggerOptions (options , c .LogLevel , logger )
@@ -598,23 +639,27 @@ func GetHTTPRoundTripper(ctx context.Context, c *Configuration, logger *zap.Logg
598
639
599
640
// Wrap with authentication layer if configured.
600
641
var roundTripper http.RoundTripper = transport
601
- if c .Authentication .BearerTokenAuthentication .AllowFromContext || c .Authentication .BearerTokenAuthentication .FilePath != "" {
602
- token := ""
603
- if c .Authentication .BearerTokenAuthentication .FilePath != "" {
604
- if c .Authentication .BearerTokenAuthentication .AllowFromContext {
605
- logger .Warn ("Token file and token propagation are both enabled, token from file won't be used" )
606
- }
607
- tokenFromFile , err := loadTokenFromFile (c .Authentication .BearerTokenAuthentication .FilePath )
608
- if err != nil {
609
- return nil , err
642
+
643
+ if c .Authentication .BearerTokenAuthentication .HasValue () {
644
+ bearerAuth := c .Authentication .BearerTokenAuthentication .Get ()
645
+ if bearerAuth .AllowFromContext || bearerAuth .FilePath != "" {
646
+ token := ""
647
+ if bearerAuth .FilePath != "" {
648
+ if bearerAuth .AllowFromContext {
649
+ logger .Warn ("Token file and token propagation are both enabled, token from file won't be used" )
650
+ }
651
+ tokenFromFile , err := loadTokenFromFile (bearerAuth .FilePath )
652
+ if err != nil {
653
+ return nil , err
654
+ }
655
+ token = tokenFromFile
610
656
}
611
- token = tokenFromFile
612
- }
613
657
614
- roundTripper = & auth.RoundTripper {
615
- Transport : transport ,
616
- OverrideFromCtx : c .Authentication .BearerTokenAuthentication .AllowFromContext ,
617
- StaticToken : token ,
658
+ roundTripper = & auth.RoundTripper {
659
+ Transport : transport ,
660
+ OverrideFromCtx : bearerAuth .AllowFromContext ,
661
+ StaticToken : token ,
662
+ }
618
663
}
619
664
}
620
665
0 commit comments