forked from elastic/fleet-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
361 lines (318 loc) · 8.85 KB
/
server_test.go
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// 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.
//go:build !integration
package api
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net/http"
"sync"
"testing"
"time"
"github.com/elastic/elastic-agent-client/v7/pkg/client"
libsconfig "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/transport/tlscommon"
"github.com/elastic/go-ucfg/yaml"
"github.com/stretchr/testify/require"
fbuild "github.com/elastic/fleet-server/v7/internal/pkg/build"
"github.com/elastic/fleet-server/v7/internal/pkg/cache"
"github.com/elastic/fleet-server/v7/internal/pkg/checkin"
"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/elastic/fleet-server/v7/internal/pkg/monitor/mock"
"github.com/elastic/fleet-server/v7/internal/pkg/policy"
ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing"
"github.com/elastic/fleet-server/v7/internal/pkg/testing/certs"
)
func Test_server_Run(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
port, err := ftesting.FreePort()
require.NoError(t, err)
cfg := &config.Server{}
cfg.InitDefaults()
cfg.Host = "localhost"
cfg.Port = port
addr := cfg.BindEndpoints()[0]
verCon := mustBuildConstraints("8.0.0")
c, err := cache.New(config.Cache{NumCounters: 100, MaxCost: 100000})
require.NoError(t, err)
bulker := ftesting.NewMockBulk()
pim := mock.NewMockMonitor()
pm := policy.NewMonitor(bulker, pim)
bc := checkin.NewBulk(nil)
ct := NewCheckinT(verCon, cfg, c, bc, pm, nil, nil, nil, nil)
et, err := NewEnrollerT(verCon, cfg, nil, c)
require.NoError(t, err)
srv := NewServer(addr, cfg, ct, et, nil, nil, nil, nil, fbuild.Info{}, nil, nil, nil, nil, nil)
errCh := make(chan error)
var wg sync.WaitGroup
wg.Add(1)
go func() {
if err := srv.Run(ctx); err != nil {
errCh <- err
}
wg.Done()
}()
var errFromChan error
select {
case err := <-errCh:
errFromChan = err
case <-time.After(500 * time.Millisecond):
break
}
cancel()
wg.Wait()
require.NoError(t, errFromChan)
if !errors.Is(err, http.ErrServerClosed) {
require.NoError(t, err)
}
}
var tlsCFGTempl = `
enabled: true
certificate_authorities: ["%s"]
certificate: "%s"
key: "%s"
client_authentication: "optional"
`
func Test_server_ClientCert(t *testing.T) {
// prep self monitor mock for status endpoint
sm := mock.NewMockMonitor()
sm.On("State").Return(client.UnitStateHealthy)
// prep server tls config
ca := certs.GenCA(t)
caPath := certs.CertToFile(t, ca, "ca")
cert := certs.GenCert(t, ca)
certPath := certs.CertToFile(t, cert, "cert")
keyPath := certs.KeyToFile(t, cert, "key")
tlsYML := fmt.Sprintf(tlsCFGTempl,
caPath,
certPath,
keyPath,
)
ucfg, err := yaml.NewConfig([]byte(tlsYML))
require.NoError(t, err)
tlsCFG := &tlscommon.ServerConfig{}
err = tlsCFG.Unpack(libsconfig.C(*ucfg))
require.NoError(t, err)
t.Run("no client certs", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
port, err := ftesting.FreePort()
require.NoError(t, err)
cfg := &config.Server{}
cfg.InitDefaults()
cfg.Host = "localhost"
cfg.Port = port
addr := cfg.BindEndpoints()[0]
cfg.TLS = tlsCFG
st := NewStatusT(cfg, nil, nil)
srv := NewServer(addr, cfg, nil, nil, nil, nil, st, sm, fbuild.Info{}, nil, nil, nil, nil, nil)
errCh := make(chan error)
// make http client with no client certs
certPool := x509.NewCertPool()
certPool.AddCert(ca.Leaf)
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
},
},
}
started := make(chan struct{}, 1)
var wg sync.WaitGroup
wg.Add(1)
go func() {
started <- struct{}{}
if err := srv.Run(ctx); err != nil {
errCh <- err
}
wg.Done()
}()
<-started
rCtx, rCancel := context.WithTimeout(ctx, time.Second)
defer rCancel()
req, err := http.NewRequestWithContext(rCtx, "GET", "https://"+addr+"/api/status", nil)
require.NoError(t, err)
resp, err := httpClient.Do(req)
require.NoError(t, err)
resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode)
select {
case err := <-errCh:
require.NoError(t, err)
default:
}
cancel()
wg.Wait()
})
t.Run("valid client certs", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
port, err := ftesting.FreePort()
require.NoError(t, err)
cfg := &config.Server{}
cfg.InitDefaults()
cfg.Host = "localhost"
cfg.Port = port
addr := cfg.BindEndpoints()[0]
cfg.TLS = tlsCFG
st := NewStatusT(cfg, nil, nil)
srv := NewServer(addr, cfg, nil, nil, nil, nil, st, sm, fbuild.Info{}, nil, nil, nil, nil, nil)
errCh := make(chan error)
// make http client with valid client certs
clientCert := certs.GenCert(t, ca)
certPool := x509.NewCertPool()
certPool.AddCert(ca.Leaf)
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
Certificates: []tls.Certificate{clientCert},
},
},
}
started := make(chan struct{}, 1)
var wg sync.WaitGroup
wg.Add(1)
go func() {
started <- struct{}{}
if err := srv.Run(ctx); err != nil {
errCh <- err
}
wg.Done()
}()
<-started
rCtx, rCancel := context.WithTimeout(ctx, time.Second)
defer rCancel()
req, err := http.NewRequestWithContext(rCtx, "GET", "https://"+addr+"/api/status", nil)
require.NoError(t, err)
resp, err := httpClient.Do(req)
require.NoError(t, err)
resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode)
select {
case err := <-errCh:
require.NoError(t, err)
default:
}
cancel()
wg.Wait()
})
t.Run("invalid client certs", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
port, err := ftesting.FreePort()
require.NoError(t, err)
cfg := &config.Server{}
cfg.InitDefaults()
cfg.Host = "localhost"
cfg.Port = port
addr := cfg.BindEndpoints()[0]
cfg.TLS = tlsCFG
st := NewStatusT(cfg, nil, nil)
srv := NewServer(addr, cfg, nil, nil, nil, nil, st, sm, fbuild.Info{}, nil, nil, nil, nil, nil)
errCh := make(chan error)
// make http client with invalid client certs
clientCA := certs.GenCA(t)
clientCert := certs.GenCert(t, clientCA)
certPool := x509.NewCertPool()
certPool.AddCert(ca.Leaf)
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
Certificates: []tls.Certificate{clientCert},
},
},
}
started := make(chan struct{}, 1)
var wg sync.WaitGroup
wg.Add(1)
go func() {
started <- struct{}{}
if err := srv.Run(ctx); err != nil {
errCh <- err
}
wg.Done()
}()
<-started
rCtx, rCancel := context.WithTimeout(ctx, time.Second)
defer rCancel()
req, err := http.NewRequestWithContext(rCtx, "GET", "https://"+addr+"/api/status", nil)
require.NoError(t, err)
_, err = httpClient.Do(req)
require.Error(t, err)
select {
case err := <-errCh:
require.NoError(t, err)
default:
}
cancel()
wg.Wait()
})
t.Run("valid client certs no certs requested", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
port, err := ftesting.FreePort()
require.NoError(t, err)
cfg := &config.Server{}
cfg.InitDefaults()
cfg.Host = "localhost"
cfg.Port = port
addr := cfg.BindEndpoints()[0]
// prep server config without specifing client_authentication
tlsYML := fmt.Sprintf(`
enabled: true
certificate: "%s"
key: %s`,
certPath, keyPath)
ucfg, err := yaml.NewConfig([]byte(tlsYML))
require.NoError(t, err)
tlsCFG := &tlscommon.ServerConfig{}
err = tlsCFG.Unpack(libsconfig.C(*ucfg))
require.NoError(t, err)
cfg.TLS = tlsCFG
st := NewStatusT(cfg, nil, nil)
srv := NewServer(addr, cfg, nil, nil, nil, nil, st, sm, fbuild.Info{}, nil, nil, nil, nil, nil)
errCh := make(chan error)
// make http client with valid client certs
clientCert := certs.GenCert(t, ca)
certPool := x509.NewCertPool()
certPool.AddCert(ca.Leaf)
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
Certificates: []tls.Certificate{clientCert},
},
},
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
if err := srv.Run(ctx); err != nil {
errCh <- err
}
wg.Done()
}()
rCtx, rCancel := context.WithTimeout(ctx, time.Second)
defer rCancel()
req, err := http.NewRequestWithContext(rCtx, "GET", "https://"+addr+"/api/status", nil)
require.NoError(t, err)
resp, err := httpClient.Do(req)
require.NoError(t, err)
resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode)
select {
case err := <-errCh:
require.NoError(t, err)
default:
}
cancel()
wg.Wait()
})
}