diff --git a/go.mod b/go.mod index dad0d2e5..b5a301e9 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/jessevdk/go-flags v1.5.0 github.com/jinzhu/gorm v1.9.16 github.com/pockost/sshpipe-k8s-lib v0.0.3 + github.com/sirupsen/logrus v1.8.1 github.com/tg123/remotesigner v0.0.0-20210928104451-7c20285909d1 github.com/tg123/sshkey v0.0.0-20201202190454-3bb356f89f1f golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 diff --git a/go.sum b/go.sum index 2d79e575..944f76fb 100644 --- a/go.sum +++ b/go.sum @@ -269,6 +269,8 @@ github.com/pockost/sshpipe-k8s-lib v0.0.3/go.mod h1:gDUHQhvnDsuvrIVuVD6HCaFYEZ1i github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/pflag v0.0.0-20170130214245-9ff6c6923cff/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= @@ -277,6 +279,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +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= @@ -395,6 +398,7 @@ golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/sshpiperd/auditor/typescriptlogger/plugin.go b/sshpiperd/auditor/typescriptlogger/plugin.go index e593759e..4437091c 100644 --- a/sshpiperd/auditor/typescriptlogger/plugin.go +++ b/sshpiperd/auditor/typescriptlogger/plugin.go @@ -1,10 +1,11 @@ package typescriptlogger import ( - "log" "os" "path" + log "github.com/sirupsen/logrus" + "golang.org/x/crypto/ssh" "github.com/tg123/sshpiper/sshpiperd/auditor" diff --git a/sshpiperd/challenger/authy/handler.go b/sshpiperd/challenger/authy/handler.go index e7015afa..722b22fc 100644 --- a/sshpiperd/challenger/authy/handler.go +++ b/sshpiperd/challenger/authy/handler.go @@ -2,10 +2,11 @@ package authy import ( "fmt" - "log" "net/url" "time" + log "github.com/sirupsen/logrus" + "golang.org/x/crypto/ssh" "github.com/dcu/go-authy" diff --git a/sshpiperd/challenger/azdevicecode/handler.go b/sshpiperd/challenger/azdevicecode/handler.go index cb023e43..c7f4042f 100644 --- a/sshpiperd/challenger/azdevicecode/handler.go +++ b/sshpiperd/challenger/azdevicecode/handler.go @@ -2,9 +2,10 @@ package azdevicecode import ( "context" - "log" "net/http" + log "github.com/sirupsen/logrus" + "golang.org/x/crypto/ssh" "github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" diff --git a/sshpiperd/challenger/plugin.go b/sshpiperd/challenger/plugin.go index 48424137..f06ad2b5 100644 --- a/sshpiperd/challenger/plugin.go +++ b/sshpiperd/challenger/plugin.go @@ -1,7 +1,7 @@ package challenger import ( - "log" + log "github.com/sirupsen/logrus" ) type plugin struct { diff --git a/sshpiperd/challenger/pome/plugin.go b/sshpiperd/challenger/pome/plugin.go index f05f744b..89bae342 100644 --- a/sshpiperd/challenger/pome/plugin.go +++ b/sshpiperd/challenger/pome/plugin.go @@ -1,9 +1,9 @@ package pome import ( + log "github.com/sirupsen/logrus" "github.com/tg123/sshpiper/sshpiperd/challenger" "github.com/tg123/sshpiper/sshpiperd/upstream" - "log" ) type plugin struct { diff --git a/sshpiperd/challenger/pome/pome.go b/sshpiperd/challenger/pome/pome.go index c8c462dd..d047847e 100644 --- a/sshpiperd/challenger/pome/pome.go +++ b/sshpiperd/challenger/pome/pome.go @@ -1,7 +1,7 @@ package pome import ( - "log" + log "github.com/sirupsen/logrus" ) type pipe struct { diff --git a/sshpiperd/cmd.go b/sshpiperd/cmd.go index b8f8f444..fe1c4cc5 100644 --- a/sshpiperd/cmd.go +++ b/sshpiperd/cmd.go @@ -3,10 +3,11 @@ package main import ( "fmt" "io/ioutil" - "log" "os" "strings" + log "github.com/sirupsen/logrus" + "github.com/jessevdk/go-flags" "github.com/tg123/sshkey" @@ -177,7 +178,9 @@ func main() { } provider := upstream.Get(config.UpstreamDriver) - err := provider.Init(log.New(ioutil.Discard, "", 0)) + logger := log.New() + logger.SetOutput(ioutil.Discard) + err := provider.Init(logger) if err != nil { return nil, err } diff --git a/sshpiperd/e2e/docker-compose.yml b/sshpiperd/e2e/docker-compose.yml index 967f7ad7..71499aaa 100644 --- a/sshpiperd/e2e/docker-compose.yml +++ b/sshpiperd/e2e/docker-compose.yml @@ -37,6 +37,7 @@ services: - "SSHPIPERD_UPSTREAM_WORKINGDIR_NOCHECKPERM=true" - "SSHPIPERD_AUDITOR=typescript-logger" - "SSHPIPERD_BANNERTEXT=hellopiper" + - "SSHPIPERD_LOG_LEVEL=trace" build: ../.. volumes: - workingdir:/var/sshpiper @@ -55,6 +56,7 @@ services: - "SSHPIPERD_UPSTREAM_YAML_FILE=/sshpiperd.yaml" - "SSHPIPERD_AUDITOR=typescript-logger" - "SSHPIPERD_BANNERTEXT=hellopiper" + - "SSHPIPERD_LOG_LEVEL=trace" build: ../.. volumes: - localkey:/local @@ -87,6 +89,7 @@ services: - "SSHPIPERD_UPSTREAM_MYSQL_HOST=mysql" - "SSHPIPERD_AUDITOR=typescript-logger" - "SSHPIPERD_BANNERTEXT=hellopiper" + - "SSHPIPERD_LOG_LEVEL=trace" build: ../.. volumes: - ./piper.sh:/piper.sh @@ -115,6 +118,7 @@ services: - "SSHPIPERD_UPSTREAM_POSTGRES_SSLMODE=disable" - "SSHPIPERD_AUDITOR=typescript-logger" - "SSHPIPERD_BANNERTEXT=hellopiper" + - "SSHPIPERD_LOG_LEVEL=trace" build: ../.. volumes: - ./piper.sh:/piper.sh @@ -145,6 +149,7 @@ services: - "SSHPIPERD_UPSTREAM_MSSQL_DBNAME=master" - "SSHPIPERD_AUDITOR=typescript-logger" - "SSHPIPERD_BANNERTEXT=hellopiper" + - "SSHPIPERD_LOG_LEVEL=trace" build: ../.. volumes: - ./piper.sh:/piper.sh @@ -179,6 +184,7 @@ services: - "SSHPIPERD_UPSTREAM_GRPC_ENDPOINT=grpc_remotesigner_host1:2233" - "SSHPIPERD_AUDITOR=typescript-logger" - "SSHPIPERD_BANNERTEXT=hellopiper" + - "SSHPIPERD_LOG_LEVEL=trace" build: ../.. volumes: - workingdir:/var/sshpiper @@ -200,6 +206,7 @@ services: - "SSHPIPERD_UPSTREAM_GRPC_ENDPOINT=grpc_privatekey_host1:2233" - "SSHPIPERD_AUDITOR=typescript-logger" - "SSHPIPERD_BANNERTEXT=hellopiper" + - "SSHPIPERD_LOG_LEVEL=trace" build: ../.. volumes: - workingdir:/var/sshpiper @@ -219,6 +226,7 @@ services: - "SSHPIPERD_UPSTREAM_GRPC_ENDPOINT=grpc_remotepass_host2:2233" - "SSHPIPERD_AUDITOR=typescript-logger" - "SSHPIPERD_BANNERTEXT=hellopiper" + - "SSHPIPERD_LOG_LEVEL=trace" build: ../.. depends_on: - grpc_remotepass_host2 diff --git a/sshpiperd/log.go b/sshpiperd/log.go index 25939780..7fa377ba 100644 --- a/sshpiperd/log.go +++ b/sshpiperd/log.go @@ -1,19 +1,20 @@ package main import ( - "log" "os" + + log "github.com/sirupsen/logrus" ) type loggerConfig struct { LogFile string `long:"log" description:"LogFile path. Leave empty or any error occurs will fall back to stdout" env:"SSHPIPERD_LOG_PATH" ini-name:"log-path"` - LogFlags int `long:"log-flags" default:"3" description:"Flags for logger see https://godoc.org/log, default LstdFlags" env:"SSHPIPERD_LOG_FLAGS" ini-name:"log-flags"` + LogLevel string `long:"log-level" default:"info" description:"These are the different logging levels, see https://pkg.go.dev/github.com/sirupsen/logrus#Level" env:"SSHPIPERD_LOG_LEVEL" ini-name:"log-level"` } func (l loggerConfig) createLogger() (logger *log.Logger) { - logger = log.New(os.Stdout, "", l.LogFlags) + logger = log.New() if l.LogFile != "" { f, err := os.OpenFile(l.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) @@ -22,8 +23,16 @@ func (l loggerConfig) createLogger() (logger *log.Logger) { return } - logger = log.New(f, "", logger.Flags()) + logger.SetOutput(f) } + level, err := log.ParseLevel(l.LogLevel) + if err != nil { + log.Fatalf("parse log level %v error %v", l.LogLevel, err) + return + } + + logger.SetLevel(level) + return } diff --git a/sshpiperd/registry/plugin.go b/sshpiperd/registry/plugin.go index 0a07bf2c..6a9fb145 100644 --- a/sshpiperd/registry/plugin.go +++ b/sshpiperd/registry/plugin.go @@ -1,7 +1,7 @@ package registry import ( - "log" + log "github.com/sirupsen/logrus" ) // Plugin is to be registered with sshpiper to provide additional functions diff --git a/sshpiperd/registry/registry_test.go b/sshpiperd/registry/registry_test.go index 58eabdf0..ca940321 100644 --- a/sshpiperd/registry/registry_test.go +++ b/sshpiperd/registry/registry_test.go @@ -1,9 +1,10 @@ package registry import ( - "log" "reflect" "testing" + + log "github.com/sirupsen/logrus" ) type testplugin struct { diff --git a/sshpiperd/sshpiperd.go b/sshpiperd/sshpiperd.go index c5803efd..a8ba1c93 100644 --- a/sshpiperd/sshpiperd.go +++ b/sshpiperd/sshpiperd.go @@ -9,7 +9,7 @@ import ( "golang.org/x/crypto/ssh" - "log" + log "github.com/sirupsen/logrus" "github.com/tg123/sshpiper/sshpiperd/auditor" "github.com/tg123/sshpiper/sshpiperd/challenger" @@ -187,11 +187,11 @@ func startPiper(config *piperdConfig, logger *log.Logger) error { for { conn, err := listener.Accept() if err != nil { - logger.Printf("failed to accept connection: %v", err) + logger.Debugf("failed to accept connection: %v", err) continue } - logger.Printf("connection accepted: %v", conn.RemoteAddr()) + logger.Debugf("connection accepted: %v", conn.RemoteAddr()) go func(c net.Conn) { defer c.Close() @@ -215,10 +215,10 @@ func startPiper(config *piperdConfig, logger *log.Logger) error { select { case p = <-pipec: case err := <-errorc: - logger.Printf("connection from %v establishing failed reason: %v", c.RemoteAddr(), err) + logger.Debugf("connection from %v establishing failed reason: %v", c.RemoteAddr(), err) return case <-time.After(config.LoginGraceTime): - logger.Printf("pipe establishing timeout, disconnected connection from %v", c.RemoteAddr()) + logger.Debugf("pipe establishing timeout, disconnected connection from %v", c.RemoteAddr()) return } @@ -236,8 +236,9 @@ func startPiper(config *piperdConfig, logger *log.Logger) error { p.HookDownstreamMsg = a.GetDownstreamHook() } + logger.Infof("ssh connection accepted from %v", c.RemoteAddr()) err = p.Wait() - logger.Printf("connection from %v closed reason: %v", c.RemoteAddr(), err) + logger.Infof("connection from %v closed reason: %v", c.RemoteAddr(), err) }(conn) } } diff --git a/sshpiperd/sshpiperd_test.go b/sshpiperd/sshpiperd_test.go index 4efead70..1ea398b7 100644 --- a/sshpiperd/sshpiperd_test.go +++ b/sshpiperd/sshpiperd_test.go @@ -1,17 +1,19 @@ package main import ( - "log" "testing" + log "github.com/sirupsen/logrus" + "fmt" + "net" + "time" + "github.com/tg123/sshpiper/sshpiperd/auditor" "github.com/tg123/sshpiper/sshpiperd/challenger" "github.com/tg123/sshpiper/sshpiperd/registry" "github.com/tg123/sshpiper/sshpiperd/upstream" "golang.org/x/crypto/ssh" - "net" - "time" ) type testplugin struct { diff --git a/sshpiperd/upstream/database/handler_test.go b/sshpiperd/upstream/database/handler_test.go index 1f997926..7a6e828b 100644 --- a/sshpiperd/upstream/database/handler_test.go +++ b/sshpiperd/upstream/database/handler_test.go @@ -1,11 +1,12 @@ package database import ( - "log" "net" "os" "testing" + log "github.com/sirupsen/logrus" + "github.com/tg123/sshkey" "golang.org/x/crypto/ssh" diff --git a/sshpiperd/upstream/database/plugin.go b/sshpiperd/upstream/database/plugin.go index 66c5f6ce..9f5a4048 100644 --- a/sshpiperd/upstream/database/plugin.go +++ b/sshpiperd/upstream/database/plugin.go @@ -1,7 +1,7 @@ package database import ( - "log" + log "github.com/sirupsen/logrus" "github.com/jinzhu/gorm" diff --git a/sshpiperd/upstream/grpcupstream/plugin.go b/sshpiperd/upstream/grpcupstream/plugin.go index 664b08fa..5912a43d 100644 --- a/sshpiperd/upstream/grpcupstream/plugin.go +++ b/sshpiperd/upstream/grpcupstream/plugin.go @@ -5,7 +5,8 @@ import ( "crypto/x509" "fmt" "io/ioutil" - "log" + + log "github.com/sirupsen/logrus" "github.com/tg123/remotesigner/grpcsigner" "github.com/tg123/sshpiper/sshpiperd/upstream" diff --git a/sshpiperd/upstream/grpcupstream/simpleserver/impl.go b/sshpiperd/upstream/grpcupstream/simpleserver/impl.go index 058ed7d1..9dc0642b 100644 --- a/sshpiperd/upstream/grpcupstream/simpleserver/impl.go +++ b/sshpiperd/upstream/grpcupstream/simpleserver/impl.go @@ -4,7 +4,8 @@ import ( "context" "fmt" "io/ioutil" - "log" + + log "github.com/sirupsen/logrus" "github.com/tg123/sshpiper/sshpiperd/upstream/grpcupstream" ) diff --git a/sshpiperd/upstream/grpcupstream/simpleserver/main.go b/sshpiperd/upstream/grpcupstream/simpleserver/main.go index f584f754..e1fd0730 100644 --- a/sshpiperd/upstream/grpcupstream/simpleserver/main.go +++ b/sshpiperd/upstream/grpcupstream/simpleserver/main.go @@ -4,11 +4,12 @@ import ( "crypto" "fmt" "io/ioutil" - "log" "net" "reflect" "unsafe" + log "github.com/sirupsen/logrus" + "github.com/jessevdk/go-flags" "github.com/tg123/remotesigner/grpcsigner" "github.com/tg123/sshpiper/sshpiperd/upstream/grpcupstream" diff --git a/sshpiperd/upstream/kubernetes/plugin.go b/sshpiperd/upstream/kubernetes/plugin.go index 627b1e88..bca52380 100644 --- a/sshpiperd/upstream/kubernetes/plugin.go +++ b/sshpiperd/upstream/kubernetes/plugin.go @@ -1,7 +1,7 @@ package kubernetes import ( - "log" + log "github.com/sirupsen/logrus" "github.com/tg123/sshpiper/sshpiperd/upstream" ) diff --git a/sshpiperd/upstream/workingdir/plugin.go b/sshpiperd/upstream/workingdir/plugin.go index 48a35489..8289c278 100644 --- a/sshpiperd/upstream/workingdir/plugin.go +++ b/sshpiperd/upstream/workingdir/plugin.go @@ -1,8 +1,8 @@ package workingdir import ( + log "github.com/sirupsen/logrus" "github.com/tg123/sshpiper/sshpiperd/upstream" - "log" ) var logger *log.Logger diff --git a/sshpiperd/upstream/workingdir/workingdir_test.go b/sshpiperd/upstream/workingdir/workingdir_test.go index b5831de1..30226e29 100644 --- a/sshpiperd/upstream/workingdir/workingdir_test.go +++ b/sshpiperd/upstream/workingdir/workingdir_test.go @@ -9,7 +9,8 @@ import ( "bytes" "io" "io/ioutil" - "log" + + log "github.com/sirupsen/logrus" "net" "os" diff --git a/sshpiperd/upstream/yaml/plugin.go b/sshpiperd/upstream/yaml/plugin.go index ebf52836..385f17ba 100644 --- a/sshpiperd/upstream/yaml/plugin.go +++ b/sshpiperd/upstream/yaml/plugin.go @@ -1,7 +1,7 @@ package yaml import ( - "log" + log "github.com/sirupsen/logrus" "github.com/tg123/sshpiper/sshpiperd/upstream" )