-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathexporter_tls_test.go
More file actions
263 lines (239 loc) · 7.63 KB
/
Copy pathexporter_tls_test.go
File metadata and controls
263 lines (239 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package logstashexporter
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"net"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/elastic/beats/v7/x-pack/otel/oteltest"
"github.com/elastic/elastic-agent-libs/transport/tlscommontest"
v2 "github.com/elastic/go-lumber/server/v2"
)
var caCert, _ = tlscommontest.GenCA()
var invalidCACert, _ = tlscommontest.GenCA()
type serverTLSOption struct {
ClientAuthType tls.ClientAuthType
AddCAToLeaf bool
CommonName string
DNSNames []string
IPs []net.IP
}
// TestLumberjackConnection tests exporter connections with different TLS settings
func TestLumberjackConnection(t *testing.T) {
const testPassphrase = "do-re-mi-fa-so-la-ti-do" // #nosec G101
caFile := caFilePath(t)
invalidCAFile := invalidCAFilePath(t)
tests := []struct {
name string
hostname string // hostname used in exporter config
serverTLSOption *serverTLSOption
buildClientConfig func() map[string]interface{}
}{
{
name: "plain connection",
hostname: "localhost",
serverTLSOption: nil,
buildClientConfig: func() map[string]interface{} {
return map[string]interface{}{}
},
},
{
name: "TLS verification none",
hostname: "localhost",
serverTLSOption: &serverTLSOption{
ClientAuthType: tls.RequestClientCert,
CommonName: "whatever",
DNSNames: []string{"whatever"},
},
buildClientConfig: func() map[string]interface{} {
return map[string]interface{}{
"ssl.certificate_authorities": []string{invalidCAFile},
"ssl.verification_mode": "none",
}
},
},
{
name: "TLS verification certificate",
hostname: "localhost",
serverTLSOption: &serverTLSOption{
ClientAuthType: tls.RequestClientCert,
CommonName: "dont-care",
DNSNames: []string{"whatever"},
},
buildClientConfig: func() map[string]interface{} {
return map[string]interface{}{
"ssl.certificate_authorities": []string{caFile},
"ssl.verification_mode": "certificate",
}
},
},
{
name: "TLS verification full",
hostname: "localhost",
serverTLSOption: &serverTLSOption{
ClientAuthType: tls.RequestClientCert,
CommonName: "localhost",
},
buildClientConfig: func() map[string]interface{} {
return map[string]interface{}{
"ssl.certificate_authorities": []string{caFile},
"ssl.verification_mode": "full",
}
},
},
{
name: "TLS verification strict",
hostname: "localhost",
serverTLSOption: &serverTLSOption{
ClientAuthType: tls.RequestClientCert,
DNSNames: []string{"localhost"},
},
buildClientConfig: func() map[string]interface{} {
return map[string]interface{}{
"ssl.certificate_authorities": []string{caFile},
"ssl.verification_mode": "strict",
}
},
},
{
name: "mutual TLS",
hostname: "localhost",
serverTLSOption: &serverTLSOption{
ClientAuthType: tls.RequireAndVerifyClientCert,
DNSNames: []string{"localhost"},
},
buildClientConfig: func() map[string]interface{} {
clientCertificate, clientKey := oteltest.GetClientCerts(t, caCert, "")
return map[string]interface{}{
"ssl.certificate_authorities": []string{caFile},
"ssl.certificate": clientCertificate,
"ssl.key": clientKey,
}
},
},
{
name: "key passphrase",
hostname: "localhost",
serverTLSOption: &serverTLSOption{
ClientAuthType: tls.RequireAndVerifyClientCert,
DNSNames: []string{"localhost"},
},
buildClientConfig: func() map[string]interface{} {
clientCertificate, clientKey := oteltest.GetClientCerts(t, caCert, testPassphrase)
return map[string]interface{}{
"ssl.certificate_authorities": []string{caFile},
"ssl.certificate": clientCertificate,
"ssl.key": clientKey,
"ssl.key_passphrase": testPassphrase,
}
},
},
{
name: "CA trusted fingerprint",
hostname: "localhost",
serverTLSOption: &serverTLSOption{
ClientAuthType: tls.RequestClientCert,
AddCAToLeaf: true,
DNSNames: []string{"localhost"},
},
buildClientConfig: func() map[string]interface{} {
return map[string]interface{}{
"ssl.ca_trusted_fingerprint": tlscommontest.GetCertFingerprint(caCert.Leaf),
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var serverTLSConfig *tls.Config
if tt.serverTLSOption != nil {
serverTLSConfig = newServerTLSConfig(t, *tt.serverTLSOption)
}
clientConfig := tt.buildClientConfig()
testWithLumberjackServer(t, tt.hostname, serverTLSConfig, clientConfig)
})
}
}
func testWithLumberjackServer(t *testing.T, hostname string, tlsConfig *tls.Config, expConfig map[string]any) {
server, addr := createLumberjackServer(t, tlsConfig)
t.Logf("Lumberjack Server address %v", addr)
// add lumberjack address to exporter config
var hosts []string
if v, ok := expConfig["hosts"]; ok {
hosts, _ = v.([]string)
}
hostname = strings.Replace(addr, "127.0.0.1", hostname, 1)
hosts = append(hosts, hostname)
expConfig["hosts"] = hosts
exp := newExporterWithDefaultsWith(t, expConfig)
t.Cleanup(func() { _ = exp.Shutdown(t.Context()) })
logs := newTestLogs()
go func() {
defer server.Close()
timeoutCtx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
clientCtx := newTestBeatsClientContext(timeoutCtx)
err := exp.ConsumeLogs(clientCtx, logs)
require.NoError(t, err)
}()
for batch := range server.ReceiveChan() {
batch.ACK()
events := batch.Events
assert.Equal(t, 1, len(events))
msg, ok := events[0].(map[string]interface{})
assert.True(t, ok)
assert.Equal(t, "test log message", msg["value"])
}
}
func createLumberjackServer(t *testing.T, tlsConfig *tls.Config) (*v2.Server, string) {
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("failed to generate TCP listener: %v", err)
}
if tlsConfig != nil {
listener = tls.NewListener(listener, tlsConfig)
}
server, _ := v2.NewWithListener(listener)
return server, listener.Addr().String()
}
func newServerTLSConfig(t *testing.T, option serverTLSOption) *tls.Config {
serverCerts, err := tlscommontest.GenSignedCert(caCert, x509.KeyUsageCertSign, false, option.CommonName, option.DNSNames, option.IPs, false)
if err != nil {
t.Fatalf("could not generate certificates: %s", err)
}
if option.AddCAToLeaf {
serverCerts.Certificate = append(serverCerts.Certificate, caCert.Certificate...)
}
certPool := x509.NewCertPool()
certPool.AddCert(caCert.Leaf)
return &tls.Config{
ClientAuth: option.ClientAuthType,
ClientCAs: certPool,
Certificates: []tls.Certificate{serverCerts},
MinVersion: tls.VersionTLS12,
}
}
func caFilePath(t *testing.T) string {
filePath := filepath.Join(t.TempDir(), "ca.pem")
require.NoError(t, os.WriteFile(filePath, pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: caCert.Leaf.Raw}), 0o644), "error writing ca pem blocks to a file")
return filePath
}
func invalidCAFilePath(t *testing.T) string {
filePath := filepath.Join(t.TempDir(), "invalid-ca.pem")
require.NoError(t, os.WriteFile(filePath, pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: invalidCACert.Leaf.Raw}), 0o644), "error writing ca pem blocks to a file")
return filePath
}