Repository navigation
Expand file tree
/
Copy pathtls_test.go
More file actions
438 lines (406 loc) · 13.4 KB
/
Copy pathtls_test.go
File metadata and controls
438 lines (406 loc) · 13.4 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package tls
import (
"context"
"crypto/tls"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"testing"
"k8s.io/client-go/rest"
)
func testLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
}
func TestIntermediateDefaults(t *testing.T) {
cfg := intermediateDefaults()
if cfg.MinVersion != tls.VersionTLS12 {
t.Errorf("expected MinVersion TLS 1.2, got %d", cfg.MinVersion)
}
if len(cfg.CipherSuites) != 6 {
t.Errorf("expected 6 cipher suites, got %d", len(cfg.CipherSuites))
}
if len(cfg.NextProtos) != 2 || cfg.NextProtos[0] != "h2" || cfg.NextProtos[1] != "http/1.1" {
t.Errorf("expected NextProtos [h2, http/1.1], got %v", cfg.NextProtos)
}
}
func TestIntermediateCiphersAreValid(t *testing.T) {
for _, id := range intermediateCiphers {
name := tls.CipherSuiteName(id)
if name == "" {
t.Errorf("unknown cipher suite ID %d", id)
}
}
}
func TestResolveProfileSpecNil(t *testing.T) {
spec, err := resolveProfileSpec(nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := wellKnownProfiles["Intermediate"]
if spec.MinTLSVersion != expected.MinTLSVersion {
t.Errorf("expected Intermediate MinTLSVersion %s, got %s", expected.MinTLSVersion, spec.MinTLSVersion)
}
}
func TestResolveProfileSpecIntermediate(t *testing.T) {
profile := &tlsSecurityProfile{Type: "Intermediate"}
spec, err := resolveProfileSpec(profile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if spec.MinTLSVersion != "VersionTLS12" {
t.Errorf("expected VersionTLS12, got %s", spec.MinTLSVersion)
}
}
func TestResolveProfileSpecModern(t *testing.T) {
profile := &tlsSecurityProfile{Type: "Modern"}
spec, err := resolveProfileSpec(profile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if spec.MinTLSVersion != "VersionTLS13" {
t.Errorf("expected VersionTLS13, got %s", spec.MinTLSVersion)
}
}
func TestResolveProfileSpecOld(t *testing.T) {
profile := &tlsSecurityProfile{Type: "Old"}
spec, err := resolveProfileSpec(profile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if spec.MinTLSVersion != "VersionTLS10" {
t.Errorf("expected VersionTLS10, got %s", spec.MinTLSVersion)
}
}
func TestResolveProfileSpecCustomNil(t *testing.T) {
profile := &tlsSecurityProfile{Type: "Custom"}
_, err := resolveProfileSpec(profile)
if err == nil {
t.Fatal("expected error for custom profile with nil Custom field")
}
}
func TestResolveProfileSpecCustom(t *testing.T) {
profile := &tlsSecurityProfile{
Type: "Custom",
Custom: &struct {
tlsProfileSpec
}{
tlsProfileSpec: tlsProfileSpec{
MinTLSVersion: "VersionTLS13",
Ciphers: []string{},
},
},
}
spec, err := resolveProfileSpec(profile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if spec.MinTLSVersion != "VersionTLS13" {
t.Errorf("expected VersionTLS13, got %s", spec.MinTLSVersion)
}
}
func TestResolveProfileSpecUnknownType(t *testing.T) {
profile := &tlsSecurityProfile{Type: "FutureType"}
spec, err := resolveProfileSpec(profile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should fall back to Intermediate
if spec.MinTLSVersion != "VersionTLS12" {
t.Errorf("expected VersionTLS12 (Intermediate fallback), got %s", spec.MinTLSVersion)
}
}
func TestResolveCiphers(t *testing.T) {
names := []string{
"ECDHE-ECDSA-AES128-GCM-SHA256",
"ECDHE-RSA-AES128-GCM-SHA256",
"UNKNOWN-CIPHER",
}
codes, dropped := resolveCiphers(names)
if len(codes) != 2 {
t.Errorf("expected 2 codes, got %d", len(codes))
}
if len(dropped) != 1 || dropped[0] != "UNKNOWN-CIPHER" {
t.Errorf("expected [UNKNOWN-CIPHER] dropped, got %v", dropped)
}
}
func TestResolveCiphersDHEDropped(t *testing.T) {
// DHE ciphers are not supported by Go and should be dropped, not remapped.
names := []string{
"ECDHE-RSA-AES128-GCM-SHA256",
"DHE-RSA-AES128-GCM-SHA256",
"DHE-RSA-AES256-GCM-SHA384",
}
codes, dropped := resolveCiphers(names)
if len(codes) != 1 {
t.Errorf("expected 1 resolved code, got %d", len(codes))
}
if len(dropped) != 2 {
t.Errorf("expected 2 dropped DHE ciphers, got %v", dropped)
}
for _, d := range dropped {
if d != "DHE-RSA-AES128-GCM-SHA256" && d != "DHE-RSA-AES256-GCM-SHA384" {
t.Errorf("unexpected dropped cipher: %s", d)
}
}
}
func TestResolveCiphersDeduplicate(t *testing.T) {
// Same cipher listed under both OpenSSL and IANA names should be deduplicated.
names := []string{
"ECDHE-RSA-AES128-GCM-SHA256",
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
}
codes, dropped := resolveCiphers(names)
if len(codes) != 1 {
t.Errorf("expected 1 deduplicated code, got %d", len(codes))
}
if len(dropped) != 0 {
t.Errorf("expected no dropped ciphers, got %v", dropped)
}
}
func TestResolveCiphersIANA(t *testing.T) {
names := []string{
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
}
codes, dropped := resolveCiphers(names)
if len(codes) != 2 {
t.Errorf("expected 2 codes, got %d", len(codes))
}
if len(dropped) != 0 {
t.Errorf("expected no dropped, got %v", dropped)
}
if codes[0] != tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 {
t.Errorf("wrong first cipher code: %d", codes[0])
}
}
func TestTLSConfigFromNilProfile(t *testing.T) {
cfg, err := tlsConfigFromProfile(nil, testLogger())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Nil profile = Intermediate defaults
if cfg.MinVersion != tls.VersionTLS12 {
t.Errorf("expected TLS 1.2, got %d", cfg.MinVersion)
}
if len(cfg.NextProtos) != 2 {
t.Errorf("expected 2 NextProtos, got %d", len(cfg.NextProtos))
}
}
func TestTLSConfigFromModernProfile(t *testing.T) {
profile := &tlsSecurityProfile{Type: "Modern"}
cfg, err := tlsConfigFromProfile(profile, testLogger())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Modern = TLS 1.3
if cfg.MinVersion != tls.VersionTLS13 {
t.Errorf("expected TLS 1.3, got %d", cfg.MinVersion)
}
// TLS 1.3: CipherSuites should not be set (Go manages them)
if cfg.CipherSuites != nil {
t.Errorf("expected nil CipherSuites for TLS 1.3, got %v", cfg.CipherSuites)
}
if len(cfg.NextProtos) != 2 {
t.Errorf("expected 2 NextProtos, got %d", len(cfg.NextProtos))
}
}
func TestTLSConfigFromIntermediateProfile(t *testing.T) {
profile := &tlsSecurityProfile{Type: "Intermediate"}
cfg, err := tlsConfigFromProfile(profile, testLogger())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.MinVersion != tls.VersionTLS12 {
t.Errorf("expected TLS 1.2, got %d", cfg.MinVersion)
}
// Should have ciphers set (the ones that are mappable)
if len(cfg.CipherSuites) == 0 {
t.Error("expected non-empty CipherSuites for Intermediate profile")
}
if len(cfg.NextProtos) != 2 {
t.Errorf("expected 2 NextProtos, got %d", len(cfg.NextProtos))
}
}
func TestTLSConfigFromCustomAllUnsupportedCiphers(t *testing.T) {
// A custom profile where every cipher is unmapped should return an error.
profile := &tlsSecurityProfile{
Type: "Custom",
Custom: &struct {
tlsProfileSpec
}{
tlsProfileSpec: tlsProfileSpec{
MinTLSVersion: "VersionTLS12",
Ciphers: []string{"BOGUS-CIPHER-1", "BOGUS-CIPHER-2"},
},
},
}
_, err := tlsConfigFromProfile(profile, testLogger())
if err == nil {
t.Fatal("expected error when all custom ciphers are unsupported")
}
}
func TestTLSConfigFromOldProfile(t *testing.T) {
profile := &tlsSecurityProfile{Type: "Old"}
cfg, err := tlsConfigFromProfile(profile, testLogger())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.MinVersion != tls.VersionTLS10 {
t.Errorf("expected TLS 1.0, got %d", cfg.MinVersion)
}
// Old profile has many ciphers; verify the CBC/SHA ciphers from the Old profile resolve.
// The Old profile lists 20 ciphers; DHE and TLS 1.3 ciphers get dropped, the rest should resolve.
if len(cfg.CipherSuites) < 10 {
t.Errorf("expected at least 10 resolved cipher suites for Old profile, got %d", len(cfg.CipherSuites))
}
}
func TestResolveCiphersOldProfileCBCCiphers(t *testing.T) {
// Verify the CBC ciphers from the Old profile all resolve.
cbcCiphers := []string{
"ECDHE-ECDSA-AES128-SHA256",
"ECDHE-ECDSA-AES128-SHA",
"ECDHE-RSA-AES128-SHA",
"AES128-SHA",
}
codes, dropped := resolveCiphers(cbcCiphers)
if len(dropped) != 0 {
t.Errorf("expected no dropped CBC ciphers, got %v", dropped)
}
if len(codes) != 4 {
t.Errorf("expected 4 resolved CBC ciphers, got %d", len(codes))
}
}
// TestServerTLSConfigFromRESTBuildsClient verifies the REST client path end to
// end: given a rest.Config with no NegotiatedSerializer (as returned by
// rest.InClusterConfig), serverTLSConfigFromREST must still construct a working
// client, fetch the APIServer resource and apply the returned profile.
//
// This is a regression test for the crash where UnversionedRESTClientFor failed
// with "NegotiatedSerializer is required when initializing a RESTClient" because
// the config never set one.
func TestServerTLSConfigFromRESTBuildsClient(t *testing.T) {
const body = `{"spec":{"tlsSecurityProfile":{"type":"Modern"}}}`
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/apis/config.openshift.io/v1/apiservers/cluster" {
http.Error(w, "unexpected path: "+r.URL.Path, http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(body))
}))
defer srv.Close()
// Intentionally leave NegotiatedSerializer unset, mirroring InClusterConfig.
cfg := &rest.Config{Host: srv.URL}
tlsCfg, err := serverTLSConfigFromREST(context.Background(), cfg, testLogger())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tlsCfg.MinVersion != tls.VersionTLS13 {
t.Errorf("expected Modern profile (TLS 1.3), got MinVersion %d", tlsCfg.MinVersion)
}
}
// TestServerTLSConfigFromRESTNotFoundFailsOpen verifies that a 404 for the
// APIServer resource fails open to Intermediate defaults rather than erroring.
func TestServerTLSConfigFromRESTNotFoundFailsOpen(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, `{"kind":"Status","apiVersion":"v1","status":"Failure","reason":"NotFound","code":404}`, http.StatusNotFound)
}))
defer srv.Close()
cfg := &rest.Config{Host: srv.URL}
tlsCfg, err := serverTLSConfigFromREST(context.Background(), cfg, testLogger())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tlsCfg.MinVersion != tls.VersionTLS12 || len(tlsCfg.CipherSuites) != 6 {
t.Errorf("expected Intermediate defaults on 404, got MinVersion %d with %d ciphers",
tlsCfg.MinVersion, len(tlsCfg.CipherSuites))
}
}
func TestResolveGroups(t *testing.T) {
names := []string{"X25519", "P-256", "P-384", "X25519MLKEM768", "UNKNOWN-GROUP"}
curves, dropped := resolveGroups(names)
if len(curves) != 4 {
t.Errorf("expected 4 resolved curves, got %d", len(curves))
}
if len(dropped) != 1 || dropped[0] != "UNKNOWN-GROUP" {
t.Errorf("expected [UNKNOWN-GROUP] dropped, got %v", dropped)
}
if curves[0] != tls.X25519 || curves[1] != tls.CurveP256 {
t.Errorf("groups not mapped in order: %v", curves)
}
}
func TestResolveGroupsDeduplicate(t *testing.T) {
curves, dropped := resolveGroups([]string{"P-256", "P-256"})
if len(curves) != 1 {
t.Errorf("expected 1 deduplicated curve, got %d", len(curves))
}
if len(dropped) != 0 {
t.Errorf("expected no dropped groups, got %v", dropped)
}
}
func TestTLSConfigWellKnownProfileNoCurvePreferences(t *testing.T) {
// Well-known profiles don't restrict groups, so CurvePreferences stays nil
// and Go's secure defaults apply.
profile := &tlsSecurityProfile{Type: "Intermediate"}
cfg, err := tlsConfigFromProfile(profile, testLogger())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.CurvePreferences != nil {
t.Errorf("expected nil CurvePreferences for well-known profile, got %v", cfg.CurvePreferences)
}
}
func TestTLSConfigFromCustomProfileGroups(t *testing.T) {
// A Custom profile that restricts groups must be enforced via CurvePreferences.
profile := &tlsSecurityProfile{
Type: "Custom",
Custom: &struct {
tlsProfileSpec
}{
tlsProfileSpec: tlsProfileSpec{
MinTLSVersion: "VersionTLS13",
Groups: []string{"P-384", "P-256"},
},
},
}
cfg, err := tlsConfigFromProfile(profile, testLogger())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(cfg.CurvePreferences) != 2 {
t.Fatalf("expected 2 curve preferences, got %d", len(cfg.CurvePreferences))
}
if cfg.CurvePreferences[0] != tls.CurveP384 || cfg.CurvePreferences[1] != tls.CurveP256 {
t.Errorf("curve preferences not honored in order: %v", cfg.CurvePreferences)
}
}
func TestTLSConfigFromCustomAllUnsupportedGroups(t *testing.T) {
// A custom profile where every group is unmapped should fail closed.
profile := &tlsSecurityProfile{
Type: "Custom",
Custom: &struct {
tlsProfileSpec
}{
tlsProfileSpec: tlsProfileSpec{
MinTLSVersion: "VersionTLS13",
Groups: []string{"BOGUS-GROUP-1", "BOGUS-GROUP-2"},
},
},
}
_, err := tlsConfigFromProfile(profile, testLogger())
if err == nil {
t.Fatal("expected error when all custom groups are unsupported")
}
}
func TestTLSConfigFromUnknownProfileLogs(t *testing.T) {
// Unknown profile type should not error but should fall back to Intermediate.
profile := &tlsSecurityProfile{Type: "FutureProfile"}
cfg, err := tlsConfigFromProfile(profile, testLogger())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.MinVersion != tls.VersionTLS12 {
t.Errorf("expected TLS 1.2 (Intermediate fallback), got %d", cfg.MinVersion)
}
}