From ef3e77ac847ded4ce0650aed748ad57bda1ac342 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Mon, 4 Oct 2021 13:01:43 +0000 Subject: [PATCH] happy lint --- .github/workflows/go.yml | 6 ++++ sshpiperd/auditor/typescriptlogger/audit.go | 6 +++- sshpiperd/challenger/authy/authyid_test.go | 6 ++-- sshpiperd/challenger/authy/handler.go | 3 +- sshpiperd/challenger/azdevicecode/handler.go | 7 +++-- sshpiperd/challenger/pome/chanllenger.go | 8 ++--- sshpiperd/challenger/pome/plugin.go | 2 +- sshpiperd/challenger/pome/upstream.go | 6 ++-- sshpiperd/cmd.go | 6 ++-- sshpiperd/loader.go | 2 +- sshpiperd/pipemgr.go | 2 +- sshpiperd/sshpiperd.go | 4 +-- sshpiperd/sshpiperd_test.go | 14 ++++++--- sshpiperd/upstream/database/handler_test.go | 2 +- .../grpcupstream/simpleserver/main.go | 2 +- sshpiperd/upstream/kubernetes/kubernetes.go | 13 ++++---- .../upstream/workingdir/workingdir_test.go | 11 +++++-- sshpiperd/upstream/yaml/pipemgr.go | 30 +++++++++---------- sshpiperd/ver.go | 2 +- 19 files changed, 76 insertions(+), 56 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 02113d68..07a8810d 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -28,6 +28,12 @@ jobs: set -e go version + - name: fmt + run: | + if [ "$(gofmt -s -l libpiper sshpiperd | wc -l)" -gt 0 ]; then + exit 1; + fi + - name: golangci-lint uses: golangci/golangci-lint-action@v2 diff --git a/sshpiperd/auditor/typescriptlogger/audit.go b/sshpiperd/auditor/typescriptlogger/audit.go index b45a0845..90525ee7 100644 --- a/sshpiperd/auditor/typescriptlogger/audit.go +++ b/sshpiperd/auditor/typescriptlogger/audit.go @@ -78,7 +78,11 @@ func (l *filePtyLogger) loggingTty(conn ssh.ConnMetadata, msg []byte) ([]byte, e } func (l *filePtyLogger) Close() (err error) { - _, err = l.typescript.Write([]byte(fmt.Sprintf("Script done on %v\n", time.Now().Format(time.ANSIC)))) + // if _, err = ; err != nil { + // return err + // } + _, _ = l.typescript.Write([]byte(fmt.Sprintf("Script done on %v\n", time.Now().Format(time.ANSIC)))) + l.typescript.Close() l.timing.Close() diff --git a/sshpiperd/challenger/authy/authyid_test.go b/sshpiperd/challenger/authy/authyid_test.go index f0d5ffed..c7ce18d7 100644 --- a/sshpiperd/challenger/authy/authyid_test.go +++ b/sshpiperd/challenger/authy/authyid_test.go @@ -18,11 +18,13 @@ func Test_findAuthyId(t *testing.T) { defer os.Remove(tmpfile.Name()) a.Config.File = tmpfile.Name() - ioutil.WriteFile(tmpfile.Name(), []byte(` + if err := ioutil.WriteFile(tmpfile.Name(), []byte(` piper 123 hook 456 hook 789 -`), os.ModePerm) +`), os.ModePerm); err != nil { + t.Fatal(err) + } { id, err := a.findAuthyID("piper") diff --git a/sshpiperd/challenger/authy/handler.go b/sshpiperd/challenger/authy/handler.go index 2eb49b1f..e7015afa 100644 --- a/sshpiperd/challenger/authy/handler.go +++ b/sshpiperd/challenger/authy/handler.go @@ -14,7 +14,8 @@ import ( type authyClient struct { Config struct { APIKey string `long:"challenger-authy-apikey" description:"Authy API Key" env:"SSHPIPERD_CHALLENGER_AUTHY_APIKEY" ini-name:"challenger-authy-apikey"` - Method string `long:"challenger-authy-method" default:"token" description:"Authy authentication method" env:"SSHPIPERD_CHALLENGER_AUTHY_METHOD" ini-name:"challenger-authy-method" choice:"token" choice:"onetouch"` + // Method string `long:"challenger-authy-method" default:"token" description:"Authy authentication method" env:"SSHPIPERD_CHALLENGER_AUTHY_METHOD" ini-name:"challenger-authy-method" choice:"token" choice:"onetouch"` + Method string `long:"challenger-authy-method" default:"token" description:"Authy authentication method" env:"SSHPIPERD_CHALLENGER_AUTHY_METHOD" ini-name:"challenger-authy-method" choice:"token"` File string `long:"challenger-authy-idfile" description:"Path to a file with ssh_name [space] authy_id per line (first line win if duplicate)" env:"SSHPIPERD_CHALLENGER_AUTHY_IDFILE" ini-name:"challenger-authy-idfile"` } diff --git a/sshpiperd/challenger/azdevicecode/handler.go b/sshpiperd/challenger/azdevicecode/handler.go index 6529bb7a..cb023e43 100644 --- a/sshpiperd/challenger/azdevicecode/handler.go +++ b/sshpiperd/challenger/azdevicecode/handler.go @@ -15,9 +15,10 @@ import ( type authClient struct { Config struct { - TenantID string `long:"challenger-azdevicecode-tenantid" description:"Azure AD tenant id" env:"SSHPIPERD_CHALLENGER_AZDEVICECODE_TENANTID" ini-name:"challenger-azdevicecode-tenantid"` - ClientID string `long:"challenger-azdevicecode-clientid" description:"Azure AD client id" env:"SSHPIPERD_CHALLENGER_AZDEVICECODE_CLIENTID" ini-name:"challenger-azdevicecode-clientid"` - Env string `long:"challenger-azdevicecode-env" default:"AzurePublicCloud" description:"Azure AD Cloud to request" env:"SSHPIPERD_CHALLENGER_AZDEVICECODE_ENV" ini-name:"challenger-azdevicecode-env" choice:"AzureChinaCloud" choice:"AzureGermanCloud" choice:"AzurePublicCloud" choice:"AzureUSGovernmentCloud"` + TenantID string `long:"challenger-azdevicecode-tenantid" description:"Azure AD tenant id" env:"SSHPIPERD_CHALLENGER_AZDEVICECODE_TENANTID" ini-name:"challenger-azdevicecode-tenantid"` + ClientID string `long:"challenger-azdevicecode-clientid" description:"Azure AD client id" env:"SSHPIPERD_CHALLENGER_AZDEVICECODE_CLIENTID" ini-name:"challenger-azdevicecode-clientid"` + // Env string `long:"challenger-azdevicecode-env" default:"AzurePublicCloud" description:"Azure AD Cloud to request" env:"SSHPIPERD_CHALLENGER_AZDEVICECODE_ENV" ini-name:"challenger-azdevicecode-env" choice:"AzureChinaCloud" choice:"AzureGermanCloud" choice:"AzurePublicCloud" choice:"AzureUSGovernmentCloud"` + Env string `long:"challenger-azdevicecode-env" default:"AzurePublicCloud" description:"Azure AD Cloud to request" env:"SSHPIPERD_CHALLENGER_AZDEVICECODE_ENV" ini-name:"challenger-azdevicecode-env"` Resource string `long:"challenger-azdevicecode-resource" default:"https://graph.windows.net/" description:"Resource URI to access, default is Graph API" env:"SSHPIPERD_CHALLENGER_AZDEVICECODE_RESOURCE" ini-name:"challenger-azdevicecode-resource"` NoReadGraph bool `long:"challenger-azdevicecode-noreadgraph" description:"Disable query user info from user graph" env:"SSHPIPERD_CHALLENGER_AZDEVICECODE_NOREADGRAPH" ini-name:"challenger-azdevicecode-noreadgraph"` } diff --git a/sshpiperd/challenger/pome/chanllenger.go b/sshpiperd/challenger/pome/chanllenger.go index 7e67712f..f8fc18d6 100644 --- a/sshpiperd/challenger/pome/chanllenger.go +++ b/sshpiperd/challenger/pome/chanllenger.go @@ -84,9 +84,7 @@ func (p *pome) challenge(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveCh return err } - err = say(fmt.Sprintf("Open %v in browser to login (timeout %v senconds)", url, p.Config.Timeout)) - - if err != nil { + if err := say(fmt.Sprintf("Open %v in browser to login (timeout %v senconds)", url, p.Config.Timeout)); err != nil { return nil, err } @@ -98,11 +96,11 @@ func (p *pome) challenge(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveCh pipe := <-c if pipe == nil { - say(fmt.Sprintf("Login timeout")) + _ = say("Login timeout") return nil, fmt.Errorf("timeout") } - say(fmt.Sprintf("Connecting to %v@%v", pipe.Username, pipe.Address)) + _ = say(fmt.Sprintf("Connecting to %v@%v", pipe.Username, pipe.Address)) pipe.say = say return pipe, nil diff --git a/sshpiperd/challenger/pome/plugin.go b/sshpiperd/challenger/pome/plugin.go index 914a1cc8..f05f744b 100644 --- a/sshpiperd/challenger/pome/plugin.go +++ b/sshpiperd/challenger/pome/plugin.go @@ -19,7 +19,7 @@ func (plugin) GetOpts() interface{} { } func (p *plugin) Init(logger *log.Logger) error { - p.logger = logger + p.pome.logger = logger return nil } diff --git a/sshpiperd/challenger/pome/upstream.go b/sshpiperd/challenger/pome/upstream.go index 8e7497b4..dc0e65e4 100644 --- a/sshpiperd/challenger/pome/upstream.go +++ b/sshpiperd/challenger/pome/upstream.go @@ -19,15 +19,13 @@ func (p *pome) authWithPipe(conn ssh.ConnMetadata, challengeContext ssh.Addition host, port, err := upstream.SplitHostPortForSSH(pipe.Address) if err != nil { - pipe.say("Not add Please check your configure") - return nil, nil, err + return nil, nil, pipe.say("Not add Please check your configure") } addr := fmt.Sprintf("%v:%v", host, port) c, err := net.Dial("tcp", addr) if err != nil { - pipe.say(fmt.Sprintf("Cannot connect to %v, reason: %v", addr, err)) - return nil, nil, err + return nil, nil, pipe.say(fmt.Sprintf("Cannot connect to %v, reason: %v", addr, err)) } callback := func() (ssh.AuthPipeType, ssh.AuthMethod, error) { diff --git a/sshpiperd/cmd.go b/sshpiperd/cmd.go index 6698de19..23980737 100644 --- a/sshpiperd/cmd.go +++ b/sshpiperd/cmd.go @@ -61,7 +61,7 @@ func addPlugins(group *flags.Group, name string, pluginNames []string, getter fu func populateFromConfig(ini *flags.IniParser, data interface{}, longopt string) error { parser := flags.NewParser(data, flags.IgnoreUnknown) - parser.Parse() + _, _ = parser.Parse() o := parser.FindOptionByLongName(longopt) file := o.Value().(flags.Filename) @@ -95,7 +95,7 @@ func main() { ini.ParseAsDefaults = true err := populateFromConfig(ini, configFile, "config") if err != nil { - fmt.Println(fmt.Sprintf("load config file failed %v", err)) + fmt.Printf("load config file failed %v", err) os.Exit(1) } } @@ -175,7 +175,7 @@ func main() { return nil, fmt.Errorf("must provider upstream driver") } - provider := upstream.Get(config.UpstreamDriver).(upstream.Provider) + provider := upstream.Get(config.UpstreamDriver) err := provider.Init(log.New(ioutil.Discard, "", 0)) if err != nil { return nil, err diff --git a/sshpiperd/loader.go b/sshpiperd/loader.go index d8ac1be1..af241130 100644 --- a/sshpiperd/loader.go +++ b/sshpiperd/loader.go @@ -2,10 +2,10 @@ package main import ( _ "github.com/tg123/sshpiper/sshpiperd/upstream/database" + _ "github.com/tg123/sshpiper/sshpiperd/upstream/grpcupstream" _ "github.com/tg123/sshpiper/sshpiperd/upstream/kubernetes" _ "github.com/tg123/sshpiper/sshpiperd/upstream/workingdir" _ "github.com/tg123/sshpiper/sshpiperd/upstream/yaml" - _ "github.com/tg123/sshpiper/sshpiperd/upstream/grpcupstream" _ "github.com/tg123/sshpiper/sshpiperd/challenger/authy" _ "github.com/tg123/sshpiper/sshpiperd/challenger/azdevicecode" diff --git a/sshpiperd/pipemgr.go b/sshpiperd/pipemgr.go index ff53b797..815cbc02 100644 --- a/sshpiperd/pipemgr.go +++ b/sshpiperd/pipemgr.go @@ -51,7 +51,7 @@ func createPipeMgr(load func() (upstream.Provider, error)) interface{} { t := template.Must(template.New("").Parse(`{{.Username}} -> {{.UpstreamUsername}}@{{.Host}}:{{.Port}}`)) for _, pipe := range pipes { - t.Execute(os.Stdout, pipe) + _ = t.Execute(os.Stdout, pipe) fmt.Println() } diff --git a/sshpiperd/sshpiperd.go b/sshpiperd/sshpiperd.go index f82501a0..c5803efd 100644 --- a/sshpiperd/sshpiperd.go +++ b/sshpiperd/sshpiperd.go @@ -196,8 +196,8 @@ func startPiper(config *piperdConfig, logger *log.Logger) error { go func(c net.Conn) { defer c.Close() - pipec := make(chan *ssh.PiperConn, 0) - errorc := make(chan error, 0) + pipec := make(chan *ssh.PiperConn) + errorc := make(chan error) go func() { p, err := ssh.NewSSHPiperConn(c, piper) diff --git a/sshpiperd/sshpiperd_test.go b/sshpiperd/sshpiperd_test.go index 094fe349..4efead70 100644 --- a/sshpiperd/sshpiperd_test.go +++ b/sshpiperd/sshpiperd_test.go @@ -35,9 +35,10 @@ func (p *testplugin) Init(logger *log.Logger) error { } func Test_getAndInstall(t *testing.T) { + var err error // ignore empty - getAndInstall("", "", func(n string) registry.Plugin { + _ = getAndInstall("", "", func(n string) registry.Plugin { t.Errorf("should not call get") return nil }, func(plugin registry.Plugin) error { @@ -46,7 +47,7 @@ func Test_getAndInstall(t *testing.T) { }, nil) // fail when not found - err := getAndInstall("", "test", func(n string) registry.Plugin { + err = getAndInstall("", "test", func(n string) registry.Plugin { if n != "test" { t.Errorf("plugin name changed") } @@ -338,12 +339,17 @@ func Test_installDriver(t *testing.T) { m := []byte{0} - a.GetUpstreamHook()(nil, m) + if _, err := a.GetUpstreamHook()(nil, m); err != nil { + t.Errorf("run upstream hook %v", err) + } + if m[0] != 42 { t.Errorf("upstream not handled") } - a.GetDownstreamHook()(nil, m) + if _, err := a.GetDownstreamHook()(nil, m); err != nil { + t.Errorf("run downstream hook %v", err) + } if m[0] != 100 { t.Errorf("downstream not handled") } diff --git a/sshpiperd/upstream/database/handler_test.go b/sshpiperd/upstream/database/handler_test.go index fd62bdcb..d33ef8a7 100644 --- a/sshpiperd/upstream/database/handler_test.go +++ b/sshpiperd/upstream/database/handler_test.go @@ -197,6 +197,6 @@ func createListener(t *testing.T) (net.Listener, error) { if err != nil { t.Fatalf("cant create fake server: %v", err) } - go listener.Accept() + // go listener.Accept() return listener, err } diff --git a/sshpiperd/upstream/grpcupstream/simpleserver/main.go b/sshpiperd/upstream/grpcupstream/simpleserver/main.go index d1539405..f584f754 100644 --- a/sshpiperd/upstream/grpcupstream/simpleserver/main.go +++ b/sshpiperd/upstream/grpcupstream/simpleserver/main.go @@ -68,5 +68,5 @@ func main() { } log.Printf("serving on %v", addr) - s.Serve(l) + panic(s.Serve(l)) } diff --git a/sshpiperd/upstream/kubernetes/kubernetes.go b/sshpiperd/upstream/kubernetes/kubernetes.go index 87d2f5d6..e7dbb0fc 100644 --- a/sshpiperd/upstream/kubernetes/kubernetes.go +++ b/sshpiperd/upstream/kubernetes/kubernetes.go @@ -21,11 +21,11 @@ type pipeConfig struct { UpstreamHost string `kubernetes:"upstream_host"` } -type createPipeCtx struct { - pipe pipeConfig - conn ssh.ConnMetadata - challengeContext ssh.AdditionalChallengeContext -} +// type createPipeCtx struct { +// pipe pipeConfig +// conn ssh.ConnMetadata +// challengeContext ssh.AdditionalChallengeContext +// } func (p *plugin) getClientSet() (*sshpipeclientset.Clientset, error) { /* @@ -67,8 +67,7 @@ func (p *plugin) getConfig(clientset *sshpipeclientset.Clientset) ([]pipeConfig, var config []pipeConfig for _, pipe := range pipes.Items { - var targetHost string - targetHost = fmt.Sprintf("%s.%s", pipe.Spec.Target.Name, pipe.ObjectMeta.Namespace) + targetHost := fmt.Sprintf("%s.%s", pipe.Spec.Target.Name, pipe.ObjectMeta.Namespace) for _, username := range pipe.Spec.Users { config = append( diff --git a/sshpiperd/upstream/workingdir/workingdir_test.go b/sshpiperd/upstream/workingdir/workingdir_test.go index 5caf6c49..b5831de1 100644 --- a/sshpiperd/upstream/workingdir/workingdir_test.go +++ b/sshpiperd/upstream/workingdir/workingdir_test.go @@ -34,7 +34,9 @@ func buildWorkingDir(users []string, t *testing.T) { config.WorkingDir = dir for _, u := range users { - os.Mkdir(config.WorkingDir+"/"+u, os.ModePerm) + if err := os.Mkdir(config.WorkingDir+"/"+u, os.ModePerm); err != nil { + t.Fatalf("mkdir dir:%v", err) + } } t.Logf("switch workingdir to %v", config.WorkingDir) @@ -229,8 +231,11 @@ func TestFindUpstreamFromUserfile(t *testing.T) { t.Errorf("fake server error %v", err) return } - io.Copy(c, c) - c.Close() + defer c.Close() + if _, err := io.Copy(c, c); err != nil { + t.Errorf("fake server copy error %v", err) + return + } }() addr := listener.Addr().String() diff --git a/sshpiperd/upstream/yaml/pipemgr.go b/sshpiperd/upstream/yaml/pipemgr.go index 259b35dd..601fb579 100644 --- a/sshpiperd/upstream/yaml/pipemgr.go +++ b/sshpiperd/upstream/yaml/pipemgr.go @@ -51,26 +51,26 @@ func (p *plugin) ListPipe() ([]upstream.Pipe, error) { return pipes, nil } -func findnode(root *yaml.Node, test func(*yaml.Node) bool) *yaml.Node { - var q []*yaml.Node +// func findnode(root *yaml.Node, test func(*yaml.Node) bool) *yaml.Node { +// var q []*yaml.Node - q = append(q, root) +// q = append(q, root) - for len(q) > 0 { - e := q[0] - q = q[1:] +// for len(q) > 0 { +// e := q[0] +// q = q[1:] - if test(e) { - return e - } +// if test(e) { +// return e +// } - for _, n := range e.Content { - q = append(q, n) - } - } +// for _, n := range e.Content { +// q = append(q, n) +// } +// } - return nil -} +// return nil +// } func findByMapKey(m *yaml.Node, k string) (*yaml.Node, int) { for i := 1; i < len(m.Content); i += 2 { diff --git a/sshpiperd/ver.go b/sshpiperd/ver.go index 9bceb6e7..71cdaaf7 100644 --- a/sshpiperd/ver.go +++ b/sshpiperd/ver.go @@ -19,7 +19,7 @@ Go Runtime : {{.GOVER}} Git Commit : {{.GITHASH}} `[1:])) - versionTemplate.Execute(os.Stdout, struct { + _ = versionTemplate.Execute(os.Stdout, struct { VER string GOVER string GITHASH string