Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Move context and log libraries to trufflehog (#718)
Miccah619ab2027a41
diff --git a/go.mod b/go.modindex 68114b1ed..393ffa577 100644--- a/go.mod+++ b/go.mod@@ -11,6 +11,7 @@ replace github.com/gitleaks/go-gitdiff => github.com/trufflesecurity/go-gitdiffrequire (cloud.google.com/go/secretmanager v1.5.0github.com/Azure/go-autorest/autorest/azure/auth v0.5.11+ github.com/TheZeroSlave/zapsentry v1.11.0github.com/aws/aws-sdk-go v1.44.61github.com/bill-rich/disk-buffer-reader v0.1.2github.com/bill-rich/go-syslog v0.0.0-20220413021637-49edb52a574c@@ -20,9 +21,12 @@ require (github.com/envoyproxy/protoc-gen-validate v0.6.7github.com/fatih/color v1.13.0github.com/felixge/fgprof v0.9.2+ github.com/getsentry/sentry-go v0.13.0github.com/gitleaks/go-gitdiff v0.7.4github.com/go-errors/errors v1.4.2github.com/go-git/go-git/v5 v5.4.2+ github.com/go-logr/logr v1.2.3+ github.com/go-logr/zapr v1.2.3github.com/golang-jwt/jwt v3.2.2+incompatiblegithub.com/google/go-github/v42 v42.0.0github.com/gorilla/mux v1.8.0@@ -43,6 +47,7 @@ require (github.com/tailscale/depaware v0.0.0-20210622194025-720c4b409502github.com/xanzy/go-gitlab v0.69.0github.com/zricethezav/gitleaks/v8 v8.5.2+ go.uber.org/zap v1.22.0golang.org/x/crypto v0.0.0-20211215165025-cf75a172585egolang.org/x/net v0.0.0-20220617184016-355a448f1bc9golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb@@ -70,6 +75,7 @@ require (github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirectgithub.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirectgithub.com/andybalholm/brotli v1.0.4 // indirect+ github.com/benbjohnson/clock v1.1.0 // indirectgithub.com/davecgh/go-spew v1.1.1 // indirectgithub.com/dimchansky/utfbom v1.1.1 // indirectgithub.com/dsnet/compress v0.0.1 // indirect@@ -107,6 +113,8 @@ require (github.com/xanzy/ssh-agent v0.3.0 // indirectgithub.com/yusufpapurcu/wmi v1.2.2 // indirectgo.opencensus.io v0.23.0 // indirect+ go.uber.org/atomic v1.7.0 // indirect+ go.uber.org/multierr v1.6.0 // indirectgolang.org/x/mod v0.5.0 // indirectgolang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirectgolang.org/x/text v0.3.7 // indirectdiff --git a/go.sum b/go.sumindex 95a0cfece..ddb859621 100644--- a/go.sum+++ b/go.sum@@ -85,6 +85,8 @@ github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXgithub.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ=github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=+github.com/TheZeroSlave/zapsentry v1.11.0 h1:QtmkinFhsfKehvxE/tm/VkrBe/K8iQZXjp+ieOevsQI=+github.com/TheZeroSlave/zapsentry v1.11.0/go.mod h1:00uO/VpPrSJG/XigAfTi0F4WMFIw2DmP/IDVUhPBvNw=github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=@@ -100,6 +102,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdgithub.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=github.com/aws/aws-sdk-go v1.44.61 h1:NcpLSS3Z0MiVQIYugx4I40vSIEEAXT0baO684ExNRco=github.com/aws/aws-sdk-go v1.44.61/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=+github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=+github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=github.com/bill-rich/disk-buffer-reader v0.1.2 h1:pN9K5JoacTcNWp2SCd3n7mPouSwMP9ouTS66Qa+5IPY=github.com/bill-rich/disk-buffer-reader v0.1.2/go.mod h1:VVzzsK1Ac2AnpOfp/5r9JlIFaFkZ9uSf7zisZayCt0Y=github.com/bill-rich/go-syslog v0.0.0-20220413021637-49edb52a574c h1:tSME5FDS02qQll3JYodI6RZR/g4EKOHApGv1wMZT+Z0=@@ -155,6 +159,8 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFgithub.com/felixge/fgprof v0.9.2 h1:tAMHtWMyl6E0BimjVbFt7fieU6FpjttsZN7j0wT5blc=github.com/felixge/fgprof v0.9.2/go.mod h1:+VNi+ZXtHIQ6wIw6bUT8nXQRefQflWECoFyRealT5sg=github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=+github.com/getsentry/sentry-go v0.13.0 h1:20dgTiUSfxRB/EhMPtxcL9ZEbM1ZdR+W/7f7NWD+xWo=+github.com/getsentry/sentry-go v0.13.0/go.mod h1:EOsfu5ZdvKPfeHYV6pTVQnsjfp30+XA7//UooKNumH0=github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=@@ -172,6 +178,11 @@ github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+ztigithub.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=+github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=+github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=+github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=+github.com/go-logr/zapr v1.2.3 h1:a9vnzlIBPQBBkeaR9IuMUfmVOrQlkoC4YfPoFkX3T7A=+github.com/go-logr/zapr v1.2.3/go.mod h1:eIauM6P8qSvTw5o2ez6UEAfGjQKrxQTl5EoK+Qa2oG4=github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=github.com/gobwas/httphead v0.0.0-20200921212729-da3d93bc3c58/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=@@ -357,6 +368,7 @@ github.com/paulbellamy/ratecounter v0.2.0 h1:2L/RhJq+HA8gBQImDXtLPrDXK5qAj6ozWVKgithub.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChlfo5C6hzIHwPqfFE=github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE=github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=+github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7 h1:+/+DxvQaYifJ+grD4klzrS5y+KJXldn/2YTl5JG+vZ8=github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7/go.mod h1:zO8QMzTeZd5cpnIkz/Gn6iK0jDfGicM1nynOkkPIl28=github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=@@ -391,6 +403,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=@@ -431,6 +444,15 @@ go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M=go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=+go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=+go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=+go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=+go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=+go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=+go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=+go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=+go.uber.org/zap v1.22.0 h1:Zcye5DUgBloQ9BaT4qc9BnjOFog5TvBSAGkJ3Nf70c0=+go.uber.org/zap v1.22.0/go.mod h1:H4siCOZOrAolnUPJEkfaSjDqyP+BDS0DdDWzwcgt3+U=golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=@@ -665,6 +687,7 @@ golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwgolang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=+golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=golang.org/x/tools v0.0.0-20191113191852-77e3bb0ad9e7/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=@@ -910,6 +933,7 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=+gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=diff --git a/pkg/context/context.go b/pkg/context/context.gonew file mode 100644index 000000000..34725de7b--- /dev/null+++ b/pkg/context/context.go@@ -0,0 +1,125 @@+package context++import (+ "context"+ "time"++ "github.com/go-logr/logr"+)++var (+ // defaultLogger can be set via SetDefaultLogger.+ defaultLogger logr.Logger = logr.Discard()+)++// Context wraps context.Context and includes an additional Logger() method.+type Context interface {+ context.Context+ Logger() logr.Logger+}++type CancelFunc context.CancelFunc++// logCtx implements Context.+type logCtx struct {+ // Embed context.Context to get all methods for free.+ context.Context+ log logr.Logger+}++// Logger returns a structured logger.+func (l logCtx) Logger() logr.Logger {+ return l.log+}++// Background returns context.Background with a default logger.+func Background() Context {+ return logCtx{+ log: defaultLogger,+ Context: context.Background(),+ }+}++// TODO returns context.TODO with a default logger.+func TODO() Context {+ return logCtx{+ log: defaultLogger,+ Context: context.TODO(),+ }+}++// WithCancel returns context.WithCancel with the log object propagated.+func WithCancel(parent Context) (Context, context.CancelFunc) {+ ctx, cancel := context.WithCancel(parent)+ return logCtx{+ log: parent.Logger(),+ Context: ctx,+ }, cancel+}++// WithDeadline returns context.WithDeadline with the log object propagated and+// the deadline added to the structured log values.+func WithDeadline(parent Context, d time.Time) (Context, context.CancelFunc) {+ ctx, cancel := context.WithDeadline(parent, d)+ return logCtx{+ log: parent.Logger().WithValues("deadline", d),+ Context: ctx,+ }, cancel+}++// WithTimeout returns context.WithTimeout with the log object propagated and+// the timeout added to the structured log values.+func WithTimeout(parent Context, timeout time.Duration) (Context, context.CancelFunc) {+ ctx, cancel := context.WithTimeout(parent, timeout)+ return logCtx{+ log: parent.Logger().WithValues("timeout", timeout),+ Context: ctx,+ }, cancel+}++// WithValue returns context.WithValue with the log object propagated and+// the value added to the structured log values (if the key is a string).+func WithValue(parent Context, key, val any) Context {+ logger := parent.Logger()+ if k, ok := key.(string); ok {+ logger = logger.WithValues(k, val)+ }+ return logCtx{+ log: logger,+ Context: context.WithValue(parent, key, val),+ }+}++// WithValues returns context.WithValue with the log object propagated and+// the values added to the structured log values (if the key is a string).+func WithValues(parent Context, keyAndVals ...any) Context {+ ctx := parent+ for i := 0; i < len(keyAndVals)-1; i += 2 {+ ctx = WithValue(ctx, keyAndVals[i], keyAndVals[i+1])+ }+ return ctx+}++// WithLogger converts a context.Context into a Context by adding a logger.+func WithLogger(parent context.Context, logger logr.Logger) Context {+ return logCtx{+ log: logger,+ Context: parent,+ }+}++// AddLogger converts a context.Context into a Context. If the underlying type+// is already a Context, that will be returned, otherwise a default logger will+// be added.+func AddLogger(parent context.Context) Context {+ if loggerCtx, ok := parent.(Context); ok {+ return loggerCtx+ }+ return WithLogger(parent, defaultLogger)+}++// SetupDefaultLogger sets the package-level global default logger that will be+// used for Background and TODO contexts.+func SetDefaultLogger(l logr.Logger) {+ defaultLogger = l+}diff --git a/pkg/context/context_test.go b/pkg/context/context_test.gonew file mode 100644index 000000000..de2e0ca36--- /dev/null+++ b/pkg/context/context_test.go@@ -0,0 +1,171 @@+package context++import (+ "bytes"+ "context"+ "strings"+ "testing"+ "time"++ "github.com/go-logr/logr"+ "github.com/go-logr/zapr"+ "github.com/stretchr/testify/assert"+ "github.com/trufflesecurity/trufflehog/v3/pkg/log"+ "go.uber.org/zap"+ "go.uber.org/zap/zapcore"+ "go.uber.org/zap/zaptest"+)++// testLogger is a helper function to create a logger with a closure callback.+func testLogger(t *testing.T, f func(zapcore.Entry)) logr.Logger {+ return zapr.NewLogger(zaptest.NewLogger(t,+ zaptest.WrapOptions(zap.Hooks(func(e zapcore.Entry) error {+ f(e)+ return nil+ }))))+}++// infoCounterContext is a helper function to create a Context that will count+// the number of Info messages logged.+func infoCounterContext(t *testing.T) (Context, *int) {+ var infoCount int+ logger := testLogger(t, func(e zapcore.Entry) {+ if e.Level == zap.InfoLevel {+ infoCount+++ }+ })+ return WithLogger(context.Background(), logger), &infoCount+}++func TestWithCancel(t *testing.T) {+ parentCtx, infoCount := infoCounterContext(t)+ ctx, cancel := WithCancel(parentCtx)+ cancel()+ assert.Equal(t, 0, *infoCount)+ select {+ case <-ctx.Done():+ ctx.Logger().Info("yay")+ case <-time.After(1 * time.Second):+ assert.Fail(t, "context should be done")+ }+ assert.Equal(t, 1, *infoCount)+}++func TestWithTimeout(t *testing.T) {+ parentCtx, infoCount := infoCounterContext(t)+ ctx, cancel := WithTimeout(parentCtx, 10*time.Millisecond)+ defer cancel()++ assert.Equal(t, 0, *infoCount)+ select {+ case <-ctx.Done():+ ctx.Logger().Info("yay")+ case <-time.After(1 * time.Second):+ assert.Fail(t, "context should be done")+ }+ assert.Equal(t, 1, *infoCount)++ ctx, cancel = WithTimeout(parentCtx, 1*time.Second)+ defer cancel()+ select {+ case <-ctx.Done():+ assert.Fail(t, "context should not be done")+ case <-time.After(10 * time.Millisecond):+ ctx.Logger().Info("yay")+ }+ assert.Equal(t, 2, *infoCount)+}++func TestWithLogger(t *testing.T) {+ var infoCount int+ logger := testLogger(t, func(e zapcore.Entry) {+ if e.Level == zap.InfoLevel {+ infoCount+++ }+ })++ ctx := WithLogger(context.Background(), logger)+ assert.Equal(t, logger, ctx.Logger())++ assert.Equal(t, 0, infoCount)+ ctx.Logger().Info("yay")+ assert.Equal(t, 1, infoCount)+}++func TestAsContext(t *testing.T) {+ var gotValue any+ normalFuncThatTakesContext := func(ctx context.Context) {+ if logCtx, ok := ctx.(Context); ok {+ logCtx.Logger().Info("yay")+ }+ gotValue = ctx.Value("key")+ }+ parentCtx, infoCount := infoCounterContext(t)+ ctx := WithValue(parentCtx, "key", "value")++ assert.Equal(t, 0, *infoCount)+ normalFuncThatTakesContext(ctx)+ assert.Equal(t, 1, *infoCount)+ assert.Equal(t, "value", gotValue)+}++func TestWithValues(t *testing.T) {+ var buffer bytes.Buffer+ logger, sync := log.New("test",+ log.WithConsoleSink(&buffer),+ )+ defer func(prevLogger logr.Logger) {+ defaultLogger = prevLogger+ }(defaultLogger)+ SetDefaultLogger(logger)++ {+ ctx1 := Background()+ ctx1.Logger().Info("only a", "a", 0)++ ctx2 := WithValue(ctx1, "b", 1)+ ctx2.Logger().Info("only b")+ assert.Equal(t, 1, ctx2.Value("b"))++ ctx3 := WithLogger(ctx2, ctx2.Logger().WithValues("c", 2, "d", 3))+ ctx3.Logger().Info("bcd")++ ctx2.Logger().Info("only b again")++ type customKey string+ ctx4 := WithValue(Background(), customKey("foo"), "bar")+ // foo:bar shouldn't be added to the logger because the key isn't a string+ ctx4.Logger().Info("foo")++ ctx5 := WithValues(ctx2, "e", 4, "f", 5, 6, "six")+ ctx5.Logger().Info("bef")+ assert.Equal(t, "six", ctx5.Value(6))++ ctx6 := WithValues(ctx2, "what does this do?")+ ctx6.Logger().Info("silently fail I suppose")+ }+ assert.Nil(t, sync())+ logs := strings.Split(strings.TrimSpace(buffer.String()), "\n")++ assert.Equal(t, 7, len(logs))+ assert.Contains(t, logs[0], `{"a": 0}`)+ assert.Contains(t, logs[1], `{"b": 1}`)+ assert.Contains(t, logs[2], `{"b": 1, "c": 2, "d": 3}`)+ assert.Contains(t, logs[3], `{"b": 1}`)+ assert.NotContains(t, logs[4], `{"foo": "bar"}`)+ assert.Contains(t, logs[5], `{"b": 1, "e": 4, "f": 5}`)+ assert.Contains(t, logs[6], `silently fail`)+ assert.NotContains(t, logs[6], `what does this do?`)+}++func TestDiscardLogger(t *testing.T) {+ var panicked bool+ defer func() {+ if r := recover(); r != nil {+ panicked = true+ }+ assert.False(t, panicked)+ }()+ ctx := Background()+ ctx.Logger().Info("this shouldn't panic")+}diff --git a/pkg/log/log.go b/pkg/log/log.gonew file mode 100644index 000000000..93fd4e4b3--- /dev/null+++ b/pkg/log/log.go@@ -0,0 +1,183 @@+package log++import (+ "errors"+ "io"+ "time"++ "github.com/TheZeroSlave/zapsentry"+ "github.com/getsentry/sentry-go"+ "github.com/go-logr/logr"+ "github.com/go-logr/zapr"+ "go.uber.org/zap"+ "go.uber.org/zap/zapcore"+)++var (+ // global log level control (see SetLevel)+ logLevel zap.AtomicLevel = zap.NewAtomicLevel()+)++type logConfig struct {+ core zapcore.Core+ cleanup func() error+ err error+}++// New creates a new log object with the provided configurations. If no sinks+// are provided, a no-op sink will be used. Returns the logger and a cleanup+// function that should be executed before the program exits.+func New(service string, configs ...logConfig) (logr.Logger, func() error) {+ var cores []zapcore.Core+ var cleanupFuncs []func() error++ // create cores for the logger+ for _, config := range configs {+ if config.err != nil {+ continue+ }+ cores = append(cores, config.core)+ if config.cleanup != nil {+ cleanupFuncs = append(cleanupFuncs, config.cleanup)+ }+ }+ // create logger+ zapLogger := zap.New(zapcore.NewTee(cores...))+ cleanupFuncs = append(cleanupFuncs, zapLogger.Sync)+ logger := zapr.NewLogger(zapLogger).WithName(service)++ // report the errors we encountered in the configs+ for _, config := range configs {+ if config.err != nil {+ logger.Error(config.err, "error configuring logger")+ }+ }++ return logger, firstErrorFunc(cleanupFuncs...)+}++// SetLevel sets the log level for all loggers created with WithJSONSink and+// WithConsoleSink.+func SetLevel(level int8) {+ // Zap's levels get more verbose as the number gets smaller, as explained+ // by zapr here: https://github.com/go-logr/zapr#increasing-verbosity+ // For example setting the level to -2 below, means log.V(2) will be enabled.+ logLevel.SetLevel(zapcore.Level(-level))+}++// WithSentry adds sentry integration to the logger. This configuration may+// fail, in which case, sentry will not be added and execution will continue+// normally.+func WithSentry(opts sentry.ClientOptions, tags map[string]string) logConfig {+ client, err := sentry.NewClient(opts)+ if err != nil {+ return logConfig{err: err}+ }++ // create sentry core+ cfg := zapsentry.Configuration{+ Tags: tags,+ Level: zapcore.ErrorLevel,+ EnableBreadcrumbs: true,+ BreadcrumbLevel: zapcore.InfoLevel,+ }+ core, err := zapsentry.NewCore(cfg, zapsentry.NewSentryClientFromClient(client))+ if err != nil {+ return logConfig{err: err}+ }++ return logConfig{+ core: core,+ cleanup: func() error {+ sentry.Flush(5 * time.Second)+ return nil+ },+ }+}++// WithJSONSink adds a JSON encoded output to the logger.+func WithJSONSink(sink io.Writer) logConfig {+ return logConfig{+ core: zapcore.NewCore(+ zapcore.NewJSONEncoder(defaultEncoderConfig()),+ zapcore.Lock(+ zapcore.AddSync(sink),+ ),+ logLevel,+ ),+ }+}++// WithConsoleSink adds a console-style output to the logger.+func WithConsoleSink(sink io.Writer) logConfig {+ return logConfig{+ core: zapcore.NewCore(+ zapcore.NewConsoleEncoder(defaultEncoderConfig()),+ zapcore.Lock(+ zapcore.AddSync(sink),+ ),+ logLevel,+ ),+ }+}++func defaultEncoderConfig() zapcore.EncoderConfig {+ conf := zap.NewProductionEncoderConfig()+ // Use more human-readable time format.+ conf.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339)+ return conf+}++// WithCore adds any user supplied zap core to the logger.+func WithCore(core zapcore.Core) logConfig {+ return logConfig{core: core}+}++// AddSentry initializes a sentry client and extends an existing+// logr.Logger with the hook.+func AddSentry(l logr.Logger, opts sentry.ClientOptions, tags map[string]string) (logr.Logger, func() error, error) {+ conf := WithSentry(opts, tags)+ if conf.err != nil {+ return l, nil, conf.err+ }+ return AddSink(l, conf)+}++// AddSink extends an existing logr.Logger with a new sink. It returns the new+// logr.Logger, a cleanup function, and an error.+func AddSink(l logr.Logger, sink logConfig) (logr.Logger, func() error, error) {+ zapLogger, err := getZapLogger(l)+ if err != nil {+ return l, nil, errors.New("unsupported logr implementation")+ }+ zapLogger = zapLogger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {+ return zapcore.NewTee(core, sink.core)+ }))+ return zapr.NewLogger(zapLogger), firstErrorFunc(zapLogger.Sync, sink.cleanup), nil+}++// getZapLogger is a helper function that gets the underlying zap logger from a+// logr.Logger interface.+func getZapLogger(l logr.Logger) (*zap.Logger, error) {+ if u, ok := l.GetSink().(zapr.Underlier); ok {+ return u.GetUnderlying(), nil+ }+ return nil, errors.New("not a zapr logger")+}++// firstErrorFunc is a helper function that returns a function that executes+// all provided args and returns the first error, if any.+func firstErrorFunc(fs ...func() error) func() error {+ return func() error {+ var firstErr error = nil+ for _, f := range fs {+ if f == nil {+ continue+ }+ if err := f(); err != nil && firstErr == nil {+ firstErr = err+ }+ }+ return firstErr+ }+}diff --git a/pkg/log/log_test.go b/pkg/log/log_test.gonew file mode 100644index 000000000..f5a356314--- /dev/null+++ b/pkg/log/log_test.go@@ -0,0 +1,152 @@+package log++import (+ "bytes"+ "strings"+ "testing"+ "time"++ "github.com/getsentry/sentry-go"+ "github.com/stretchr/testify/assert"+)++func TestNew(t *testing.T) {+ var jsonBuffer, consoleBuffer bytes.Buffer+ logger, sync := New("service-name",+ WithJSONSink(&jsonBuffer),+ WithConsoleSink(&consoleBuffer),+ )+ logger.Info("yay")+ assert.Nil(t, sync())++ assert.Contains(t, jsonBuffer.String(), `"logger":"service-name"`)+ assert.Contains(t, jsonBuffer.String(), `"msg":"yay"`)+ assert.Contains(t, consoleBuffer.String(), "info\tservice-name\tyay")+}++func TestSetLevel(t *testing.T) {+ var buffer bytes.Buffer+ logger, _ := New("service-name",+ WithConsoleSink(&buffer),+ )++ assert.Equal(t, true, logger.GetSink().Enabled(0))+ assert.Equal(t, false, logger.GetSink().Enabled(1))+ assert.Equal(t, false, logger.GetSink().Enabled(2))++ SetLevel(1)+ assert.Equal(t, true, logger.GetSink().Enabled(0))+ assert.Equal(t, true, logger.GetSink().Enabled(1))+ assert.Equal(t, false, logger.GetSink().Enabled(2))++ SetLevel(2)+ assert.Equal(t, true, logger.GetSink().Enabled(0))+ assert.Equal(t, true, logger.GetSink().Enabled(1))+ assert.Equal(t, true, logger.GetSink().Enabled(2))+}++func TestWithSentryFailure(t *testing.T) {+ var buffer bytes.Buffer+ logger, sync := New("service-name",+ WithSentry(sentry.ClientOptions{Dsn: "fail"}, nil),+ WithConsoleSink(&buffer),+ )+ logger.Info("yay")+ assert.Nil(t, sync())++ assert.Contains(t, buffer.String(), "error configuring logger")+ assert.Contains(t, buffer.String(), "yay")+}++func TestAddSentryFailure(t *testing.T) {+ var buffer bytes.Buffer+ logger, sync := New("service-name",+ WithConsoleSink(&buffer),+ )+ logger, _, err := AddSentry(logger, sentry.ClientOptions{Dsn: "fail"}, nil)+ assert.NotNil(t, err)+ assert.NotContains(t, err.Error(), "unsupported")++ logger.Info("yay")+ assert.Nil(t, sync())++ assert.Contains(t, buffer.String(), "yay")+}++func TestAddSentry(t *testing.T) {+ var buffer bytes.Buffer+ var sentryMessage string+ logger, _ := New("service-name",+ WithConsoleSink(&buffer),+ )+ logger, sync, err := AddSentry(logger, sentry.ClientOptions{+ BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {+ sentryMessage = event.Message+ return nil+ },+ }, nil)+ assert.Nil(t, err)++ logger.Info("yay")+ logger.Error(nil, "oops")+ assert.Nil(t, sync())++ assert.Contains(t, buffer.String(), "yay")+ assert.Contains(t, buffer.String(), "oops")+ assert.Equal(t, "oops", sentryMessage)+}++func TestWithSentry(t *testing.T) {+ var buffer bytes.Buffer+ var sentryMessage string+ logger, sync := New("service-name",+ WithConsoleSink(&buffer),+ WithSentry(sentry.ClientOptions{+ BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {+ sentryMessage = event.Message+ return nil+ },+ }, nil),+ )+ logger.Info("yay")+ logger.Error(nil, "oops")+ assert.Nil(t, sync())++ assert.Contains(t, buffer.String(), "yay")+ assert.Contains(t, buffer.String(), "oops")+ assert.Equal(t, "oops", sentryMessage)+}++func TestHumanReadableTimestamp(t *testing.T) {+ var buffer bytes.Buffer+ logger, sync := New("service-name",+ WithConsoleSink(&buffer),+ )+ logger.Info("yay")+ assert.Nil(t, sync())++ ts := strings.Split(buffer.String(), "\t")[0]+ assert.NotContains(t, ts, "e+09")++ parsedTime, err := time.Parse(time.RFC3339, ts)+ assert.Nil(t, err)+ assert.Less(t, time.Since(parsedTime), 5*time.Second)+}++func TestAddSink(t *testing.T) {+ var buf1, buf2 bytes.Buffer+ logger, _ := New("service-name",+ WithConsoleSink(&buf1),+ )+ logger.Info("line 1")+ logger, sync, err := AddSink(logger, WithConsoleSink(&buf2))+ assert.Nil(t, err)+ logger.Info("line 2")+ assert.Nil(t, sync())++ assert.Contains(t, buf1.String(), "line 1")+ assert.Contains(t, buf1.String(), "line 2")+ // buf2 should only have "line 2"+ assert.NotContains(t, buf2.String(), "line 1")+ assert.Contains(t, buf2.String(), "line 2")+}