Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor plugin.proto and pluginbase.go to add support for metadata in connection context #475

Merged
merged 8 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ENV CGO_ENABLED=0
RUN mkdir -p /sshpiperd/plugins
WORKDIR /src
RUN --mount=target=/src,type=bind,source=. --mount=type=cache,target=/root/.cache/go-build if [ "$EXTERNAL" = "1" ]; then cp sshpiperd /sshpiperd; else go build -o /sshpiperd -ldflags "-X main.mainver=$VER" ./cmd/... ; fi
RUN --mount=target=/src,type=bind,source=. --mount=type=cache,target=/root/.cache/go-build if [ "$EXTERNAL" = "1" ]; then cp -r plugins /sshpiperd ; else go build -o /sshpiperd/plugins -tags "$BUILDTAGS" ./plugin/...; fi
RUN --mount=target=/src,type=bind,source=. --mount=type=cache,target=/root/.cache/go-build if [ "$EXTERNAL" = "1" ]; then cp -r plugins /sshpiperd ; else go build -o /sshpiperd/plugins -tags "$BUILDTAGS" ./plugin/... ./e2e/testplugin/...; fi
ADD entrypoint.sh /sshpiperd

FROM builder as testrunner
Expand Down
10 changes: 10 additions & 0 deletions cmd/sshpiperd/internal/plugin/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func (cp *ChainPlugins) Append(p *GrpcPlugin) error {
func (cp *ChainPlugins) onNextPlugin(challengeCtx ssh.ChallengeContext, upstream *libplugin.UpstreamNextPluginAuth) error {
chain := challengeCtx.(*chainConnMeta)

if upstream.Meta != nil {
if chain.Metadata == nil {
chain.Metadata = make(map[string]string)
}

for k, v := range upstream.Meta {
chain.Metadata[k] = v
}
}

if chain.current+1 >= len(cp.pluginsCallback) {
return fmt.Errorf("no more plugins")
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/sshpiperd/internal/plugin/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func (g *GrpcPlugin) CreateChallengeContext(conn ssh.ConnMetadata) (ssh.Challeng
UserName: conn.User(),
FromAddr: conn.RemoteAddr().String(),
UniqId: uiq.String(),
Metadata: make(map[string]string),
}

return &meta, g.NewConnection(&meta)
Expand All @@ -180,6 +181,7 @@ func (g *GrpcPlugin) NewConnection(meta *connMeta) error {
UserName: meta.UserName,
FromAddr: meta.FromAddr,
UniqId: meta.UniqId,
Metadata: meta.Metadata,
},
})

Expand Down
61 changes: 61 additions & 0 deletions e2e/connmeta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package e2e_test

import (
"fmt"
"testing"
"time"

"github.com/google/uuid"
)

func TestConnMeta(t *testing.T) {
piperaddr, piperport := nextAvailablePiperAddress()

piper, _, _, err := runCmd("/sshpiperd/sshpiperd",
"-p",
piperport,
"/sshpiperd/plugins/testsetmetaplugin",
"--targetaddr",
"host-password:2222",
"--",
"/sshpiperd/plugins/testgetmetaplugin",
)

if err != nil {
t.Errorf("failed to run sshpiperd: %v", err)
}

defer killCmd(piper)

waitForEndpointReady(piperaddr)

randtext := uuid.New().String()
targetfie := uuid.New().String()

c, stdin, stdout, err := runCmd(
"ssh",
"-v",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
"-p",
piperport,
"-l",
"user",
"127.0.0.1",
fmt.Sprintf(`sh -c "echo -n %v > /shared/%v"`, randtext, targetfie),
)

if err != nil {
t.Errorf("failed to ssh to piper-fixed, %v", err)
}

defer killCmd(c)

enterPassword(stdin, stdout, "pass")

time.Sleep(time.Second) // wait for file flush

checkSharedFileContent(t, targetfie, randtext)
}
2 changes: 2 additions & 0 deletions e2e/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
- PASSWORD_ACCESS=true
- USER_PASSWORD=pass
- USER_NAME=user
- LOG_STDOUT=true
labels:
- sshpiper.username=pass
- sshpiper.container_username=user
Expand All @@ -23,6 +24,7 @@ services:
image: lscr.io/linuxserver/openssh-server:latest
environment:
- USER_NAME=user
- LOG_STDOUT=true
labels:
- sshpiper.container_username=user
- sshpiper.port=2222
Expand Down
4 changes: 2 additions & 2 deletions e2e/plugin_test.go → e2e/grpcplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func createRpcServer(r *rpcServer) net.Listener {
return l
}

func TestPlugin(t *testing.T) {
func TestGrpcPlugin(t *testing.T) {

privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestPlugin(t *testing.T) {
piper, _, _, err := runCmd("/sshpiperd/sshpiperd",
"-p",
piperport,
"/sshpiperd/plugins/testplugin",
"/sshpiperd/plugins/testgrpcplugin",
"--testsshserver",
sshsvr.Addr().String(),
"--rpcserver",
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions e2e/testplugin/testgetmetaplugin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build e2e

package main

import (
log "github.com/sirupsen/logrus"
"github.com/tg123/sshpiper/libplugin"
"github.com/urfave/cli/v2"
)

func main() {

libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{
Name: "getmeta",
CreateConfig: func(c *cli.Context) (*libplugin.SshPiperPluginConfig, error) {


return &libplugin.SshPiperPluginConfig{
PasswordCallback: func(conn libplugin.ConnMetadata, password []byte) (*libplugin.Upstream, error) {


target := conn.GetMeta("targetaddr")

host, port, err := libplugin.SplitHostPortForSSH(target)
if err != nil {
return nil, err
}

log.Info("routing to ", target)
return &libplugin.Upstream{
Host: host,
Port: int32(port),
IgnoreHostKey: true,
Auth: libplugin.CreatePasswordAuth(password),
}, nil
},
}, nil
},
})
}
File renamed without changes.
34 changes: 34 additions & 0 deletions e2e/testplugin/testsetmetaplugin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build e2e

package main

import (
"github.com/tg123/sshpiper/libplugin"
"github.com/urfave/cli/v2"
)

func main() {

libplugin.CreateAndRunPluginTemplate(&libplugin.PluginTemplate{
Name: "setmeta",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "targetaddr",
Required: true,
},
},
CreateConfig: func(ctx *cli.Context) (*libplugin.SshPiperPluginConfig, error) {
return &libplugin.SshPiperPluginConfig{

NoClientAuthCallback: func(conn libplugin.ConnMetadata) (*libplugin.Upstream, error) {

return &libplugin.Upstream{
Auth: libplugin.CreateNextPluginAuth(map[string]string{
"targetaddr": ctx.String("targetaddr"),
}),
}, nil
},
}, nil
},
})
}
Loading
Loading