Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed: source uri reload for download/verify components #1252

Merged
merged 7 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
- Use the Elastic Agent configuration directory as the root of the `inputs.d` folder. {issues}663[663]
- Fix a panic caused by a race condition when installing the Elastic Agent. {issues}806[806]
- Remove fleet event reporter and events from checkin body. {issue}993[993]
- Source uri reload for download/verify components {pull}1252[1252]
michalpristas marked this conversation as resolved.
Show resolved Hide resolved

==== New features

Expand Down
39 changes: 39 additions & 0 deletions internal/pkg/agent/operation/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,52 @@ func (o *Operator) Reload(rawConfig *config.Config) error {
return errors.New(err, "failed to unpack artifact config")
}

sourceURI, err := reloadSourceURI(o.logger, rawConfig)
if err != nil {
return errors.New(err, "failed to parse source URI")
}
tmp.C.SourceURI = sourceURI

if err := o.reloadComponent(o.downloader, "downloader", tmp.C); err != nil {
return err
}

return o.reloadComponent(o.verifier, "verifier", tmp.C)
}

func reloadSourceURI(logger *logger.Logger, rawConfig *config.Config) (string, error) {
type reloadConfig struct {
// SourceURI: source of the artifacts, e.g https://artifacts.elastic.co/downloads/
SourceURI string `json:"agent.download.sourceURI" config:"agent.download.sourceURI"`

// FleetSourceURI: source of the artifacts, e.g https://artifacts.elastic.co/downloads/ coming from fleet which uses
// different naming.
FleetSourceURI string `json:"agent.download.source_uri" config:"agent.download.source_uri"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update this in config.go as well?

// SourceURI: source of the artifacts, e.g https://artifacts.elastic.co/downloads/
SourceURI string `json:"sourceURI" config:"sourceURI"`

Handling this here instead of having this function live with the rest of the config parsing logic might mean this bug can come back in a different part of the code in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also an option but i was a bit reluctant as it would enable using this in our config file which does not really make sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can document that Fleet uses a different key, also we can file a follow up issue with the Fleet UI team to use the key we actually intended to use.

Copy link
Member

@cmacknz cmacknz Sep 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see pretty much the same code added in #686 which highly suggests we should try to handle this in a single place so we don't end up with this workaround duplicated or forgotten elsewhere in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with not duplicating, but I'd rather fix fleet first, then using your config here and on the reloader introduced in #686.

}
cfg := &reloadConfig{}
if err := rawConfig.Unpack(&cfg); err != nil {
return "", errors.New(err, "failed to unpack config during reload")
}

var newSourceURI string
if fleetURI := strings.TrimSpace(cfg.FleetSourceURI); fleetURI != "" {
// fleet configuration takes precedence
newSourceURI = fleetURI
} else if sourceURI := strings.TrimSpace(cfg.SourceURI); sourceURI != "" {
newSourceURI = sourceURI
}

if newSourceURI != "" {
logger.Infof("Source URI in operator changed to %q", newSourceURI)
return newSourceURI, nil
}

// source uri unset, reset to default
logger.Infof("Source URI in reset %q", artifact.DefaultSourceURI)
return artifact.DefaultSourceURI, nil
Comment on lines +179 to +186
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion]
It's minor but I'd swap it, as the intention of the function is to reload the source URI, I'd do an earlier return on everything else.

Suggested change
if newSourceURI != "" {
logger.Infof("Source URI in operator changed to %q", newSourceURI)
return newSourceURI, nil
}
// source uri unset, reset to default
logger.Infof("Source URI in reset %q", artifact.DefaultSourceURI)
return artifact.DefaultSourceURI, nil
// source uri unset, reset to default
if newSourceURI == "" {
logger.Infof("Source URI in reset %q", artifact.DefaultSourceURI)
return artifact.DefaultSourceURI, nil
}
logger.Infof("Source URI in operator changed to %q", newSourceURI)
return newSourceURI, nil


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT]

Suggested change

}

func (o *Operator) reloadComponent(component interface{}, name string, cfg *artifact.Config) error {
r, ok := component.(artifact.ConfigReloader)
if !ok {
Expand Down
64 changes: 58 additions & 6 deletions internal/pkg/agent/operation/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-client/v7/pkg/proto"

"github.com/elastic/elastic-agent/internal/pkg/agent/program"
"github.com/elastic/elastic-agent/internal/pkg/artifact"
"github.com/elastic/elastic-agent/internal/pkg/config"
"github.com/elastic/elastic-agent/internal/pkg/core/state"
)

Expand Down Expand Up @@ -71,7 +74,7 @@ func TestConfigurableRun(t *testing.T) {
if err := operator.start(p, nil); err != nil {
t.Fatal(err)
}
defer operator.stop(p) // failure catch, to ensure no sub-process stays running
defer func() { _ = operator.stop(p) }() // failure catch, to ensure no sub-process stays running

waitFor(t, func() error {
items := operator.State()
Expand All @@ -87,6 +90,7 @@ func TestConfigurableRun(t *testing.T) {

// try to configure
cfg := make(map[string]interface{})
//nolint:gosec // rand is ok for test
tstFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("tmp%d", rand.Uint32()))
cfg["TestFile"] = tstFilePath
if err := operator.pushConfig(p, cfg); err != nil {
Expand Down Expand Up @@ -145,7 +149,7 @@ func TestConfigurableFailed(t *testing.T) {
if err := operator.start(p, nil); err != nil {
t.Fatal(err)
}
defer operator.stop(p) // failure catch, to ensure no sub-process stays running
defer func() { _ = operator.stop(p) }() // failure catch, to ensure no sub-process stays running

var pid int
waitFor(t, func() error {
Expand All @@ -172,6 +176,7 @@ func TestConfigurableFailed(t *testing.T) {

// try to configure (with failed status)
cfg := make(map[string]interface{})
//nolint:gosec // rand is ok for test
tstFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("tmp%d", rand.Uint32()))
cfg["TestFile"] = tstFilePath
cfg["Status"] = proto.StateObserved_FAILED
Expand Down Expand Up @@ -254,7 +259,7 @@ func TestConfigurableCrash(t *testing.T) {
if err := operator.start(p, nil); err != nil {
t.Fatal(err)
}
defer operator.stop(p) // failure catch, to ensure no sub-process stays running
defer func() { _ = operator.stop(p) }() // failure catch, to ensure no sub-process stays running

var pid int
waitFor(t, func() error {
Expand All @@ -272,6 +277,7 @@ func TestConfigurableCrash(t *testing.T) {

// try to configure (with failed status)
cfg := make(map[string]interface{})
//nolint:gosec // rand is ok for test
tstFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("tmp%d", rand.Uint32()))
cfg["TestFile"] = tstFilePath
cfg["Crash"] = true
Expand Down Expand Up @@ -352,7 +358,7 @@ func TestConfigurableStartStop(t *testing.T) {
p := getProgram("configurable", "1.0")

operator := getTestOperator(t, downloadPath, installPath, p)
defer operator.stop(p) // failure catch, to ensure no sub-process stays running
defer func() { _ = operator.stop(p) }() // failure catch, to ensure no sub-process stays running

// start and stop it 3 times
for i := 0; i < 3; i++ {
Expand Down Expand Up @@ -396,11 +402,11 @@ func TestConfigurableService(t *testing.T) {
if err := operator.start(p, nil); err != nil {
t.Fatal(err)
}
defer operator.stop(p) // failure catch, to ensure no sub-process stays running
defer func() { _ = operator.stop(p) }() // failure catch, to ensure no sub-process stays running

// emulating a service, so we need to start the binary here in the test
spec := p.ProcessSpec()
cmd := exec.Command(spec.BinaryPath, fmt.Sprintf("%d", p.ServicePort()))
cmd := exec.Command(spec.BinaryPath, fmt.Sprintf("%d", p.ServicePort())) //nolint:gosec,G204 // this is fine
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Dir = filepath.Dir(spec.BinaryPath)
cmd.Stdout = os.Stdout
Expand All @@ -423,6 +429,7 @@ func TestConfigurableService(t *testing.T) {

// try to configure
cfg := make(map[string]interface{})
//nolint:gosec // rand is ok for test
tstFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("tmp%d", rand.Uint32()))
cfg["TestFile"] = tstFilePath
if err := operator.pushConfig(p, cfg); err != nil {
Expand Down Expand Up @@ -462,6 +469,51 @@ func TestConfigurableService(t *testing.T) {
}
}

func TestReloadSourceURI(t *testing.T) {
testCases := map[string]struct {
IncomingConfig map[string]interface{}
ExpectedSourceURI string
}{
"no-config": {
IncomingConfig: map[string]interface{}{},
ExpectedSourceURI: artifact.DefaultSourceURI,
},
"source-uri-provided": {
IncomingConfig: map[string]interface{}{
"agent.download.sourceURI": "http://source-uri",
},
ExpectedSourceURI: "http://source-uri",
},
"fleet-source-uri-provided": {
IncomingConfig: map[string]interface{}{
"agent.download.source_uri": "http://fleet-source-uri",
},
ExpectedSourceURI: "http://fleet-source-uri",
},
"both-source-uri-provided": {
IncomingConfig: map[string]interface{}{
"agent.download.sourceURI": "http://source-uri",
"agent.download.source_uri": "http://fleet-source-uri",
},
ExpectedSourceURI: "http://fleet-source-uri",
},
}

l := getLogger()
for testName, tc := range testCases {
t.Run(testName, func(t *testing.T) {
cfg, err := config.NewConfigFrom(tc.IncomingConfig)
require.NoError(t, err)
require.NotNil(t, cfg)

sourceUri, err := reloadSourceURI(l, cfg)
require.NoError(t, err)
require.Equal(t, tc.ExpectedSourceURI, sourceUri)

})
}
}

func isAvailable(name, version string) error {
p := getProgram(name, version)
spec := p.ProcessSpec()
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/artifact/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
linux = "linux"
windows = "windows"

defaultSourceURI = "https://artifacts.elastic.co/downloads/"
DefaultSourceURI = "https://artifacts.elastic.co/downloads/"
)

type ConfigReloader interface {
Expand Down Expand Up @@ -139,8 +139,8 @@ func (r *Reloader) reloadSourceURI(rawConfig *config.Config) error {
r.cfg.SourceURI = newSourceURI
} else {
// source uri unset, reset to default
r.log.Infof("Source URI reset from %q to %q", r.cfg.SourceURI, defaultSourceURI)
r.cfg.SourceURI = defaultSourceURI
r.log.Infof("Source URI reset from %q to %q", r.cfg.SourceURI, DefaultSourceURI)
r.cfg.SourceURI = DefaultSourceURI
}

return nil
Expand All @@ -156,7 +156,7 @@ func DefaultConfig() *Config {
transport.Timeout = 10 * time.Minute

return &Config{
SourceURI: defaultSourceURI,
SourceURI: DefaultSourceURI,
TargetDirectory: paths.Downloads(),
InstallPath: paths.Install(),
HTTPTransportSettings: transport,
Expand Down