Add PipeCreateErrorCallback to handle pipe creation errors (#290)

* Add PipeCreateErrorCallback to handle pipe creation errors

* Add test for FailtobanPipeCreateFail

* Fix missing newline at end of file in pluginbase.go
This commit is contained in:
Boshi Lian 2023-12-24 03:54:49 -08:00 committed by GitHub
parent 295262fe68
commit 9fee039d93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 499 additions and 117 deletions

View file

@ -196,9 +196,17 @@ func (d *daemon) run() error {
case p = <-pipec:
case err := <-errorc:
log.Debugf("connection from %v establishing failed reason: %v", c.RemoteAddr(), err)
if d.config.PipeCreateErrorCallback != nil {
d.config.PipeCreateErrorCallback(c, err)
}
return
case <-time.After(d.loginGraceTime):
log.Debugf("pipe establishing timeout, disconnected connection from %v", c.RemoteAddr())
if d.config.PipeCreateErrorCallback != nil {
d.config.PipeCreateErrorCallback(c, fmt.Errorf("pipe establishing timeout"))
}
return
}

View file

@ -2,6 +2,7 @@ package plugin
import (
"fmt"
"net"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
@ -157,5 +158,13 @@ func (cp *ChainPlugins) InstallPiperConfig(config *GrpcPluginConfig) error {
}
}
config.PipeCreateErrorCallback = func(conn net.Conn, err error) {
for _, p := range cp.pluginsCallback {
if p.PipeCreateErrorCallback != nil {
p.PipeCreateErrorCallback(conn, err)
}
}
}
return nil
}

View file

@ -22,8 +22,9 @@ import (
type GrpcPluginConfig struct {
ssh.PiperConfig
PipeStartCallback func(conn ssh.ConnMetadata, challengeCtx ssh.ChallengeContext)
PipeErrorCallback func(conn ssh.ConnMetadata, challengeCtx ssh.ChallengeContext, err error)
PipeCreateErrorCallback func(conn net.Conn, err error)
PipeStartCallback func(conn ssh.ConnMetadata, challengeCtx ssh.ChallengeContext)
PipeErrorCallback func(conn ssh.ConnMetadata, challengeCtx ssh.ChallengeContext, err error)
}
type GrpcPlugin struct {
@ -130,6 +131,8 @@ func (g *GrpcPlugin) InstallPiperConfig(config *GrpcPluginConfig) error {
config.PipeStartCallback = g.PipeStartCallback
case "PipeError":
config.PipeErrorCallback = g.PipeErrorCallback
case "PipeCreateError":
config.PipeCreateErrorCallback = g.PipeCreateErrorCallback
default:
return fmt.Errorf("unknown callback %s", c)
}
@ -503,6 +506,13 @@ func (g *GrpcPlugin) BannerCallback(conn ssh.ConnMetadata, challengeCtx ssh.Chal
return reply.GetMessage()
}
func (g *GrpcPlugin) PipeCreateErrorCallback(conn net.Conn, err error) {
_, _ = g.client.PipeCreateErrorNotice(context.Background(), &libplugin.PipeCreateErrorNoticeRequest{
FromAddr: conn.RemoteAddr().String(),
Error: err.Error(),
})
}
func (g *GrpcPlugin) PipeStartCallback(conn ssh.ConnMetadata, challengeCtx ssh.ChallengeContext) {
meta := toMeta(challengeCtx, conn)
_, _ = g.client.PipeStartNotice(context.Background(), &libplugin.PipeStartNoticeRequest{