Skip to content

Commit

Permalink
Add an integration test for HTTPS Proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurai-youhei committed Oct 29, 2023
1 parent 1740c28 commit 1c98d82
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 16 deletions.
4 changes: 3 additions & 1 deletion libbeat/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ services:
interval: 1s
ports:
- 9200:9200
- 3128:3128 # Squid listens in the proxy service container.
# Squid listens on 3128 (HTTP) and 3129 (HTTPS) from the proxy service container.
- 3128:3128
- 3129:3129

elasticsearchssl:
extends:
Expand Down
11 changes: 5 additions & 6 deletions libbeat/esleg/eslegclient/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,18 @@ func (h *httpClientProxySettings) ProxyDialer(_ *url.URL, forward proxy.Dialer)
}

func (conn *Connection) testProxyDialer(d testing.Driver, forward transport.Dialer) transport.Dialer {
switch scheme := conn.Transport.Proxy.URL.Scheme; scheme {
case "http", "https":
proxy.RegisterDialerType(scheme, ((*httpClientProxySettings)(&conn.Transport.Proxy)).ProxyDialer)
}

dialer := forward

if conn.Transport.Proxy.URL.Scheme == "https" {
switch scheme := conn.Transport.Proxy.URL.Scheme; scheme {
case "https":
tls, err := tlscommon.LoadTLSConfig(conn.Transport.TLS)
if err != nil {
d.Fatal("load tls config", err)
}
dialer = transport.TestTLSDialer(d, dialer, tls, conn.Transport.Timeout)
fallthrough
case "http":
proxy.RegisterDialerType(scheme, ((*httpClientProxySettings)(&conn.Transport.Proxy)).ProxyDialer)
}

dialer, err := transport.ProxyDialer(logp.L(), &transport.ProxyConfig{URL: conn.Transport.Proxy.URL.String()}, dialer)
Expand Down
19 changes: 17 additions & 2 deletions libbeat/tests/integration/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestCmdTestOutputBadHost(t *testing.T) {

func TestCmdTestOutputProxy(t *testing.T) {
esURL := GetESURL(t, "http")
proxyURL := GetProxyURL(t)
proxyURL := GetProxyURL(t, "http")
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test")
mockbeat.WriteConfigFile(fmt.Sprintf(CmdTestCfg, esURL.String()))
mockbeat.Start("test", "output", "-E", "output.elasticsearch.proxy_url="+proxyURL.String())
Expand All @@ -101,8 +101,23 @@ func TestCmdTestOutputProxy(t *testing.T) {
mockbeat.WaitStdOutContains("talk to server... OK", 10*time.Second)
}

func TestCmdTestOutputProxyTLS(t *testing.T) {
esURL := GetESURL(t, "http")
proxyURL := GetProxyURL(t, "https")
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test")
mockbeat.WriteConfigFile(fmt.Sprintf(CmdTestCfg, esURL.String()))
mockbeat.Start("test", "output", "-E", "output.elasticsearch.proxy_url="+proxyURL.String(), "-E", "output.elasticsearch.ssl.verification_mode=none")
procState, err := mockbeat.Process.Wait()
require.NoError(t, err)
require.Equal(t, 0, procState.ExitCode(), "incorrect exit code")
mockbeat.WaitStdOutContains("parse url... OK", 10*time.Second)
mockbeat.WaitStdOutContains("proxy... OK", 10*time.Second)
mockbeat.WaitStdOutContains("TLS... WARN secure connection disabled", 10*time.Second)
mockbeat.WaitStdOutContains("talk to server... OK", 10*time.Second)
}

func TestCmdTestOutputProxyBadHost(t *testing.T) {
proxyURL := GetProxyURL(t)
proxyURL := GetProxyURL(t, "http")
mockbeat := NewBeat(t, "mockbeat", "../../libbeat.test")
mockbeat.WriteConfigFile(fmt.Sprintf(CmdTestCfg, "badhost:9200"))
mockbeat.Start("test", "output", "-E", "output.elasticsearch.proxy_url="+proxyURL.String())
Expand Down
16 changes: 9 additions & 7 deletions libbeat/tests/integration/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,22 +574,24 @@ func GetKibana(t *testing.T) (url.URL, *url.Userinfo) {
return kibanaURL, kibanaUser
}

func GetProxyURL(t *testing.T) url.URL {
func GetProxyURL(t *testing.T, scheme string) url.URL {
t.Helper()

scheme := os.Getenv("PROXY_SCHEME")
if scheme == "" {
scheme = "http"
}

proxyHost := os.Getenv("PROXY_HOST")
if proxyHost == "" {
proxyHost = "localhost"
}

proxyPort := os.Getenv("PROXY_PORT")
if proxyPort == "" {
proxyPort = "3128"
switch scheme {
case "http":
proxyPort = "3128"
case "https":
proxyPort = "3129"
default:
t.Fatalf("could not determine port from env variable: PROXY_PORT=%s", proxyPort)
}
}

user := os.Getenv("PROXY_USER")
Expand Down
5 changes: 5 additions & 0 deletions testing/environments/docker/proxy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ FROM alpine:edge
RUN apk add --no-cache squid bash

COPY squid.conf /etc/squid/squid.conf
COPY pki /etc/pki

RUN chmod 600 /etc/squid/squid.conf; \
chmod 600 /etc/pki/tls/certs/*; \
chmod 600 /etc/pki/tls/private/*;

HEALTHCHECK --interval=1s --retries=600 CMD nc -z localhost 3128
EXPOSE 3128
Expand Down
19 changes: 19 additions & 0 deletions testing/environments/docker/proxy/pki/tls/certs/proxy.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDAzCCAeugAwIBAgIUGQ2GT9PuU7bTNxYTQeSqbYarkXAwDQYJKoZIhvcNAQEL
BQAwEDEOMAwGA1UEAwwFcHJveHkwIBcNMjMxMDI5MTUxNjMzWhgPMjEyMzEwMDUx
NTE2MzNaMBAxDjAMBgNVBAMMBXByb3h5MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
MIIBCgKCAQEAsbbY3X+eSd2qMF55M4IRG91IQuj8H1mqvG+QfndvU6mKVGhw4rEf
S3a1CgK2WbsDvJORteRYn6FZK8owfGx/pYlHWYfYB4+7rmBIn5Z5EmFhyj9SbRRk
N0nlHH/NsbOExhSg4scfhIUlZiYbjG8dPdprU4db4Qm+zls/Opl/Vc9xdMPdJqQ8
JTB1or7KLFK3KcbaoIGGSZ8KkboMBN3hYv6KcjkgH/nsXgaqQZHw/FyoHZDXlff3
JXJdtU936vC96qQONs1qPgmgquGWst616KH9t9Y1+S4DItqBm2pQ1q+pm832zkRi
i4PxSkmVvSfBOXlrIh/vqmyDIRa/Vd6aKQIDAQABo1MwUTAdBgNVHQ4EFgQUTe7E
Hwu56Ojzyj0rfCnU5gsT/fgwHwYDVR0jBBgwFoAUTe7EHwu56Ojzyj0rfCnU5gsT
/fgwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAZ8jCSIToxPVT
dVl28Eb4bByHkcwwJj2qNaBGsxDEd45I6OQOyMvGziYvw7lFNeu12aqCPBMNXwqS
1Ffl/XrU5FuAg0B+Z3BDMq6T0sPCldfCU5ERJjyJGXBP7O+C4b8Jf0V/RAO+ylM9
ulroC+RoU8xpf9e1LOJDOf75owm29OU2Vi31SCpJmx51okqc5fWJcc+o414/1zL1
NqNN8FHxfDcquP5Aj9xEEAmazt4Nh1htaYW691BoBNwDjyYQmZleUlpJf6M9Rcfe
cNqicJZkBBwcWuCYvfMMhDdR/qgQVH3cEtC5NVZcCK2gaFW1HDPEqODV1y4gXUnh
fncmlS8pww==
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions testing/environments/docker/proxy/pki/tls/private/proxy.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCxttjdf55J3aow
XnkzghEb3UhC6PwfWaq8b5B+d29TqYpUaHDisR9LdrUKArZZuwO8k5G15FifoVkr
yjB8bH+liUdZh9gHj7uuYEiflnkSYWHKP1JtFGQ3SeUcf82xs4TGFKDixx+EhSVm
JhuMbx092mtTh1vhCb7OWz86mX9Vz3F0w90mpDwlMHWivsosUrcpxtqggYZJnwqR
ugwE3eFi/opyOSAf+exeBqpBkfD8XKgdkNeV9/clcl21T3fq8L3qpA42zWo+CaCq
4Zay3rXoof231jX5LgMi2oGbalDWr6mbzfbORGKLg/FKSZW9J8E5eWsiH++qbIMh
Fr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0j4GOcyvpM1
VZHF4+W6l+BfNzRK/fo6uVJQWt7SaXIzKF1qoCNlWEfa6FcaptoAoCgK0zTtZmX6
89R+AKpMY/81XvzX2J3s/KmVa7BLvhILFjfL9TeLTaR1fA3MEnVU8h5PimwEjJL8
hWcwalivszosnKl+gs7SaA==
-----END PRIVATE KEY-----
1 change: 1 addition & 0 deletions testing/environments/docker/proxy/squid.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ http_access deny all

# General settings
http_port 3128
https_port 3129 cert=/etc/pki/tls/certs/proxy.crt key=/etc/pki/tls/private/proxy.key
dns_timeout 3 seconds

0 comments on commit 1c98d82

Please sign in to comment.