Skip to content

Commit

Permalink
[kibana] support api_key (#40360)
Browse files Browse the repository at this point in the history
* [kibana] support api_key

* changes username and password to generic values

* make check formatting changes

* removes skip-go-installation option

* fixes: naked return in func

* adds skip-go-installation back

* only sets header if apikey is not empty

---------

Co-authored-by: VihasMakwana <121151420+VihasMakwana@users.noreply.github.com>
  • Loading branch information
sachin-frayne and VihasMakwana committed Sep 20, 2024
1 parent 9c0ff04 commit f934ab8
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 13 deletions.
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)
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: "",
}
}
9 changes: 6 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,8 @@ func fetchPath(http *helper.HTTP, currentPath, newPath string) ([]byte, error) {

// Http helper includes the HostData with username and password
http.SetURI(u.String())
if apiKey != "" {
http.SetHeader("Authorization", "ApiKey "+apiKey)
}
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
3 changes: 3 additions & 0 deletions metricbeat/module/kibana/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ 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 {
if m.ApiKey != "" {
m.http.SetHeader("Authorization", "ApiKey "+m.ApiKey)
}
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 @@ -892,6 +892,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

0 comments on commit f934ab8

Please sign in to comment.