Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kibana] support api_key #40360

Merged
merged 9 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,5 @@ jobs:
# into fixing all linting issues in the whole file instead
args: --timeout=30m --whole-files

# Optional: if set to true then the action will use pre-installed Go.
skip-go-installation: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was deprecated in

v4.0.0+ requires an explicit actions/setup-go installation step before using this action: uses: actions/setup-go@v5. The skip-go-installation option has been removed.

But we are running version v1.55.2, so I am not sure removing this achieves what you want.
Why we wan to remove this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will revert this change now.


# Optional: show only new issues if it's a pull request. The default value is `false`.
only-new-issues: true
3 changes: 3 additions & 0 deletions metricbeat/docs/modules/kibana.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ metricbeat.modules:
hosts: ["localhost:5601"]
basepath: ""
enabled: true
#username: "user"
#password: "secret"
#api_key: "foo:bar"

# Set to true to send data collected by module to X-Pack
# Monitoring instead of metricbeat-* indices.
Expand Down
3 changes: 3 additions & 0 deletions metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ metricbeat.modules:
hosts: ["localhost:5601"]
basepath: ""
enabled: true
#username: "user"
#password: "secret"
#api_key: "foo:bar"

# Set to true to send data collected by module to X-Pack
# Monitoring instead of metricbeat-* indices.
Expand Down
1 change: 1 addition & 0 deletions metricbeat/module/kibana/_meta/config-xpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#basepath: ""
#username: "user"
#password: "secret"
#api_key: "foo:bar"
3 changes: 3 additions & 0 deletions metricbeat/module/kibana/_meta/config.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
hosts: ["localhost:5601"]
basepath: ""
enabled: true
#username: "user"
#password: "secret"
#api_key: "foo:bar"

# Set to true to send data collected by module to X-Pack
# Monitoring instead of metricbeat-* indices.
Expand Down
1 change: 1 addition & 0 deletions metricbeat/module/kibana/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
#basepath: ""
#username: "user"
#password: "secret"
#api_key: "foo:bar"
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) (err error) {
}

func (m *MetricSet) validate() (err error, versionSupported bool) {
kibanaVersion, err := kibana.GetVersion(m.actionsHTTP, kibana.ClusterActionsPath)
kibanaVersion, err := kibana.GetVersion(m.actionsHTTP, kibana.ClusterActionsPath, m.ApiKey)
sachin-frayne marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err, false
}
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/kibana/cluster_rules/cluster_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) (err error) {
}

func (m *MetricSet) validate() (err error, versionSupported bool) {
kibanaVersion, err := kibana.GetVersion(m.rulesHTTP, kibana.ClusterRulesPath)
kibanaVersion, err := kibana.GetVersion(m.rulesHTTP, kibana.ClusterRulesPath, m.ApiKey)
if err != nil {
return err, false
}
Expand Down
4 changes: 3 additions & 1 deletion metricbeat/module/kibana/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ package kibana

// Config defines the structure for the Kibana module configuration options
type Config struct {
XPackEnabled bool `config:"xpack.enabled"`
XPackEnabled bool `config:"xpack.enabled"`
ApiKey string `config:"api_key"`
}

// DefaultConfig returns the default configuration for the Kibana module
func DefaultConfig() Config {
return Config{
XPackEnabled: false,
ApiKey: "",
}
}
7 changes: 4 additions & 3 deletions metricbeat/module/kibana/kibana.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func NewModule(base mb.BaseModule) (mb.Module, error) {
}

// GetVersion returns the version of the Kibana instance
func GetVersion(http *helper.HTTP, currentPath string) (*version.V, error) {
content, err := fetchPath(http, currentPath, StatusPath)
func GetVersion(http *helper.HTTP, currentPath string, apiKey string) (*version.V, error) {
content, err := fetchPath(http, currentPath, StatusPath, apiKey)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func IsUsageExcludable(currentKibanaVersion *version.V) bool {
v7_0_1.LessThanOrEqual(false, currentKibanaVersion)
}

func fetchPath(http *helper.HTTP, currentPath, newPath string) ([]byte, error) {
func fetchPath(http *helper.HTTP, currentPath, newPath string, apiKey string) ([]byte, error) {
currentURI := http.GetURI()
defer http.SetURI(currentURI) // Reset after this request

Expand All @@ -159,5 +159,6 @@ func fetchPath(http *helper.HTTP, currentPath, newPath string) ([]byte, error) {

// Http helper includes the HostData with username and password
http.SetURI(u.String())
http.SetHeader("Authorization", "ApiKey "+apiKey)
sachin-frayne marked this conversation as resolved.
Show resolved Hide resolved
return http.FetchContent()
}
12 changes: 12 additions & 0 deletions metricbeat/module/kibana/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
package kibana

import (
"encoding/base64"
"fmt"

"github.com/elastic/beats/v7/metricbeat/mb"
)

// MetricSet can be used to build other metricsets within the Kibana module.
type MetricSet struct {
mb.BaseMetricSet
XPackEnabled bool
ApiKey string
}

// NewMetricSet creates a metricset that can be used to build other metricsets
Expand All @@ -35,8 +39,16 @@ func NewMetricSet(base mb.BaseMetricSet) (*MetricSet, error) {
return nil, err
}

if config.ApiKey != "" {
hostData := base.HostData()
if hostData.User != "" || hostData.Password != "" {
return nil, fmt.Errorf("cannot set both api_key and username/password")
}
}

return &MetricSet{
base,
config.XPackEnabled,
base64.StdEncoding.EncodeToString([]byte(config.ApiKey)),
}, nil
}
2 changes: 1 addition & 1 deletion metricbeat/module/kibana/node_actions/node_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) (err error) {
}

func (m *MetricSet) validate() (err error, versionSupported bool) {
kibanaVersion, err := kibana.GetVersion(m.actionsHTTP, kibana.NodeActionsPath)
kibanaVersion, err := kibana.GetVersion(m.actionsHTTP, kibana.NodeActionsPath, m.ApiKey)
if err != nil {
return err, false
}
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/kibana/node_rules/node_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) (err error) {
}

func (m *MetricSet) validate() (err error, versionSupported bool) {
kibanaVersion, err := kibana.GetVersion(m.rulesHTTP, kibana.NodeRulesPath)
kibanaVersion, err := kibana.GetVersion(m.rulesHTTP, kibana.NodeRulesPath, m.ApiKey)
if err != nil {
return err, false
}
Expand Down
14 changes: 10 additions & 4 deletions metricbeat/module/kibana/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ var (

// MetricSet type defines all fields of the MetricSet
type MetricSet struct {
*kibana.MetricSet
mb.BaseMetricSet
settingsHTTP *helper.HTTP
}

// New create a new instance of the MetricSet
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
ms, err := kibana.NewMetricSet(base)
if err != nil {
return nil, err
}
return &MetricSet{
MetricSet: ms,
BaseMetricSet: base,
}, nil
}
Expand All @@ -61,12 +67,12 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// descriptive error must be returned.
func (m *MetricSet) Fetch(r mb.ReporterV2) (err error) {
if err = m.init(); err != nil {
return
return err
}

content, err := m.settingsHTTP.FetchContent()
if err != nil {
return
return err
}

return eventMapping(r, content)
Expand All @@ -80,7 +86,7 @@ func (m *MetricSet) init() (err error) {

httpHelper.SetHeaderDefault(productorigin.Header, productorigin.Beats)

kibanaVersion, err := kibana.GetVersion(httpHelper, kibana.SettingsPath)
kibanaVersion, err := kibana.GetVersion(httpHelper, kibana.SettingsPath, m.ApiKey)
if err != nil {
return err
}
Expand All @@ -93,5 +99,5 @@ func (m *MetricSet) init() (err error) {

m.settingsHTTP, err = helper.NewHTTP(m.BaseMetricSet)

return
return err
}
2 changes: 1 addition & 1 deletion metricbeat/module/kibana/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) (err error) {
}

func (m *MetricSet) init() (err error, versionSupported bool) {
kibanaVersion, err := kibana.GetVersion(m.statsHTTP, kibana.StatsPath)
kibanaVersion, err := kibana.GetVersion(m.statsHTTP, kibana.StatsPath, m.ApiKey)
if err != nil {
return err, false
}
Expand Down
2 changes: 2 additions & 0 deletions metricbeat/module/kibana/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// It returns the event which is then forward to the output. In case of an error, a
// descriptive error must be returned.
func (m *MetricSet) Fetch(r mb.ReporterV2) error {
apiKey := m.ApiKey
m.http.SetHeader("Authorization", "ApiKey "+apiKey)
sachin-frayne marked this conversation as resolved.
Show resolved Hide resolved
content, err := m.http.FetchContent()
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions metricbeat/modules.d/kibana-xpack.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
#basepath: ""
#username: "user"
#password: "secret"
#api_key: "foo:bar"
1 change: 1 addition & 0 deletions metricbeat/modules.d/kibana.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
#basepath: ""
#username: "user"
#password: "secret"
#api_key: "foo:bar"
3 changes: 3 additions & 0 deletions x-pack/metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,9 @@ metricbeat.modules:
hosts: ["localhost:5601"]
basepath: ""
enabled: true
#username: "user"
#password: "secret"
#api_key: "foo:bar"

# Set to true to send data collected by module to X-Pack
# Monitoring instead of metricbeat-* indices.
Expand Down