Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
244 lines (216 loc) · 7.64 KB
/
Copy pathmain_test.go
File metadata and controls
244 lines (216 loc) · 7.64 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
/*
Copyright 2023 The Metal3 Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"crypto/tls"
"testing"
. "github.com/onsi/gomega"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
)
func TestTLSInsecureCiperSuite(t *testing.T) {
t.Run("test insecure cipher suite passed as TLS flag", func(t *testing.T) {
g := NewWithT(t)
tlsMockOptions := TLSOptions{
TLSMaxVersion: "TLS13",
TLSMinVersion: "TLS12",
TLSCipherSuites: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
}
ctrl.Log.WithName("setup")
ctrl.SetLogger(klog.Background())
bufWriter := bytes.NewBuffer(nil)
klog.SetOutput(bufWriter)
klog.LogToStderr(false) // this is important, because klog by default logs to stderr only
_, err := GetTLSOptionOverrideFuncs(tlsMockOptions)
g.Expect(err).ShouldNot(HaveOccurred())
// Check for structured log format: key=value pairs
g.Expect(bufWriter.String()).Should(ContainSubstring("use of insecure cipher detected"))
g.Expect(bufWriter.String()).Should(ContainSubstring("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"))
})
}
func TestTLSMinAndMaxVersion(t *testing.T) {
t.Run("should fail if TLS min version is greater than max version.", func(t *testing.T) {
g := NewWithT(t)
tlsMockOptions := TLSOptions{
TLSMaxVersion: "TLS12",
TLSMinVersion: "TLS13",
}
_, err := GetTLSOptionOverrideFuncs(tlsMockOptions)
g.Expect(err.Error()).To(Equal("TLS version flag min version (TLS13) is greater than max version (TLS12)"))
})
}
func Test13CipherSuite(t *testing.T) {
t.Run("should reset ciphersuite flag if TLS min and max version are set to 1.3", func(t *testing.T) {
g := NewWithT(t)
// Here TLS_RSA_WITH_AES_128_CBC_SHA is a tls12 cipher suite.
tlsMockOptions := TLSOptions{
TLSMaxVersion: "TLS13",
TLSMinVersion: "TLS13",
TLSCipherSuites: "TLS_RSA_WITH_AES_128_CBC_SHA,TLS_AES_256_GCM_SHA384",
}
ctrl.Log.WithName("setup")
ctrl.SetLogger(klog.Background())
bufWriter := bytes.NewBuffer(nil)
klog.SetOutput(bufWriter)
klog.LogToStderr(false) // this is important, because klog by default logs to stderr only
_, err := GetTLSOptionOverrideFuncs(tlsMockOptions)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(bufWriter.String()).Should(ContainSubstring("warning: Cipher suites should not be set for TLS version 1.3. Ignoring ciphers"))
})
}
func TestGetTLSOverrideFuncs(t *testing.T) {
t.Run("should error out when incorrect min tls version passed", func(t *testing.T) {
g := NewWithT(t)
_, err := GetTLSOptionOverrideFuncs(TLSOptions{
TLSMinVersion: "TLS11",
TLSMaxVersion: "TLS12",
})
g.Expect(err.Error()).Should(Equal("unexpected TLS version \"TLS11\" (must be one of: TLS12, TLS13)"))
})
t.Run("should error out when incorrect max tls version passed", func(t *testing.T) {
g := NewWithT(t)
_, err := GetTLSOptionOverrideFuncs(TLSOptions{
TLSMinVersion: "TLS12",
TLSMaxVersion: "TLS11",
})
g.Expect(err.Error()).Should(Equal("unexpected TLS version \"TLS11\" (must be one of: TLS12, TLS13)"))
})
t.Run("should apply the requested TLS versions", func(t *testing.T) {
g := NewWithT(t)
tlsOptionOverrides, err := GetTLSOptionOverrideFuncs(TLSOptions{
TLSMinVersion: "TLS12",
TLSMaxVersion: "TLS13",
})
var tlsConfig tls.Config
for _, apply := range tlsOptionOverrides {
apply(&tlsConfig)
}
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(tlsConfig.MinVersion).To(Equal(uint16(tls.VersionTLS12)))
g.Expect(tlsConfig.MaxVersion).To(Equal(uint16(tls.VersionTLS13)))
})
t.Run("should apply the requested non-default TLS versions", func(t *testing.T) {
g := NewWithT(t)
tlsOptionOverrides, err := GetTLSOptionOverrideFuncs(TLSOptions{
TLSMinVersion: "TLS13",
TLSMaxVersion: "TLS13",
})
var tlsConfig tls.Config
for _, apply := range tlsOptionOverrides {
apply(&tlsConfig)
}
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(tlsConfig.MinVersion).To(Equal(uint16(tls.VersionTLS13)))
g.Expect(tlsConfig.MaxVersion).To(Equal(uint16(tls.VersionTLS13)))
})
}
func TestTLSOptions(t *testing.T) {
t.Run("should pass with all the correct options below with no error.", func(t *testing.T) {
g := NewWithT(t)
tlsMockOptions := TLSOptions{
TLSMinVersion: "TLS12",
TLSMaxVersion: "TLS13",
TLSCipherSuites: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
}
_, err := GetTLSOptionOverrideFuncs(tlsMockOptions)
g.Expect(err).ShouldNot(HaveOccurred())
})
}
func TestTLSCurvePreferences(t *testing.T) {
t.Run("valid curve preferences should be parsed correctly", func(t *testing.T) {
g := NewWithT(t)
tlsMockOptions := TLSOptions{
TLSMinVersion: "TLS12",
TLSMaxVersion: "TLS13",
TLSCurvePreferences: "X25519,CurveP256,CurveP384",
}
tlsOptionOverrides, err := GetTLSOptionOverrideFuncs(tlsMockOptions)
g.Expect(err).ShouldNot(HaveOccurred())
var tlsConfig tls.Config
for _, apply := range tlsOptionOverrides {
apply(&tlsConfig)
}
g.Expect(tlsConfig.CurvePreferences).To(Equal([]tls.CurveID{
tls.X25519,
tls.CurveP256,
tls.CurveP384,
}))
})
t.Run("all supported curve names should be accepted", func(t *testing.T) {
g := NewWithT(t)
tlsMockOptions := TLSOptions{
TLSMinVersion: "TLS12",
TLSMaxVersion: "TLS13",
TLSCurvePreferences: "X25519,CurveP256,CurveP384,CurveP521,X25519MLKEM768",
}
tlsOptionOverrides, err := GetTLSOptionOverrideFuncs(tlsMockOptions)
g.Expect(err).ShouldNot(HaveOccurred())
var tlsConfig tls.Config
for _, apply := range tlsOptionOverrides {
apply(&tlsConfig)
}
g.Expect(tlsConfig.CurvePreferences).To(Equal([]tls.CurveID{
tls.X25519,
tls.CurveP256,
tls.CurveP384,
tls.CurveP521,
tls.X25519MLKEM768,
}))
})
t.Run("empty curve preferences should have no effect", func(t *testing.T) {
g := NewWithT(t)
tlsMockOptions := TLSOptions{
TLSMinVersion: "TLS12",
TLSMaxVersion: "TLS13",
}
tlsOptionOverrides, err := GetTLSOptionOverrideFuncs(tlsMockOptions)
g.Expect(err).ShouldNot(HaveOccurred())
var tlsConfig tls.Config
for _, apply := range tlsOptionOverrides {
apply(&tlsConfig)
}
g.Expect(tlsConfig.CurvePreferences).To(BeNil())
})
t.Run("invalid curve name should return error", func(t *testing.T) {
g := NewWithT(t)
tlsMockOptions := TLSOptions{
TLSMinVersion: "TLS12",
TLSMaxVersion: "TLS13",
TLSCurvePreferences: "X25519,InvalidCurve",
}
_, err := GetTLSOptionOverrideFuncs(tlsMockOptions)
g.Expect(err).Should(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring("unrecognized TLS curve preference"))
g.Expect(err.Error()).To(ContainSubstring("InvalidCurve"))
})
t.Run("whitespace around names should be tolerated", func(t *testing.T) {
g := NewWithT(t)
tlsMockOptions := TLSOptions{
TLSMinVersion: "TLS12",
TLSMaxVersion: "TLS13",
TLSCurvePreferences: "X25519, CurveP256, CurveP384",
}
tlsOptionOverrides, err := GetTLSOptionOverrideFuncs(tlsMockOptions)
g.Expect(err).ShouldNot(HaveOccurred())
var tlsConfig tls.Config
for _, apply := range tlsOptionOverrides {
apply(&tlsConfig)
}
g.Expect(tlsConfig.CurvePreferences).To(Equal([]tls.CurveID{
tls.X25519,
tls.CurveP256,
tls.CurveP384,
}))
})
}