Skip to content

Commit 2195b65

Browse files
committed
refactor
Signed-off-by: Huabing Zhao <[email protected]>
1 parent 9350110 commit 2195b65

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

internal/gatewayapi/securitypolicy.go

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package gatewayapi
77

88
import (
99
"crypto/tls"
10-
"crypto/x509"
1110
"encoding/json"
1211
"errors"
1312
"fmt"
@@ -785,25 +784,16 @@ type OpenIDConfig struct {
785784
}
786785

787786
func fetchEndpointsFromIssuer(issuerURL string, providerTLS *ir.TLSUpstreamConfig) (string, string, error) {
788-
var tlsConfig *tls.Config
787+
var (
788+
tlsConfig *tls.Config
789+
err error
790+
)
789791

790792
if providerTLS != nil {
791-
tlsConfig = &tls.Config{
792-
ServerName: providerTLS.SNI,
793-
MinVersion: tls.VersionTLS13,
794-
}
795-
if providerTLS.CACertificate != nil {
796-
caCertPool := x509.NewCertPool()
797-
caCertPool.AppendCertsFromPEM(providerTLS.CACertificate.Certificate)
798-
tlsConfig.RootCAs = caCertPool
799-
}
800-
for _, cert := range providerTLS.ClientCertificates {
801-
cert, err := tls.X509KeyPair(cert.Certificate, cert.PrivateKey)
802-
if err != nil {
803-
return "", "", err
804-
}
805-
tlsConfig.Certificates = append(tlsConfig.Certificates, cert)
806-
}
793+
tlsConfig, err = providerTLS.ToTLSConfig()
794+
}
795+
if err != nil {
796+
return "", "", err
807797
}
808798

809799
// Fetch the OpenID configuration from the issuer URL

internal/ir/xds.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package ir
77

88
import (
99
"cmp"
10+
"crypto/tls"
11+
"crypto/x509"
1012
"encoding"
1113
"encoding/json"
1214
"errors"
@@ -359,6 +361,23 @@ const (
359361
TLSv13 = TLSVersion(egv1a1.TLSv13)
360362
)
361363

364+
func (t TLSVersion) Int() uint16 {
365+
switch t {
366+
case TLSAuto:
367+
return tls.VersionTLS13
368+
case TLSv10:
369+
return tls.VersionTLS10
370+
case TLSv11:
371+
return tls.VersionTLS11
372+
case TLSv12:
373+
return tls.VersionTLS12
374+
case TLSv13:
375+
return tls.VersionTLS13
376+
default:
377+
return tls.VersionTLS13
378+
}
379+
}
380+
362381
// TLSConfig holds the configuration for downstream TLS context.
363382
// +k8s:deepcopy-gen=true
364383
type TLSConfig struct {
@@ -2539,6 +2558,28 @@ type TLSUpstreamConfig struct {
25392558
TLSConfig `json:",inline"`
25402559
}
25412560

2561+
func (t *TLSUpstreamConfig) ToTLSConfig() (*tls.Config, error) {
2562+
// nolint:gosec
2563+
tlsConfig := &tls.Config{
2564+
ServerName: t.SNI,
2565+
MinVersion: t.MinVersion.Int(),
2566+
MaxVersion: t.MaxVersion.Int(),
2567+
}
2568+
if t.CACertificate != nil {
2569+
caCertPool := x509.NewCertPool()
2570+
caCertPool.AppendCertsFromPEM(t.CACertificate.Certificate)
2571+
tlsConfig.RootCAs = caCertPool
2572+
}
2573+
for _, cert := range t.ClientCertificates {
2574+
cert, err := tls.X509KeyPair(cert.Certificate, cert.PrivateKey)
2575+
if err != nil {
2576+
return nil, err
2577+
}
2578+
tlsConfig.Certificates = append(tlsConfig.Certificates, cert)
2579+
}
2580+
return tlsConfig, nil
2581+
}
2582+
25422583
// BackendConnection settings for upstream connections
25432584
// +k8s:deepcopy-gen=true
25442585
type BackendConnection struct {

0 commit comments

Comments
 (0)