Skip to content

Commit 8507904

Browse files
authored
Wire kibana config from fleet (#4670)
* Adds kibana and sourcemap api keys to package
1 parent 0c7bd22 commit 8507904

18 files changed

Lines changed: 137 additions & 25 deletions

File tree

apmpackage/apm/0.1.0/_dev/docs/README.template.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ IMPORTANT: If you run APM Server with Elastic Agent manually in standalone mode,
6666
- `Host`: APM Server host and port to listen on.
6767
- `Secret token`: Authorization token for sending data to APM Server. See the [documentation](https://www.elastic.co/guide/en/apm/server/current/configuration-rum.html) for details.
6868
- `Enable RUM`: Enables support for RUM monitoring. See the [documentation](https://www.elastic.co/guide/en/apm/server/current/configuration-rum.html) for details.
69-
69+
- `API Key for Central Configuration`: Gives privileges for APM Agent central configuration. See the [documentation](https://www.elastic.co/guide/en/kibana/master/agent-configuration.html)
70+
- `API Key for Sourcemaps`: Gives priveleges to read sourcemaps. See the [documentation](https://www.elastic.co/guide/en/apm/agent/rum-js/current/sourcemap.html).
7071

7172
## Traces
7273

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
apm-server:
22
host: {{host}}
33
secret_token: {{secret_token}}
4-
rum.enabled: {{enable_rum}}
4+
rum:
5+
enabled: {{enable_rum}}
6+
source_mapping.elasticsearch.api_key: {{sourcemap_api_key}}
7+
kibana:
8+
api_key: {{kibana_api_key}}

apmpackage/apm/0.1.0/docs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ IMPORTANT: If you run APM Server with Elastic Agent manually in standalone mode,
6666
- `Host`: APM Server host and port to listen on.
6767
- `Secret token`: Authorization token for sending data to APM Server. See the [documentation](https://www.elastic.co/guide/en/apm/server/current/configuration-rum.html) for details.
6868
- `Enable RUM`: Enables support for RUM monitoring. See the [documentation](https://www.elastic.co/guide/en/apm/server/current/configuration-rum.html) for details.
69-
69+
- `API Key for Central Configuration`: Gives privileges for APM Agent central configuration. See the [documentation](https://www.elastic.co/guide/en/kibana/master/agent-configuration.html)
70+
- `API Key for Sourcemaps`: Gives priveleges to read sourcemaps. See the [documentation](https://www.elastic.co/guide/en/apm/agent/rum-js/current/sourcemap.html).
7071

7172
## Traces
7273

apmpackage/apm/0.1.0/manifest.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ policy_templates:
4242
required: true
4343
show_user: true
4444
default: false
45+
- name: kibana_api_key
46+
type: string
47+
title: API Key for Central Configuration
48+
required: false
49+
description: Enter as <Id>:<API Key>
50+
show_user: true
51+
- name: sourcemap_api_key
52+
type: string
53+
title: API Key for Sourcemaps
54+
required: false
55+
description: Enter as <Id>:<API Key>
56+
show_user: true
4557
template_path: template.yml.hbs
4658
owner:
4759
github: elastic/apm-server

beater/api/config/agent/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func TestIfNoneMatch(t *testing.T) {
355355
}
356356

357357
func TestAgentConfigTraceContext(t *testing.T) {
358-
kibanaCfg := libkibana.DefaultClientConfig()
358+
kibanaCfg := config.KibanaConfig{Enabled: true, ClientConfig: libkibana.DefaultClientConfig()}
359359
kibanaCfg.Host = "testKibana:12345"
360360
client := kibana.NewConnectingClient(&kibanaCfg)
361361
handler := Handler(client, &config.AgentConfig{Cache: &config.Cache{Expiration: 5 * time.Minute}})

beater/api/mux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ type middlewareFunc func(*config.Config, *authorization.Handler, map[request.Res
151151
func agentConfigHandler(cfg *config.Config, authHandler *authorization.Handler, middlewareFunc middlewareFunc) (request.Handler, error) {
152152
var client kibana.Client
153153
if cfg.Kibana.Enabled {
154-
client = kibana.NewConnectingClient(&cfg.Kibana.ClientConfig)
154+
client = kibana.NewConnectingClient(&cfg.Kibana)
155155
}
156156
h := agent.Handler(client, cfg.AgentConfig)
157157
msg := "Agent remote configuration is disabled. " +

beater/beater.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"sync"
2626
"time"
2727

28+
"github.com/elastic/beats/v7/libbeat/kibana"
29+
2830
"github.com/pkg/errors"
2931
"go.elastic.co/apm"
3032
"golang.org/x/sync/errgroup"
@@ -184,6 +186,7 @@ func (bt *beater) start(ctx context.Context, cancelContext context.CancelFunc, b
184186
inputs.Stop()
185187
}
186188
reload.Register.MustRegisterList("inputs", inputs)
189+
187190
} else {
188191
// Management disabled, use statically defined config.
189192
s, err := newServerRunner(ctx, serverRunnerParams{
@@ -233,6 +236,7 @@ func (s *serverCreator) Create(p beat.PipelineConnector, rawConfig *common.Confi
233236
sharedServerRunnerParams: s.args,
234237
Namespace: namespace,
235238
Pipeline: p,
239+
KibanaConfig: &integrationConfig.Fleet.Kibana,
236240
RawConfig: apmServerCommonConfig,
237241
})
238242
}
@@ -265,9 +269,10 @@ type serverRunner struct {
265269
type serverRunnerParams struct {
266270
sharedServerRunnerParams
267271

268-
Namespace string
269-
Pipeline beat.PipelineConnector
270-
RawConfig *common.Config
272+
Namespace string
273+
Pipeline beat.PipelineConnector
274+
KibanaConfig *kibana.ClientConfig
275+
RawConfig *common.Config
271276
}
272277

273278
type sharedServerRunnerParams struct {
@@ -284,6 +289,11 @@ func newServerRunner(ctx context.Context, args serverRunnerParams) (*serverRunne
284289
if err != nil {
285290
return nil, err
286291
}
292+
293+
if cfg.DataStreams.Enabled && args.KibanaConfig != nil {
294+
cfg.Kibana.ClientConfig = *args.KibanaConfig
295+
}
296+
287297
runServerContext, cancel := context.WithCancel(ctx)
288298
return &serverRunner{
289299
backgroundContext: ctx,

beater/config/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ const (
4040
)
4141

4242
type KibanaConfig struct {
43-
Enabled bool `config:"enabled"`
43+
Enabled bool `config:"enabled"`
44+
APIKey string `config:"api_key"`
4445
kibana.ClientConfig `config:",inline"`
4546
}
4647

4748
func (k *KibanaConfig) Unpack(cfg *common.Config) error {
48-
if err := cfg.Unpack(&k.ClientConfig); err != nil {
49+
type kibanaConfig KibanaConfig
50+
if err := cfg.Unpack((*kibanaConfig)(k)); err != nil {
4951
return err
5052
}
5153
k.Enabled = cfg.Enabled()
5254
k.Host = strings.TrimRight(k.Host, "/")
53-
5455
return nil
5556
}
5657

@@ -119,7 +120,7 @@ func NewConfig(ucfg *common.Config, outputESCfg *common.Config) (*Config, error)
119120
return nil, errors.New(msgInvalidConfigAgentCfg)
120121
}
121122

122-
if err := c.RumConfig.setup(logger, outputESCfg); err != nil {
123+
if err := c.RumConfig.setup(logger, c.DataStreams.Enabled, outputESCfg); err != nil {
123124
return nil, err
124125
}
125126

beater/config/integration.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222

2323
"github.com/elastic/beats/v7/libbeat/common"
24+
"github.com/elastic/beats/v7/libbeat/kibana"
2425
)
2526

2627
func NewIntegrationConfig(rootConfig *common.Config) (*IntegrationConfig, error) {
@@ -48,6 +49,7 @@ type IntegrationConfig struct {
4849
Meta *Meta `config:"meta"`
4950
DataStream *DataStream `config:"data_stream"`
5051
APMServer *common.Config `config:"apm-server"`
52+
Fleet Fleet `config:"fleet"`
5153
}
5254

5355
type DataStream struct {
@@ -62,3 +64,7 @@ type Package struct {
6264
Name string `config:"name"`
6365
Version string `config:"version"`
6466
}
67+
68+
type Fleet struct {
69+
Kibana kibana.ClientConfig `config:"kibana"`
70+
}

beater/config/rum.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (s *SourceMapping) IsEnabled() bool {
7676
return s == nil || s.Enabled == nil || *s.Enabled
7777
}
7878

79-
func (c *RumConfig) setup(log *logp.Logger, outputESCfg *common.Config) error {
79+
func (c *RumConfig) setup(log *logp.Logger, dataStreamsEnabled bool, outputESCfg *common.Config) error {
8080
if !c.IsEnabled() {
8181
return nil
8282
}
@@ -88,8 +88,14 @@ func (c *RumConfig) setup(log *logp.Logger, outputESCfg *common.Config) error {
8888
return errors.Wrapf(err, "Invalid regex for `exclude_from_grouping`: ")
8989
}
9090

91+
var apiKey string
9192
if c.SourceMapping == nil || c.SourceMapping.esConfigured {
92-
return nil
93+
if dataStreamsEnabled {
94+
// when running under Fleet, the only setting configured is the api key
95+
apiKey = c.SourceMapping.ESConfig.APIKey
96+
} else {
97+
return nil
98+
}
9399
}
94100

95101
// fall back to elasticsearch output configuration for sourcemap storage if possible
@@ -101,6 +107,9 @@ func (c *RumConfig) setup(log *logp.Logger, outputESCfg *common.Config) error {
101107
if err := outputESCfg.Unpack(c.SourceMapping.ESConfig); err != nil {
102108
return errors.Wrap(err, "unpacking Elasticsearch config into Sourcemap config")
103109
}
110+
if c.SourceMapping.ESConfig.APIKey == "" {
111+
c.SourceMapping.ESConfig.APIKey = apiKey
112+
}
104113
return nil
105114
}
106115

0 commit comments

Comments
 (0)