Skip to content

Commit c379fcf

Browse files
committed
MEDIUM: use unix socket when mixing ssl passthrough and offloading
1 parent 9d9640d commit c379fcf

File tree

4 files changed

+64
-35
lines changed

4 files changed

+64
-35
lines changed

.aspell.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ allowed:
2828
- linters
2929
- tls
3030
- lifecycle
31+
- passthrough
32+
- ssl
33+
- unix

pkg/controller/builder.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/client"
2929

3030
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
31+
"github.com/haproxytech/kubernetes-ingress/pkg/controller/constants"
3132
gateway "github.com/haproxytech/kubernetes-ingress/pkg/gateways"
3233
"github.com/haproxytech/kubernetes-ingress/pkg/handler"
3334
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
@@ -67,10 +68,10 @@ var defaultEnv = env.Env{
6768
RuntimeDir: "/var/run",
6869
StateDir: "/var/state/haproxy/",
6970
Proxies: env.Proxies{
70-
FrontHTTP: "http",
71-
FrontHTTPS: "https",
72-
FrontSSL: "ssl",
73-
BackSSL: "ssl",
71+
FrontHTTP: constants.HTTP_FRONTEND,
72+
FrontHTTPS: constants.HTTPS_FRONTEND,
73+
FrontSSL: constants.SSL_FRONTEND,
74+
BackSSL: constants.SSL_BACKEND,
7475
},
7576
}
7677

pkg/controller/constants/const.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@
1414

1515
package constants
1616

17-
const DefaultsSectionName = "haproxytech"
17+
//nolint:golint, stylecheck
18+
const (
19+
DefaultsSectionName = "haproxytech"
20+
SSL_FRONTEND = "ssl"
21+
SSL_BACKEND = "ssl"
22+
HTTP_FRONTEND = "http"
23+
HTTPS_FRONTEND = "https"
24+
)

pkg/handler/https.go

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package handler
1717
import (
1818
"errors"
1919
"fmt"
20+
"path"
2021

2122
"github.com/haproxytech/client-native/v5/models"
2223

@@ -45,49 +46,53 @@ type HTTPS struct {
4546
}
4647

4748
//nolint:golint, stylecheck
48-
const HTTPS_PORT_SSLPASSTHROUGH int64 = 8444
49+
const (
50+
HTTPS_PORT_SSLPASSTHROUGH int64 = 8444
51+
BIND_UNIX_SOCKET = "unixsock"
52+
BIND_IP_V4 = "v4"
53+
BIND_IP_V6 = "v6"
54+
)
4955

50-
func (handler HTTPS) bindList(passhthrough bool) (binds []models.Bind) {
56+
func (handler HTTPS) bindList(h haproxy.HAProxy) (binds []models.Bind) {
5157
if handler.IPv4 {
5258
binds = append(binds, models.Bind{
5359
Address: handler.AddrIPv4,
54-
Port: func() *int64 {
55-
if passhthrough {
56-
return utils.PtrInt64(HTTPS_PORT_SSLPASSTHROUGH)
57-
}
58-
return utils.PtrInt64(handler.Port)
59-
}(),
60+
Port: utils.PtrInt64(handler.Port),
6061
BindParams: models.BindParams{
61-
Name: "v4",
62-
AcceptProxy: passhthrough,
62+
Name: BIND_IP_V4,
63+
AcceptProxy: false,
6364
},
6465
})
6566
}
6667
if handler.IPv6 {
6768
binds = append(binds, models.Bind{
68-
Address: func() (addr string) {
69-
addr = handler.AddrIPv6
70-
if passhthrough {
71-
addr = "::"
72-
}
73-
return
74-
}(),
75-
Port: func() *int64 {
76-
if passhthrough {
77-
return utils.PtrInt64(HTTPS_PORT_SSLPASSTHROUGH)
78-
}
79-
return utils.PtrInt64(handler.Port)
80-
}(),
69+
Address: handler.AddrIPv6,
70+
Port: utils.PtrInt64(handler.Port),
8171
BindParams: models.BindParams{
82-
AcceptProxy: passhthrough,
83-
Name: "v6",
72+
AcceptProxy: false,
73+
Name: BIND_IP_V6,
8474
V4v6: true,
8575
},
8676
})
8777
}
8878
return binds
8979
}
9080

81+
func (handler HTTPS) bindListPassthrough(h haproxy.HAProxy) (binds []models.Bind) {
82+
binds = append(binds, models.Bind{
83+
Address: "unix@" + handler.unixSocketPath(h),
84+
BindParams: models.BindParams{
85+
Name: BIND_UNIX_SOCKET,
86+
AcceptProxy: true,
87+
},
88+
})
89+
return binds
90+
}
91+
92+
func (handler HTTPS) unixSocketPath(h haproxy.HAProxy) string {
93+
return path.Join(h.Env.RuntimeDir, "ssl-frontend.sock")
94+
}
95+
9196
func (handler HTTPS) handleClientTLSAuth(k store.K8s, h haproxy.HAProxy) (err error) {
9297
// Parsing
9398
var caFile string
@@ -211,7 +216,7 @@ func (handler HTTPS) enableSSLPassthrough(h haproxy.HAProxy) (err error) {
211216
if err != nil {
212217
return err
213218
}
214-
for _, b := range handler.bindList(false) {
219+
for _, b := range handler.bindList(h) {
215220
if err = h.FrontendBindCreate(h.FrontSSL, b); err != nil {
216221
return fmt.Errorf("cannot create bind for SSL Passthrough: %w", err)
217222
}
@@ -226,8 +231,7 @@ func (handler HTTPS) enableSSLPassthrough(h haproxy.HAProxy) (err error) {
226231
}),
227232
h.BackendServerCreate(h.BackSSL, models.Server{
228233
Name: h.FrontHTTPS,
229-
Address: "127.0.0.1",
230-
Port: utils.PtrInt64(HTTPS_PORT_SSLPASSTHROUGH),
234+
Address: "unix@" + handler.unixSocketPath(h),
231235
ServerParams: models.ServerParams{SendProxyV2: "enabled"},
232236
}),
233237
h.BackendSwitchingRuleCreate(h.FrontSSL, models.BackendSwitchingRule{
@@ -255,8 +259,13 @@ func (handler HTTPS) disableSSLPassthrough(h haproxy.HAProxy) (err error) {
255259
}
256260

257261
func (handler HTTPS) toggleSSLPassthrough(passthrough bool, h haproxy.HAProxy) (err error) {
258-
for _, bind := range handler.bindList(passthrough) {
259-
if err = h.FrontendBindEdit(h.FrontHTTPS, bind); err != nil {
262+
handler.deleteHTTPSFrontendBinds(h)
263+
bindListFunc := handler.bindList
264+
if passthrough {
265+
bindListFunc = handler.bindListPassthrough
266+
}
267+
for _, bind := range bindListFunc(h) {
268+
if err = h.FrontendBindCreate(h.FrontHTTPS, bind); err != nil {
260269
return err
261270
}
262271
}
@@ -266,6 +275,15 @@ func (handler HTTPS) toggleSSLPassthrough(passthrough bool, h haproxy.HAProxy) (
266275
return nil
267276
}
268277

278+
func (handler HTTPS) deleteHTTPSFrontendBinds(h haproxy.HAProxy) {
279+
bindsToDelete := []string{BIND_IP_V4, BIND_IP_V6, BIND_UNIX_SOCKET}
280+
for _, bind := range bindsToDelete {
281+
if err := h.FrontendBindDelete(h.FrontHTTPS, bind); err != nil {
282+
logger.Tracef("cannot delete bind %s: %s", bind, err)
283+
}
284+
}
285+
}
286+
269287
func (handler HTTPS) sslPassthroughRules(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) error {
270288
inspectTimeout, err := a.Timeout("timeout-client", k.ConfigMaps.Main.Annotations)
271289
if inspectTimeout == nil {

0 commit comments

Comments
 (0)