From c175fcde8d2ea0f94f660170df39705494810830 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Mon, 1 Jun 2020 17:32:48 -0400 Subject: [PATCH] Add a gorm log formatter to adapt to hclog (#93) This is also added in a test that otherwise output to stdout, which validates functionality. --- internal/cmd/base/servers.go | 19 +++++++++++++------ internal/db/db.go | 15 +++++++++++++++ internal/db/read_writer_test.go | 14 ++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/internal/cmd/base/servers.go b/internal/cmd/base/servers.go index e084a6fd8c..8f24868d73 100644 --- a/internal/cmd/base/servers.go +++ b/internal/cmd/base/servers.go @@ -30,10 +30,7 @@ import ( "github.com/hashicorp/watchtower/version" "github.com/jinzhu/gorm" "github.com/mitchellh/cli" - "golang.org/x/net/http/httpproxy" "google.golang.org/grpc/grpclog" - - _ "github.com/lib/pq" ) type Server struct { @@ -114,9 +111,16 @@ func (b *Server) SetupLogging(flagLogLevel, flagLogFormat, configLogLevel, confi b.LogLevel = logLevel // log proxy settings - proxyCfg := httpproxy.FromEnvironment() - b.Logger.Info("proxy environment", "http_proxy", proxyCfg.HTTPProxy, - "https_proxy", proxyCfg.HTTPSProxy, "no_proxy", proxyCfg.NoProxy) + // TODO: It would be good to show this but Vault has, or will soon, address + // the fact that this can log users/passwords if they are part of the proxy + // URL. When they change things to address that we should update the below + // logic and re-enable. + /* + proxyCfg := httpproxy.FromEnvironment() + b.Logger.Info("proxy environment", "http_proxy", proxyCfg.HTTPProxy, + "https_proxy", proxyCfg.HTTPSProxy, "no_proxy", proxyCfg.NoProxy) + */ + // Setup gorm logging return nil } @@ -397,6 +401,9 @@ func (b *Server) CreateDevDatabase(dialect string) error { } b.Database = dbase + gorm.LogFormatter = db.GetGormLogFormatter(b.Logger) + b.Database.LogMode(true) + rw := db.New(b.Database) repo, err := iam.NewRepository(rw, rw, b.ControllerKMS) if err != nil { diff --git a/internal/db/db.go b/internal/db/db.go index 7ac748f3be..4244ca4a5b 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -8,9 +8,11 @@ import ( "strings" "github.com/golang-migrate/migrate/v4" + "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-multierror" "github.com/hashicorp/watchtower/internal/db/migrations" "github.com/jinzhu/gorm" + "github.com/lib/pq" "github.com/ory/dockertest" ) @@ -167,3 +169,16 @@ func cleanupDockerResource(pool *dockertest.Pool, resource *dockertest.Resource) } return fmt.Errorf("Failed to cleanup local container: %s", err) } + +func GetGormLogFormatter(log hclog.Logger) func(values ...interface{}) (messages []interface{}) { + return func(values ...interface{}) (messages []interface{}) { + if len(values) > 2 && values[0].(string) == "log" { + switch values[2].(type) { + case *pq.Error: + log.Trace("error from database adapter", "location", values[1], "error", values[2]) + } + return nil + } + return nil + } +} diff --git a/internal/db/read_writer_test.go b/internal/db/read_writer_test.go index 090731cb80..ac864db63b 100644 --- a/internal/db/read_writer_test.go +++ b/internal/db/read_writer_test.go @@ -1,6 +1,7 @@ package db import ( + "bytes" "context" "errors" "fmt" @@ -9,6 +10,7 @@ import ( "time" "github.com/golang/protobuf/ptypes" + "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-uuid" "github.com/hashicorp/vault/sdk/helper/base62" "github.com/hashicorp/watchtower/internal/db/db_test" @@ -655,6 +657,18 @@ func TestDb_SearchWhere(t *testing.T) { }() assert := assert.New(t) defer db.Close() + + buf := new(bytes.Buffer) + log := hclog.New(&hclog.LoggerOptions{ + Output: buf, + Level: hclog.Trace, + }) + gorm.LogFormatter = GetGormLogFormatter(log) + db.LogMode(true) + defer func() { + assert.True(strings.Contains(buf.String(), "syntax error at or near")) + }() + t.Run("simple", func(t *testing.T) { w := Db{underlying: db} id, err := uuid.GenerateUUID()