Skip to content

Commit cfbd81b

Browse files
authored
[Elastic Agent] Fix merging of fleet.yml. Add --staging to enroll cmd. (elastic#20026)
* Fix usage of merged elastic-agent.yml and fleet.yml. Add --staging command line option to enroll. * Add to docs. * Add changelog. * Fix import sorting.
1 parent d7a130f commit cfbd81b

5 files changed

Lines changed: 35 additions & 5 deletions

File tree

x-pack/elastic-agent/CHANGELOG.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
- Forward revision number of the configuration to the endpoint. {pull}19759[19759]
5151
- Remove support for logs type and use logfile {pull}19761[19761]
5252
- Avoid comparing uncomparable types on enroll {issue}19976[19976]
53+
- Fix issues with merging of elastic-agent.yml and fleet.yml {pull}20026[20026]
5354

5455
==== New features
5556

@@ -88,3 +89,4 @@
8889
- Agent now sends its own logs to elasticsearch {pull}19811[19811]
8990
- Add --insecure option to enroll command {pull}19900[19900]
9091
- Will retry to enroll if the server return a 429. {pull}19918[19811]
92+
- Add --staging option to enroll command {pull}20026[20026]

x-pack/elastic-agent/docs/elastic-agent-command-line.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ Force overwrite the current and do not prompt for confirmation.
3838

3939
`--insecure`::
4040
Allow insecure connection to Kibana.
41+
42+
`--staging`::
43+
Configures agent to download artifacts from a staging build.

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/core/logger"
2323
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/fleetapi"
2424
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/kibana"
25+
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/release"
2526
)
2627

2728
type store interface {
@@ -64,6 +65,7 @@ type EnrollCmdOption struct {
6465
Insecure bool
6566
UserProvidedMetadata map[string]interface{}
6667
EnrollAPIKey string
68+
Staging string
6769
}
6870

6971
func (e *EnrollCmdOption) kibanaConfig() (*kibana.Config, error) {
@@ -173,11 +175,19 @@ func (c *EnrollCmd) Execute() error {
173175
}
174176

175177
fleetConfig, err := createFleetConfigFromEnroll(resp.Item.AccessAPIKey, c.kibanaConfig)
178+
agentConfig := map[string]interface{}{
179+
"id": resp.Item.ID,
180+
}
181+
if c.options.Staging != "" {
182+
staging := fmt.Sprintf("https://staging.elastic.co/%s-%s/downloads/", release.Version(), c.options.Staging[:8])
183+
agentConfig["download"] = map[string]interface{}{
184+
"sourceURI": staging,
185+
}
186+
}
187+
176188
configToStore := map[string]interface{}{
177189
"fleet": fleetConfig,
178-
"agent": map[string]interface{}{
179-
"id": resp.Item.ID,
180-
},
190+
"agent": agentConfig,
181191
}
182192

183193
reader, err := yamlToReader(configToStore)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,15 @@ func newManaged(
8181
}
8282

8383
// merge local configuration and configuration persisted from fleet.
84-
rawConfig.Merge(config)
84+
err = rawConfig.Merge(config)
85+
if err != nil {
86+
return nil, errors.New(err,
87+
fmt.Sprintf("fail to merge configuration with %s for the elastic-agent", path),
88+
errors.TypeConfig,
89+
errors.M(errors.MetaKeyPath, path))
90+
}
8591

86-
cfg, err := configuration.NewFromConfig(config)
92+
cfg, err := configuration.NewFromConfig(rawConfig)
8793
if err != nil {
8894
return nil, errors.New(err,
8995
fmt.Sprintf("fail to unpack configuration from %s", path),

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func newEnrollCommandWithArgs(flags *globalFlags, _ []string, streams *cli.IOStr
4444
cmd.Flags().StringP("ca-sha256", "p", "", "Comma separated list of certificate authorities hash pins used for certificate verifications")
4545
cmd.Flags().BoolP("force", "f", false, "Force overwrite the current and do not prompt for confirmation")
4646
cmd.Flags().BoolP("insecure", "i", false, "Allow insecure connection to Kibana")
47+
cmd.Flags().StringP("staging", "", "", "Configures agent to download artifacts from a staging build")
4748

4849
return cmd
4950
}
@@ -67,6 +68,13 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args
6768
errors.M(errors.MetaKeyPath, pathConfigFile))
6869
}
6970

71+
staging, _ := cmd.Flags().GetString("staging")
72+
if staging != "" {
73+
if len(staging) < 8 {
74+
return errors.New(fmt.Errorf("invalid staging build hash; must be at least 8 characters"), "Error")
75+
}
76+
}
77+
7078
force, _ := cmd.Flags().GetBool("force")
7179
if !force {
7280
confirm, err := c.Confirm("This will replace your current settings. Do you want to continue?", true)
@@ -105,6 +113,7 @@ func enroll(streams *cli.IOStreams, cmd *cobra.Command, flags *globalFlags, args
105113
CASha256: caSHA256,
106114
Insecure: insecure,
107115
UserProvidedMetadata: make(map[string]interface{}),
116+
Staging: staging,
108117
}
109118

110119
c, err := application.NewEnrollCmd(

0 commit comments

Comments
 (0)