Skip to content

Commit 4cb3d36

Browse files
Fix ES Output TLS tests
1 parent 484de39 commit 4cb3d36

File tree

3 files changed

+254
-78
lines changed

3 files changed

+254
-78
lines changed
+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
//go:build !integration && requirefips
6+
7+
package config
8+
9+
import (
10+
"crypto/tls"
11+
"net/http"
12+
"testing"
13+
"time"
14+
15+
"github.com/google/go-cmp/cmp"
16+
"github.com/google/go-cmp/cmp/cmpopts"
17+
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
19+
20+
"github.com/elastic/elastic-agent-libs/transport/tlscommon"
21+
testlog "github.com/elastic/fleet-server/v7/internal/pkg/testing/log"
22+
"github.com/elastic/go-elasticsearch/v8"
23+
)
24+
25+
func TestToESConfigTLS(t *testing.T) {
26+
testcases := map[string]struct {
27+
cfg Elasticsearch
28+
result elasticsearch.Config
29+
}{
30+
"https": {
31+
cfg: Elasticsearch{
32+
Protocol: "https",
33+
Hosts: []string{"localhost:9200", "other-host:9200"},
34+
ServiceToken: "test-token",
35+
Headers: map[string]string{
36+
"X-Custom-Header": "Header-Value",
37+
},
38+
MaxRetries: 6,
39+
MaxConnPerHost: 256,
40+
Timeout: 120 * time.Second,
41+
TLS: &tlscommon.Config{
42+
VerificationMode: tlscommon.VerifyNone,
43+
},
44+
},
45+
result: elasticsearch.Config{
46+
Addresses: []string{"https://localhost:9200", "https://other-host:9200"},
47+
ServiceToken: "test-token",
48+
Header: http.Header{"X-Custom-Header": {"Header-Value"}},
49+
MaxRetries: 6,
50+
Transport: &http.Transport{
51+
TLSClientConfig: &tls.Config{
52+
InsecureSkipVerify: true, //nolint:gosec // test case
53+
MinVersion: tls.VersionTLS12,
54+
MaxVersion: tls.VersionTLS13,
55+
Certificates: []tls.Certificate{},
56+
CurvePreferences: []tls.CurveID{},
57+
},
58+
TLSHandshakeTimeout: 10 * time.Second,
59+
MaxIdleConns: 100,
60+
MaxIdleConnsPerHost: 32,
61+
MaxConnsPerHost: 256,
62+
IdleConnTimeout: 60 * time.Second,
63+
ResponseHeaderTimeout: 120 * time.Second,
64+
ExpectContinueTimeout: 1 * time.Second,
65+
},
66+
},
67+
},
68+
"mixed-https": {
69+
cfg: Elasticsearch{
70+
Protocol: "http",
71+
Hosts: []string{"localhost:9200", "https://other-host:9200"},
72+
ServiceToken: "test-token",
73+
Headers: map[string]string{
74+
"X-Custom-Header": "Header-Value",
75+
},
76+
MaxRetries: 6,
77+
MaxConnPerHost: 256,
78+
Timeout: 120 * time.Second,
79+
TLS: &tlscommon.Config{
80+
VerificationMode: tlscommon.VerifyNone,
81+
},
82+
},
83+
result: elasticsearch.Config{
84+
Addresses: []string{"http://localhost:9200", "https://other-host:9200"},
85+
ServiceToken: "test-token",
86+
Header: http.Header{"X-Custom-Header": {"Header-Value"}},
87+
MaxRetries: 6,
88+
Transport: &http.Transport{
89+
TLSClientConfig: &tls.Config{
90+
InsecureSkipVerify: true, //nolint:gosec // test case
91+
MinVersion: tls.VersionTLS12,
92+
MaxVersion: tls.VersionTLS13,
93+
Certificates: []tls.Certificate{},
94+
CurvePreferences: []tls.CurveID{},
95+
},
96+
TLSHandshakeTimeout: 10 * time.Second,
97+
MaxIdleConns: 100,
98+
MaxIdleConnsPerHost: 32,
99+
MaxConnsPerHost: 256,
100+
IdleConnTimeout: 60 * time.Second,
101+
ResponseHeaderTimeout: 120 * time.Second,
102+
ExpectContinueTimeout: 1 * time.Second,
103+
},
104+
},
105+
},
106+
}
107+
108+
copts := cmp.Options{
109+
cmpopts.IgnoreUnexported(http.Transport{}),
110+
cmpopts.IgnoreFields(http.Transport{}, "DialContext"),
111+
cmpopts.IgnoreUnexported(tls.Config{}), //nolint:gosec //test case
112+
}
113+
114+
for name, test := range testcases {
115+
t.Run(name, func(t *testing.T) {
116+
_ = testlog.SetLogger(t)
117+
res, err := test.cfg.ToESConfig(false)
118+
require.NoError(t, err)
119+
120+
// cmp.Diff can't handle function pointers.
121+
res.Transport.(*http.Transport).Proxy = nil
122+
123+
test.result.Header.Set("X-elastic-product-origin", "fleet")
124+
assert.True(t, cmp.Equal(test.result, res, copts...), "mismatch (-want +got)\n%s", cmp.Diff(test.result, res, copts...))
125+
})
126+
}
127+
}
+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
//go:build !integration && !requirefips
6+
7+
package config
8+
9+
import (
10+
"crypto/tls"
11+
"net/http"
12+
"testing"
13+
"time"
14+
15+
"github.com/google/go-cmp/cmp"
16+
"github.com/google/go-cmp/cmp/cmpopts"
17+
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
19+
20+
"github.com/elastic/elastic-agent-libs/transport/tlscommon"
21+
testlog "github.com/elastic/fleet-server/v7/internal/pkg/testing/log"
22+
"github.com/elastic/go-elasticsearch/v8"
23+
)
24+
25+
func TestToESConfigTLS(t *testing.T) {
26+
testcases := map[string]struct {
27+
cfg Elasticsearch
28+
result elasticsearch.Config
29+
}{
30+
"https": {
31+
cfg: Elasticsearch{
32+
Protocol: "https",
33+
Hosts: []string{"localhost:9200", "other-host:9200"},
34+
ServiceToken: "test-token",
35+
Headers: map[string]string{
36+
"X-Custom-Header": "Header-Value",
37+
},
38+
MaxRetries: 6,
39+
MaxConnPerHost: 256,
40+
Timeout: 120 * time.Second,
41+
TLS: &tlscommon.Config{
42+
VerificationMode: tlscommon.VerifyNone,
43+
},
44+
},
45+
result: elasticsearch.Config{
46+
Addresses: []string{"https://localhost:9200", "https://other-host:9200"},
47+
ServiceToken: "test-token",
48+
Header: http.Header{"X-Custom-Header": {"Header-Value"}},
49+
MaxRetries: 6,
50+
Transport: &http.Transport{
51+
TLSClientConfig: &tls.Config{
52+
InsecureSkipVerify: true, //nolint:gosec // test case
53+
MinVersion: tls.VersionTLS11,
54+
MaxVersion: tls.VersionTLS13,
55+
Certificates: []tls.Certificate{},
56+
CurvePreferences: []tls.CurveID{},
57+
},
58+
TLSHandshakeTimeout: 10 * time.Second,
59+
MaxIdleConns: 100,
60+
MaxIdleConnsPerHost: 32,
61+
MaxConnsPerHost: 256,
62+
IdleConnTimeout: 60 * time.Second,
63+
ResponseHeaderTimeout: 120 * time.Second,
64+
ExpectContinueTimeout: 1 * time.Second,
65+
},
66+
},
67+
},
68+
"mixed-https": {
69+
cfg: Elasticsearch{
70+
Protocol: "http",
71+
Hosts: []string{"localhost:9200", "https://other-host:9200"},
72+
ServiceToken: "test-token",
73+
Headers: map[string]string{
74+
"X-Custom-Header": "Header-Value",
75+
},
76+
MaxRetries: 6,
77+
MaxConnPerHost: 256,
78+
Timeout: 120 * time.Second,
79+
TLS: &tlscommon.Config{
80+
VerificationMode: tlscommon.VerifyNone,
81+
},
82+
},
83+
result: elasticsearch.Config{
84+
Addresses: []string{"http://localhost:9200", "https://other-host:9200"},
85+
ServiceToken: "test-token",
86+
Header: http.Header{"X-Custom-Header": {"Header-Value"}},
87+
MaxRetries: 6,
88+
Transport: &http.Transport{
89+
TLSClientConfig: &tls.Config{
90+
InsecureSkipVerify: true, //nolint:gosec // test case
91+
MinVersion: tls.VersionTLS11,
92+
MaxVersion: tls.VersionTLS13,
93+
Certificates: []tls.Certificate{},
94+
CurvePreferences: []tls.CurveID{},
95+
},
96+
TLSHandshakeTimeout: 10 * time.Second,
97+
MaxIdleConns: 100,
98+
MaxIdleConnsPerHost: 32,
99+
MaxConnsPerHost: 256,
100+
IdleConnTimeout: 60 * time.Second,
101+
ResponseHeaderTimeout: 120 * time.Second,
102+
ExpectContinueTimeout: 1 * time.Second,
103+
},
104+
},
105+
},
106+
}
107+
108+
copts := cmp.Options{
109+
cmpopts.IgnoreUnexported(http.Transport{}),
110+
cmpopts.IgnoreFields(http.Transport{}, "DialContext"),
111+
cmpopts.IgnoreUnexported(tls.Config{}), //nolint:gosec //test case
112+
}
113+
114+
for name, test := range testcases {
115+
t.Run(name, func(t *testing.T) {
116+
_ = testlog.SetLogger(t)
117+
res, err := test.cfg.ToESConfig(false)
118+
require.NoError(t, err)
119+
120+
// cmp.Diff can't handle function pointers.
121+
res.Transport.(*http.Transport).Proxy = nil
122+
123+
test.result.Header.Set("X-elastic-product-origin", "fleet")
124+
assert.True(t, cmp.Equal(test.result, res, copts...), "mismatch (-want +got)\n%s", cmp.Diff(test.result, res, copts...))
125+
})
126+
}
127+
}

internal/pkg/config/output_test.go

-78
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
"github.com/stretchr/testify/require"
2626

2727
"github.com/elastic/go-elasticsearch/v8"
28-
29-
"github.com/elastic/elastic-agent-libs/transport/tlscommon"
3028
)
3129

3230
func TestToESConfig(t *testing.T) {
@@ -113,82 +111,6 @@ func TestToESConfig(t *testing.T) {
113111
},
114112
},
115113
},
116-
"https": {
117-
cfg: Elasticsearch{
118-
Protocol: "https",
119-
Hosts: []string{"localhost:9200", "other-host:9200"},
120-
ServiceToken: "test-token",
121-
Headers: map[string]string{
122-
"X-Custom-Header": "Header-Value",
123-
},
124-
MaxRetries: 6,
125-
MaxConnPerHost: 256,
126-
Timeout: 120 * time.Second,
127-
TLS: &tlscommon.Config{
128-
VerificationMode: tlscommon.VerifyNone,
129-
},
130-
},
131-
result: elasticsearch.Config{
132-
Addresses: []string{"https://localhost:9200", "https://other-host:9200"},
133-
ServiceToken: "test-token",
134-
Header: http.Header{"X-Custom-Header": {"Header-Value"}},
135-
MaxRetries: 6,
136-
Transport: &http.Transport{
137-
TLSClientConfig: &tls.Config{
138-
InsecureSkipVerify: true, //nolint:gosec // test case
139-
MinVersion: tls.VersionTLS11,
140-
MaxVersion: tls.VersionTLS13,
141-
Certificates: []tls.Certificate{},
142-
CurvePreferences: []tls.CurveID{},
143-
},
144-
TLSHandshakeTimeout: 10 * time.Second,
145-
MaxIdleConns: 100,
146-
MaxIdleConnsPerHost: 32,
147-
MaxConnsPerHost: 256,
148-
IdleConnTimeout: 60 * time.Second,
149-
ResponseHeaderTimeout: 120 * time.Second,
150-
ExpectContinueTimeout: 1 * time.Second,
151-
},
152-
},
153-
},
154-
"mixed-https": {
155-
cfg: Elasticsearch{
156-
Protocol: "http",
157-
Hosts: []string{"localhost:9200", "https://other-host:9200"},
158-
ServiceToken: "test-token",
159-
Headers: map[string]string{
160-
"X-Custom-Header": "Header-Value",
161-
},
162-
MaxRetries: 6,
163-
MaxConnPerHost: 256,
164-
Timeout: 120 * time.Second,
165-
TLS: &tlscommon.Config{
166-
VerificationMode: tlscommon.VerifyNone,
167-
},
168-
},
169-
result: elasticsearch.Config{
170-
Addresses: []string{"http://localhost:9200", "https://other-host:9200"},
171-
ServiceToken: "test-token",
172-
Header: http.Header{"X-Custom-Header": {"Header-Value"}},
173-
MaxRetries: 6,
174-
Transport: &http.Transport{
175-
TLSClientConfig: &tls.Config{
176-
InsecureSkipVerify: true, //nolint:gosec // test case
177-
MinVersion: tls.VersionTLS11,
178-
MaxVersion: tls.VersionTLS13,
179-
Certificates: []tls.Certificate{},
180-
CurvePreferences: []tls.CurveID{},
181-
},
182-
TLSHandshakeTimeout: 10 * time.Second,
183-
MaxIdleConns: 100,
184-
MaxIdleConnsPerHost: 32,
185-
MaxConnsPerHost: 256,
186-
IdleConnTimeout: 60 * time.Second,
187-
ResponseHeaderTimeout: 120 * time.Second,
188-
ExpectContinueTimeout: 1 * time.Second,
189-
},
190-
},
191-
},
192114
}
193115

194116
copts := cmp.Options{

0 commit comments

Comments
 (0)