Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type PublicKey struct {

type PublicKeyContextKey struct{}
type UserContextKey struct{}
type RepositoryContextKey struct{}

const (
keyID = "key-id"
Expand All @@ -51,6 +52,7 @@ type SSH struct {
PublicKeyLookupFunc func(ctx context.Context, publicKeyPayload string) (*PublicKey, error)
PreLoginFunc func(ctx context.Context, metadata ssh.ConnMetadata) error
AuthoriseOperationFunc func(ctx context.Context, cmd *GitCommand) error
FinaliseFunc func(ctx context.Context)
}

func NewSSH(config Config) *SSH {
Expand Down Expand Up @@ -233,6 +235,9 @@ func (s SSH) handleExecRequest(ctx context.Context, ch ssh.Channel, req *ssh.Req
return fmt.Errorf("ssh: command failed: %w", err)
}

ctx = context.WithValue(ctx, RepositoryContextKey{}, gitcmd)
s.FinaliseFunc(ctx)

_, err = ch.SendRequest("exit-status", true, []byte{0, 0, 0, 0})

return
Expand Down Expand Up @@ -283,6 +288,10 @@ func (s SSH) defaultPreLoginFunc(ctx context.Context, metadata ssh.ConnMetadata)
return nil
}

func (s SSH) defaultFinaliseFunc(ctx context.Context) {
return
}

func (s *SSH) setup() error {
if s.sshconfig != nil {
return nil
Expand All @@ -307,6 +316,10 @@ func (s *SSH) setup() error {
s.PreLoginFunc = s.defaultPreLoginFunc
}

if s.FinaliseFunc == nil {
s.FinaliseFunc = s.defaultFinaliseFunc
}

config.PublicKeyCallback = func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
ctx := context.WithValue(context.Background(), UserContextKey{}, conn.User())
err := s.PreLoginFunc(ctx, conn)
Expand Down