From 5e9f689999cd98e3c6ea0d020dd9d5e82d07b314 Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Fri, 8 Jul 2022 00:05:01 +0000 Subject: [PATCH] move e2e piper process manager to go code --- Dockerfile | 14 ++++++-------- e2e/Dockerfile_e2e | 11 ----------- e2e/docker-compose.yml | 39 ++++++--------------------------------- e2e/e2eentry.sh | 10 ++++++++++ e2e/fixed_test.go | 23 ++++++++++++++++++++--- e2e/main_test.go | 15 +++++++++++++++ e2e/workingdir_test.go | 28 +++++++++++++++++++++++----- entrypoint.sh | 13 ++++++++----- 8 files changed, 88 insertions(+), 65 deletions(-) delete mode 100644 e2e/Dockerfile_e2e create mode 100755 e2e/e2eentry.sh diff --git a/Dockerfile b/Dockerfile index 1e10db6d..7d1b253e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" 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"] diff --git a/e2e/Dockerfile_e2e b/e2e/Dockerfile_e2e deleted file mode 100644 index 09d17927..00000000 --- a/e2e/Dockerfile_e2e +++ /dev/null @@ -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 \ No newline at end of file diff --git a/e2e/docker-compose.yml b/e2e/docker-compose.yml index be086aba..750b11eb 100644 --- a/e2e/docker-compose.yml +++ b/e2e/docker-compose.yml @@ -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: diff --git a/e2e/e2eentry.sh b/e2e/e2eentry.sh new file mode 100755 index 00000000..f1268d9f --- /dev/null +++ b/e2e/e2eentry.sh @@ -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 \ No newline at end of file diff --git a/e2e/fixed_test.go b/e2e/fixed_test.go index fd3d6741..87479277 100644 --- a/e2e/fixed_test.go +++ b/e2e/fixed_test.go @@ -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), ) diff --git a/e2e/main_test.go b/e2e/main_test.go index d1d77b4f..26116dc0 100644 --- a/e2e/main_test.go +++ b/e2e/main_test.go @@ -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" { diff --git a/e2e/workingdir_test.go b/e2e/workingdir_test.go index 3a291379..4396201a 100644 --- a/e2e/workingdir_test.go +++ b/e2e/workingdir_test.go @@ -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), ) diff --git a/entrypoint.sh b/entrypoint.sh index 6ba90d0d..cf5ed4ac 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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 \ No newline at end of file