Skip to content

Commit cacceef

Browse files
committed
[Elastic Agent] Fix issues with dynamic inputs and conditions (#23886)
* Fix loading of configurations to not parse variables in inputs. Fix issue with EQL string methods. * Run fmt. * Add changelog entry. * Cleanup the config loading more. * Fix NewConfigFrom to handle maps. (cherry picked from commit de7906b)
1 parent 4c302ec commit cacceef

13 files changed

Lines changed: 164 additions & 172 deletions

File tree

x-pack/elastic-agent/CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- Fix issue of missing log messages from filebeat monitor {pull}23514[23514]
3737
- Increase checkin grace period to 30 seconds {pull}23568[23568]
3838
- Fix libbeat from reporting back degraded on config update {pull}23537[23537]
39+
- Fix issues with dynamic inputs and conditions {pull}23886[23886]
3940

4041
==== New features
4142

x-pack/elastic-agent/pkg/agent/application/application.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func New(log *logger.Logger, pathConfigFile string, reexec reexecManager, uc upg
3535
// Load configuration from disk to understand in which mode of operation
3636
// we must start the elastic-agent, the mode of operation cannot be changed without restarting the
3737
// elastic-agent.
38-
rawConfig, err := LoadConfigFromFile(pathConfigFile)
38+
rawConfig, err := config.LoadFile(pathConfigFile)
3939
if err != nil {
4040
return nil, err
4141
}

x-pack/elastic-agent/pkg/agent/application/config.go

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,8 @@
55
package application
66

77
import (
8-
"io/ioutil"
9-
10-
"github.com/elastic/go-ucfg"
11-
12-
"gopkg.in/yaml.v2"
13-
14-
"github.com/elastic/beats/v7/libbeat/common"
158
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration"
169
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
17-
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
1810
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/kibana"
1911
)
2012

@@ -34,53 +26,3 @@ func createFleetConfigFromEnroll(accessAPIKey string, kbn *kibana.Config) (*conf
3426
}
3527
return cfg, nil
3628
}
37-
38-
// LoadConfigFromFile loads the Agent configuration from a file.
39-
//
40-
// This must be used to load the Agent configuration, so that variables defined in the inputs are not
41-
// parsed by go-ucfg. Variables from the inputs should be parsed by the transpiler.
42-
func LoadConfigFromFile(path string) (*config.Config, error) {
43-
in, err := ioutil.ReadFile(path)
44-
if err != nil {
45-
return nil, err
46-
}
47-
var m map[string]interface{}
48-
if err := yaml.Unmarshal(in, &m); err != nil {
49-
return nil, err
50-
}
51-
return LoadConfig(m)
52-
}
53-
54-
// LoadConfig loads the Agent configuration from a map.
55-
//
56-
// This must be used to load the Agent configuration, so that variables defined in the inputs are not
57-
// parsed by go-ucfg. Variables from the inputs should be parsed by the transpiler.
58-
func LoadConfig(in map[string]interface{}) (*config.Config, error) {
59-
// make copy of a map so we dont affect a caller
60-
m := common.MapStr(in).Clone()
61-
62-
inputs, ok := m["inputs"]
63-
if ok {
64-
// remove the inputs
65-
delete(m, "inputs")
66-
}
67-
cfg, err := config.NewConfigFrom(m)
68-
if err != nil {
69-
return nil, err
70-
}
71-
if ok {
72-
inputsOnly := map[string]interface{}{
73-
"inputs": inputs,
74-
}
75-
// convert to config without variable substitution
76-
inputsCfg, err := config.NewConfigFrom(inputsOnly, ucfg.PathSep("."), ucfg.ResolveNOOP)
77-
if err != nil {
78-
return nil, err
79-
}
80-
err = cfg.Merge(inputsCfg, ucfg.PathSep("."), ucfg.ResolveNOOP)
81-
if err != nil {
82-
return nil, err
83-
}
84-
}
85-
return cfg, err
86-
}

x-pack/elastic-agent/pkg/agent/application/config_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package application
66

77
import (
88
"io/ioutil"
9-
"os"
10-
"path/filepath"
119
"testing"
1210
"time"
1311

@@ -20,44 +18,6 @@ import (
2018
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
2119
)
2220

23-
func TestLoadConfig(t *testing.T) {
24-
contents := map[string]interface{}{
25-
"outputs": map[string]interface{}{
26-
"default": map[string]interface{}{
27-
"type": "elasticsearch",
28-
"hosts": []interface{}{"127.0.0.1:9200"},
29-
"username": "elastic",
30-
"password": "changeme",
31-
},
32-
},
33-
"inputs": []interface{}{
34-
map[string]interface{}{
35-
"type": "logfile",
36-
"streams": []interface{}{
37-
map[string]interface{}{
38-
"paths": []interface{}{"/var/log/${host.name}"},
39-
},
40-
},
41-
},
42-
},
43-
}
44-
45-
tmp, err := ioutil.TempDir("", "config")
46-
require.NoError(t, err)
47-
defer os.RemoveAll(tmp)
48-
49-
cfgPath := filepath.Join(tmp, "config.yml")
50-
dumpToYAML(t, cfgPath, contents)
51-
52-
cfg, err := LoadConfigFromFile(cfgPath)
53-
require.NoError(t, err)
54-
55-
cfgData, err := cfg.ToMapStr()
56-
require.NoError(t, err)
57-
58-
assert.Equal(t, contents, cfgData)
59-
}
60-
6121
func TestConfig(t *testing.T) {
6222
testMgmtMode(t)
6323
testLocalConfig(t)

x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (h *handlerPolicyChange) Handle(ctx context.Context, a action, acker fleetA
4444
return fmt.Errorf("invalid type, expected ActionPolicyChange and received %T", a)
4545
}
4646

47-
c, err := LoadConfig(action.Policy)
47+
c, err := config.NewConfigFrom(action.Policy)
4848
if err != nil {
4949
return errors.New(err, "could not parse the configuration from the policy", errors.TypeConfig)
5050
}

x-pack/elastic-agent/pkg/agent/application/inspect_config_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *InspectConfigCmd) inspectConfig() error {
6161
}
6262

6363
func loadConfig(configPath string) (*config.Config, error) {
64-
rawConfig, err := LoadConfigFromFile(configPath)
64+
rawConfig, err := config.LoadFile(configPath)
6565
if err != nil {
6666
return nil, err
6767
}

x-pack/elastic-agent/pkg/agent/cmd/enroll.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/control/client"
15+
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
1516

1617
"github.com/spf13/cobra"
1718

@@ -92,7 +93,7 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args
9293
}
9394

9495
pathConfigFile := flags.Config()
95-
rawConfig, err := application.LoadConfigFromFile(pathConfigFile)
96+
rawConfig, err := config.LoadFile(pathConfigFile)
9697
if err != nil {
9798
return errors.New(err,
9899
fmt.Sprintf("could not read configuration file %s", pathConfigFile),

x-pack/elastic-agent/pkg/agent/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func run(flags *globalFlags, streams *cli.IOStreams) error { // Windows: Mark se
7979
service.HandleSignals(stopBeat, cancel)
8080

8181
pathConfigFile := flags.Config()
82-
rawConfig, err := application.LoadConfigFromFile(pathConfigFile)
82+
rawConfig, err := config.LoadFile(pathConfigFile)
8383
if err != nil {
8484
return errors.New(err,
8585
fmt.Sprintf("could not read configuration file %s", pathConfigFile),

x-pack/elastic-agent/pkg/agent/cmd/watch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/configuration"
2222
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
2323
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/cli"
24+
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config"
2425
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger"
2526
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release"
2627
)
@@ -182,7 +183,7 @@ func gracePeriod(marker *upgrade.UpdateMarker) (bool, time.Duration) {
182183

183184
func configuredLogger(flags *globalFlags) (*logger.Logger, error) {
184185
pathConfigFile := flags.Config()
185-
rawConfig, err := application.LoadConfigFromFile(pathConfigFile)
186+
rawConfig, err := config.LoadFile(pathConfigFile)
186187
if err != nil {
187188
return nil, errors.New(err,
188189
fmt.Sprintf("could not read configuration file %s", pathConfigFile),

0 commit comments

Comments
 (0)