Skip to content

Commit d76eddf

Browse files
committed
listener
Change-Id: I3e68009a97adb2206f29a09f8ed74ad4e196ce9b
1 parent 8f793a2 commit d76eddf

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

pkg/gateway/listener.go

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
resourcev3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
1010
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
1111
"google.golang.org/protobuf/types/known/anypb"
12+
"google.golang.org/protobuf/types/known/wrapperspb"
1213
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
1314
)
1415

@@ -88,29 +89,37 @@ func translateListenerToEnvoyListener(listener gatewayv1.Listener) (map[resource
8889
if err != nil {
8990
return nil, err
9091
}
91-
envoyListener.FilterChains = []*listenerv3.FilterChain{{
92+
filterChain := &listenerv3.FilterChain{
9293
Filters: []*listenerv3.Filter{{
9394
Name: wellknown.HTTPConnectionManager,
9495
ConfigType: &listenerv3.Filter_TypedConfig{
9596
TypedConfig: hcmAny,
9697
},
9798
}},
98-
}}
99+
}
100+
101+
// Add SNI matching if a hostname is specified
102+
if listener.Hostname != nil && *listener.Hostname != "" {
103+
filterChain.FilterChainMatch = &listenerv3.FilterChainMatch{
104+
ServerNames: []string{string(*listener.Hostname)},
105+
}
106+
}
99107

100108
if listener.Protocol == gatewayv1.HTTPSProtocolType || listener.TLS != nil {
101109
// Configure TLS if it's an HTTPS listener or TLS is explicitly configured
102-
/*
103-
tlsContext := buildDownstreamTLSContext(listener)
104-
pbst, err := anypb.New(tlsContext)
105-
if err != nil {
106-
return nil, err
107-
}
108-
envoyListener.TransportSocket = &corev3.TransportSocket{
109-
Name: wellknown.TransportSocketTls,
110-
Config: pbst, // Use Config instead of TypedConfig directly
111-
}
112-
*/
110+
tlsContext := buildDownstreamTLSContext(listener)
111+
pbst, err := anypb.New(tlsContext)
112+
if err != nil {
113+
return nil, err
114+
}
115+
filterChain.TransportSocket = &corev3.TransportSocket{
116+
Name: wellknown.TransportSocketTls,
117+
ConfigType: &corev3.TransportSocket_TypedConfig{
118+
TypedConfig: pbst,
119+
},
120+
}
113121
}
122+
envoyListener.FilterChains = []*listenerv3.FilterChain{filterChain}
114123

115124
case gatewayv1.TCPProtocolType:
116125
// TCP listener needs a pass-through filter
@@ -182,53 +191,42 @@ func translateListenerToEnvoyListener(listener gatewayv1.Listener) (map[resource
182191
}
183192

184193
func buildDownstreamTLSContext(listener gatewayv1.Listener) *tlsv3.DownstreamTlsContext {
185-
downstreamTLSContext := &tlsv3.DownstreamTlsContext{}
186-
187-
/*
188-
if listener.TLS != nil {
189-
if listener.TLS.CertificateRefs != nil {
190-
for _, certRef := range listener.TLS.CertificateRefs {
191-
if certRef.Kind == "Secret" && *certRef.Group == "" { // Assuming corev1.Secret in the same namespace
192-
downstreamTLSContext.CommonTlsContext = &corev3.CommonTlsContext{
193-
TlsCertificates: []*corev3.TlsCertificate{{
194-
CertificateChain: &corev3.DataSource{
195-
Specifier: &corev3.DataSource_Secret{
196-
Secret: &corev3.SecretDataSource{
197-
Name: string(certRef.Name),
198-
},
199-
},
200-
},
201-
PrivateKey: &corev3.DataSource{
202-
Specifier: &corev3.DataSource_Secret{
203-
Secret: &corev3.SecretDataSource{
204-
Name: string(certRef.Name),
205-
},
206-
},
207-
},
208-
}},
209-
}
210-
// Basic SNI configuration based on hostname in the listener
211-
if len(listener.Hostname) > 0 {
212-
downstreamTLSContext.CommonTlsContext.SniMatchers = []*corev3.StringMatcher{{
213-
MatchPattern: &corev3.StringMatcher_Exact{Exact: string(listener.Hostname)},
214-
}}
215-
}
216-
break // For now, just take the first valid certificate ref
217-
}
218-
// Handle other kinds and groups if needed
219-
}
220-
}
194+
if listener.TLS == nil {
195+
return &tlsv3.DownstreamTlsContext{}
196+
}
197+
198+
downstreamTLSContext := &tlsv3.DownstreamTlsContext{
199+
CommonTlsContext: &tlsv3.CommonTlsContext{},
200+
}
221201

222-
if listener.TLS.Mode != nil && *listener.TLS.Mode == gatewayv1.TLSModeTerminate {
223-
downstreamTLSContext.RequireClientCertificate = false // Or true based on your requirements
202+
if listener.TLS.CertificateRefs != nil {
203+
for _, certRef := range listener.TLS.CertificateRefs {
204+
// Check Kind: Default is "Secret"
205+
refKind := gatewayv1.Kind("Secret")
206+
if certRef.Kind != nil {
207+
refKind = *certRef.Kind
224208
}
225-
// Add other TLS context configurations as needed (e.g., ALPN protocols)
226-
if listener.Protocol == gatewayv1.HTTPSProtocolType {
227-
downstreamTLSContext.CommonTlsContext.AlpnProtocols = []string{"h2", "http/1.1"}
228-
} else if listener.TLS != nil && len(listener.TLS.AlpnProtocols) > 0 {
229-
downstreamTLSContext.CommonTlsContext.AlpnProtocols = listener.TLS.AlpnProtocols
209+
// Check Group: Default is "" (core group)
210+
refGroup := gatewayv1.Group("")
211+
if certRef.Group != nil {
212+
refGroup = *certRef.Group
230213
}
214+
if refKind == "Secret" && refGroup == "" {
215+
downstreamTLSContext.CommonTlsContext.TlsCertificates = []*tlsv3.TlsCertificate{{
216+
CertificateChain: &corev3.DataSource{},
217+
PrivateKey: &corev3.DataSource{},
218+
}}
219+
break // For now, just take the first valid certificate ref
220+
}
221+
// Handle other kinds and groups if needed
222+
}
223+
}
224+
225+
if listener.TLS.Mode != nil && *listener.TLS.Mode == gatewayv1.TLSModeTerminate {
226+
downstreamTLSContext.RequireClientCertificate = &wrapperspb.BoolValue{
227+
Value: true,
231228
}
232-
*/
229+
}
230+
233231
return downstreamTLSContext
234232
}

0 commit comments

Comments
 (0)