From 25b9dc02746919c35d620853bc6fd1e4d9fd97d9 Mon Sep 17 00:00:00 2001 From: Sam Hazlehurst Date: Mon, 11 Nov 2024 14:59:27 -0500 Subject: [PATCH] Fix snapshot reporting to use HTTP_PROXY and HTTPS_PROXY env vars --- internal/report/agent_client.go | 20 +++++++++++++++++++- internal/report/agent_client_test.go | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/report/agent_client.go b/internal/report/agent_client.go index 6ecd0b211..25d6c8013 100644 --- a/internal/report/agent_client.go +++ b/internal/report/agent_client.go @@ -16,7 +16,9 @@ package report import ( "crypto/tls" + "net" "net/http" + "time" ) var _ Client = (*AgentClient)(nil) @@ -31,12 +33,28 @@ type AgentClient struct { // NewAgentClient creates a new AgentClient func NewAgentClient(agentID string, secretKey *string, tlsConfig *tls.Config) *AgentClient { + + // Values are copied from http.DefaultTransport. We don't use a copy of http.DefaultTransport with + // our own values because the http.DefaultTransport struct has private mutexes that we can't copy. + // http.DefaultClient is equivalent to &http.Client{} + + dialer := &net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + } return &AgentClient{ agentID: agentID, secretKey: secretKey, client: &http.Client{ Transport: &http.Transport{ - TLSClientConfig: tlsConfig, + Proxy: http.ProxyFromEnvironment, + DialContext: dialer.DialContext, + ForceAttemptHTTP2: true, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + TLSClientConfig: tlsConfig, }, }, } diff --git a/internal/report/agent_client_test.go b/internal/report/agent_client_test.go index 5b4e5c765..555c7af0d 100644 --- a/internal/report/agent_client_test.go +++ b/internal/report/agent_client_test.go @@ -34,6 +34,9 @@ func TestNewAgentClient(t *testing.T) { require.NotNil(t, client.client) } +// TestAgentClientDo tests the AgentClient Do method +// Note: this unit test can't test the proxy settings since the test server has a loopback address +// (127.*.*.* or localhost) and the proxy settings are ignored for these addresses. func TestAgentClientDo(t *testing.T) { secretKey := "secret_key" testCases := []struct {