From bc9875c4b52de30aa5e95038ddda1fa06e0cf57d Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Wed, 6 Jul 2022 17:27:39 +0000 Subject: [PATCH] happy lint --- cmd/sshpiperd/grpc.go | 2 ++ cmd/sshpiperd/internal/plugin/grpc.go | 16 ++++++++++------ cmd/sshpiperd/main.go | 8 +++++++- e2e/e2e_test.go | 23 ++++++++++++++++++----- libplugin/util.go | 13 ------------- plugin/simplemath/main.go | 2 +- plugin/workingdirbykey/main.go | 2 +- 7 files changed, 39 insertions(+), 27 deletions(-) diff --git a/cmd/sshpiperd/grpc.go b/cmd/sshpiperd/grpc.go index ac7db6dd..ba6f96ce 100644 --- a/cmd/sshpiperd/grpc.go +++ b/cmd/sshpiperd/grpc.go @@ -90,6 +90,8 @@ func createNetGrpcPlugin(args []string) (grpcPlugin *plugin.GrpcPlugin, err erro return err } + grpcPlugin.Name = fmt.Sprintf("grpc://%s", c.String("endpoint")) + return nil }, } diff --git a/cmd/sshpiperd/internal/plugin/grpc.go b/cmd/sshpiperd/internal/plugin/grpc.go index 9974b29f..a28a262d 100644 --- a/cmd/sshpiperd/internal/plugin/grpc.go +++ b/cmd/sshpiperd/internal/plugin/grpc.go @@ -7,7 +7,6 @@ import ( "net" "os/exec" "strconv" - "time" "github.com/google/uuid" log "github.com/sirupsen/logrus" @@ -17,9 +16,11 @@ import ( "github.com/tg123/sshpiper/libplugin/ioconn" "golang.org/x/crypto/ssh" "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" ) type GrpcPlugin struct { + Name string OnNextPlugin func(conn ssh.ChallengeContext, upstream *libplugin.UpstreamNextPluginAuth) error grpcconn *grpc.ClientConn @@ -249,7 +250,7 @@ func (g *GrpcPlugin) UpstreamAuthFailureCallbackRemote(conn ssh.ConnMetadata, me } } - g.client.UpstreamAuthFailureNotice(context.Background(), &libplugin.UpstreamAuthFailureNoticeRequest{ + _, _ = g.client.UpstreamAuthFailureNotice(context.Background(), &libplugin.UpstreamAuthFailureNoticeRequest{ Meta: toMeta(challengeCtx, conn), Method: method, Error: err.Error(), @@ -396,8 +397,9 @@ func (g *GrpcPlugin) KeyboardInteractiveCallback(conn ssh.ConnMetadata, client s if err != nil { return nil, err } - - defer stream.CloseSend() + defer func() { + _ = stream.CloseSend() + }() for { msg, err := stream.Recv() @@ -497,7 +499,9 @@ func DialCmd(cmd *exec.Cmd) (*CmdPlugin, error) { return nil, err } - go io.Copy(log.StandardLogger().Out, stderr) + go func() { + _, _ = io.Copy(log.StandardLogger().Out, stderr) + }() go func() { err := cmd.Wait() @@ -506,7 +510,7 @@ func DialCmd(cmd *exec.Cmd) (*CmdPlugin, error) { } }() - conn, err := grpc.Dial("", grpc.WithInsecure(), grpc.WithDialer(func(_ string, _ time.Duration) (net.Conn, error) { + conn, err := grpc.Dial("", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(_ context.Context, _ string) (net.Conn, error) { return cmdconn, nil })) diff --git a/cmd/sshpiperd/main.go b/cmd/sshpiperd/main.go index 2a604d31..fa4e724b 100644 --- a/cmd/sshpiperd/main.go +++ b/cmd/sshpiperd/main.go @@ -68,6 +68,8 @@ func createPlugin(args []string) (*plugin.GrpcPlugin, error) { return nil, err } + p.Name = exe + return &p.GrpcPlugin, nil } } @@ -157,7 +159,11 @@ func main() { return err } - go p.RecvLogs(log.StandardLogger().Out) + go func() { + if err := p.RecvLogs(log.StandardLogger().Out); err != nil { + log.Errorf("plugin %v recv logs error: %v", p.Name, err) + } + }() plugins = append(plugins, p) } diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index 4b6d05c6..5af7a980 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -49,11 +49,17 @@ func runCmd(cmd string, args ...string) (*exec.Cmd, io.Writer, io.Reader, error) var buf bytes.Buffer r := io.TeeReader(f, &buf) - go io.Copy(os.Stdout, r) + go func() { + _, _ = io.Copy(os.Stdout, r) + }() log.Printf("starting %v", c.Args) - go c.Wait() + go func() { + if err := c.Wait(); err != nil { + log.Printf("wait %v returns %v", c.Args, err) + } + }() return c, f, &buf, nil } @@ -65,7 +71,7 @@ func enterPassword(stdin io.Writer, stdout io.Reader, password string) { for scanner.Scan() { line := scanner.Text() if strings.Contains(line, "'s password") { - stdin.Write([]byte(fmt.Sprintf("%v\n", password))) + _, _ = stdin.Write([]byte(fmt.Sprintf("%v\n", password))) log.Printf("got password prompt, sending password") return } @@ -96,7 +102,7 @@ func checkSharedFileContent(t *testing.T, targetfie string, expected string) { } func TestMain(m *testing.M) { - runCmd("ssh", "-V") + _, _, _, _ = runCmd("ssh", "-V") for _, ep := range []string{ "host-password:2222", @@ -132,7 +138,14 @@ func TestFixed(t *testing.T) { t.Errorf("failed to ssh to piper-fixed, %v", err) } - defer c.Process.Kill() + defer func() { + if c.Process != nil { + if err = c.Process.Kill(); err != nil { + log.Printf("failed to kill ssh process, %v", err) + } + } + }() + enterPassword(stdin, stdout, "pass") time.Sleep(time.Second) // wait for file flush diff --git a/libplugin/util.go b/libplugin/util.go index 275f737b..a1a2d442 100644 --- a/libplugin/util.go +++ b/libplugin/util.go @@ -2,25 +2,12 @@ package libplugin import ( "fmt" - "io" "net" - "os" "strconv" "github.com/sirupsen/logrus" ) -func WriterToFile(writer io.Writer) (*os.File, error) { - r, w, err := os.Pipe() - if err != nil { - return nil, err - } - - go io.Copy(writer, r) - - return w, nil -} - func AuthMethodTypeToName(a AuthMethod) string { switch a { case AuthMethod_NONE: diff --git a/plugin/simplemath/main.go b/plugin/simplemath/main.go index 5b698b01..a0e6e0f5 100644 --- a/plugin/simplemath/main.go +++ b/plugin/simplemath/main.go @@ -18,7 +18,7 @@ func main() { CreateConfig: func(_ *cli.Context) (*libplugin.SshPiperPluginConfig, error) { return &libplugin.SshPiperPluginConfig{ KeyboardInteractiveCallback: func(conn libplugin.ConnMetadata, client libplugin.KeyboardInteractiveChallenge) (*libplugin.Upstream, error) { - client("", "lets do math", "", false) + _, _ = client("", "lets do math", "", false) for { diff --git a/plugin/workingdirbykey/main.go b/plugin/workingdirbykey/main.go index dc7adf8d..f277e5a0 100644 --- a/plugin/workingdirbykey/main.go +++ b/plugin/workingdirbykey/main.go @@ -53,7 +53,7 @@ func main() { var upstream *libplugin.Upstream - filepath.Walk(userdir, func(path string, info os.FileInfo, err error) error { + _ = filepath.Walk(userdir, func(path string, info os.FileInfo, err error) error { log.Infof("search public key in path: %v", path) if err != nil {