From 89ab72bee04603c3c913e43d7ee0c50325602bc7 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Fri, 29 Aug 2025 20:04:31 -0700 Subject: [PATCH] all: run gofumpt (#652) * all: run gofumpt https://github.com/mvdan/gofumpt The main appeal for me here is that my (and many other peoples') editors run this on save, so doing a single diff here keeps other diffs minimal. * .github/workflows: add gofumpt checker --- .github/workflows/gofumpt.yml | 34 ++++++++++++++++++++++++ cmd/sshpiperd/asciicast.go | 6 +---- cmd/sshpiperd/daemon.go | 6 ++--- cmd/sshpiperd/grpc.go | 1 - cmd/sshpiperd/hook.go | 1 - cmd/sshpiperd/internal/plugin/chain.go | 1 - cmd/sshpiperd/internal/plugin/grpc.go | 11 -------- cmd/sshpiperd/internal/plugin/tty.go | 3 ++- cmd/sshpiperd/main.go | 7 ++--- cmd/sshpiperd/snap/configgen/main.go | 4 --- cmd/sshpiperd/snap/launcher/main.go | 4 +-- cmd/sshpiperd/typescript.go | 10 ++----- e2e/banner_test.go | 8 ------ e2e/connmeta_test.go | 2 -- e2e/docker_test.go | 8 ++---- e2e/failtoban_test.go | 21 +++------------ e2e/fixed_test.go | 5 ---- e2e/grpcplugin_test.go | 3 --- e2e/kubernetes_test.go | 10 ++----- e2e/main_test.go | 2 -- e2e/testplugin/testgetmetaplugin/main.go | 3 --- e2e/testplugin/testgrpcplugin/main.go | 2 -- e2e/testplugin/testsetmetaplugin/main.go | 3 --- e2e/workingdir_test.go | 21 +++++---------- e2e/yaml_test.go | 15 +---------- libplugin/ioconn/conn_test.go | 1 - libplugin/pluginbase.go | 2 -- plugin/failtoban/main.go | 6 ++--- plugin/fixed/main.go | 1 - plugin/simplemath/main.go | 1 - plugin/username-router/main.go | 3 --- plugin/yaml/yaml.go | 2 +- plugin/yaml/yaml_test.go | 1 - 33 files changed, 63 insertions(+), 145 deletions(-) create mode 100644 .github/workflows/gofumpt.yml diff --git a/.github/workflows/gofumpt.yml b/.github/workflows/gofumpt.yml new file mode 100644 index 00000000..23838396 --- /dev/null +++ b/.github/workflows/gofumpt.yml @@ -0,0 +1,34 @@ +name: check gofumpt + +on: + push: + branches: [master] + pull_request: + +permissions: + contents: read + +jobs: + gofumpt: + name: Check gofumpt formatting + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v5 + + - name: Download gofumpt binary + run: | + curl -L https://github.com/mvdan/gofumpt/releases/download/v0.8.0/gofumpt_v0.8.0_linux_amd64 -o /tmp/gofumpt + chmod +x /tmp/gofumpt + + - name: Check gofumpt formatting + run: | + unformatted_files=$(/tmp/gofumpt -l .) + if [ -n "$unformatted_files" ]; then + echo "The following files are not gofumpt'd:" + echo "$unformatted_files" + echo + echo "Please run: gofumpt -w ." + exit 1 + fi + echo "All files are properly formatted" diff --git a/cmd/sshpiperd/asciicast.go b/cmd/sshpiperd/asciicast.go index 1b0d2125..a65a174c 100644 --- a/cmd/sshpiperd/asciicast.go +++ b/cmd/sshpiperd/asciicast.go @@ -70,7 +70,6 @@ func (l *asciicastLogger) uphook(msg []byte) error { t := time.Since(l.starttime).Seconds() _, err := fmt.Fprintf(f, "[%v,\"o\",\"%s\"]\n", t, jsonEscape(string(buf))) - if err != nil { return err } @@ -118,7 +117,6 @@ func (l *asciicastLogger) downhook(msg []byte) error { } case "shell", "exec": jsonEnvs, err := json.Marshal(l.envs) - if err != nil { return err } @@ -126,9 +124,8 @@ func (l *asciicastLogger) downhook(msg []byte) error { f, err := os.OpenFile( path.Join(l.recorddir, fmt.Sprintf("%s%s-channel-%d.cast", l.prefix, reqType, clientChannelID)), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, - 0600, + 0o600, ) - if err != nil { return err } @@ -145,7 +142,6 @@ func (l *asciicastLogger) downhook(msg []byte) error { l.starttime.Unix(), string(jsonEnvs), ) - if err != nil { return err } diff --git a/cmd/sshpiperd/daemon.go b/cmd/sshpiperd/daemon.go index 5e8e4d6c..b4d5c002 100644 --- a/cmd/sshpiperd/daemon.go +++ b/cmd/sshpiperd/daemon.go @@ -44,7 +44,7 @@ func generateSshKey(keyfile string) error { privateKeyBytes := pem.EncodeToMemory(privateKeyPEM) - return os.WriteFile(keyfile, privateKeyBytes, 0600) + return os.WriteFile(keyfile, privateKeyBytes, 0o600) } func newDaemon(ctx *cli.Context) (*daemon, error) { @@ -157,7 +157,6 @@ func newDaemon(ctx *cli.Context) (*daemon, error) { } case "dedup": config.UpstreamBannerCallback = func(downstream ssh.ServerPreAuthConn, banner string, ctx ssh.ChallengeContext) error { - meta, ok := ctx.Meta().(*plugin.PluginConnMeta) if !ok { // should not happen, but just in case @@ -244,7 +243,6 @@ func (d *daemon) run() error { go func() { p, err := ssh.NewSSHPiperConn(c, &d.config.PiperConfig) - if err != nil { errorc <- err return @@ -288,7 +286,7 @@ func (d *daemon) run() error { uniqID := plugin.GetUniqueID(p.ChallengeContext()) recorddir = path.Join(d.recorddir, uniqID) } - err = os.MkdirAll(recorddir, 0700) + err = os.MkdirAll(recorddir, 0o700) if err != nil { log.Errorf("cannot create screen recording dir %v: %v", recorddir, err) return diff --git a/cmd/sshpiperd/grpc.go b/cmd/sshpiperd/grpc.go index aba4df7d..49ff4f2a 100644 --- a/cmd/sshpiperd/grpc.go +++ b/cmd/sshpiperd/grpc.go @@ -48,7 +48,6 @@ func createNetGrpcPlugin(args []string) (grpcPlugin *plugin.GrpcPlugin, err erro }, }, Action: func(c *cli.Context) error { - var secopt grpc.DialOption if c.Bool("insecure") { secopt = grpc.WithTransportCredentials(insecure.NewCredentials()) diff --git a/cmd/sshpiperd/hook.go b/cmd/sshpiperd/hook.go index 4eb1a040..18200674 100644 --- a/cmd/sshpiperd/hook.go +++ b/cmd/sshpiperd/hook.go @@ -14,7 +14,6 @@ func (h *hookChain) append(hook ssh.PipePacketHook) { } func (h *hookChain) hook() ssh.PipePacketHook { - if len(h.hooks) == 0 { return nil } diff --git a/cmd/sshpiperd/internal/plugin/chain.go b/cmd/sshpiperd/internal/plugin/chain.go index 5a1b9ee4..a1af26cd 100644 --- a/cmd/sshpiperd/internal/plugin/chain.go +++ b/cmd/sshpiperd/internal/plugin/chain.go @@ -109,7 +109,6 @@ func (cp *ChainPlugins) NextAuthMethods(conn ssh.ConnMetadata, challengeCtx ssh. } func (cp *ChainPlugins) InstallPiperConfig(config *GrpcPluginConfig) error { - config.CreateChallengeContext = func(conn ssh.ServerPreAuthConn) (ssh.ChallengeContext, error) { ctx, err := cp.CreateChallengeContext(conn) if err != nil { diff --git a/cmd/sshpiperd/internal/plugin/grpc.go b/cmd/sshpiperd/internal/plugin/grpc.go index fad905a7..614492ad 100644 --- a/cmd/sshpiperd/internal/plugin/grpc.go +++ b/cmd/sshpiperd/internal/plugin/grpc.go @@ -50,7 +50,6 @@ func DialGrpc(conn *grpc.ClientConn) (*GrpcPlugin, error) { } func (g *GrpcPlugin) InstallPiperConfig(config *GrpcPluginConfig) error { - cb, err := g.client.ListCallbacks(context.Background(), &libplugin.ListCallbackRequest{}) if err != nil { return err @@ -206,7 +205,6 @@ func (g *GrpcPlugin) NextAuthMethodsRemote(conn ssh.ConnMetadata, challengeCtx s reply, err := g.client.NextAuthMethods(context.Background(), &libplugin.NextAuthMethodsRequest{ Meta: meta, }) - if err != nil { return nil, err } @@ -295,7 +293,6 @@ func (g *GrpcPlugin) createUpstream(conn ssh.ConnMetadata, challengeCtx ssh.Chal Netaddress: addr.String(), Key: key.Marshal(), }) - if err != nil { return err } @@ -379,7 +376,6 @@ func (g *GrpcPlugin) createUpstream(conn ssh.ConnMetadata, challengeCtx ssh.Chal Address: addr, ClientConfig: config, }, nil - } func (g *GrpcPlugin) dialUpstream(uri string) (net.Conn, string, error) { @@ -417,7 +413,6 @@ func (g *GrpcPlugin) NoClientAuthCallback(conn ssh.ConnMetadata, challengeCtx ss reply, err := g.client.NoneAuth(context.Background(), &libplugin.NoneAuthRequest{ Meta: meta, }) - if err != nil { return nil, err } @@ -431,7 +426,6 @@ func (g *GrpcPlugin) PasswordCallback(conn ssh.ConnMetadata, password []byte, ch Meta: meta, Password: password, }) - if err != nil { return nil, err } @@ -445,7 +439,6 @@ func (g *GrpcPlugin) PublicKeyCallback(conn ssh.ConnMetadata, key ssh.PublicKey, Meta: meta, PublicKey: key.Marshal(), }) - if err != nil { return nil, err } @@ -454,7 +447,6 @@ func (g *GrpcPlugin) PublicKeyCallback(conn ssh.ConnMetadata, key ssh.PublicKey, } func (g *GrpcPlugin) KeyboardInteractiveCallback(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge, challengeCtx ssh.ChallengeContext) (*ssh.Upstream, error) { - stream, err := g.client.KeyboardInteractiveAuth(context.Background()) if err != nil { return nil, err @@ -525,7 +517,6 @@ func (g *GrpcPlugin) DownstreamBannerCallback(conn ssh.ConnMetadata, challengeCt reply, err := g.client.Banner(context.Background(), &libplugin.BannerRequest{ Meta: meta, }) - if err != nil { log.Debugf("failed to get banner: %v", err) return "" @@ -606,13 +597,11 @@ func DialCmd(cmd *exec.Cmd) (*CmdPlugin, error) { conn, err := grpc.NewClient("127.0.0.1", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(func(_ context.Context, _ string) (net.Conn, error) { return cmdconn, nil })) - if err != nil { return nil, err } g, err := DialGrpc(conn) - if err != nil { return nil, err } diff --git a/cmd/sshpiperd/internal/plugin/tty.go b/cmd/sshpiperd/internal/plugin/tty.go index 00420690..a615eed1 100644 --- a/cmd/sshpiperd/internal/plugin/tty.go +++ b/cmd/sshpiperd/internal/plugin/tty.go @@ -1,9 +1,10 @@ package plugin import ( - "golang.org/x/term" "io" "os" + + "golang.org/x/term" ) // checkIfTerminal returns whether the given file descriptor is a terminal. diff --git a/cmd/sshpiperd/main.go b/cmd/sshpiperd/main.go index 23bb60bc..05e3a0cb 100644 --- a/cmd/sshpiperd/main.go +++ b/cmd/sshpiperd/main.go @@ -18,8 +18,7 @@ import ( var mainver string = "(devel)" func version() string { - - var v = mainver + v := mainver bi, ok := debug.ReadBuildInfo() if !ok { @@ -79,7 +78,6 @@ func isValidLogFormat(logFormat string) bool { } func main() { - app := &cli.App{ Name: "sshpiperd", Usage: "the missing reverse proxy for ssh scp", @@ -240,7 +238,6 @@ func main() { log.Info("starting sshpiperd version: ", version()) d, err := newDaemon(ctx) - if err != nil { return err } @@ -288,7 +285,7 @@ func main() { if dir == "" { continue } - + pluginexe := filepath.Join(dir, pluginEnv) if _, err := os.Stat(pluginexe); err == nil { args = append(args, pluginexe) diff --git a/cmd/sshpiperd/snap/configgen/main.go b/cmd/sshpiperd/snap/configgen/main.go index 828747dd..4e48258a 100644 --- a/cmd/sshpiperd/snap/configgen/main.go +++ b/cmd/sshpiperd/snap/configgen/main.go @@ -11,7 +11,6 @@ import ( ) func main() { - configs := map[string]string{ "sshpiperd": "../../main.go", "workingdir": "../../../../plugin/workingdir/main.go", @@ -40,9 +39,7 @@ func extractFlags(namespace, filePath string) { } ast.Inspect(node, func(n ast.Node) bool { - if cl, ok := n.(*ast.CompositeLit); ok { - if t, ok := cl.Type.(*ast.SelectorExpr); ok { o, ok := t.X.(*ast.Ident) @@ -63,7 +60,6 @@ func extractFlags(namespace, filePath string) { for _, v := range cl.Elts { if kv, ok := v.(*ast.KeyValueExpr); ok { - switch kv.Key.(*ast.Ident).Name { case "Name": flagName = strings.Trim(kv.Value.(*ast.BasicLit).Value, " \"") diff --git a/cmd/sshpiperd/snap/launcher/main.go b/cmd/sshpiperd/snap/launcher/main.go index 40b06dd5..3add3d03 100644 --- a/cmd/sshpiperd/snap/launcher/main.go +++ b/cmd/sshpiperd/snap/launcher/main.go @@ -23,7 +23,7 @@ func main() { if len(os.Args) > 1 && os.Args[1] == "generate" { flags = loadFromSnapctl() cache, _ := json.Marshal(flags) - if err := os.WriteFile(configfile, cache, 0600); err != nil { + if err := os.WriteFile(configfile, cache, 0o600); err != nil { log.Fatal(err) } @@ -143,7 +143,7 @@ func loadFromSnapctl() map[string][][]string { v = "workingdir" dir := path.Join(commondir, v) - _ = os.MkdirAll(dir, 0700) + _ = os.MkdirAll(dir, 0o700) flags["workingdir"] = append(flags["workingdir"], []string{"root", dir}) } } diff --git a/cmd/sshpiperd/typescript.go b/cmd/sshpiperd/typescript.go index cd78f485..d11b8114 100644 --- a/cmd/sshpiperd/typescript.go +++ b/cmd/sshpiperd/typescript.go @@ -19,25 +19,21 @@ type filePtyLogger struct { } func newFilePtyLogger(outputdir string) (*filePtyLogger, error) { - now := time.Now() filename := fmt.Sprintf("%d", now.Unix()) - typescript, err := os.OpenFile(path.Join(outputdir, fmt.Sprintf("%v.typescript", filename)), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - + typescript, err := os.OpenFile(path.Join(outputdir, fmt.Sprintf("%v.typescript", filename)), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) if err != nil { return nil, err } _, err = fmt.Fprintf(typescript, "Script started on %v\n", now.Format(time.ANSIC)) - if err != nil { return nil, err } - timing, err := os.OpenFile(path.Join(outputdir, fmt.Sprintf("%v.timing", filename)), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - + timing, err := os.OpenFile(path.Join(outputdir, fmt.Sprintf("%v.timing", filename)), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) if err != nil { return nil, err } @@ -50,7 +46,6 @@ func newFilePtyLogger(outputdir string) (*filePtyLogger, error) { } func (l *filePtyLogger) loggingTty(msg []byte) error { - if msg[0] == msgChannelData { buf := msg[9:] @@ -65,7 +60,6 @@ func (l *filePtyLogger) loggingTty(msg []byte) error { l.oldtime = now _, err := l.typescript.Write(buf) - if err != nil { return err } diff --git a/e2e/banner_test.go b/e2e/banner_test.go index 812c184a..7712d7a0 100644 --- a/e2e/banner_test.go +++ b/e2e/banner_test.go @@ -8,7 +8,6 @@ import ( ) func TestBanner(t *testing.T) { - t.Run("args", func(t *testing.T) { piperaddr, piperport := nextAvailablePiperAddress() randtext := uuid.New().String() @@ -22,7 +21,6 @@ func TestBanner(t *testing.T) { "--target", "host-password:2222", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -44,7 +42,6 @@ func TestBanner(t *testing.T) { "user", "127.0.0.1", ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -56,7 +53,6 @@ func TestBanner(t *testing.T) { }) t.Run("file", func(t *testing.T) { - piperaddr, piperport := nextAvailablePiperAddress() randtext := uuid.New().String() @@ -83,7 +79,6 @@ func TestBanner(t *testing.T) { "--target", "host-password:2222", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -105,7 +100,6 @@ func TestBanner(t *testing.T) { "user", "127.0.0.1", ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -125,7 +119,6 @@ func TestBanner(t *testing.T) { "--target", "host-password:2222", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -147,7 +140,6 @@ func TestBanner(t *testing.T) { "user", "127.0.0.1", ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } diff --git a/e2e/connmeta_test.go b/e2e/connmeta_test.go index 64c67181..e6ffb8d8 100644 --- a/e2e/connmeta_test.go +++ b/e2e/connmeta_test.go @@ -20,7 +20,6 @@ func TestConnMeta(t *testing.T) { "--", "/sshpiperd/plugins/testgetmetaplugin", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -46,7 +45,6 @@ func TestConnMeta(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-fixed, %v", err) } diff --git a/e2e/docker_test.go b/e2e/docker_test.go index 9f6c150b..01c21660 100644 --- a/e2e/docker_test.go +++ b/e2e/docker_test.go @@ -18,7 +18,6 @@ func TestDocker(t *testing.T) { piperport, "/sshpiperd/plugins/docker", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -45,7 +44,6 @@ func TestDocker(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-fixed, %v", err) } @@ -60,7 +58,6 @@ func TestDocker(t *testing.T) { }) t.Run("key", func(t *testing.T) { - keyfiledir, err := os.MkdirTemp("", "") if err != nil { t.Errorf("failed to create temp key file: %v", err) @@ -68,11 +65,11 @@ func TestDocker(t *testing.T) { keyfile := path.Join(keyfiledir, "key") - if err := os.WriteFile(keyfile, []byte(testprivatekey), 0400); err != nil { + if err := os.WriteFile(keyfile, []byte(testprivatekey), 0o400); err != nil { t.Errorf("failed to write to test key: %v", err) } - if err := os.WriteFile("/publickey_authorized_keys/authorized_keys", []byte(`ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINRGTH325rDUp12tplwukHmR8ytbC9TPZ886gCstynP1`), 0400); err != nil { + if err := os.WriteFile("/publickey_authorized_keys/authorized_keys", []byte(`ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINRGTH325rDUp12tplwukHmR8ytbC9TPZ886gCstynP1`), 0o400); err != nil { t.Errorf("failed to write to authorized_keys: %v", err) } @@ -95,7 +92,6 @@ func TestDocker(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-fixed, %v", err) } diff --git a/e2e/failtoban_test.go b/e2e/failtoban_test.go index 6b0e14c2..b5ec6160 100644 --- a/e2e/failtoban_test.go +++ b/e2e/failtoban_test.go @@ -26,7 +26,6 @@ func TestFailtoban(t *testing.T) { "--max-failures", "3", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -74,7 +73,6 @@ func TestFailtoban(t *testing.T) { "user", "127.0.0.1", ) - if err != nil { t.Errorf("failed to ssh to piper-fixed, %v", err) } @@ -90,7 +88,6 @@ func TestFailtoban(t *testing.T) { t.Errorf("expected connection closed by") } } - } func TestFailtobanPipeCreateFail(t *testing.T) { @@ -107,7 +104,6 @@ func TestFailtobanPipeCreateFail(t *testing.T) { "--max-failures", "3", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -122,11 +118,11 @@ func TestFailtobanPipeCreateFail(t *testing.T) { userdir := path.Join(workingdir, "bypassword") { - if err := os.MkdirAll(userdir, 0700); err != nil { + if err := os.MkdirAll(userdir, 0o700); err != nil { t.Errorf("failed to create working directory %s: %v", userdir, err) } - if err := os.WriteFile(path.Join(userdir, "sshpiper_upstream"), []byte("user@host-password:2222"), 0400); err != nil { + if err := os.WriteFile(path.Join(userdir, "sshpiper_upstream"), []byte("user@host-password:2222"), 0o400); err != nil { t.Errorf("failed to write upstream file: %v", err) } } @@ -138,12 +134,11 @@ func TestFailtobanPipeCreateFail(t *testing.T) { "2222", "host-password", ) - if err != nil { t.Errorf("failed to run ssh-keyscan: %v", err) } - if err := os.WriteFile(path.Join(userdir, "known_hosts"), b, 0400); err != nil { + if err := os.WriteFile(path.Join(userdir, "known_hosts"), b, 0o400); err != nil { t.Errorf("failed to write known_hosts: %v", err) } } @@ -166,7 +161,6 @@ func TestFailtobanPipeCreateFail(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-workingdir, %v", err) } @@ -181,7 +175,6 @@ func TestFailtobanPipeCreateFail(t *testing.T) { } { - // run 5 times to trigger ban for i := 0; i < 3; i++ { c, stdin, stdout, err := runCmd( @@ -197,7 +190,6 @@ func TestFailtobanPipeCreateFail(t *testing.T) { fmt.Sprintf("notexist_%v", i), "127.0.0.1", ) - if err != nil { t.Errorf("ssh fail") } @@ -222,7 +214,6 @@ func TestFailtobanPipeCreateFail(t *testing.T) { "bypassword", "127.0.0.1", ) - if err != nil { t.Errorf("failed to ssh to workingdir, %v", err) } @@ -257,7 +248,6 @@ func TestFailtobanIgnoreIP(t *testing.T) { "--ignore-ip", "127.0.0.1", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -284,7 +274,6 @@ func TestFailtobanIgnoreIP(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-workingdir, %v", err) } @@ -342,7 +331,6 @@ func TestFailtobanIgnoreIP(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-workingdir, %v", err) } @@ -373,7 +361,6 @@ func TestFailtobanIgnoreCIDR(t *testing.T) { "--ignore-ip", "127.0.0.1/8", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -400,7 +387,6 @@ func TestFailtobanIgnoreCIDR(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-workingdir, %v", err) } @@ -458,7 +444,6 @@ func TestFailtobanIgnoreCIDR(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-workingdir, %v", err) } diff --git a/e2e/fixed_test.go b/e2e/fixed_test.go index 89263b96..586ed452 100644 --- a/e2e/fixed_test.go +++ b/e2e/fixed_test.go @@ -11,7 +11,6 @@ import ( ) func TestOldSshd(t *testing.T) { - piperaddr, piperport := nextAvailablePiperAddress() piper, _, _, err := runCmd("/sshpiperd/sshpiperd", @@ -21,7 +20,6 @@ func TestOldSshd(t *testing.T) { "--target", "host-password-old:2222", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -63,7 +61,6 @@ func TestOldSshd(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo SSHREADY && sleep 1 && echo -n %v > /shared/%v"`, randtext, targetfie), // sleep 5 to cover https://github.com/tg123/sshpiper/issues/323 ) - if err != nil { t.Errorf("failed to ssh to piper-fixed, %v", err) } @@ -81,7 +78,6 @@ func TestOldSshd(t *testing.T) { checkSharedFileContent(t, targetfie, randtext) }) } - } func TestHostkeyParam(t *testing.T) { @@ -97,7 +93,6 @@ func TestHostkeyParam(t *testing.T) { "--target", "host-password:2222", ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } diff --git a/e2e/grpcplugin_test.go b/e2e/grpcplugin_test.go index bf4a1999..a2a26310 100644 --- a/e2e/grpcplugin_test.go +++ b/e2e/grpcplugin_test.go @@ -115,7 +115,6 @@ func createRpcServer(r *rpcServer) net.Listener { } func TestGrpcPlugin(t *testing.T) { - privateKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { t.Fatalf("failed to generate private key: %v", err) @@ -180,7 +179,6 @@ func TestGrpcPlugin(t *testing.T) { "--testremotekey", base64.StdEncoding.EncodeToString(privKeyPem), ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -196,7 +194,6 @@ func TestGrpcPlugin(t *testing.T) { }, HostKeyCallback: ssh.InsecureIgnoreHostKey(), }) - if err != nil { t.Fatalf("failed to connect to sshpiperd: %v", err) } diff --git a/e2e/kubernetes_test.go b/e2e/kubernetes_test.go index f4d5437b..a11fb868 100644 --- a/e2e/kubernetes_test.go +++ b/e2e/kubernetes_test.go @@ -44,7 +44,6 @@ func TestKubernetes(t *testing.T) { for _, testcase := range pubkeycases { t.Run(testcase.title, func(t *testing.T) { - keyfiledir, err := os.MkdirTemp("", "") if err != nil { t.Errorf("failed to create temp key file: %v", err) @@ -52,11 +51,11 @@ func TestKubernetes(t *testing.T) { keyfile := path.Join(keyfiledir, "key") - if err := os.WriteFile(keyfile, []byte(testprivatekey), 0400); err != nil { + if err := os.WriteFile(keyfile, []byte(testprivatekey), 0o400); err != nil { t.Errorf("failed to write to test key: %v", err) } - if err := os.WriteFile("/publickey_authorized_keys/authorized_keys", []byte(`ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINRGTH325rDUp12tplwukHmR8ytbC9TPZ886gCstynP1`), 0400); err != nil { + if err := os.WriteFile("/publickey_authorized_keys/authorized_keys", []byte(`ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINRGTH325rDUp12tplwukHmR8ytbC9TPZ886gCstynP1`), 0o400); err != nil { t.Errorf("failed to write to authorized_keys: %v", err) } @@ -79,7 +78,6 @@ func TestKubernetes(t *testing.T) { piperhost, fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-fixed, %v", err) } @@ -115,7 +113,6 @@ func TestKubernetes(t *testing.T) { } for _, testcase := range passwordcases { - t.Run(testcase.title, func(t *testing.T) { randtext := uuid.New().String() targetfie := uuid.New().String() @@ -134,7 +131,6 @@ func TestKubernetes(t *testing.T) { piperhost, fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-fixed, %v", err) } @@ -147,7 +143,6 @@ func TestKubernetes(t *testing.T) { checkSharedFileContent(t, targetfie, randtext) }) - } { @@ -189,7 +184,6 @@ func TestKubernetes(t *testing.T) { piperhost, fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-fixed, %v", err) } diff --git a/e2e/main_test.go b/e2e/main_test.go index 4a342959..d70558ab 100644 --- a/e2e/main_test.go +++ b/e2e/main_test.go @@ -140,7 +140,6 @@ func killCmd(c *exec.Cmd) { func runAndGetStdout(cmd string, args ...string) ([]byte, error) { c, _, stdout, err := runCmd(cmd, args...) - if err != nil { return nil, err } @@ -167,7 +166,6 @@ func nextAvailablePiperAddress() (string, string) { } func TestMain(m *testing.M) { - if os.Getenv("SSHPIPERD_E2E_TEST") != "1" { log.Printf("skipping e2e test") os.Exit(0) diff --git a/e2e/testplugin/testgetmetaplugin/main.go b/e2e/testplugin/testgetmetaplugin/main.go index 649c8e9f..74e9e41a 100644 --- a/e2e/testplugin/testgetmetaplugin/main.go +++ b/e2e/testplugin/testgetmetaplugin/main.go @@ -9,14 +9,11 @@ import ( ) func main() { - libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ Name: "getmeta", CreateConfig: func(c *cli.Context) (*libplugin.SshPiperPluginConfig, error) { - return &libplugin.SshPiperPluginConfig{ PasswordCallback: func(conn libplugin.ConnMetadata, password []byte) (*libplugin.Upstream, error) { - target := conn.GetMeta("targetaddr") host, port, err := libplugin.SplitHostPortForSSH(target) diff --git a/e2e/testplugin/testgrpcplugin/main.go b/e2e/testplugin/testgrpcplugin/main.go index 8bd1c407..646404a6 100644 --- a/e2e/testplugin/testgrpcplugin/main.go +++ b/e2e/testplugin/testgrpcplugin/main.go @@ -14,7 +14,6 @@ import ( ) func main() { - libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ Name: "testplugin", Usage: "e2e test plugin only", @@ -33,7 +32,6 @@ func main() { }, }, CreateConfig: func(c *cli.Context) (*libplugin.SshPiperPluginConfig, error) { - rpcclient, err := rpc.DialHTTP("tcp", c.String("rpcserver")) if err != nil { return nil, err diff --git a/e2e/testplugin/testsetmetaplugin/main.go b/e2e/testplugin/testsetmetaplugin/main.go index c19c6b6d..c19bc8d1 100644 --- a/e2e/testplugin/testsetmetaplugin/main.go +++ b/e2e/testplugin/testsetmetaplugin/main.go @@ -8,7 +8,6 @@ import ( ) func main() { - libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ Name: "setmeta", Flags: []cli.Flag{ @@ -19,9 +18,7 @@ func main() { }, CreateConfig: func(ctx *cli.Context) (*libplugin.SshPiperPluginConfig, error) { return &libplugin.SshPiperPluginConfig{ - NoClientAuthCallback: func(conn libplugin.ConnMetadata) (*libplugin.Upstream, error) { - return &libplugin.Upstream{ Auth: libplugin.CreateNextPluginAuth(map[string]string{ "targetaddr": ctx.String("targetaddr"), diff --git a/e2e/workingdir_test.go b/e2e/workingdir_test.go index 6f9a0263..2dbfaada 100644 --- a/e2e/workingdir_test.go +++ b/e2e/workingdir_test.go @@ -14,14 +14,13 @@ import ( const workingdir = "/shared/workingdir" func ensureWorkingDirectory() { - err := os.MkdirAll(workingdir, 0700) + err := os.MkdirAll(workingdir, 0o700) if err != nil { log.Panicf("failed to create working directory %s: %v", workingdir, err) } } func TestWorkingDirectory(t *testing.T) { - piperaddr, piperport := nextAvailablePiperAddress() piper, _, _, err := runCmd("/sshpiperd/sshpiperd", @@ -31,7 +30,6 @@ func TestWorkingDirectory(t *testing.T) { "--root", workingdir, ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -46,11 +44,11 @@ func TestWorkingDirectory(t *testing.T) { userdir := path.Join(workingdir, "bypassword") { - if err := os.MkdirAll(userdir, 0700); err != nil { + if err := os.MkdirAll(userdir, 0o700); err != nil { t.Errorf("failed to create working directory %s: %v", userdir, err) } - if err := os.WriteFile(path.Join(userdir, "sshpiper_upstream"), []byte("user@host-password:2222"), 0400); err != nil { + if err := os.WriteFile(path.Join(userdir, "sshpiper_upstream"), []byte("user@host-password:2222"), 0o400); err != nil { t.Errorf("failed to write upstream file: %v", err) } } @@ -62,12 +60,11 @@ func TestWorkingDirectory(t *testing.T) { "2222", "host-password", ) - if err != nil { t.Errorf("failed to run ssh-keyscan: %v", err) } - if err := os.WriteFile(path.Join(userdir, "known_hosts"), b, 0400); err != nil { + if err := os.WriteFile(path.Join(userdir, "known_hosts"), b, 0o400); err != nil { t.Errorf("failed to write known_hosts: %v", err) } } @@ -90,7 +87,6 @@ func TestWorkingDirectory(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-workingdir, %v", err) } @@ -107,11 +103,11 @@ func TestWorkingDirectory(t *testing.T) { t.Run("bypublickey", func(t *testing.T) { userdir := path.Join(workingdir, "bypublickey") - if err := os.MkdirAll(userdir, 0700); err != nil { + if err := os.MkdirAll(userdir, 0o700); err != nil { t.Errorf("failed to create working directory %s: %v", userdir, err) } - if err := os.WriteFile(path.Join(userdir, "sshpiper_upstream"), []byte("user@host-publickey:2222"), 0400); err != nil { + if err := os.WriteFile(path.Join(userdir, "sshpiper_upstream"), []byte("user@host-publickey:2222"), 0o400); err != nil { t.Errorf("failed to write upstream file: %v", err) } @@ -122,12 +118,11 @@ func TestWorkingDirectory(t *testing.T) { "2222", "host-publickey", ) - if err != nil { t.Errorf("failed to run ssh-keyscan: %v", err) } - if err := os.WriteFile(path.Join(userdir, "known_hosts"), b, 0400); err != nil { + if err := os.WriteFile(path.Join(userdir, "known_hosts"), b, 0o400); err != nil { t.Errorf("failed to write known_hosts: %v", err) } } @@ -214,7 +209,6 @@ func TestWorkingDirectory(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper-workingdir, %v", err) } @@ -225,6 +219,5 @@ func TestWorkingDirectory(t *testing.T) { checkSharedFileContent(t, targetfie, randtext) } - }) } diff --git a/e2e/yaml_test.go b/e2e/yaml_test.go index 818ea143..feb643c4 100644 --- a/e2e/yaml_test.go +++ b/e2e/yaml_test.go @@ -83,13 +83,12 @@ pipes: ` func TestYaml(t *testing.T) { - yamldir, err := os.MkdirTemp("", "") if err != nil { t.Fatalf("Failed to create temp dir: %v", err) } - yamlfile, err := os.OpenFile(path.Join(yamldir, "config.yaml"), os.O_RDWR|os.O_CREATE, 0400) + yamlfile, err := os.OpenFile(path.Join(yamldir, "config.yaml"), os.O_RDWR|os.O_CREATE, 0o400) if err != nil { t.Fatalf("Failed to create temp file: %v", err) } @@ -192,7 +191,6 @@ func TestYaml(t *testing.T) { "2222", "host-publickey", ) - if err != nil { t.Errorf("failed to run ssh-keyscan: %v", err) } @@ -203,7 +201,6 @@ func TestYaml(t *testing.T) { "2222", "host-password", ) - if err != nil { t.Errorf("failed to run ssh-keyscan : %v", err) } @@ -241,7 +238,6 @@ func TestYaml(t *testing.T) { "--config", yamlfile.Name(), ) - if err != nil { t.Errorf("failed to run sshpiperd: %v", err) } @@ -267,7 +263,6 @@ func TestYaml(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -299,7 +294,6 @@ func TestYaml(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -331,7 +325,6 @@ func TestYaml(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -365,7 +358,6 @@ func TestYaml(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -397,7 +389,6 @@ func TestYaml(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -448,7 +439,6 @@ func TestYaml(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -482,7 +472,6 @@ func TestYaml(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -514,7 +503,6 @@ func TestYaml(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } @@ -544,7 +532,6 @@ func TestYaml(t *testing.T) { "127.0.0.1", fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie), ) - if err != nil { t.Errorf("failed to ssh to piper, %v", err) } diff --git a/libplugin/ioconn/conn_test.go b/libplugin/ioconn/conn_test.go index 3ca1d347..b2c4703b 100644 --- a/libplugin/ioconn/conn_test.go +++ b/libplugin/ioconn/conn_test.go @@ -17,7 +17,6 @@ func TestDial(t *testing.T) { defer conn.Close() go func() { - _, _ = conn.Write([]byte("hello")) }() buf := make([]byte, 5) diff --git a/libplugin/pluginbase.go b/libplugin/pluginbase.go index fa95e612..059785c0 100644 --- a/libplugin/pluginbase.go +++ b/libplugin/pluginbase.go @@ -164,7 +164,6 @@ func (s *server) Logs(req *StartLogRequest, stream SshPiperPlugin_LogsServer) er } func (s *server) ListCallbacks(ctx context.Context, req *ListCallbackRequest) (*ListCallbackResponse, error) { - var cb []string if s.config.NewConnectionCallback != nil { @@ -362,7 +361,6 @@ func (s *server) KeyboardInteractiveAuth(stream SshPiperPlugin_KeyboardInteracti return userInput.Answers[0], nil }) - if err != nil { return err } diff --git a/plugin/failtoban/main.go b/plugin/failtoban/main.go index c6a0a5a0..b0a44b06 100644 --- a/plugin/failtoban/main.go +++ b/plugin/failtoban/main.go @@ -4,7 +4,6 @@ package main import ( "fmt" - "go4.org/netipx" "net" "net/netip" "os" @@ -13,6 +12,8 @@ import ( "syscall" "time" + "go4.org/netipx" + gocache "github.com/patrickmn/go-cache" log "github.com/sirupsen/logrus" "github.com/tg123/sshpiper/libplugin" @@ -20,7 +21,6 @@ import ( ) func main() { - libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ Name: "failtoban", Usage: "failtoban plugin, block ip after too many auth failures", @@ -51,7 +51,6 @@ func main() { }, }, CreateConfig: func(c *cli.Context) (*libplugin.SshPiperPluginConfig, error) { - maxFailures := c.Int("max-failures") banDuration := c.Duration("ban-duration") logOnly := c.Bool("log-only") @@ -133,7 +132,6 @@ func main() { } func buildIPSet(cidrs []string) *netipx.IPSet { - var ipsetBuilder netipx.IPSetBuilder for _, cidr := range cidrs { diff --git a/plugin/fixed/main.go b/plugin/fixed/main.go index ff928709..8f7bfb9a 100644 --- a/plugin/fixed/main.go +++ b/plugin/fixed/main.go @@ -9,7 +9,6 @@ import ( ) func main() { - libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ Name: "fixed", Usage: "sshpiperd fixed plugin, only password auth is supported", diff --git a/plugin/simplemath/main.go b/plugin/simplemath/main.go index 962a456b..f3faf170 100644 --- a/plugin/simplemath/main.go +++ b/plugin/simplemath/main.go @@ -13,7 +13,6 @@ import ( ) func main() { - libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ Name: "simplemath", Usage: "sshpiperd simplemath plugin, do math before ssh login", diff --git a/plugin/username-router/main.go b/plugin/username-router/main.go index f3092c50..b861e5d2 100644 --- a/plugin/username-router/main.go +++ b/plugin/username-router/main.go @@ -25,15 +25,12 @@ func parseTargetUser(raw string) (target string, username string, err error) { } func main() { - libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{ Name: "username-router", Usage: "routing based on target inside username, format: 'target:port+realuser@sshpiper-host'", CreateConfig: func(c *cli.Context) (*libplugin.SshPiperPluginConfig, error) { - return &libplugin.SshPiperPluginConfig{ PasswordCallback: func(conn libplugin.ConnMetadata, password []byte) (*libplugin.Upstream, error) { - address, user, err := parseTargetUser(conn.User()) if err != nil { return nil, fmt.Errorf("invalid username format %q: %w", conn.User(), err) diff --git a/plugin/yaml/yaml.go b/plugin/yaml/yaml.go index f7f37c6b..23b01a04 100644 --- a/plugin/yaml/yaml.go +++ b/plugin/yaml/yaml.go @@ -107,7 +107,7 @@ func (p *plugin) checkPerm(filename string) error { return nil } - if fi.Mode().Perm()&0077 != 0 { + if fi.Mode().Perm()&0o077 != 0 { return fmt.Errorf("%v's perm is too open", filename) } diff --git a/plugin/yaml/yaml_test.go b/plugin/yaml/yaml_test.go index d708263b..d60fdc8c 100644 --- a/plugin/yaml/yaml_test.go +++ b/plugin/yaml/yaml_test.go @@ -54,5 +54,4 @@ func TestYamlDecode(t *testing.T) { if err != nil { t.Fatalf("Failed to unmarshal yaml: %v", err) } - }