Skip to content

Commit 74ce2ba

Browse files
Fix unpacking of artifact config (#776)
Fix unpacking of artifact config (#776)
1 parent 7a88635 commit 74ce2ba

File tree

4 files changed

+337
-11
lines changed

4 files changed

+337
-11
lines changed

internal/pkg/agent/application/local_mode.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
2323
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
2424
"github.com/elastic/elastic-agent/internal/pkg/agent/operation"
25+
"github.com/elastic/elastic-agent/internal/pkg/artifact"
2526
"github.com/elastic/elastic-agent/internal/pkg/capabilities"
2627
"github.com/elastic/elastic-agent/internal/pkg/composable"
2728
"github.com/elastic/elastic-agent/internal/pkg/config"
@@ -131,6 +132,7 @@ func newLocal(
131132
},
132133
caps,
133134
monitor,
135+
artifact.NewReloader(cfg.Settings.DownloadConfig, log),
134136
)
135137
if err != nil {
136138
return nil, err
@@ -203,7 +205,7 @@ func (l *Local) AgentInfo() *info.AgentInfo {
203205
}
204206

205207
func discoverer(patterns ...string) discoverFunc {
206-
var p []string
208+
p := make([]string, 0, len(patterns))
207209
for _, newP := range patterns {
208210
if len(newP) == 0 {
209211
continue

internal/pkg/artifact/config.go

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12+
c "github.com/elastic/elastic-agent-libs/config"
1213
"github.com/elastic/elastic-agent-libs/transport/httpcommon"
1314
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
1415
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
@@ -64,6 +65,42 @@ func NewReloader(cfg *Config, log *logger.Logger) *Reloader {
6465
}
6566

6667
func (r *Reloader) Reload(rawConfig *config.Config) error {
68+
if err := r.reloadConfig(rawConfig); err != nil {
69+
return errors.New(err, "failed to reload config")
70+
}
71+
72+
if err := r.reloadSourceURI(rawConfig); err != nil {
73+
return errors.New(err, "failed to reload source URI")
74+
}
75+
76+
return nil
77+
}
78+
79+
func (r *Reloader) reloadConfig(rawConfig *config.Config) error {
80+
type reloadConfig struct {
81+
C *Config `json:"agent.download" config:"agent.download"`
82+
}
83+
tmp := &reloadConfig{
84+
C: DefaultConfig(),
85+
}
86+
if err := rawConfig.Unpack(&tmp); err != nil {
87+
return err
88+
}
89+
90+
*(r.cfg) = Config{
91+
OperatingSystem: tmp.C.OperatingSystem,
92+
Architecture: tmp.C.Architecture,
93+
SourceURI: tmp.C.SourceURI,
94+
TargetDirectory: tmp.C.TargetDirectory,
95+
InstallPath: tmp.C.InstallPath,
96+
DropPath: tmp.C.DropPath,
97+
HTTPTransportSettings: tmp.C.HTTPTransportSettings,
98+
}
99+
100+
return nil
101+
}
102+
103+
func (r *Reloader) reloadSourceURI(rawConfig *config.Config) error {
67104
type reloadConfig struct {
68105
// SourceURI: source of the artifacts, e.g https://artifacts.elastic.co/downloads/
69106
SourceURI string `json:"agent.download.sourceURI" config:"agent.download.sourceURI"`
@@ -78,11 +115,11 @@ func (r *Reloader) Reload(rawConfig *config.Config) error {
78115
}
79116

80117
var newSourceURI string
81-
if cfg.FleetSourceURI != "" {
118+
if fleetURI := strings.TrimSpace(cfg.FleetSourceURI); fleetURI != "" {
82119
// fleet configuration takes precedence
83-
newSourceURI = cfg.FleetSourceURI
84-
} else if cfg.SourceURI != "" {
85-
newSourceURI = cfg.SourceURI
120+
newSourceURI = fleetURI
121+
} else if sourceURI := strings.TrimSpace(cfg.SourceURI); sourceURI != "" {
122+
newSourceURI = sourceURI
86123
}
87124

88125
if newSourceURI != "" {
@@ -148,3 +185,42 @@ func (c *Config) Arch() string {
148185
c.Architecture = arch
149186
return c.Architecture
150187
}
188+
189+
// Unpack reads a config object into the settings.
190+
func (c *Config) Unpack(cfg *c.C) error {
191+
tmp := struct {
192+
OperatingSystem string `json:"-" config:",ignore"`
193+
Architecture string `json:"-" config:",ignore"`
194+
SourceURI string `json:"sourceURI" config:"sourceURI"`
195+
TargetDirectory string `json:"targetDirectory" config:"target_directory"`
196+
InstallPath string `yaml:"installPath" config:"install_path"`
197+
DropPath string `yaml:"dropPath" config:"drop_path"`
198+
}{
199+
OperatingSystem: c.OperatingSystem,
200+
Architecture: c.Architecture,
201+
SourceURI: c.SourceURI,
202+
TargetDirectory: c.TargetDirectory,
203+
InstallPath: c.InstallPath,
204+
DropPath: c.DropPath,
205+
}
206+
207+
if err := cfg.Unpack(&tmp); err != nil {
208+
return err
209+
}
210+
211+
transport := DefaultConfig().HTTPTransportSettings
212+
if err := cfg.Unpack(&transport); err != nil {
213+
return err
214+
}
215+
216+
*c = Config{
217+
OperatingSystem: tmp.OperatingSystem,
218+
Architecture: tmp.Architecture,
219+
SourceURI: tmp.SourceURI,
220+
TargetDirectory: tmp.TargetDirectory,
221+
InstallPath: tmp.InstallPath,
222+
DropPath: tmp.DropPath,
223+
HTTPTransportSettings: transport,
224+
}
225+
return nil
226+
}
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package artifact
6+
7+
import (
8+
"testing"
9+
"time"
10+
11+
"github.com/elastic/elastic-agent/internal/pkg/config"
12+
"github.com/elastic/elastic-agent/pkg/core/logger"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestReload(t *testing.T) {
17+
type testCase struct {
18+
input string
19+
initialConfig *Config
20+
expectedSourceURI string
21+
expectedTargetDirectory string
22+
expectedInstallDirectory string
23+
expectedDropDirectory string
24+
expectedFingerprint string
25+
expectedTLS bool
26+
expectedTLSEnabled bool
27+
expectedDisableProxy bool
28+
expectedTimeout time.Duration
29+
}
30+
defaultValues := DefaultConfig()
31+
testCases := []testCase{
32+
{
33+
input: `agent.download:
34+
sourceURI: "testing.uri"
35+
target_directory: "a/b/c"
36+
install_path: "i/p"
37+
drop_path: "d/p"
38+
proxy_disable: true
39+
timeout: 33s
40+
ssl.enabled: true
41+
ssl.ca_trusted_fingerprint: "my_finger_print"
42+
`,
43+
initialConfig: DefaultConfig(),
44+
expectedSourceURI: "testing.uri",
45+
expectedTargetDirectory: "a/b/c",
46+
expectedInstallDirectory: "i/p",
47+
expectedDropDirectory: "d/p",
48+
expectedFingerprint: "my_finger_print",
49+
expectedTLS: true,
50+
expectedTLSEnabled: true,
51+
expectedDisableProxy: true,
52+
expectedTimeout: 33 * time.Second,
53+
},
54+
{
55+
input: `agent.download:
56+
sourceURI: "testing.uri"
57+
`,
58+
initialConfig: DefaultConfig(),
59+
expectedSourceURI: "testing.uri",
60+
expectedTargetDirectory: defaultValues.TargetDirectory,
61+
expectedInstallDirectory: defaultValues.InstallPath,
62+
expectedDropDirectory: defaultValues.DropPath,
63+
expectedFingerprint: "",
64+
expectedTLS: defaultValues.TLS != nil,
65+
expectedTLSEnabled: false,
66+
expectedDisableProxy: defaultValues.Proxy.Disable,
67+
expectedTimeout: defaultValues.Timeout,
68+
},
69+
{
70+
input: `agent.download:
71+
sourceURI: ""
72+
`,
73+
initialConfig: &Config{
74+
SourceURI: "testing.uri",
75+
HTTPTransportSettings: defaultValues.HTTPTransportSettings,
76+
},
77+
expectedSourceURI: defaultValues.SourceURI, // fallback to default when set to empty
78+
expectedTargetDirectory: defaultValues.TargetDirectory,
79+
expectedInstallDirectory: defaultValues.InstallPath,
80+
expectedDropDirectory: defaultValues.DropPath,
81+
expectedFingerprint: "",
82+
expectedTLS: defaultValues.TLS != nil,
83+
expectedTLSEnabled: false,
84+
expectedDisableProxy: defaultValues.Proxy.Disable,
85+
expectedTimeout: defaultValues.Timeout,
86+
},
87+
{
88+
input: ``,
89+
initialConfig: &Config{
90+
SourceURI: "testing.uri",
91+
HTTPTransportSettings: defaultValues.HTTPTransportSettings,
92+
},
93+
expectedSourceURI: defaultValues.SourceURI, // fallback to default when not set
94+
expectedTargetDirectory: defaultValues.TargetDirectory,
95+
expectedInstallDirectory: defaultValues.InstallPath,
96+
expectedDropDirectory: defaultValues.DropPath,
97+
expectedFingerprint: "",
98+
expectedTLS: defaultValues.TLS != nil,
99+
expectedTLSEnabled: false,
100+
expectedDisableProxy: defaultValues.Proxy.Disable,
101+
expectedTimeout: defaultValues.Timeout,
102+
},
103+
{
104+
input: `agent.download:
105+
sourceURI: " "
106+
`,
107+
initialConfig: &Config{
108+
SourceURI: "testing.uri",
109+
HTTPTransportSettings: defaultValues.HTTPTransportSettings,
110+
},
111+
expectedSourceURI: defaultValues.SourceURI, // fallback to default when set to whitespace
112+
expectedTargetDirectory: defaultValues.TargetDirectory,
113+
expectedInstallDirectory: defaultValues.InstallPath,
114+
expectedDropDirectory: defaultValues.DropPath,
115+
expectedFingerprint: "",
116+
expectedTLS: defaultValues.TLS != nil,
117+
expectedTLSEnabled: false,
118+
expectedDisableProxy: defaultValues.Proxy.Disable,
119+
expectedTimeout: defaultValues.Timeout,
120+
},
121+
{
122+
input: `agent.download:
123+
source_uri: " "
124+
`,
125+
initialConfig: &Config{
126+
SourceURI: "testing.uri",
127+
HTTPTransportSettings: defaultValues.HTTPTransportSettings,
128+
},
129+
expectedSourceURI: defaultValues.SourceURI, // fallback to default when set to whitespace
130+
expectedTargetDirectory: defaultValues.TargetDirectory,
131+
expectedInstallDirectory: defaultValues.InstallPath,
132+
expectedDropDirectory: defaultValues.DropPath,
133+
expectedFingerprint: "",
134+
expectedTLS: defaultValues.TLS != nil,
135+
expectedTLSEnabled: false,
136+
expectedDisableProxy: defaultValues.Proxy.Disable,
137+
expectedTimeout: defaultValues.Timeout,
138+
},
139+
{
140+
input: `agent.download:
141+
source_uri: " "
142+
sourceURI: " "
143+
`,
144+
initialConfig: DefaultConfig(),
145+
expectedSourceURI: defaultValues.SourceURI, // fallback to default when set to whitespace
146+
expectedTargetDirectory: defaultValues.TargetDirectory,
147+
expectedInstallDirectory: defaultValues.InstallPath,
148+
expectedDropDirectory: defaultValues.DropPath,
149+
expectedFingerprint: "",
150+
expectedTLS: defaultValues.TLS != nil,
151+
expectedTLSEnabled: false,
152+
expectedDisableProxy: defaultValues.Proxy.Disable,
153+
expectedTimeout: defaultValues.Timeout,
154+
},
155+
{
156+
input: ``,
157+
initialConfig: &Config{
158+
SourceURI: "testing.uri",
159+
HTTPTransportSettings: defaultValues.HTTPTransportSettings,
160+
},
161+
expectedSourceURI: defaultValues.SourceURI,
162+
expectedTargetDirectory: defaultValues.TargetDirectory,
163+
expectedInstallDirectory: defaultValues.InstallPath,
164+
expectedDropDirectory: defaultValues.DropPath,
165+
expectedFingerprint: "",
166+
expectedTLS: defaultValues.TLS != nil,
167+
expectedTLSEnabled: false,
168+
expectedDisableProxy: defaultValues.Proxy.Disable,
169+
expectedTimeout: defaultValues.Timeout,
170+
},
171+
{
172+
input: `agent.download:
173+
source_uri: " "
174+
sourceURI: "testing.uri"
175+
`,
176+
initialConfig: DefaultConfig(),
177+
expectedSourceURI: "testing.uri",
178+
expectedTargetDirectory: defaultValues.TargetDirectory,
179+
expectedInstallDirectory: defaultValues.InstallPath,
180+
expectedDropDirectory: defaultValues.DropPath,
181+
expectedFingerprint: "",
182+
expectedTLS: defaultValues.TLS != nil,
183+
expectedTLSEnabled: false,
184+
expectedDisableProxy: defaultValues.Proxy.Disable,
185+
expectedTimeout: defaultValues.Timeout,
186+
},
187+
{
188+
input: `agent.download:
189+
source_uri: "testing.uri"
190+
sourceURI: " "
191+
`,
192+
initialConfig: DefaultConfig(),
193+
expectedSourceURI: "testing.uri",
194+
expectedTargetDirectory: defaultValues.TargetDirectory,
195+
expectedInstallDirectory: defaultValues.InstallPath,
196+
expectedDropDirectory: defaultValues.DropPath,
197+
expectedFingerprint: "",
198+
expectedTLS: defaultValues.TLS != nil,
199+
expectedTLSEnabled: false,
200+
expectedDisableProxy: defaultValues.Proxy.Disable,
201+
expectedTimeout: defaultValues.Timeout,
202+
},
203+
{
204+
input: `agent.download:
205+
source_uri: "testing.uri"
206+
sourceURI: "another.uri"
207+
`,
208+
initialConfig: DefaultConfig(),
209+
expectedSourceURI: "testing.uri",
210+
expectedTargetDirectory: defaultValues.TargetDirectory,
211+
expectedInstallDirectory: defaultValues.InstallPath,
212+
expectedDropDirectory: defaultValues.DropPath,
213+
expectedFingerprint: "",
214+
expectedTLS: defaultValues.TLS != nil,
215+
expectedTLSEnabled: false,
216+
expectedDisableProxy: defaultValues.Proxy.Disable,
217+
expectedTimeout: defaultValues.Timeout,
218+
},
219+
}
220+
221+
l, _ := logger.NewTesting("t")
222+
for _, tc := range testCases {
223+
cfg := tc.initialConfig
224+
reloader := NewReloader(cfg, l)
225+
226+
c, err := config.NewConfigFrom(tc.input)
227+
require.NoError(t, err)
228+
229+
require.NoError(t, reloader.Reload(c))
230+
231+
require.Equal(t, tc.expectedSourceURI, cfg.SourceURI)
232+
require.Equal(t, tc.expectedTargetDirectory, cfg.TargetDirectory)
233+
require.Equal(t, tc.expectedInstallDirectory, cfg.InstallPath)
234+
require.Equal(t, tc.expectedDropDirectory, cfg.DropPath)
235+
require.Equal(t, tc.expectedTimeout, cfg.Timeout)
236+
237+
require.Equal(t, tc.expectedDisableProxy, cfg.Proxy.Disable)
238+
239+
if tc.expectedTLS {
240+
require.NotNil(t, cfg.TLS)
241+
require.Equal(t, tc.expectedTLSEnabled, *cfg.TLS.Enabled)
242+
require.Equal(t, tc.expectedFingerprint, cfg.TLS.CATrustedFingerprint)
243+
} else {
244+
require.Nil(t, cfg.TLS)
245+
}
246+
}
247+
}

0 commit comments

Comments
 (0)