-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: (WIP) Proxy Protocol Check for Traffic Endpoint
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package acceptance_tests | ||
|
||
import ( | ||
"fmt" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
proxyproto "github.com/pires/go-proxyproto" | ||
"io" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
var _ = Describe("Proxy Protocol", func() { | ||
opsfileProxyProtocol := `--- | ||
# Enable Proxy Protocol | ||
- type: replace | ||
path: /instance_groups/name=haproxy/jobs/name=haproxy/properties/ha_proxy/accept_proxy? | ||
value: true | ||
` | ||
It("Correctly proxies Proxy Protocol requests", func() { | ||
haproxyBackendPort := 12000 | ||
haproxyInfo, _ := deployHAProxy(baseManifestVars{ | ||
haproxyBackendPort: haproxyBackendPort, | ||
haproxyBackendServers: []string{"127.0.1"}, | ||
deploymentName: deploymentNameForTestNode(), | ||
}, []string{opsfileProxyProtocol}, map[string]interface{}{}, true) | ||
|
||
closeLocalServer, localPort := startDefaultTestServer() | ||
defer closeLocalServer() | ||
|
||
closeTunnel := setupTunnelFromHaproxyToTestServer(haproxyInfo, haproxyBackendPort, localPort) | ||
defer closeTunnel() | ||
|
||
By("Sending a request with Proxy Protocol Header to HAProxy") | ||
err := performProxyProtocolRequest(haproxyInfo.PublicIP, 443) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("Sending a request without Proxy Protocol Header to HAProxy") | ||
expect400(http.Get(fmt.Sprintf("http://%s", haproxyInfo.PublicIP))) | ||
}) | ||
}) | ||
|
||
func performProxyProtocolRequest(ip string, port int) error { | ||
// Dial some proxy listener e.g. https://github.com/mailgun/proxyproto | ||
target, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", ip, port)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
conn, err := net.DialTCP("tcp", nil, target) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func(conn *net.TCPConn) { | ||
_ = conn.Close() | ||
}(conn) | ||
|
||
// Create a proxyprotocol header or use HeaderProxyFromAddrs() if you | ||
// have two conn's | ||
header := &proxyproto.Header{ | ||
Version: 1, | ||
Command: proxyproto.PROXY, | ||
TransportProtocol: proxyproto.TCPv4, | ||
SourceAddr: &net.TCPAddr{ | ||
IP: net.ParseIP("10.1.1.1"), | ||
Port: 1000, | ||
}, | ||
DestinationAddr: &net.TCPAddr{ | ||
IP: net.ParseIP(ip), | ||
Port: port, | ||
}, | ||
} | ||
// After the connection was created write the proxy headers first | ||
_, err = header.WriteTo(conn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = io.WriteString(conn, "GET / HTTP/1.1\r\n\r\n") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Read the response | ||
buf := make([]byte, 1024) | ||
_, err = conn.Read(buf) | ||
if err != nil { | ||
return err | ||
} | ||
// Get HTTP status code | ||
if string(buf[9:12]) != "200" { | ||
return fmt.Errorf("expected HTTP status code 200, got %s", string(buf)) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters