add banner opt back (#106)

* add banner opt back

* happy lint
This commit is contained in:
Boshi Lian 2022-11-28 05:56:12 -08:00 committed by GitHub
parent 3232bee379
commit 01e5a009e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 158 additions and 5 deletions

View file

@ -56,6 +56,23 @@ func newDaemon(ctx *cli.Context) (*daemon, error) {
return nil, fmt.Errorf("failed to listen for connection: %v", err)
}
bannertext := ctx.String("banner-text")
bannerfile := ctx.String("banner-file")
if bannertext != "" || bannerfile != "" {
config.BannerCallback = func(_ ssh.ConnMetadata, _ ssh.ChallengeContext) string {
if bannerfile != "" {
text, err := os.ReadFile(bannerfile)
if err != nil {
log.Warnf("cannot read banner file %v: %v", bannerfile, err)
} else {
return string(text)
}
}
return bannertext
}
}
return &daemon{
config: config,
lis: lis,

View file

@ -115,6 +115,18 @@ func main() {
Usage: "create typescript format screen recording and save into the directory see https://linux.die.net/man/1/script",
EnvVars: []string{"SSHPIPERD_TYPESCRIPT_LOG_DIR"},
},
&cli.StringFlag{
Name: "banner-text",
Value: "",
Usage: "display a banner before authentication, would be ignored if banner file was set",
EnvVars: []string{"SSHPIPERD_BANNERTEXT"},
},
&cli.StringFlag{
Name: "banner-file",
Value: "",
Usage: "display a banner from file before authentication",
EnvVars: []string{"SSHPIPERD_BANNERFILE"},
},
},
Action: func(ctx *cli.Context) error {
level, err := log.ParseLevel(ctx.String("log-level"))

118
e2e/banner_test.go Normal file
View file

@ -0,0 +1,118 @@
package e2e_test
import (
"os"
"testing"
"github.com/google/uuid"
)
func TestBanner(t *testing.T) {
t.Run("args", func(t *testing.T) {
piperaddr, piperport := nextAvailablePiperAddress()
randtext := uuid.New().String()
piper, _, _, err := runCmd("/sshpiperd/sshpiperd",
"--banner-text",
randtext,
"-p",
piperport,
"/sshpiperd/plugins/fixed",
"--target",
"host-password:2222",
)
if err != nil {
t.Errorf("failed to run sshpiperd: %v", err)
}
defer killCmd(piper)
waitForEndpointReady(piperaddr)
c, _, stdout, err := runCmd(
"ssh",
"-v",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
"-p",
piperport,
"-l",
"user",
"127.0.0.1",
)
if err != nil {
t.Errorf("failed to ssh to piper, %v", err)
}
defer killCmd(c)
waitForStdoutContains(stdout, randtext, func(_ string) {
})
})
t.Run("file", func(t *testing.T) {
piperaddr, piperport := nextAvailablePiperAddress()
randtext := uuid.New().String()
bannerfile, err := os.CreateTemp("", "banner")
if err != nil {
t.Errorf("failed to create temp file: %v", err)
}
defer os.Remove(bannerfile.Name())
if _, err := bannerfile.WriteString(randtext); err != nil {
t.Errorf("failed to write to temp file: %v", err)
}
if err := bannerfile.Close(); err != nil {
t.Errorf("failed to close temp file: %v", err)
}
piper, _, _, err := runCmd("/sshpiperd/sshpiperd",
"--banner-file",
bannerfile.Name(),
"-p",
piperport,
"/sshpiperd/plugins/fixed",
"--target",
"host-password:2222",
)
if err != nil {
t.Errorf("failed to run sshpiperd: %v", err)
}
defer killCmd(piper)
waitForEndpointReady(piperaddr)
c, _, stdout, err := runCmd(
"ssh",
"-v",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
"-p",
piperport,
"-l",
"user",
"127.0.0.1",
)
if err != nil {
t.Errorf("failed to ssh to piper, %v", err)
}
defer killCmd(c)
waitForStdoutContains(stdout, randtext, func(_ string) {
})
})
}

View file

@ -113,21 +113,20 @@ func runCmdAndWait(cmd string, args ...string) error {
return c.Wait()
}
func enterPassword(stdin io.Writer, stdout io.Reader, password string) {
func waitForStdoutContains(stdout io.Reader, text string, cb func(string)) {
st := time.Now()
for {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "'s password") {
_, _ = stdin.Write([]byte(fmt.Sprintf("%v\n", password)))
log.Printf("got password prompt, sending password")
if strings.Contains(line, text) {
cb(line)
return
}
}
if time.Since(st) > waitTimeout {
log.Panic("timeout waiting for password prompt")
log.Panicf("timeout waiting for [%s] from prompt", text)
return
}
@ -135,6 +134,13 @@ func enterPassword(stdin io.Writer, stdout io.Reader, password string) {
}
}
func enterPassword(stdin io.Writer, stdout io.Reader, password string) {
waitForStdoutContains(stdout, "'s password", func(_ string) {
_, _ = stdin.Write([]byte(fmt.Sprintf("%v\n", password)))
log.Printf("got password prompt, sending password")
})
}
func checkSharedFileContent(t *testing.T, targetfie string, expected string) {
f, err := os.Open(fmt.Sprintf("/shared/%v", targetfie))
if err != nil {