@@ -17,6 +17,7 @@ package handler
1717import (
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+
9196func (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
257261func (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+
269287func (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