Skip to content

Commit 5cf31d4

Browse files
authored
Merge default values with custom API Key Elasticsearch settings (#3342) (#3354)
When configuring an Elasticsearch instance for API Key usage, Elasticsearch default values should be merged with the custom config, to avoid having to configure all settings. fixes #3341
1 parent f6d437f commit 5cf31d4

File tree

7 files changed

+46
-24
lines changed

7 files changed

+46
-24
lines changed

_meta/beat.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ apm-server:
171171
#proxy_disable: false
172172

173173
# Configure http request timeout before failing an request to Elasticsearch.
174-
#timeout: 10s
174+
#timeout: 5s
175175

176176
# Enable custom SSL settings. Set to false to ignore custom SSL settings for secure communication.
177177
#ssl.enabled: true

apm-server.docker.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ apm-server:
171171
#proxy_disable: false
172172

173173
# Configure http request timeout before failing an request to Elasticsearch.
174-
#timeout: 10s
174+
#timeout: 5s
175175

176176
# Enable custom SSL settings. Set to false to ignore custom SSL settings for secure communication.
177177
#ssl.enabled: true

apm-server.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ apm-server:
171171
#proxy_disable: false
172172

173173
# Configure http request timeout before failing an request to Elasticsearch.
174-
#timeout: 10s
174+
#timeout: 5s
175175

176176
# Enable custom SSL settings. Set to false to ignore custom SSL settings for secure communication.
177177
#ssl.enabled: true

beater/config/api_key.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type APIKeyConfig struct {
3434
Enabled bool `config:"enabled"`
3535
LimitPerMin int `config:"limit"`
3636
ESConfig *elasticsearch.Config `config:"elasticsearch"`
37+
38+
esConfigured bool
3739
}
3840

3941
// IsEnabled returns whether or not API Key authorization is enabled
@@ -42,22 +44,31 @@ func (c *APIKeyConfig) IsEnabled() bool {
4244
}
4345

4446
func (c *APIKeyConfig) setup(log *logp.Logger, outputESCfg *common.Config) error {
45-
if c == nil || !c.Enabled || c.ESConfig != nil {
46-
return nil
47-
}
48-
c.ESConfig = elasticsearch.DefaultConfig()
49-
if outputESCfg == nil {
47+
if c == nil || !c.Enabled || c.esConfigured || outputESCfg == nil {
5048
return nil
5149
}
5250
log.Info("Falling back to elasticsearch output for API Key usage")
5351
if err := outputESCfg.Unpack(c.ESConfig); err != nil {
5452
return errors.Wrap(err, "unpacking Elasticsearch config into API key config")
5553
}
56-
5754
return nil
5855

5956
}
6057

6158
func defaultAPIKeyConfig() *APIKeyConfig {
62-
return &APIKeyConfig{Enabled: false, LimitPerMin: apiKeyLimit}
59+
return &APIKeyConfig{Enabled: false, LimitPerMin: apiKeyLimit, ESConfig: elasticsearch.DefaultConfig()}
60+
}
61+
62+
func (c *APIKeyConfig) Unpack(inp *common.Config) error {
63+
cfg := tmpAPIKeyConfig(*defaultAPIKeyConfig())
64+
if err := inp.Unpack(&cfg); err != nil {
65+
return errors.Errorf("error unpacking api_key config: %w", err)
66+
}
67+
*c = APIKeyConfig(cfg)
68+
if inp.HasField("elasticsearch") {
69+
c.esConfigured = true
70+
}
71+
return nil
6372
}
73+
74+
type tmpAPIKeyConfig APIKeyConfig

beater/config/api_key_test.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"testing"
2222
"time"
2323

24+
"github.com/stretchr/testify/require"
25+
2426
"github.com/elastic/beats/libbeat/logp"
2527

2628
"github.com/stretchr/testify/assert"
@@ -38,39 +40,42 @@ func TestAPIKeyConfig_IsEnabled(t *testing.T) {
3840

3941
func TestAPIKeyConfig_ESConfig(t *testing.T) {
4042
for name, tc := range map[string]struct {
41-
cfg *APIKeyConfig
43+
cfg *common.Config
4244
esCfg *common.Config
4345

4446
expectedConfig *APIKeyConfig
4547
expectedErr error
4648
}{
4749
"default": {
48-
cfg: defaultAPIKeyConfig(),
50+
cfg: common.NewConfig(),
4951
expectedConfig: defaultAPIKeyConfig(),
5052
},
5153
"ES config missing": {
52-
cfg: &APIKeyConfig{Enabled: true, LimitPerMin: apiKeyLimit},
54+
cfg: common.MustNewConfigFrom(`{"enabled": true}`),
5355
expectedConfig: &APIKeyConfig{
5456
Enabled: true,
5557
LimitPerMin: apiKeyLimit,
5658
ESConfig: elasticsearch.DefaultConfig()},
5759
},
5860
"ES configured": {
59-
cfg: &APIKeyConfig{
60-
Enabled: true,
61-
ESConfig: &elasticsearch.Config{Hosts: elasticsearch.Hosts{"192.0.0.1:9200"}}},
61+
cfg: common.MustNewConfigFrom(`{"enabled": true, "elasticsearch.timeout":"7s"}`),
6262
esCfg: common.MustNewConfigFrom(`{"hosts":["186.0.0.168:9200"]}`),
6363
expectedConfig: &APIKeyConfig{
64-
Enabled: true,
65-
ESConfig: &elasticsearch.Config{Hosts: elasticsearch.Hosts{"192.0.0.1:9200"}}},
64+
Enabled: true,
65+
LimitPerMin: apiKeyLimit,
66+
ESConfig: &elasticsearch.Config{
67+
Hosts: elasticsearch.Hosts{"localhost:9200"},
68+
Protocol: "http",
69+
Timeout: 7 * time.Second},
70+
esConfigured: true},
6671
},
6772
"disabled with ES from output": {
68-
cfg: defaultAPIKeyConfig(),
73+
cfg: common.NewConfig(),
6974
esCfg: common.MustNewConfigFrom(`{"hosts":["192.0.0.168:9200"]}`),
7075
expectedConfig: defaultAPIKeyConfig(),
7176
},
7277
"ES from output": {
73-
cfg: &APIKeyConfig{Enabled: true, LimitPerMin: 20},
78+
cfg: common.MustNewConfigFrom(`{"enabled": true, "limit": 20}`),
7479
esCfg: common.MustNewConfigFrom(`{"hosts":["192.0.0.168:9200"],"username":"foo","password":"bar"}`),
7580
expectedConfig: &APIKeyConfig{
7681
Enabled: true,
@@ -84,13 +89,15 @@ func TestAPIKeyConfig_ESConfig(t *testing.T) {
8489
},
8590
} {
8691
t.Run(name, func(t *testing.T) {
87-
err := tc.cfg.setup(logp.NewLogger("api_key"), tc.esCfg)
92+
var apiKeyConfig APIKeyConfig
93+
require.NoError(t, tc.cfg.Unpack(&apiKeyConfig))
94+
err := apiKeyConfig.setup(logp.NewLogger("api_key"), tc.esCfg)
8895
if tc.expectedErr == nil {
8996
assert.NoError(t, err)
9097
} else {
9198
assert.Error(t, err)
9299
}
93-
assert.Equal(t, tc.expectedConfig, tc.cfg)
100+
assert.Equal(t, tc.expectedConfig, &apiKeyConfig)
94101

95102
})
96103
}

beater/config/config_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ func Test_UnpackConfig(t *testing.T) {
187187
APIKeyConfig: &APIKeyConfig{
188188
Enabled: true,
189189
LimitPerMin: 200,
190-
ESConfig: &elasticsearch.Config{Hosts: elasticsearch.Hosts{"localhost:9201", "localhost:9202"}},
190+
ESConfig: &elasticsearch.Config{
191+
Hosts: elasticsearch.Hosts{"localhost:9201", "localhost:9202"},
192+
Protocol: "http",
193+
Timeout: 5 * time.Second},
194+
esConfigured: true,
191195
},
192196
},
193197
},

elasticsearch/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var (
4444

4545
// Config holds all configurable fields that are used to create a Client
4646
type Config struct {
47-
Hosts Hosts `config:"hosts"`
47+
Hosts Hosts `config:"hosts" validate:"required"`
4848
Protocol string `config:"protocol"`
4949
Path string `config:"path"`
5050
ProxyURL string `config:"proxy_url"`

0 commit comments

Comments
 (0)