|
9 | 9 | resourcev3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3" |
10 | 10 | "github.com/envoyproxy/go-control-plane/pkg/wellknown" |
11 | 11 | "google.golang.org/protobuf/types/known/anypb" |
| 12 | + "google.golang.org/protobuf/types/known/wrapperspb" |
12 | 13 | gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
13 | 14 | ) |
14 | 15 |
|
@@ -88,29 +89,37 @@ func translateListenerToEnvoyListener(listener gatewayv1.Listener) (map[resource |
88 | 89 | if err != nil { |
89 | 90 | return nil, err |
90 | 91 | } |
91 | | - envoyListener.FilterChains = []*listenerv3.FilterChain{{ |
| 92 | + filterChain := &listenerv3.FilterChain{ |
92 | 93 | Filters: []*listenerv3.Filter{{ |
93 | 94 | Name: wellknown.HTTPConnectionManager, |
94 | 95 | ConfigType: &listenerv3.Filter_TypedConfig{ |
95 | 96 | TypedConfig: hcmAny, |
96 | 97 | }, |
97 | 98 | }}, |
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 | + } |
99 | 107 |
|
100 | 108 | if listener.Protocol == gatewayv1.HTTPSProtocolType || listener.TLS != nil { |
101 | 109 | // 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 | + } |
113 | 121 | } |
| 122 | + envoyListener.FilterChains = []*listenerv3.FilterChain{filterChain} |
114 | 123 |
|
115 | 124 | case gatewayv1.TCPProtocolType: |
116 | 125 | // TCP listener needs a pass-through filter |
@@ -182,53 +191,42 @@ func translateListenerToEnvoyListener(listener gatewayv1.Listener) (map[resource |
182 | 191 | } |
183 | 192 |
|
184 | 193 | 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 | + } |
221 | 201 |
|
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 |
224 | 208 | } |
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 |
230 | 213 | } |
| 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, |
231 | 228 | } |
232 | | - */ |
| 229 | + } |
| 230 | + |
233 | 231 | return downstreamTLSContext |
234 | 232 | } |
0 commit comments