-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
380 lines (305 loc) · 11 KB
/
Copy pathhandler_test.go
File metadata and controls
380 lines (305 loc) · 11 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package certwebhook
import (
"context"
"sync"
"testing"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
ipfs "go.lumeweb.com/ipfs-sdk"
servicemocks "go.lumeweb.com/ipfs-sdk/mocks/services"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
)
func newTestApp(t *testing.T, mockSvc *servicemocks.MockWebsitesService, statusFn certStatusFunc) *CertWebhookApp {
t.Helper()
obs, _ := observer.New(zap.DebugLevel)
logger := zap.New(obs)
delivery := NewWebhookDelivery(mockSvc, logger)
if statusFn == nil {
statusFn = func(domain string) SSLStatus { return SSLStatusIssuing }
}
return &CertWebhookApp{
logger: logger,
delivery: delivery,
throttle: &throttleMap{lastSent: make(map[string]lastSentEntry), interval: 5 * time.Minute},
throttleInterval: 5 * time.Minute,
certStatusFn: statusFn,
}
}
func tlsGetCertEvent(domain string) caddy.Event {
return caddy.Event{
Data: map[string]any{
"client_hello": map[string]any{
"ServerName": domain,
},
},
}
}
func expectStatus(t *testing.T, mockSvc *servicemocks.MockWebsitesService, domain string, expectedStatus SSLStatus) {
t.Helper()
mockSvc.EXPECT().UpdateSSLStatusInternal(
mock.Anything,
mock.Anything,
mock.Anything,
).Run(func(ctx context.Context, d string, req ipfs.SSLStatusUpdateRequest) {
assert.Equal(t, string(expectedStatus), req.Status)
}).Return(nil)
}
func TestHandle_TLSGetCertificate_Issuing(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, func(domain string) SSLStatus {
return SSLStatusIssuing
})
expectStatus(t, mockSvc, "example.com", SSLStatusIssuing)
err := app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("example.com"))
assert.NoError(t, err)
app.delivery.Wait()
}
func TestHandle_TLSGetCertificate_Ready(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, func(domain string) SSLStatus {
return SSLStatusReady
})
expectStatus(t, mockSvc, "example.com", SSLStatusReady)
err := app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("example.com"))
assert.NoError(t, err)
app.delivery.Wait()
}
func TestHandle_TLSGetCertificate_Failed(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, func(domain string) SSLStatus {
return SSLStatusFailed
})
expectStatus(t, mockSvc, "example.com", SSLStatusFailed)
err := app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("example.com"))
assert.NoError(t, err)
app.delivery.Wait()
}
func TestHandle_TLSGetCertificate_Throttled(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
app.throttle.checkAndMark("example.com", SSLStatusIssuing)
err := app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("example.com"))
assert.NoError(t, err)
app.delivery.Wait()
}
func TestHandle_TLSGetCertificate_StatusTransitionBypass(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
app.throttle.checkAndMark("example.com", SSLStatusIssuing)
expectStatus(t, mockSvc, "example.com", SSLStatusReady)
assert.True(t, app.shouldSend("example.com", SSLStatusReady))
ts := time.Now().Format(time.RFC3339)
err := app.sendWebhook("example.com", SSLStatusReady, "", ts)
assert.NoError(t, err)
app.delivery.Wait()
}
func TestHandle_TLSGetCertificate_CertEventThrottledSameStatus(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
expectStatus(t, mockSvc, "example.com", SSLStatusReady)
assert.True(t, app.shouldSend("example.com", SSLStatusReady))
ts := time.Now().Format(time.RFC3339)
err := app.sendWebhook("example.com", SSLStatusReady, "", ts)
assert.NoError(t, err)
app.delivery.Wait()
assert.False(t, app.shouldSend("example.com", SSLStatusReady))
}
func TestHandle_TLSGetCertificate_EmptyData(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
event := caddy.Event{Data: map[string]any{}}
err := app.handleTLSGetCertificateEvent(context.Background(), event)
assert.NoError(t, err)
}
func TestHandle_ReadyFailedReadyRoundTrip(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, func(domain string) SSLStatus {
return SSLStatusReady
})
var delivered []SSLStatus
mockSvc.EXPECT().UpdateSSLStatusInternal(
mock.Anything, mock.Anything, mock.Anything,
).Run(func(ctx context.Context, d string, req ipfs.SSLStatusUpdateRequest) {
delivered = append(delivered, SSLStatus(req.Status))
}).Return(nil).Times(3)
err := app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("example.com"))
assert.NoError(t, err)
app.delivery.Wait()
app.certStatusFn = func(domain string) SSLStatus { return SSLStatusFailed }
err = app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("example.com"))
assert.NoError(t, err)
app.delivery.Wait()
app.certStatusFn = func(domain string) SSLStatus { return SSLStatusReady }
err = app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("example.com"))
assert.NoError(t, err)
app.delivery.Wait()
assert.False(t, app.shouldSend("example.com", SSLStatusReady))
assert.Equal(t, []SSLStatus{SSLStatusReady, SSLStatusFailed, SSLStatusReady}, delivered)
}
func TestHandle_CertEventAndTLSGetCertRace(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, func(domain string) SSLStatus {
return SSLStatusReady
})
var mu sync.Mutex
var delivered []SSLStatus
mockSvc.EXPECT().UpdateSSLStatusInternal(
mock.Anything, mock.Anything, mock.Anything,
).Run(func(ctx context.Context, d string, req ipfs.SSLStatusUpdateRequest) {
mu.Lock()
delivered = append(delivered, SSLStatus(req.Status))
mu.Unlock()
}).Return(nil)
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
if app.shouldSend("example.com", SSLStatusReady) {
ts := time.Now().Format(time.RFC3339)
app.sendWebhook("example.com", SSLStatusReady, "", ts)
}
}()
go func() {
defer wg.Done()
if app.shouldSend("example.com", SSLStatusFailed) {
ts := time.Now().Format(time.RFC3339)
app.sendWebhook("example.com", SSLStatusFailed, "", ts)
}
}()
go func() {
defer wg.Done()
app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("example.com"))
}()
wg.Wait()
app.delivery.Wait()
mu.Lock()
defer mu.Unlock()
assert.GreaterOrEqual(t, len(delivered), 2)
hasReady := false
hasFailed := false
for _, s := range delivered {
if s == SSLStatusReady {
hasReady = true
}
if s == SSLStatusFailed {
hasFailed = true
}
}
assert.True(t, hasReady, "expected at least one ready delivery")
assert.True(t, hasFailed, "expected at least one failed delivery")
}
func TestCertStatusFn_MockedIssuing(t *testing.T) {
called := false
fn := func(domain string) SSLStatus {
called = true
return SSLStatusIssuing
}
result := fn("example.com")
assert.True(t, called)
assert.Equal(t, SSLStatusIssuing, result)
}
func TestCertStatusFn_MockedReady(t *testing.T) {
fn := func(domain string) SSLStatus { return SSLStatusReady }
assert.Equal(t, SSLStatusReady, fn("example.com"))
}
func TestCertStatusFn_MockedFailed(t *testing.T) {
fn := func(domain string) SSLStatus { return SSLStatusFailed }
assert.Equal(t, SSLStatusFailed, fn("example.com"))
}
func TestIsIPAddress_IPv4(t *testing.T) {
assert.True(t, isIPAddress("104.243.38.32"))
}
func TestIsIPAddress_IPv6(t *testing.T) {
assert.True(t, isIPAddress("::1"))
}
func TestIsIPAddress_Domain(t *testing.T) {
assert.False(t, isIPAddress("example.com"))
}
func TestIsIPAddress_EmptyString(t *testing.T) {
assert.False(t, isIPAddress(""))
}
func TestSendWebhook_SkipsIPAddress(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
ts := time.Now().Format(time.RFC3339)
err := app.sendWebhook("104.243.38.32", SSLStatusReady, "", ts)
assert.NoError(t, err)
app.delivery.Wait()
mockSvc.AssertNotCalled(t, "UpdateSSLStatusInternal")
}
func TestSendWebhook_DomainNotSkipped(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, func(domain string) SSLStatus {
return SSLStatusReady
})
expectStatus(t, mockSvc, "example.com", SSLStatusReady)
ts := time.Now().Format(time.RFC3339)
err := app.sendWebhook("example.com", SSLStatusReady, "", ts)
assert.NoError(t, err)
app.delivery.Wait()
}
func TestHandle_TLSGetCertificate_SkipsIP(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
err := app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("104.243.38.32"))
assert.NoError(t, err)
app.delivery.Wait()
mockSvc.AssertNotCalled(t, "UpdateSSLStatusInternal")
}
func TestHandle_CertEvent_SkipsIP(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
err := app.handleCertEvent(context.Background(), EventCertObtained, caddy.Event{
Data: map[string]any{
"domain": "104.243.38.32",
"timestamp": float64(time.Now().Unix()),
},
})
assert.NoError(t, err)
app.delivery.Wait()
mockSvc.AssertNotCalled(t, "UpdateSSLStatusInternal")
}
func TestSendWebhook_SkipsIgnoredDomain(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
app.ignoredDomains = map[string]struct{}{"gateway.example.com": {}}
ts := time.Now().Format(time.RFC3339)
err := app.sendWebhook("gateway.example.com", SSLStatusReady, "", ts)
assert.NoError(t, err)
app.delivery.Wait()
mockSvc.AssertNotCalled(t, "UpdateSSLStatusInternal")
}
func TestSendWebhook_NonIgnoredDomainNotSkipped(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, func(domain string) SSLStatus {
return SSLStatusReady
})
app.ignoredDomains = map[string]struct{}{"gateway.example.com": {}}
expectStatus(t, mockSvc, "example.com", SSLStatusReady)
ts := time.Now().Format(time.RFC3339)
err := app.sendWebhook("example.com", SSLStatusReady, "", ts)
assert.NoError(t, err)
app.delivery.Wait()
}
func TestHandle_TLSGetCertificate_SkipsIgnoredDomain(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
app.ignoredDomains = map[string]struct{}{"gateway.example.com": {}}
err := app.handleTLSGetCertificateEvent(context.Background(), tlsGetCertEvent("gateway.example.com"))
assert.NoError(t, err)
app.delivery.Wait()
mockSvc.AssertNotCalled(t, "UpdateSSLStatusInternal")
}
func TestSendWebhook_IgnoredDomainCaseInsensitive(t *testing.T) {
mockSvc := servicemocks.NewMockWebsitesService(t)
app := newTestApp(t, mockSvc, nil)
app.ignoredDomains = map[string]struct{}{"gateway.example.com": {}}
ts := time.Now().Format(time.RFC3339)
err := app.sendWebhook("GATEWAY.Example.com", SSLStatusReady, "", ts)
assert.NoError(t, err)
app.delivery.Wait()
mockSvc.AssertNotCalled(t, "UpdateSSLStatusInternal")
}