move e2e piper process manager to go code

This commit is contained in:
Boshi Lian 2022-07-08 00:05:01 +00:00
parent 57a57e89ef
commit 5e9f689999
8 changed files with 88 additions and 65 deletions

View file

@ -10,22 +10,20 @@ COPY go.mod go.sum /cache/
WORKDIR /cache
RUN go mod download
RUN mkdir -p /out/plugins
RUN mkdir -p /sshpiperd/plugins
ADD . /src/
WORKDIR /src
RUN --mount=type=cache,target=/root/.cache/go-build go build -o /out -ldflags "-X main.mainver=$VER" ./cmd/...
RUN --mount=type=cache,target=/root/.cache/go-build go build -o /out/plugins -ldflags "-X main.mainver=$VER" ./plugin/...
RUN --mount=type=cache,target=/root/.cache/go-build go build -o /sshpiperd -ldflags "-X main.mainver=$VER" ./cmd/...
RUN --mount=type=cache,target=/root/.cache/go-build go build -o /sshpiperd/plugins -ldflags "-X main.mainver=$VER" ./plugin/...
ADD entrypoint.sh /sshpiperd
FROM busybox
LABEL maintainer="Boshi Lian<farmer1992@gmail.com>"
COPY --from=ep76/openssh-static:latest /usr/bin/ssh-keygen /bin/ssh-keygen
RUN mkdir /etc/ssh/
RUN mkdir /sshpiperd
ADD entrypoint.sh /sshpiperd
COPY --from=builder /out/ /sshpiperd
COPY --from=builder /sshpiperd/ /sshpiperd
EXPOSE 2222
ENTRYPOINT ["/sshpiperd/entrypoint.sh"]
CMD ["/sshpiperd/sshpiperd", "/sshpiperd/plugins/workingdir"]
CMD ["/sshpiperd/entrypoint.sh"]

View file

@ -1,11 +0,0 @@
FROM golang:1.18-bullseye as builder
ARG VER=devel
ENV CGO_ENABLED=0
RUN mkdir -p /cache/crypto
COPY crypto /cache/crypto
COPY go.mod go.sum /cache/
WORKDIR /cache
RUN go mod download

View file

@ -1,4 +1,4 @@
version: '3'
version: '3.4'
services:
host-password:
@ -19,50 +19,23 @@ services:
- shared:/shared
- sshconfig_publickey:/config
piper-fixed:
environment:
- SSHPIPERD_LOG_LEVEL=trace
build: ../
command:
- "/sshpiperd/sshpiperd"
- "/sshpiperd/plugins/fixed"
- "--target"
- "host-password:2222"
depends_on:
- host-password
piper-workingdir:
environment:
- SSHPIPERD_LOG_LEVEL=trace
- SSHPIPERD_WORKINGDIR_STRICTHOSTKEY=true
volumes:
- shared:/shared
build: ../
command:
- "/sshpiperd/sshpiperd"
- "/sshpiperd/plugins/workingdir"
- "--root"
- "/shared/workingdir"
depends_on:
- host-password
- host-publickey
testrunner:
environment:
- SSHPIPERD_LOG_LEVEL=trace
- SSHPIPERD_E2E_TEST=1
build:
context: ../
dockerfile: e2e/Dockerfile_e2e
target: builder
volumes:
- ..:/src
- shared:/shared
- sshconfig_publickey:/sshconfig_publickey
- sshconfig_password:/sshconfig_password
command: ["bash", "-c", "if [ \"${SSHPIPERD_DEBUG}\" == \"1\" ]; then sleep infinity; else go test -v; fi"]
command: ["./e2eentry.sh"]
working_dir: /src/e2e
depends_on:
- piper-fixed
- piper-workingdir
- host-publickey
- host-password
volumes:
shared:

10
e2e/e2eentry.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
# use entrypoint.sh to generate the ssh_host_rsa_key
PLUGIN="dummy_badname/" bash /sshpiperd/entrypoint.sh 2>/dev/null
if [ "${SSHPIPERD_DEBUG}" == "1" ]; then
sleep infinity;
else
go test -v;
fi

View file

@ -9,7 +9,24 @@ import (
)
func TestFixed(t *testing.T) {
waitForEndpointReady("piper-fixed:2222")
piperaddr, piperport := nextAvailablePiperAddress()
piper, _, _, err := runCmd("/sshpiperd/sshpiperd",
"-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)
randtext := uuid.New().String()
targetfie := uuid.New().String()
@ -22,10 +39,10 @@ func TestFixed(t *testing.T) {
"-o",
"UserKnownHostsFile=/dev/null",
"-p",
"2222",
piperport,
"-l",
"user",
"piper-fixed",
"127.0.0.1",
fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie),
)

View file

@ -11,6 +11,7 @@ import (
"net"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"testing"
@ -125,6 +126,20 @@ func runAndGetStdout(cmd string, args ...string) ([]byte, error) {
return io.ReadAll(stdout)
}
func nextAvaliablePort() int {
l, err := net.Listen("tcp", ":0")
if err != nil {
log.Panic(err)
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}
func nextAvailablePiperAddress() (string, string) {
port := strconv.Itoa(nextAvaliablePort())
return net.JoinHostPort("127.0.0.1", (port)), port
}
func TestMain(m *testing.M) {
if os.Getenv("SSHPIPERD_E2E_TEST") != "1" {

View file

@ -22,8 +22,26 @@ func ensureWorkingDirectory() {
}
func TestWorkingDirectory(t *testing.T) {
piperaddr, piperport := nextAvailablePiperAddress()
piper, _, _, err := runCmd("/sshpiperd/sshpiperd",
"-p",
piperport,
"/sshpiperd/plugins/workingdir",
"--root",
workingdir,
)
if err != nil {
t.Errorf("failed to run sshpiperd: %v", err)
}
defer killCmd(piper)
waitForEndpointReady(piperaddr)
ensureWorkingDirectory()
waitForEndpointReady("piper-workingdir:2222")
t.Run("bypassword", func(t *testing.T) {
userdir := path.Join(workingdir, "bypassword")
@ -67,10 +85,10 @@ func TestWorkingDirectory(t *testing.T) {
"-o",
"UserKnownHostsFile=/dev/null",
"-p",
"2222",
piperport,
"-l",
"bypassword",
"piper-workingdir",
"127.0.0.1",
fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie),
)
@ -189,12 +207,12 @@ func TestWorkingDirectory(t *testing.T) {
"-o",
"UserKnownHostsFile=/dev/null",
"-p",
"2222",
piperport,
"-l",
"bypublickey",
"-i",
path.Join(keydir, "id_rsa"),
"piper-workingdir",
"127.0.0.1",
fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie),
)

View file

@ -1,9 +1,12 @@
#!/bin/sh
set -euo pipefail
set -eo pipefail
if [ ! -f /etc/ssh/ssh_host_rsa_key ];then
ssh-keygen -t rsa -N '' -f /etc/ssh/ssh_host_rsa_key
if [ -z "$SSHPIPERD_SERVER_KEY" ]; then
if [ ! -f /etc/ssh/ssh_host_rsa_key ];then
ssh-keygen -t rsa -N '' -f /etc/ssh/ssh_host_rsa_key
fi
fi
exec "$@"
PLUGIN=${PLUGIN:-workingdir}
exec /sshpiperd/sshpiperd /sshpiperd/plugins/$PLUGIN