diff --git a/apmproxy/receiver.go b/apmproxy/receiver.go index d28d99e8..6c082f41 100644 --- a/apmproxy/receiver.go +++ b/apmproxy/receiver.go @@ -107,6 +107,13 @@ func (c *Client) handleInfoRequest() (func(w http.ResponseWriter, r *http.Reques r.Header.Set("X-Forwarded-Host", r.Header.Get("Host")) r.Host = parsedApmServerURL.Host + // Override authorization header sent by the APM agents + if c.ServerAPIKey != "" { + r.Header.Add("Authorization", "ApiKey "+c.ServerAPIKey) + } else if c.ServerSecretToken != "" { + r.Header.Add("Authorization", "Bearer "+c.ServerSecretToken) + } + // Forward request to the APM server reverseProxy.ServeHTTP(w, r) }, nil diff --git a/apmproxy/receiver_test.go b/apmproxy/receiver_test.go index 9cdc5567..c5e08b6c 100644 --- a/apmproxy/receiver_test.go +++ b/apmproxy/receiver_test.go @@ -41,7 +41,7 @@ func TestInfoProxy(t *testing.T) { // Create apm server and handler apmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { for key := range headers { - assert.Equal(t, 1, len(r.Header[key])) + assert.Equal(t, 2, len(r.Header[key])) assert.Equal(t, headers[key], r.Header[key][0]) } w.Header().Add("test", "header") @@ -88,6 +88,38 @@ func TestInfoProxy(t *testing.T) { require.NoError(t, resp.Body.Close()) } +func TestInfoProxyAuth(t *testing.T) { + // Create apm server and handler + apmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "ApiKey bar", r.Header.Get("Authorization")) + w.WriteHeader(http.StatusTeapot) + })) + defer apmServer.Close() + + // Create extension config and start the server + apmClient, err := apmproxy.NewClient( + apmproxy.WithURL(apmServer.URL), + apmproxy.WithAPIKey("bar"), + // Use ipv4 to avoid issues in CI + apmproxy.WithReceiverAddress("127.0.0.1:1234"), + apmproxy.WithReceiverTimeout(15*time.Second), + apmproxy.WithLogger(zaptest.NewLogger(t).Sugar()), + ) + require.NoError(t, err) + + require.NoError(t, apmClient.StartReceiver()) + defer func() { + require.NoError(t, apmClient.Shutdown()) + }() + + url := "http://127.0.0.1:1234" + + // Send the request to the extension + resp, err := http.Get(url) + require.NoError(t, err) + require.NoError(t, resp.Body.Close()) +} + func TestInfoProxyErrorStatusCode(t *testing.T) { // Create apm server and handler apmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {