Skip to content

Commit

Permalink
feat(exporter/elasticsearch): support Basic Auth for static config
Browse files Browse the repository at this point in the history
Signed-off-by: hainenber <[email protected]>
  • Loading branch information
hainenber committed Nov 25, 2023
1 parent 3c0d324 commit 67b236f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
6 changes: 3 additions & 3 deletions component/prometheus/exporter/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"

"github.com/grafana/agent/component"
commoncfg "github.com/grafana/agent/component/common/config"
commonCfg "github.com/grafana/agent/component/common/config"
"github.com/grafana/agent/component/prometheus/exporter"
"github.com/grafana/agent/pkg/integrations"
"github.com/grafana/agent/pkg/integrations/elasticsearch_exporter"
Expand Down Expand Up @@ -53,7 +53,7 @@ type Arguments struct {
InsecureSkipVerify bool `river:"ssl_skip_verify,attr,optional"`
ExportDataStreams bool `river:"data_stream,attr,optional"`
ExportSLM bool `river:"slm,attr,optional"`
BasicAuth *commoncfg.BasicAuth `river:"basic_auth,block,optional"`
BasicAuth *commonCfg.BasicAuth `river:"basic_auth,block,optional"`
}

// SetToDefault implements river.Defaulter.
Expand All @@ -80,6 +80,6 @@ func (a *Arguments) Convert() *elasticsearch_exporter.Config {
InsecureSkipVerify: a.InsecureSkipVerify,
ExportDataStreams: a.ExportDataStreams,
ExportSLM: a.ExportSLM,
BasicAuth: a.BasicAuth,
BasicAuth: a.BasicAuth.Convert(),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"testing"
"time"

commoncfg "github.com/grafana/agent/component/common/config"
commonCfg "github.com/grafana/agent/component/common/config"
"github.com/grafana/agent/pkg/integrations/elasticsearch_exporter"
"github.com/grafana/river"
"github.com/grafana/river/rivertypes"
promCfg "github.com/prometheus/common/config"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -56,7 +57,7 @@ func TestRiverUnmarshal(t *testing.T) {
InsecureSkipVerify: true,
ExportDataStreams: true,
ExportSLM: true,
BasicAuth: &commoncfg.BasicAuth{
BasicAuth: &commonCfg.BasicAuth{
Username: "username",
Password: rivertypes.Secret("pass"),
},
Expand Down Expand Up @@ -111,9 +112,9 @@ func TestConvert(t *testing.T) {
InsecureSkipVerify: true,
ExportDataStreams: true,
ExportSLM: true,
BasicAuth: &commoncfg.BasicAuth{
BasicAuth: &promCfg.BasicAuth{
Username: "username",
Password: rivertypes.Secret("pass"),
Password: promCfg.Secret("pass"),
},
}
require.Equal(t, expected, *res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,12 @@ Full reference of options:

# Export stats for SLM (Snapshot Lifecycle Management).
[ slm: <boolean> ]

# Sets the `Authorization` header on every ES probe with the
# configured username and password.
# password and password_file are mutually exclusive.
basic_auth:
[ username: <string> ]
[ password: <secret> ]
[ password_file: <string> ]
```
14 changes: 11 additions & 3 deletions pkg/integrations/elasticsearch_exporter/elasticsearch_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

"github.com/go-kit/log"
"github.com/go-kit/log/level"
commoncfg "github.com/grafana/agent/component/common/config"
"github.com/grafana/agent/pkg/integrations"
integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"
"github.com/grafana/agent/pkg/integrations/v2/metricsutils"
"github.com/prometheus/client_golang/prometheus"
promCfg "github.com/prometheus/common/config"

"github.com/prometheus-community/elasticsearch_exporter/collector"
"github.com/prometheus-community/elasticsearch_exporter/pkg/clusterinfo"
Expand Down Expand Up @@ -71,7 +71,7 @@ type Config struct {
// Export stats for Snapshot Lifecycle Management
ExportSLM bool `yaml:"slm,omitempty"`
// BasicAuth block allows secure connection with Elasticsearch cluster via Basic-Auth
BasicAuth *commoncfg.BasicAuth `yaml:"basic_auth,omitempty"`
BasicAuth *promCfg.BasicAuth `yaml:"basic_auth,omitempty"`
}

// Custom http.Transport struct for Basic Auth-secured communication with ES cluster
Expand Down Expand Up @@ -150,7 +150,15 @@ func New(logger log.Logger, c *Config) (integrations.Integration, error) {
}
password = strings.TrimSpace(string(buff))
}
encodedAuth := base64.StdEncoding.EncodeToString([]byte(c.BasicAuth.Username + ":" + password))
username := string(c.BasicAuth.Username)
if len(c.BasicAuth.UsernameFile) > 0 {
buff, err := os.ReadFile(c.BasicAuth.UsernameFile)
if err != nil {
return nil, fmt.Errorf("unable to load username file %s: %w", c.BasicAuth.UsernameFile, err)
}
username = strings.TrimSpace(string(buff))
}
encodedAuth := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
esHttpTransport.authHeader = "Basic " + encodedAuth
}

Expand Down

0 comments on commit 67b236f

Please sign in to comment.