Skip to content

Commit 6768f25

Browse files
Revert "agent-mode loads output from policy (#3411)" (#3495)
This reverts commit fe7955b.
1 parent 1540659 commit 6768f25

13 files changed

+69
-714
lines changed

changelog/fragments/1712108631-Use-policy-outputs-when-running-in-agent-mode.yaml

-38
This file was deleted.

internal/pkg/config/config.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ const kRedacted = "[redacted]"
3939
// The env vars that `elastic-agent container` command uses are unrelated.
4040
// The agent will do all substitutions before sending fleet-server the complete config.
4141
type Config struct {
42-
Fleet Fleet `config:"fleet"`
43-
Output Output `config:"output"`
44-
Inputs []Input `config:"inputs"`
45-
Logging Logging `config:"logging"`
46-
HTTP HTTP `config:"http"`
47-
RevisionIdx int64 `config:",ignore"`
48-
m sync.Mutex
42+
Fleet Fleet `config:"fleet"`
43+
Output Output `config:"output"`
44+
Inputs []Input `config:"inputs"`
45+
Logging Logging `config:"logging"`
46+
HTTP HTTP `config:"http"`
47+
m sync.Mutex
4948
}
5049

5150
var deprecatedConfigOptions = map[string]string{

internal/pkg/config/output.go

+5-116
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package config
66

77
import (
8-
"crypto/tls"
98
"fmt"
109
"net"
1110
"net/http"
@@ -26,14 +25,6 @@ import (
2625
const httpTransportLongPollTimeout = 10 * time.Minute
2726
const schemeHTTP = "http"
2827

29-
const (
30-
DefaultElasticsearchHost = "localhost:9200"
31-
DefaultElasticsearchTimeout = 90 * time.Second
32-
DefaultElasticsearchMaxRetries = 3
33-
DefaultElasticsearchMaxConnPerHost = 128
34-
DefaultElasticsearchMaxContentLength = 100 * 1024 * 1024
35-
)
36-
3728
var hasScheme = regexp.MustCompile(`^([a-z][a-z0-9+\-.]*)://`)
3829

3930
// Output is the output configuration to elasticsearch.
@@ -63,11 +54,11 @@ type Elasticsearch struct {
6354
// InitDefaults initializes the defaults for the configuration.
6455
func (c *Elasticsearch) InitDefaults() {
6556
c.Protocol = schemeHTTP
66-
c.Hosts = []string{DefaultElasticsearchHost}
67-
c.Timeout = DefaultElasticsearchTimeout
68-
c.MaxRetries = DefaultElasticsearchMaxRetries
69-
c.MaxConnPerHost = DefaultElasticsearchMaxConnPerHost
70-
c.MaxContentLength = DefaultElasticsearchMaxContentLength
57+
c.Hosts = []string{"localhost:9200"}
58+
c.Timeout = 90 * time.Second
59+
c.MaxRetries = 3
60+
c.MaxConnPerHost = 128
61+
c.MaxContentLength = 100 * 1024 * 1024
7162
}
7263

7364
// Validate ensures that the configuration is valid.
@@ -182,108 +173,6 @@ func (c *Elasticsearch) ToESConfig(longPoll bool) (elasticsearch.Config, error)
182173
}, nil
183174
}
184175

185-
// MergeElasticsearchPolicy will merge elasticsearch settings retrieved from the fleet-server's policy into the base configuration and return the resulting config.
186-
// ucfg.Merge and config.Config.Merge will both fail at merging configs because the verification mode is not detect as a string type value
187-
func MergeElasticsearchFromPolicy(cfg, pol Elasticsearch) Elasticsearch {
188-
res := Elasticsearch{
189-
Protocol: cfg.Protocol,
190-
Hosts: cfg.Hosts,
191-
Headers: cfg.Headers,
192-
ServiceToken: cfg.ServiceToken, // ServiceToken will always be specified from the settings and not in the policy.
193-
ServiceTokenPath: cfg.ServiceTokenPath,
194-
ProxyURL: cfg.ProxyURL,
195-
ProxyDisable: cfg.ProxyDisable,
196-
ProxyHeaders: cfg.ProxyHeaders,
197-
TLS: mergeElasticsearchTLS(cfg.TLS, pol.TLS), // tls can be a special case
198-
MaxRetries: cfg.MaxRetries,
199-
MaxConnPerHost: cfg.MaxConnPerHost,
200-
Timeout: cfg.Timeout,
201-
MaxContentLength: cfg.MaxContentLength,
202-
}
203-
// If policy has a non-default Hosts value use it's values for Protocol and hosts
204-
if pol.Hosts != nil && !(len(pol.Hosts) == 1 && pol.Hosts[0] == DefaultElasticsearchHost) {
205-
res.Protocol = pol.Protocol
206-
res.Hosts = pol.Hosts
207-
}
208-
if pol.Headers != nil {
209-
res.Headers = pol.Headers
210-
}
211-
// If the policy ProxyURL is set, use all of the policy's Proxy values.
212-
if pol.ProxyURL != "" {
213-
res.ProxyURL = pol.ProxyURL
214-
res.ProxyDisable = pol.ProxyDisable
215-
res.ProxyHeaders = pol.ProxyHeaders
216-
}
217-
if pol.MaxRetries != DefaultElasticsearchMaxRetries {
218-
res.MaxRetries = pol.MaxRetries
219-
}
220-
if pol.MaxConnPerHost != DefaultElasticsearchMaxConnPerHost {
221-
res.MaxConnPerHost = pol.MaxConnPerHost
222-
}
223-
if pol.Timeout != DefaultElasticsearchTimeout {
224-
res.Timeout = pol.Timeout
225-
}
226-
if pol.MaxContentLength != DefaultElasticsearchMaxContentLength {
227-
res.MaxContentLength = pol.MaxContentLength
228-
}
229-
return res
230-
}
231-
232-
// mergeElasticsearchTLS merges the TLS settings received from the fleet-server's policy into the settings the agent passes
233-
func mergeElasticsearchTLS(cfg, pol *tlscommon.Config) *tlscommon.Config {
234-
if cfg == nil && pol == nil {
235-
return nil
236-
} else if cfg == nil && pol != nil {
237-
return pol
238-
} else if cfg != nil && pol == nil {
239-
return cfg
240-
}
241-
res := &tlscommon.Config{
242-
Enabled: cfg.Enabled,
243-
VerificationMode: cfg.VerificationMode,
244-
Versions: cfg.Versions,
245-
CipherSuites: cfg.CipherSuites,
246-
CAs: cfg.CAs,
247-
Certificate: cfg.Certificate,
248-
CurveTypes: cfg.CurveTypes,
249-
Renegotiation: cfg.Renegotiation,
250-
CASha256: cfg.CASha256,
251-
CATrustedFingerprint: cfg.CATrustedFingerprint,
252-
}
253-
if pol.Enabled != nil {
254-
res.Enabled = pol.Enabled
255-
}
256-
if pol.VerificationMode != tlscommon.VerifyFull {
257-
res.VerificationMode = pol.VerificationMode // VerificationMode defaults to VerifyFull
258-
}
259-
if pol.Versions != nil {
260-
res.Versions = pol.Versions
261-
}
262-
if pol.CipherSuites != nil {
263-
res.CipherSuites = pol.CipherSuites
264-
}
265-
if pol.CAs != nil {
266-
res.CAs = pol.CAs
267-
}
268-
if pol.Certificate.Certificate != "" {
269-
res.Certificate = pol.Certificate
270-
}
271-
if pol.CurveTypes != nil {
272-
res.CurveTypes = pol.CurveTypes
273-
}
274-
if pol.Renegotiation != tlscommon.TLSRenegotiationSupport(tls.RenegotiateNever) {
275-
res.Renegotiation = pol.Renegotiation
276-
}
277-
if pol.CASha256 != nil {
278-
res.CASha256 = pol.CASha256
279-
}
280-
if pol.CATrustedFingerprint != "" {
281-
res.CATrustedFingerprint = pol.CATrustedFingerprint
282-
}
283-
284-
return res
285-
}
286-
287176
// Validate validates that only elasticsearch is defined on the output.
288177
func (c *Output) Validate() error {
289178
if c.Extra == nil {

internal/pkg/config/output_test.go

-167
Original file line numberDiff line numberDiff line change
@@ -382,170 +382,3 @@ func setTestEnv(t *testing.T, env map[string]string) {
382382
t.Setenv(k, v)
383383
}
384384
}
385-
386-
func TestMergeElasticsearchFromPolicy(t *testing.T) {
387-
cfg := Elasticsearch{
388-
Protocol: "http",
389-
Hosts: []string{"elasticsearch:9200"},
390-
ServiceToken: "token",
391-
Timeout: time.Second,
392-
MaxRetries: 1,
393-
MaxConnPerHost: 1,
394-
MaxContentLength: 1,
395-
}
396-
tests := []struct {
397-
name string
398-
pol Elasticsearch
399-
res Elasticsearch
400-
}{{
401-
name: "default policy",
402-
pol: Elasticsearch{
403-
Hosts: []string{"localhost:9200"},
404-
Timeout: DefaultElasticsearchTimeout,
405-
MaxRetries: DefaultElasticsearchMaxRetries,
406-
MaxConnPerHost: DefaultElasticsearchMaxConnPerHost,
407-
MaxContentLength: DefaultElasticsearchMaxContentLength,
408-
},
409-
res: Elasticsearch{
410-
Protocol: "http",
411-
Hosts: []string{"elasticsearch:9200"},
412-
ServiceToken: "token",
413-
Timeout: time.Second,
414-
MaxRetries: 1,
415-
MaxConnPerHost: 1,
416-
MaxContentLength: 1,
417-
},
418-
}, {
419-
name: "hosts differ",
420-
pol: Elasticsearch{
421-
Protocol: "https",
422-
Hosts: []string{"elasticsearch:9200", "other:9200"},
423-
Timeout: DefaultElasticsearchTimeout,
424-
MaxRetries: DefaultElasticsearchMaxRetries,
425-
MaxConnPerHost: DefaultElasticsearchMaxConnPerHost,
426-
MaxContentLength: DefaultElasticsearchMaxContentLength,
427-
},
428-
res: Elasticsearch{
429-
Protocol: "https",
430-
Hosts: []string{"elasticsearch:9200", "other:9200"},
431-
ServiceToken: "token",
432-
Timeout: time.Second,
433-
MaxRetries: 1,
434-
MaxConnPerHost: 1,
435-
MaxContentLength: 1,
436-
},
437-
}, {
438-
name: "all non tls attributes differ",
439-
pol: Elasticsearch{
440-
Protocol: "https",
441-
Hosts: []string{"elasticsearch:9200", "other:9200"},
442-
Headers: map[string]string{"custom": "value"},
443-
ProxyURL: "http://proxy:8080",
444-
ProxyDisable: false,
445-
ProxyHeaders: map[string]string{"proxyhead": "proxyval"},
446-
Timeout: time.Second * 2,
447-
MaxRetries: 2,
448-
MaxConnPerHost: 3,
449-
MaxContentLength: 4,
450-
},
451-
res: Elasticsearch{
452-
Protocol: "https",
453-
Hosts: []string{"elasticsearch:9200", "other:9200"},
454-
Headers: map[string]string{"custom": "value"},
455-
ProxyURL: "http://proxy:8080",
456-
ProxyDisable: false,
457-
ProxyHeaders: map[string]string{"proxyhead": "proxyval"},
458-
ServiceToken: "token",
459-
Timeout: 2 * time.Second,
460-
MaxRetries: 2,
461-
MaxConnPerHost: 3,
462-
MaxContentLength: 4,
463-
},
464-
}}
465-
for _, tc := range tests {
466-
t.Run(tc.name, func(t *testing.T) {
467-
res := MergeElasticsearchFromPolicy(cfg, tc.pol)
468-
assert.Equal(t, tc.res.Protocol, res.Protocol)
469-
require.Len(t, res.Hosts, len(tc.res.Hosts))
470-
for i, host := range tc.res.Hosts {
471-
assert.Equalf(t, host, res.Hosts[i], "host %d does not match", i)
472-
}
473-
require.Len(t, res.Headers, len(tc.res.Headers))
474-
for k, v := range tc.res.Headers {
475-
assert.Equal(t, v, res.Headers[k])
476-
}
477-
assert.Equal(t, tc.res.ServiceToken, res.ServiceToken)
478-
assert.Equal(t, tc.res.ServiceTokenPath, res.ServiceTokenPath)
479-
assert.Equal(t, tc.res.ProxyURL, res.ProxyURL)
480-
assert.Equal(t, tc.res.ProxyDisable, res.ProxyDisable)
481-
require.Len(t, res.ProxyHeaders, len(tc.res.ProxyHeaders))
482-
for k, v := range tc.res.ProxyHeaders {
483-
assert.Equal(t, v, res.ProxyHeaders[k])
484-
}
485-
assert.Nil(t, res.TLS)
486-
assert.Equal(t, tc.res.MaxRetries, res.MaxRetries)
487-
assert.Equal(t, tc.res.MaxConnPerHost, res.MaxConnPerHost)
488-
assert.Equal(t, tc.res.Timeout, res.Timeout)
489-
assert.Equal(t, tc.res.MaxContentLength, res.MaxContentLength)
490-
})
491-
}
492-
}
493-
494-
func TestMergeElasticsearchTLS(t *testing.T) {
495-
enabled := true
496-
disabled := false
497-
t.Run("both nil", func(t *testing.T) {
498-
res := mergeElasticsearchTLS(nil, nil)
499-
assert.Nil(t, res)
500-
})
501-
t.Run("cfg not nil", func(t *testing.T) {
502-
res := mergeElasticsearchTLS(&tlscommon.Config{
503-
Enabled: &enabled,
504-
VerificationMode: tlscommon.VerifyFull,
505-
}, nil)
506-
require.NotNil(t, res)
507-
assert.True(t, *res.Enabled)
508-
assert.Equal(t, tlscommon.VerifyFull, res.VerificationMode)
509-
})
510-
t.Run("pol not nil", func(t *testing.T) {
511-
res := mergeElasticsearchTLS(nil, &tlscommon.Config{
512-
Enabled: &enabled,
513-
VerificationMode: tlscommon.VerifyFull,
514-
})
515-
require.NotNil(t, res)
516-
assert.True(t, *res.Enabled)
517-
assert.Equal(t, tlscommon.VerifyFull, res.VerificationMode)
518-
})
519-
t.Run("both not nil", func(t *testing.T) {
520-
res := mergeElasticsearchTLS(&tlscommon.Config{
521-
Enabled: &disabled,
522-
VerificationMode: tlscommon.VerifyFull,
523-
}, &tlscommon.Config{
524-
Enabled: &enabled,
525-
VerificationMode: tlscommon.VerifyCertificate,
526-
Versions: []tlscommon.TLSVersion{tlscommon.TLSVersion13},
527-
CipherSuites: []tlscommon.CipherSuite{tlscommon.CipherSuite(tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA)},
528-
CAs: []string{"/path/to/ca.crt"},
529-
Certificate: tlscommon.CertificateConfig{
530-
Certificate: "/path/to/cert.crt",
531-
Key: "/path/to/key.crt",
532-
},
533-
CASha256: []string{"casha256val"},
534-
CATrustedFingerprint: "fingerprint",
535-
})
536-
require.NotNil(t, res)
537-
assert.True(t, *res.Enabled)
538-
assert.Equal(t, tlscommon.VerifyCertificate, res.VerificationMode)
539-
require.Len(t, res.Versions, 1)
540-
assert.Equal(t, tlscommon.TLSVersion13, res.Versions[0])
541-
require.Len(t, res.CipherSuites, 1)
542-
assert.Equal(t, tlscommon.CipherSuite(tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA), res.CipherSuites[0])
543-
require.Len(t, res.CAs, 1)
544-
assert.Equal(t, "/path/to/ca.crt", res.CAs[0])
545-
assert.Equal(t, "/path/to/cert.crt", res.Certificate.Certificate)
546-
assert.Equal(t, "/path/to/key.crt", res.Certificate.Key)
547-
require.Len(t, res.CASha256, 1)
548-
assert.Equal(t, "casha256val", res.CASha256[0])
549-
assert.Equal(t, "fingerprint", res.CATrustedFingerprint)
550-
})
551-
}

0 commit comments

Comments
 (0)