Skip to content

Commit

Permalink
Add custom headers config for OpenSearch (#6248)
Browse files Browse the repository at this point in the history
* Add custom headers config for opensearch

* Change custom headers config to a map
  • Loading branch information
neil-xie authored Aug 24, 2024
1 parent c0cd4c5 commit c58bb1c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions common/config/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type (
AWSSigning AWSSigning `yaml:"awsSigning"`
// optional to use Signed Certificates over https
TLS TLS `yaml:"tls"`
// optional to add custom headers
CustomHeaders map[string]string `yaml:"customHeaders,omitempty"`
}

// AWSSigning contains config to enable signing,
Expand Down Expand Up @@ -139,3 +141,11 @@ func (a AWSSigning) GetCredentials() (*credentials.Credentials, *string, error)

return awsCredentials, &a.StaticCredential.Region, nil
}

// GetCustomHeader returns the header for the specified key
func (cfg *ElasticSearchConfig) GetCustomHeader(headerKey string) string {
if headerValue, ok := cfg.CustomHeaders[headerKey]; ok {
return headerValue
}
return ""
}
34 changes: 34 additions & 0 deletions common/config/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,37 @@ func TestAWSSigning_ValidateEmpty(t *testing.T) {
}

}

func TestGetCustomHeader(t *testing.T) {

tests := []struct {
config ElasticSearchConfig
header string
expected string
}{
{
config: ElasticSearchConfig{
CustomHeaders: map[string]string{
"key1": "value1",
},
},
header: "key1",
expected: "value1",
},
{
config: ElasticSearchConfig{
CustomHeaders: map[string]string{
"key1": "value1",
},
},
header: "key2",
expected: "",
},
}

for _, tc := range tests {
val := tc.config.GetCustomHeader(tc.header)
assert.Equal(t, val, tc.expected)
}

}
8 changes: 8 additions & 0 deletions common/elasticsearch/client/os2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func NewClient(
RetryBackoff: func(i int) time.Duration { return time.Duration(i) * 100 * time.Millisecond },
}

if len(connectConfig.CustomHeaders) > 0 {
osconfig.Header = http.Header{}

for key, value := range connectConfig.CustomHeaders {
osconfig.Header.Set(key, value)
}
}

// DiscoverNodesOnStart is false by default. Turn it on only when disable sniff is set to False in ES config
if !connectConfig.DisableSniff {
osconfig.DiscoverNodesOnStart = true
Expand Down
3 changes: 3 additions & 0 deletions common/elasticsearch/client/os2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func TestNewClient(t *testing.T) {
config: &config.ElasticSearchConfig{
URL: *url,
DisableSniff: false,
CustomHeaders: map[string]string{
"key": "value",
},
},
expectedErr: false,
},
Expand Down

0 comments on commit c58bb1c

Please sign in to comment.