From fe52c1a797bd829d6728afdbfddb0a888e78d721 Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Mon, 17 Jan 2022 16:24:30 -0300 Subject: [PATCH 1/2] refactor: fix all linter issues Signed-off-by: Carlos A Becker --- accesscontrol/accesscontrol.go | 2 +- activeterm/activeterm.go | 4 ++-- bubbletea/tea.go | 4 ++-- git/git.go | 36 ++++++++++++++++++++++------------ testsession/testsession.go | 10 ++++++---- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/accesscontrol/accesscontrol.go b/accesscontrol/accesscontrol.go index 5b0661a..a268577 100644 --- a/accesscontrol/accesscontrol.go +++ b/accesscontrol/accesscontrol.go @@ -24,7 +24,7 @@ func Middleware(cmds ...string) wish.Middleware { } } fmt.Fprintln(s, "Command is not allowed: "+s.Command()[0]) - s.Exit(1) + s.Exit(1) // nolint: errcheck } } } diff --git a/activeterm/activeterm.go b/activeterm/activeterm.go index 2a42300..38cd2e5 100644 --- a/activeterm/activeterm.go +++ b/activeterm/activeterm.go @@ -15,8 +15,8 @@ func Middleware() wish.Middleware { _, _, active := s.Pty() if !active { fmt.Fprintln(s, "Requires an active PTY") - s.Exit(1) - return + s.Exit(1) // nolint: errcheck + return // unreachable } sh(s) } diff --git a/bubbletea/tea.go b/bubbletea/tea.go index 53024a7..5157fc7 100644 --- a/bubbletea/tea.go +++ b/bubbletea/tea.go @@ -15,12 +15,12 @@ import ( // start it with the tea.ProgramOptions returned. // // Deprecated: use Handler instead. -type BubbleTeaHandler func(ssh.Session) (tea.Model, []tea.ProgramOption) // nolint: revive +type BubbleTeaHandler = Handler // nolint: revive // Hander is the function Bubble Tea apps implement to hook into the // SSH Middleware. This will create a new tea.Program for every connection and // start it with the tea.ProgramOptions returned. -type Handler = BubbleTeaHandler +type Handler func(ssh.Session) (tea.Model, []tea.ProgramOption) // Middleware takes a Handler and hooks the input and output for the // ssh.Session into the tea.Program. It also captures window resize events and diff --git a/git/git.go b/git/git.go index fe82499..139bbd4 100644 --- a/git/git.go +++ b/git/git.go @@ -1,7 +1,6 @@ package git import ( - "context" "fmt" "os" "os/exec" @@ -19,16 +18,23 @@ var ErrNotAuthed = fmt.Errorf("you are not authorized to do this") // ErrSystemMalfunction represents a general system error returned to clients. var ErrSystemMalfunction = fmt.Errorf("something went wrong") -// ErrInvalidRepo represents an attempt to access a non-existent repo +// ErrInvalidRepo represents an attempt to access a non-existent repo. var ErrInvalidRepo = fmt.Errorf("invalid repo") // AccessLevel is the level of access allowed to a repo. type AccessLevel int const ( + // NoAccess does not allow access to the repo. NoAccess AccessLevel = iota + + // ReadOnlyAccess allows read-only access to the repo. ReadOnlyAccess + + // ReadWriteAccess allows read and write access to the repo. ReadWriteAccess + + // AdminAccess allows read, write, and admin access to the repo. AdminAccess ) @@ -36,18 +42,26 @@ const ( // implementations and post push/fetch notifications. Prior to git access, // AuthRepo will be called with the ssh.Session public key and the repo name. // Implementers return the appropriate AccessLevel. -type GitHooks interface { +// +// Deprecated: use Hooks instead. +type GitHooks = Hooks // nolint: revive + +// Hooks is an interface that allows for custom authorization +// implementations and post push/fetch notifications. Prior to git access, +// AuthRepo will be called with the ssh.Session public key and the repo name. +// Implementers return the appropriate AccessLevel. +type Hooks interface { AuthRepo(string, ssh.PublicKey) AccessLevel Push(string, ssh.PublicKey) Fetch(string, ssh.PublicKey) } // Middleware adds Git server functionality to the ssh.Server. Repos are stored -// in the specified repo directory. The provided GitHooks implementation will be +// in the specified repo directory. The provided Hooks implementation will be // checked for access on a per repo basis for a ssh.Session public key. -// GitHooks.Push and GitHooks.Fetch will be called on successful completion of +// Hooks.Push and Hooks.Fetch will be called on successful completion of // their commands. -func Middleware(repoDir string, gh GitHooks) wish.Middleware { +func Middleware(repoDir string, gh Hooks) wish.Middleware { return func(sh ssh.Handler) ssh.Handler { return func(s ssh.Session) { cmd := s.Command() @@ -94,8 +108,7 @@ func Middleware(repoDir string, gh GitHooks) wish.Middleware { } func gitReceivePack(s ssh.Session, gitCmd string, repoDir string, repo string) error { - ctx := s.Context() - err := ensureRepo(ctx, repoDir, repo) + err := ensureRepo(repoDir, repo) if err != nil { return err } @@ -147,10 +160,10 @@ func fatalGit(s ssh.Session, err error) { msg := err.Error() pktLine := fmt.Sprintf("%04x%s\n", len(msg)+5, msg) _, _ = s.Write([]byte(pktLine)) - s.Exit(1) + s.Exit(1) // nolint: errcheck } -func ensureRepo(ctx context.Context, dir string, repo string) error { +func ensureRepo(dir string, repo string) error { exists, err := fileExists(dir) if err != nil { return err @@ -180,8 +193,7 @@ func runCmd(s ssh.Session, dir, name string, args ...string) error { usi.Dir = dir usi.Stdout = s usi.Stdin = s - err := usi.Run() - if err != nil { + if err := usi.Run(); err != nil { return err } return nil diff --git a/testsession/testsession.go b/testsession/testsession.go index af8c85f..7a041a0 100644 --- a/testsession/testsession.go +++ b/testsession/testsession.go @@ -21,7 +21,9 @@ func New(tb testing.TB, srv *ssh.Server, cfg *gossh.ClientConfig) *gossh.Session tb.Fatalf("failed to serve: %v", err) } }() - tb.Cleanup(func() { srv.Close() }) + tb.Cleanup(func() { + srv.Close() // nolint: errcheck + }) return newClientSession(tb, l.Addr().String(), cfg) } @@ -47,7 +49,7 @@ func newClientSession(tb testing.TB, addr string, config *gossh.ClientConfig) *g } } if config.HostKeyCallback == nil { - config.HostKeyCallback = gossh.InsecureIgnoreHostKey() + config.HostKeyCallback = gossh.InsecureIgnoreHostKey() // nolint: gosec } client, err := gossh.Dial("tcp", addr, config) if err != nil { @@ -58,8 +60,8 @@ func newClientSession(tb testing.TB, addr string, config *gossh.ClientConfig) *g tb.Fatal(err) } tb.Cleanup(func() { - session.Close() - client.Close() + session.Close() // nolint: errcheck + client.Close() // nolint: errcheck }) return session } From ca573a9ddab01ea594998250195821184e818d6a Mon Sep 17 00:00:00 2001 From: Carlos A Becker Date: Mon, 17 Jan 2022 16:27:00 -0300 Subject: [PATCH 2/2] fix: typo Signed-off-by: Carlos A Becker --- bubbletea/tea.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bubbletea/tea.go b/bubbletea/tea.go index 5157fc7..b3b6607 100644 --- a/bubbletea/tea.go +++ b/bubbletea/tea.go @@ -10,14 +10,14 @@ import ( "github.com/muesli/termenv" ) -// BubbleTeaHander is the function Bubble Tea apps implement to hook into the +// BubbleTeaHandler is the function Bubble Tea apps implement to hook into the // SSH Middleware. This will create a new tea.Program for every connection and // start it with the tea.ProgramOptions returned. // // Deprecated: use Handler instead. type BubbleTeaHandler = Handler // nolint: revive -// Hander is the function Bubble Tea apps implement to hook into the +// Handler is the function Bubble Tea apps implement to hook into the // SSH Middleware. This will create a new tea.Program for every connection and // start it with the tea.ProgramOptions returned. type Handler func(ssh.Session) (tea.Model, []tea.ProgramOption)