@@ -124,3 +124,158 @@ func TestTLSMinVersionWebhookOption(t *testing.T) {
124124 }
125125 })
126126}
127+
128+ func TestTLSMaxVersionWebhookOption (t * testing.T ) {
129+ opts := newDefaultOptions ()
130+ t .Run ("when TLSMaxVersion is not configured, default is used" , func (t * testing.T ) {
131+ wh , err := newAdmissionControllerWebhook (t , opts )
132+ if err != nil {
133+ t .Fatal ("Unexpected error" , err )
134+ }
135+
136+ if wh .tlsConfig != nil && wh .tlsConfig .MaxVersion != 0 {
137+ t .Errorf ("Expected MaxVersion to be 0 (default), got %d" , wh .tlsConfig .MaxVersion )
138+ }
139+ })
140+ t .Run ("when TLSMaxVersion is configured to TLS 1.3" , func (t * testing.T ) {
141+ opts .TLSMaxVersion = tls .VersionTLS13
142+ wh , err := newAdmissionControllerWebhook (t , opts )
143+ if err != nil {
144+ t .Fatal ("Unexpected error" , err )
145+ }
146+ if wh .tlsConfig == nil {
147+ t .Fatal ("Expected tlsConfig to be set" )
148+ }
149+ if wh .tlsConfig .MaxVersion != tls .VersionTLS13 {
150+ t .Errorf ("Expected MaxVersion to be TLS 1.3, got %d" , wh .tlsConfig .MaxVersion )
151+ }
152+ })
153+ t .Run ("when both TLSMinVersion and TLSMaxVersion are TLS 1.3 (Modern profile)" , func (t * testing.T ) {
154+ opts .TLSMinVersion = tls .VersionTLS13
155+ opts .TLSMaxVersion = tls .VersionTLS13
156+ wh , err := newAdmissionControllerWebhook (t , opts )
157+ if err != nil {
158+ t .Fatal ("Unexpected error" , err )
159+ }
160+ if wh .tlsConfig == nil {
161+ t .Fatal ("Expected tlsConfig to be set" )
162+ }
163+ if wh .tlsConfig .MinVersion != tls .VersionTLS13 {
164+ t .Errorf ("Expected MinVersion to be TLS 1.3, got %d" , wh .tlsConfig .MinVersion )
165+ }
166+ if wh .tlsConfig .MaxVersion != tls .VersionTLS13 {
167+ t .Errorf ("Expected MaxVersion to be TLS 1.3, got %d" , wh .tlsConfig .MaxVersion )
168+ }
169+ })
170+ }
171+
172+ func TestTLSCipherSuitesWebhookOption (t * testing.T ) {
173+ opts := newDefaultOptions ()
174+ t .Run ("when TLSCipherSuites is not configured" , func (t * testing.T ) {
175+ wh , err := newAdmissionControllerWebhook (t , opts )
176+ if err != nil {
177+ t .Fatal ("Unexpected error" , err )
178+ }
179+
180+ if wh .tlsConfig != nil && wh .tlsConfig .CipherSuites != nil {
181+ t .Errorf ("Expected CipherSuites to be nil (default), got %v" , wh .tlsConfig .CipherSuites )
182+ }
183+ })
184+ t .Run ("when TLSCipherSuites is configured with specific ciphers" , func (t * testing.T ) {
185+ expectedCiphers := []uint16 {
186+ tls .TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ,
187+ tls .TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 ,
188+ }
189+ opts .TLSCipherSuites = expectedCiphers
190+ wh , err := newAdmissionControllerWebhook (t , opts )
191+ if err != nil {
192+ t .Fatal ("Unexpected error" , err )
193+ }
194+ if wh .tlsConfig == nil {
195+ t .Fatal ("Expected tlsConfig to be set" )
196+ }
197+ if len (wh .tlsConfig .CipherSuites ) != len (expectedCiphers ) {
198+ t .Errorf ("Expected %d cipher suites, got %d" , len (expectedCiphers ), len (wh .tlsConfig .CipherSuites ))
199+ }
200+ for i , cipher := range expectedCiphers {
201+ if wh .tlsConfig .CipherSuites [i ] != cipher {
202+ t .Errorf ("Expected cipher suite at index %d to be %d, got %d" , i , cipher , wh .tlsConfig .CipherSuites [i ])
203+ }
204+ }
205+ })
206+ }
207+
208+ func TestTLSCurvePreferencesWebhookOption (t * testing.T ) {
209+ opts := newDefaultOptions ()
210+ t .Run ("when TLSCurvePreferences is not configured" , func (t * testing.T ) {
211+ wh , err := newAdmissionControllerWebhook (t , opts )
212+ if err != nil {
213+ t .Fatal ("Unexpected error" , err )
214+ }
215+
216+ if wh .tlsConfig != nil && wh .tlsConfig .CurvePreferences != nil {
217+ t .Errorf ("Expected CurvePreferences to be nil (default), got %v" , wh .tlsConfig .CurvePreferences )
218+ }
219+ })
220+ t .Run ("when TLSCurvePreferences is configured with specific curves" , func (t * testing.T ) {
221+ expectedCurves := []tls.CurveID {
222+ tls .CurveP256 ,
223+ tls .CurveP384 ,
224+ tls .X25519 ,
225+ }
226+ opts .TLSCurvePreferences = expectedCurves
227+ wh , err := newAdmissionControllerWebhook (t , opts )
228+ if err != nil {
229+ t .Fatal ("Unexpected error" , err )
230+ }
231+ if wh .tlsConfig == nil {
232+ t .Fatal ("Expected tlsConfig to be set" )
233+ }
234+ if len (wh .tlsConfig .CurvePreferences ) != len (expectedCurves ) {
235+ t .Errorf ("Expected %d curve preferences, got %d" , len (expectedCurves ), len (wh .tlsConfig .CurvePreferences ))
236+ }
237+ for i , curve := range expectedCurves {
238+ if wh .tlsConfig .CurvePreferences [i ] != curve {
239+ t .Errorf ("Expected curve at index %d to be %d, got %d" , i , curve , wh .tlsConfig .CurvePreferences [i ])
240+ }
241+ }
242+ })
243+ }
244+
245+ func TestTLSConfigCombinedOptions (t * testing.T ) {
246+ opts := newDefaultOptions ()
247+ t .Run ("when all TLS options are configured together" , func (t * testing.T ) {
248+ opts .TLSMinVersion = tls .VersionTLS12
249+ opts .TLSMaxVersion = tls .VersionTLS13
250+ opts .TLSCipherSuites = []uint16 {
251+ tls .TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ,
252+ tls .TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 ,
253+ }
254+ opts .TLSCurvePreferences = []tls.CurveID {
255+ tls .CurveP256 ,
256+ tls .X25519 ,
257+ }
258+
259+ wh , err := newAdmissionControllerWebhook (t , opts )
260+ if err != nil {
261+ t .Fatal ("Unexpected error" , err )
262+ }
263+
264+ if wh .tlsConfig == nil {
265+ t .Fatal ("Expected tlsConfig to be set" )
266+ }
267+
268+ if wh .tlsConfig .MinVersion != tls .VersionTLS12 {
269+ t .Errorf ("Expected MinVersion to be TLS 1.2, got %d" , wh .tlsConfig .MinVersion )
270+ }
271+ if wh .tlsConfig .MaxVersion != tls .VersionTLS13 {
272+ t .Errorf ("Expected MaxVersion to be TLS 1.3, got %d" , wh .tlsConfig .MaxVersion )
273+ }
274+ if len (wh .tlsConfig .CipherSuites ) != 2 {
275+ t .Errorf ("Expected 2 cipher suites, got %d" , len (wh .tlsConfig .CipherSuites ))
276+ }
277+ if len (wh .tlsConfig .CurvePreferences ) != 2 {
278+ t .Errorf ("Expected 2 curve preferences, got %d" , len (wh .tlsConfig .CurvePreferences ))
279+ }
280+ })
281+ }
0 commit comments