Skip to content

Commit

Permalink
feat: add TLS version annotation support for per-rule configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
yunmaoQu committed Dec 13, 2024
1 parent 441408c commit a04ab0f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/ingress/config/ingress_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ type IngressConfig struct {
clusterId cluster.ID

httpsConfigMgr *cert.ConfigMgr

UpstreamTLS *TLSConfig
}

func NewIngressConfig(localKubeClient kube.Client, xdsUpdater istiomodel.XDSUpdater, namespace string, clusterId cluster.ID) *IngressConfig {
Expand Down
6 changes: 6 additions & 0 deletions pkg/ingress/kube/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ type AnnotationHandler interface {
RouteHandler
TrafficPolicyHandler
}
type TLSConfig struct {
MinVersion string
MaxVersion string
}

type AnnotationHandlerManager struct {
parsers []Parser
Expand Down Expand Up @@ -169,6 +173,7 @@ func NewAnnotationHandlerManager() AnnotationHandler {
match{},
headerControl{},
http2rpc{},
tls{},
},
gatewayHandlers: []GatewayHandler{
downstreamTLS{},
Expand All @@ -193,6 +198,7 @@ func NewAnnotationHandlerManager() AnnotationHandler {
trafficPolicyHandlers: []TrafficPolicyHandler{
upstreamTLS{},
loadBalance{},
tls{},
},
}
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/ingress/kube/annotations/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,43 @@ func TestNeedTrafficPolicy(t *testing.T) {
t.Fatal("should be true")
}
}

func TestTLSConfig(t *testing.T) {
testCases := []struct {
annotations map[string]string
expectTLS *TLSConfig
}{
{
annotations: map[string]string{
"tls-min-version": "TLSv1_2",
"tls-max-version": "TLSv1_3",
},
expectTLS: &TLSConfig{
MinVersion: "TLSv1_2",
MaxVersion: "TLSv1_3",
},
},
{
annotations: map[string]string{
"tls-min-version": "TLSv1_1",
},
expectTLS: &TLSConfig{
MinVersion: "TLSv1_1",
},
},
}

for _, testCase := range testCases {
ingress := &Ingress{}
annotations := Annotations(testCase.annotations)
handler := tls{}
err := handler.Parse(annotations, ingress, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if ingress.UpstreamTLS.MinVersion != testCase.expectTLS.MinVersion || ingress.UpstreamTLS.MaxVersion != testCase.expectTLS.MaxVersion {
t.Fatalf("expected %+v, got %+v", testCase.expectTLS, ingress.UpstreamTLS)
}
}
}
81 changes: 81 additions & 0 deletions pkg/ingress/kube/annotations/tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package annotations

import (
networking "istio.io/api/networking/v1alpha3"
)

const (
tlsMinVersion = "tls-min-version"
tlsMaxVersion = "tls-max-version"
)

var (
_ Parser = tls{}
_ TrafficPolicyHandler = tls{}
)

type tls struct{}

func (t tls) Parse(annotations Annotations, config *Ingress, _ *GlobalContext) error {
if !needTLSConfig(annotations) {
return nil
}

tlsConfig := &TLSConfig{}
defer func() {
config.UpstreamTLS = tlsConfig

Check failure on line 26 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

cannot use tlsConfig (variable of type *TLSConfig) as *UpstreamTLSConfig value in assignment
}()

// Parse minimum TLS version
if minVersion, err := annotations.ParseStringASAP(tlsMinVersion); err == nil {
tlsConfig.MinVersion = minVersion
}

// Parse maximum TLS version
if maxVersion, err := annotations.ParseStringASAP(tlsMaxVersion); err == nil {
tlsConfig.MaxVersion = maxVersion
}

return nil
}

func (t tls) ApplyTrafficPolicy(trafficPolicy *networking.TrafficPolicy, _ *networking.TrafficPolicy_PortTrafficPolicy, config *Ingress) {
tlsConfig := config.UpstreamTLS
if tlsConfig == nil {
return
}

if trafficPolicy.Tls == nil {
trafficPolicy.Tls = &networking.ClientTLSSettings{}
}

// Apply min version
if tlsConfig.MinVersion != "" {

Check failure on line 53 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

tlsConfig.MinVersion undefined (type *UpstreamTLSConfig has no field or method MinVersion)
trafficPolicy.Tls.MinProtocolVersion = convertTLSVersion(tlsConfig.MinVersion)

Check failure on line 54 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

trafficPolicy.Tls.MinProtocolVersion undefined (type *"istio.io/api/networking/v1alpha3".ClientTLSSettings has no field or method MinProtocolVersion)

Check failure on line 54 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

tlsConfig.MinVersion undefined (type *UpstreamTLSConfig has no field or method MinVersion)
}

// Apply max version
if tlsConfig.MaxVersion != "" {

Check failure on line 58 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

tlsConfig.MaxVersion undefined (type *UpstreamTLSConfig has no field or method MaxVersion)
trafficPolicy.Tls.MaxProtocolVersion = convertTLSVersion(tlsConfig.MaxVersion)

Check failure on line 59 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

trafficPolicy.Tls.MaxProtocolVersion undefined (type *"istio.io/api/networking/v1alpha3".ClientTLSSettings has no field or method MaxProtocolVersion)

Check failure on line 59 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

tlsConfig.MaxVersion undefined (type *UpstreamTLSConfig has no field or method MaxVersion)
}
}

func needTLSConfig(annotations Annotations) bool {
return annotations.HasASAP(tlsMinVersion) || annotations.HasASAP(tlsMaxVersion)
}

// Helper to convert TLS version string to istio enum
func convertTLSVersion(version string) networking.ClientTLSSettings_TLSProtocol {

Check failure on line 68 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

undefined: networking.ClientTLSSettings_TLSProtocol
switch version {
case "TLSv1_0":
return networking.ClientTLSSettings_TLSV1_0

Check failure on line 71 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

undefined: networking.ClientTLSSettings_TLSV1_0
case "TLSv1_1":
return networking.ClientTLSSettings_TLSV1_1

Check failure on line 73 in pkg/ingress/kube/annotations/tls.go

View workflow job for this annotation

GitHub Actions / coverage-test

undefined: networking.ClientTLSSettings_TLSV1_1
case "TLSv1_2":
return networking.ClientTLSSettings_TLSV1_2
case "TLSv1_3":
return networking.ClientTLSSettings_TLSV1_3
default:
return networking.ClientTLSSettings_TLS_AUTO
}
}

0 comments on commit a04ab0f

Please sign in to comment.