Skip to content

Commit 48d1053

Browse files
authored
Merge pull request stripe#252 from stripe/shubh/fix-http-role-logging
Add log enrichment with role in HTTP proxy mode
2 parents 1b618ed + 5992e05 commit 48d1053

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

pkg/smokescreen/smokescreen.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ func BuildProxy(config *Config) *goproxy.ProxyHttpServer {
503503

504504
sctx.Decision, sctx.lookupTime, pctx.Error = checkIfRequestShouldBeProxied(config, req, destination)
505505

506+
// add context fields to all future log messages sent using this smokescreen context's Logger
507+
sctx.Logger = sctx.Logger.WithFields(extractContextLogFields(pctx, sctx))
508+
506509
// Returning any kind of response in this handler is goproxy's way of short circuiting
507510
// the request. The original request will never be sent, and goproxy will invoke our
508511
// response filter attached via the OnResponse() handler.

pkg/smokescreen/smokescreen_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,3 +1637,79 @@ func proxyClientWithConnectHeaders(proxy string, proxyConnectHeaders http.Header
16371637
},
16381638
}, nil
16391639
}
1640+
1641+
func TestRoleLoggingInCanonicalProxyDecision(t *testing.T) {
1642+
r := require.New(t)
1643+
1644+
testRole := "test-local-srv"
1645+
1646+
t.Run("HTTP requests log role", func(t *testing.T) {
1647+
cfg, err := testConfig(testRole)
1648+
r.NoError(err)
1649+
err = cfg.SetAllowAddresses([]string{"127.0.0.1"})
1650+
r.NoError(err)
1651+
1652+
logHook := proxyLogHook(cfg)
1653+
proxySrv := proxyServer(cfg)
1654+
defer proxySrv.Close()
1655+
1656+
testSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1657+
w.WriteHeader(200)
1658+
w.Write([]byte("OK"))
1659+
}))
1660+
defer testSrv.Close()
1661+
1662+
client, err := proxyClient(proxySrv.URL)
1663+
r.NoError(err)
1664+
1665+
resp, err := client.Get(testSrv.URL)
1666+
r.NoError(err)
1667+
defer resp.Body.Close()
1668+
1669+
r.Equal(200, resp.StatusCode)
1670+
1671+
proxyDecision := findCanonicalProxyDecision(logHook.AllEntries())
1672+
r.NotNil(proxyDecision, "Should have CANONICAL-PROXY-DECISION log")
1673+
1674+
r.Contains(proxyDecision.Data, LogFieldRole, "CANONICAL-PROXY-DECISION should contain role field")
1675+
r.Equal(testRole, proxyDecision.Data[LogFieldRole], "Role should match expected value")
1676+
1677+
r.Contains(proxyDecision.Data, "proxy_type")
1678+
r.Equal("http", proxyDecision.Data["proxy_type"])
1679+
})
1680+
1681+
t.Run("CONNECT requests log role", func(t *testing.T) {
1682+
cfg, err := testConfig(testRole)
1683+
r.NoError(err)
1684+
err = cfg.SetAllowAddresses([]string{"127.0.0.1"})
1685+
r.NoError(err)
1686+
1687+
logHook := proxyLogHook(cfg)
1688+
proxySrv := proxyServer(cfg)
1689+
defer proxySrv.Close()
1690+
1691+
testSrv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1692+
w.WriteHeader(200)
1693+
w.Write([]byte("OK"))
1694+
}))
1695+
defer testSrv.Close()
1696+
1697+
client, err := proxyClient(proxySrv.URL)
1698+
r.NoError(err)
1699+
1700+
resp, err := client.Get(testSrv.URL)
1701+
r.NoError(err)
1702+
defer resp.Body.Close()
1703+
1704+
r.Equal(200, resp.StatusCode)
1705+
1706+
proxyDecision := findCanonicalProxyDecision(logHook.AllEntries())
1707+
r.NotNil(proxyDecision, "Should have CANONICAL-PROXY-DECISION log")
1708+
1709+
r.Contains(proxyDecision.Data, LogFieldRole, "CANONICAL-PROXY-DECISION should contain role field")
1710+
r.Equal(testRole, proxyDecision.Data[LogFieldRole], "Role should match expected value")
1711+
1712+
r.Contains(proxyDecision.Data, "proxy_type")
1713+
r.Equal("connect", proxyDecision.Data["proxy_type"])
1714+
})
1715+
}

0 commit comments

Comments
 (0)