8000 ingress/model: Support multiple certs based on SNI by sayboras · Pull Request #22671 · cilium/cilium · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ingress/model: Support multiple certs based on SNI #22671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions operator/pkg/model/translation/envoy_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func WithSocketOption(tcpKeepAlive, tcpKeepIdleInSeconds, tcpKeepAliveProbeInter
}

// NewListenerWithDefaults same as NewListener but with default mutators applied.
func NewListenerWithDefaults(name string, ciliumSecretNamespace string, tls []model.TLSSecret, mutatorFunc ...ListenerMutator) (ciliumv2.XDSResource, error) {
func NewListenerWithDefaults(name string, ciliumSecretNamespace string, tls map[model.TLSSecret][]string, mutatorFunc ...ListenerMutator) (ciliumv2.XDSResource, error) {
fns := append(mutatorFunc,
WithSocketOption(
defaultTCPKeepAlive,
Expand All @@ -95,7 +95,7 @@ func NewListenerWithDefaults(name string, ciliumSecretNamespace string, tls []mo
// NewListener creates a new Envoy listener with the given name.
// The listener will have both secure and insecure filters.
// Secret Discovery Service (SDS) is used to fetch the TLS certificates.
func NewListener(name string, ciliumSecretNamespace string, tls []model.TLSSecret, mutatorFunc ...ListenerMutator) (ciliumv2.XDSResource, error) {
func NewListener(name string, ciliumSecretNamespace string, tls map[model.TLSSecret][]string, mutatorFunc ...ListenerMutator) (ciliumv2.XDSResource, error) {
var filterChains []*envoy_config_listener.FilterChain

insecureHttpConnectionManagerName := fmt.Sprintf("%s-insecure", name)
Expand All @@ -116,20 +116,23 @@ func NewListener(name string, ciliumSecretNamespace string, tls []model.TLSSecre
},
})

if len(tls) > 0 {
for secret, hostNames := range tls {
secureHttpConnectionManagerName := fmt.Sprintf("%s-secure", name)
secureHttpConnectionManager, err := NewHTTPConnectionManager(secureHttpConnectionManagerName, secureHttpConnectionManagerName)
if err != nil {
return ciliumv2.XDSResource{}, err
}

transportSocket, err := newTransportSocket(ciliumSecretNamespace, tls)
transportSocket, err := newTransportSocket(ciliumSecretNamespace, []model.TLSSecret{secret})
if err != nil {
return ciliumv2.XDSResource{}, err
}

filterChains = append(filterChains, &envoy_config_listener.FilterChain{
FilterChainMatch: &envoy_config_listener.FilterChainMatch{TransportProtocol: tlsTransportProtocol},
FilterChainMatch: &envoy_config_listener.FilterChainMatch{
ServerNames: sortAndUnique(hostNames),
TransportProtocol: tlsTransportProtocol,
},
Filters: []*envoy_config_listener.Filter{
{
Name: httpConnectionManagerType,
Expand Down
40 changes: 27 additions & 13 deletions operator/pkg/model/translation/envoy_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,9 @@ func TestNewListener(t *testing.T) {
})

t.Run("TLS", func(t *testing.T) {
res, err := NewListener("dummy-name", "dummy-secret-namespace", []model.TLSSecret{
{
Name: "dummy-secret-1",
Namespace: "dummy-namespace",
},
{
Name: "dummy-secret-2",
Namespace: "dummy-namespace",
},
res, err := NewListener("dummy-name", "dummy-secret-namespace", map[model.TLSSecret][]string{
{Name: "dummy-secret-1", Namespace: "dummy-namespace"}: {"dummy.server.com"},
{Name: "dummy-secret-2", Namespace: "dummy-namespace"}: {"dummy.anotherserver.com"},
})
require.Nil(t, err)

Expand All @@ -49,20 +43,40 @@ func TestNewListener(t *testing.T) {

require.Equal(t, "dummy-name", listener.Name)
require.Len(t, listener.GetListenerFilters(), 1)
require.Len(t, listener.GetFilterChains(), 2)
require.Len(t, listener.GetFilterChains(), 3)
require.Equal(t, "raw_buffer", listener.GetFilterChains()[0].GetFilterChainMatch().TransportProtocol)
require.Equal(t, "tls", listener.GetFilterChains()[1].GetFilterChainMatch().TransportProtocol)
require.Equal(t, "tls", listener.GetFilterChains()[2].GetFilterChainMatch().TransportProtocol)
require.Len(t, listener.GetFilterChains()[1].GetFilters(), 1)
var serverNames []string
serverNames = append(serverNames, listener.GetFilterChains()[1].GetFilterChainMatch().ServerNames...)
serverNames = append(serverNames, listener.GetFilterChains()[2].GetFilterChainMatch().ServerNames...)
sort.Strings(serverNames)
require.Equal(t, []string{"dummy.anotherserver.com", "dummy.server.com"}, serverNames)

downStreamTLS := &envoy_extensions_transport_sockets_tls_v3.DownstreamTlsContext{}
err = proto.Unmarshal(listener.FilterChains[1].TransportSocket.ConfigType.(*envoy_config_core_v3.TransportSocket_TypedConfig).TypedConfig.Value, downStreamTLS)
require.NoError(t, err)

require.Len(t, downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs, 2)
var secretNames []string
require.Len(t, downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs, 1)
sort.Slice(downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs, func(i, j int) bool {
return downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs[i].Name < downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs[j].Name
})
require.Equal(t, "dummy-secret-namespace/dummy-namespace-dummy-secret-1", downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs[0].GetName())
require.Equal(t, "dummy-secret-namespace/dummy-namespace-dummy-secret-2", downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs[1].GetName())
secretNames = append(secretNames, downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs[0].GetName())

err = proto.Unmarshal(listener.FilterChains[2].TransportSocket.ConfigType.(*envoy_config_core_v3.TransportSocket_TypedConfig).TypedConfig.Value, downStreamTLS)
require.NoError(t, err)

require.Len(t, downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs, 1)
sort.Slice(downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs, func(i, j int) bool {
return downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs[i].Name < downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs[j].Name
})
Comment on lines +72 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have just one config at a time, we can avoid the sorting here and at line 63.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, let me clean this up in subsequent PR, just to avoid re-run all tests.

secretNames = append(secretNames, downStreamTLS.CommonTlsContext.TlsCertificateSdsSecretConfigs[0].GetName())

sort.Strings(secretNames)
require.Equal(t, "dummy-secret-namespace/dummy-namespace-dummy-secret-1", secretNames[0])
require.Equal(t, "dummy-secret-namespace/dummy-namespace-dummy-secret-2", secretNames[1])

})
}
10 changes: 3 additions & 7 deletions operator/pkg/model/translation/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,14 @@ func (i *defaultTranslator) getResources(m *model.Model) []ciliumv2.XDSResource
// listener is returned for shared LB mode, tls and non-tls filters are
// applied by default.
func (i *defaultTranslator) getListener(m *model.Model) []ciliumv2.XDSResource {
var tlsMap = make(map[string]model.TLSSecret)
var tlsMap = make(map[model.TLSSecret][]string)
for _, h := range m.HTTP {
for _, s := range h.TLS {
tlsMap[s.Namespace+"/"+s.Name] = s
tlsMap[s] = append(tlsMap[s], h.Hostname)
}
}
tls := make([]model.TLSSecret, 0, len(tlsMap))
for _, v := range tlsMap {
tls = append(tls, v)
}

l, _ := NewListenerWithDefaults("listener", i.secretsNamespace, tls)
l, _ := NewListenerWithDefaults("listener", i.secretsNamespace, tlsMap)
return []ciliumv2.XDSResource{l}
}

Expand Down
0