diff --git a/cmd/internal/agentcontainer/container.go b/cmd/internal/agentcontainer/container.go index 8ad00f987..5644b4ee7 100644 --- a/cmd/internal/agentcontainer/container.go +++ b/cmd/internal/agentcontainer/container.go @@ -31,5 +31,6 @@ func NewContainerCmd(flags *flags.GlobalFlags) *cobra.Command { containerCmd.AddCommand(NewCredentialsStatusCmd()) containerCmd.AddCommand(NewSetupDevsyPlatformAccessCmd(flags)) containerCmd.AddCommand(NewSSHServerCmd(flags)) + containerCmd.AddCommand(NewSFTPServerCmd()) return containerCmd } diff --git a/cmd/internal/agentcontainer/sftp_server.go b/cmd/internal/agentcontainer/sftp_server.go new file mode 100644 index 000000000..666457c19 --- /dev/null +++ b/cmd/internal/agentcontainer/sftp_server.go @@ -0,0 +1,36 @@ +package agentcontainer + +import ( + "fmt" + "io" + "os" + + "github.com/devsy-org/devsy/pkg/ssh/server" + "github.com/spf13/cobra" +) + +func NewSFTPServerCmd() *cobra.Command { + var home string + cmd := &cobra.Command{ + Use: "sftp-server", + Hidden: true, + Args: cobra.NoArgs, + RunE: func(_ *cobra.Command, _ []string) error { + if home == "" { + return fmt.Errorf("home directory is required") + } + return server.ServeSFTP(sftpStdio{Reader: os.Stdin, Writer: os.Stdout}, home) + }, + } + cmd.Flags().StringVar(&home, "home", "", "Home directory for SFTP paths") + return cmd +} + +type sftpStdio struct { + io.Reader + io.Writer +} + +func (sftpStdio) Close() error { + return nil +} diff --git a/pkg/ssh/server/exec_identity_test.go b/pkg/ssh/server/exec_identity_test.go index 272ad2a8c..06d518e65 100644 --- a/pkg/ssh/server/exec_identity_test.go +++ b/pkg/ssh/server/exec_identity_test.go @@ -167,6 +167,30 @@ func TestSameEffectiveGroupIDsIgnoresOrderDuplicatesAndPrimaryRepresentation(t * require.False(t, sameEffectiveGroupIDs(201, nil, 201, []int{-1})) } +func TestPrepareExecIdentityForNamedSFTPAccountRetainsGroups(t *testing.T) { + account, lookups := execIdentityTestLookups() + lookups.userByID = func(id string) (*user.User, error) { + require.Equal(t, account.Uid, id) + return &user.User{Username: "other", Uid: id, Gid: "3000", HomeDir: "/home/other"}, nil + } + lookups.groups = func(got *user.User) ([]string, error) { + require.Same(t, account, got) + return []string{"2001", "2002"}, nil + } + + cmd := exec.Command("true") + handled, err := prepareExecIdentityForAccountWithLookups(cmd, account.Uid, account, lookups) + require.NoError(t, err) + require.True(t, handled) + require.NotNil(t, cmd.SysProcAttr) + require.NotNil(t, cmd.SysProcAttr.Credential) + require.Equal(t, uint32(1000), cmd.SysProcAttr.Credential.Uid) + require.Equal(t, uint32(2000), cmd.SysProcAttr.Credential.Gid) + require.Equal(t, []uint32{2001, 2002}, cmd.SysProcAttr.Credential.Groups) + require.Equal(t, "alice", environmentMap(cmd.Environ())["USER"]) + require.Equal(t, "/home/alice", cmd.Dir) +} + func TestPrepareExecIdentityLeavesNamedUserToExistingPath(t *testing.T) { cmd := exec.Command("true") attrs := &syscall.SysProcAttr{Setpgid: true} diff --git a/pkg/ssh/server/exec_identity_unix.go b/pkg/ssh/server/exec_identity_unix.go index 29cc5b95d..1cc0bfb7f 100644 --- a/pkg/ssh/server/exec_identity_unix.go +++ b/pkg/ssh/server/exec_identity_unix.go @@ -38,6 +38,20 @@ func prepareExecIdentity(cmd *exec.Cmd, spec string) (bool, error) { return prepareExecIdentityWithLookups(cmd, spec, systemExecIdentityLookups()) } +func prepareExecIdentityForAccount(cmd *exec.Cmd, spec string, account *user.User) (bool, error) { + return prepareExecIdentityForAccountWithLookups(cmd, spec, account, systemExecIdentityLookups()) +} + +func prepareExecIdentityForAccountWithLookups( + cmd *exec.Cmd, + spec string, + account *user.User, + lookups execIdentityLookups, +) (bool, error) { + lookups.userByID = func(string) (*user.User, error) { return account, nil } + return prepareExecIdentityWithLookups(cmd, spec, lookups) +} + func prepareExecIdentityWithLookups( cmd *exec.Cmd, spec string, diff --git a/pkg/ssh/server/exec_identity_windows.go b/pkg/ssh/server/exec_identity_windows.go index 2f59e6666..fc17010af 100644 --- a/pkg/ssh/server/exec_identity_windows.go +++ b/pkg/ssh/server/exec_identity_windows.go @@ -5,6 +5,7 @@ package server import ( "errors" "os/exec" + "os/user" ) func prepareExecIdentity(_ *exec.Cmd, spec string) (bool, error) { @@ -14,3 +15,7 @@ func prepareExecIdentity(_ *exec.Cmd, spec string) (bool, error) { } return true, errors.New("numeric execution identities are not supported on Windows") } + +func prepareExecIdentityForAccount(cmd *exec.Cmd, spec string, _ *user.User) (bool, error) { + return prepareExecIdentity(cmd, spec) +} diff --git a/pkg/ssh/server/sftp_handler.go b/pkg/ssh/server/sftp_handler.go index c6f26b161..de90cac45 100644 --- a/pkg/ssh/server/sftp_handler.go +++ b/pkg/ssh/server/sftp_handler.go @@ -1,48 +1,181 @@ package server import ( + "context" "errors" + "fmt" "io" + "os" + "os/exec" + "os/user" + "runtime" + "strconv" - "github.com/devsy-org/devsy/pkg/command" "github.com/devsy-org/devsy/pkg/log" "github.com/devsy-org/ssh" "github.com/pkg/sftp" ) -func sftpHandler(sess ssh.Session, currentUser string) { - writer := log.Writer(log.LevelDebug) - defer func() { _ = writer.Close() }() +var newSFTPCommand = func(_ context.Context, home string) (*exec.Cmd, error) { + executable, err := os.Executable() + if err != nil { + return nil, err + } + return &exec.Cmd{ + Path: executable, + Args: []string{executable, "internal", "agent", "container", "sftp-server", "--home", home}, + }, nil +} - user := sess.User() - if user == currentUser { - user = "" +type sftpExecIdentity struct { + home string + credential string + account *user.User + hasGroupSpec bool +} + +func sftpHandler(sess ssh.Session) { + err := runSFTP(sess) + if err != nil { + log.Debugf("sftp session failed: %v (exit %d)", err, exitCode(err)) } + if err := sess.Exit(exitCode(err)); err != nil { + log.Debugf("sftp session exit failed: %T", err) + } +} - workingDir, _ := command.GetHome(user) - serverOptions := []sftp.ServerOption{ - sftp.WithDebug(writer), - sftp.WithServerWorkingDirectory(workingDir), +func runSFTP(sess ssh.Session) error { + identity, err := resolveSFTPIdentity(sess.User()) + if err != nil { + return err } - server, err := sftp.NewServer( - sess, - serverOptions..., - ) + cmd, err := newSFTPCommand(sess.Context(), identity.home) if err != nil { - log.Debugf("sftp server init error: %s\n", err) - return + return err } - defer func() { _ = server.Close() }() + if cmd == nil { + return errors.New("missing SFTP command") + } + if err := prepareSFTPCommand(cmd, identity); err != nil { + return err + } + cmd.Stdout = sess + cmd.Stderr = sess.Stderr() + stdin, err := cmd.StdinPipe() + if err != nil { + return err + } + if err := cmd.Start(); err != nil { + _ = stdin.Close() + return err + } + stopCancellation := context.AfterFunc(sess.Context(), func() { _ = cmd.Process.Kill() }) + defer stopCancellation() + go func() { + _, _ = io.Copy(stdin, sess) + _ = stdin.Close() + }() + return cmd.Wait() +} - // serve - err = server.Serve() - if errors.Is(err, io.EOF) { - _ = sess.Exit(0) - return +func resolveSFTPIdentity(spec string) (sftpExecIdentity, error) { + if spec == "" { + return sftpExecIdentity{}, errors.New("missing SFTP user") + } + requested, handled, err := parseRequestedExecIdentity(spec) + if err != nil { + return sftpExecIdentity{}, err + } + if !handled { + requested.user = spec + } + account, err := lookupSFTPAccount(requested) + if err != nil { + return sftpExecIdentity{}, err + } + identity := sftpExecIdentity{ + home: "/", + credential: spec, + account: account, + hasGroupSpec: requested.hasGroupSpec, } + if account != nil { + if account.HomeDir != "" { + identity.home = account.HomeDir + } + if !requested.hasGroupSpec { + identity.credential = account.Uid + } + } + return identity, nil +} + +func lookupSFTPAccount(requested requestedExecIdentity) (*user.User, error) { + if !requested.userIsID { + return user.Lookup(requested.user) + } + uid, err := parseExecID(requested.user) + if err != nil { + return nil, err + } + account, err := user.LookupId(strconv.FormatUint(uint64(uid), 10)) + var unknown user.UnknownUserIdError + if err != nil && !errors.As(err, &unknown) { + return nil, err + } + return account, nil +} + +func prepareSFTPCommand(cmd *exec.Cmd, identity sftpExecIdentity) error { + handled, err := applySFTPCredentials(cmd, identity) + if err != nil { + return err + } + if !handled { + return fmt.Errorf("SFTP identity %q cannot be applied", identity.credential) + } + if runtime.GOOS != "windows" { + cmd.Dir = "/" + } + return nil +} + +func applySFTPCredentials(cmd *exec.Cmd, identity sftpExecIdentity) (bool, error) { + if identity.account != nil && !identity.hasGroupSpec { + current, err := user.Current() + if err != nil { + return true, err + } + if identity.account.Uid == current.Uid && identity.account.Username == current.Username { + prepareCurrentSFTPUser(cmd, identity) + return true, nil + } + if runtime.GOOS == "windows" { + return true, errors.New("SFTP user switching is not supported") + } + return prepareExecIdentityForAccount(cmd, identity.credential, identity.account) + } + return prepareExecIdentity(cmd, identity.credential) +} + +func prepareCurrentSFTPUser(cmd *exec.Cmd, identity sftpExecIdentity) { + cmd.Env = mergeSessionEnvironment(cmd.Environ(), []string{ + "HOME=" + identity.home, + "USER=" + identity.account.Username, + "LOGNAME=" + identity.account.Username, + }, nil) +} +func ServeSFTP(input io.ReadWriteCloser, home string) error { + server, err := sftp.NewServer(input, sftp.WithServerWorkingDirectory(home)) if err != nil { - log.Debugf("sftp server completed with error: %v", err) + return err + } + defer func() { _ = server.Close() }() + + err = server.Serve() + if errors.Is(err, io.EOF) { + return nil } - _ = sess.Exit(1) + return err } diff --git a/pkg/ssh/server/sftp_identity_linux_test.go b/pkg/ssh/server/sftp_identity_linux_test.go new file mode 100644 index 000000000..6c8bc9c61 --- /dev/null +++ b/pkg/ssh/server/sftp_identity_linux_test.go @@ -0,0 +1,266 @@ +//go:build linux + +package server + +import ( + "context" + "crypto/ed25519" + "crypto/rand" + "errors" + "fmt" + "io" + "net" + "os" + "os/exec" + "os/user" + "path/filepath" + "strconv" + "syscall" + "testing" + "time" + + "github.com/devsy-org/ssh" + "github.com/pkg/sftp" + "github.com/stretchr/testify/require" + gossh "golang.org/x/crypto/ssh" +) + +const ( + sftpTestChildEnvironment = "DEVSY_SFTP_TEST_CHILD" + sftpExtraGroupChildEnvironment = "DEVSY_SFTP_EXTRA_GROUP_CHILD" + sftpTestExecutable = "/proc/self/exe" +) + +type sftpTestProcessIO struct { + io.Reader + io.Writer +} + +func (sftpTestProcessIO) Close() error { return nil } + +func TestSFTPChild(t *testing.T) { + if os.Getenv(sftpTestChildEnvironment) != "1" { + return + } + if len(os.Args) < 2 { + os.Exit(1) + } + if err := ServeSFTP( + sftpTestProcessIO{Reader: os.Stdin, Writer: os.Stdout}, + os.Args[len(os.Args)-1], + ); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + os.Exit(0) +} + +func sftpRootTempDir(t *testing.T) string { + t.Helper() + dir, err := os.MkdirTemp("/tmp", "devsy-sftp-") + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, os.RemoveAll(dir)) }) + return dir +} + +func configureSFTPTestChild(t *testing.T) { + t.Helper() + t.Setenv(sftpTestChildEnvironment, "1") + previous := newSFTPCommand + newSFTPCommand = func(_ context.Context, home string) (*exec.Cmd, error) { + return &exec.Cmd{ + Path: sftpTestExecutable, + Args: []string{sftpTestExecutable, "-test.run=^TestSFTPChild$", "--", home}, + }, nil + } + t.Cleanup(func() { newSFTPCommand = previous }) +} + +func openSFTPTestSession(t *testing.T, username string) (io.Reader, io.WriteCloser) { + t.Helper() + _, privateKey, err := ed25519.GenerateKey(rand.Reader) + require.NoError(t, err) + signer, err := gossh.NewSignerFromKey(privateKey) + require.NoError(t, err) + sshd := buildSSHServer("") + sshd.HostSigners = []ssh.Signer{signer} + listener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + finished := make(chan error, 1) + go func() { finished <- sshd.Serve(listener) }() + t.Cleanup(func() { + require.NoError(t, sshd.Close()) + err := <-finished + require.True( + t, + errors.Is(err, ssh.ErrServerClosed) || errors.Is(err, net.ErrClosed), + "server shutdown: %v", + err, + ) + }) + client, err := gossh.Dial("tcp", listener.Addr().String(), &gossh.ClientConfig{ + User: username, + HostKeyCallback: gossh.FixedHostKey(signer.PublicKey()), + Timeout: 5 * time.Second, + }) + require.NoError(t, err) + t.Cleanup(func() { _ = client.Close() }) + session, err := client.NewSession() + require.NoError(t, err) + t.Cleanup(func() { _ = session.Close() }) + stdin, err := session.StdinPipe() + require.NoError(t, err) + stdout, err := session.StdoutPipe() + require.NoError(t, err) + require.NoError(t, session.RequestSubsystem("sftp")) + return stdout, stdin +} + +func newSFTPTestClient(t *testing.T, stdout io.Reader, stdin io.WriteCloser) *sftp.Client { + t.Helper() + client, err := sftp.NewClientPipe(stdout, stdin) + require.NoError(t, err) + t.Cleanup(func() { _ = client.Close() }) + return client +} + +func assertSFTPFileIdentity(t *testing.T, client *sftp.Client, dir string, account *user.User) { + t.Helper() + path := filepath.Join(dir, "created-via-sftp") + file, err := client.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL) + require.NoError(t, err) + _, err = file.Write([]byte("sftp identity")) + require.NoError(t, err) + require.NoError(t, file.Close()) + require.NoError(t, client.Chmod(path, 0o600)) + uploaded, err := client.Open(path) + require.NoError(t, err) + contents, err := io.ReadAll(uploaded) + require.NoError(t, err) + require.NoError(t, uploaded.Close()) + require.Equal(t, "sftp identity", string(contents)) + info, err := os.Stat(path) + require.NoError(t, err) + uid, err := strconv.ParseUint(account.Uid, 10, 32) + require.NoError(t, err) + gid, err := strconv.ParseUint(account.Gid, 10, 32) + require.NoError(t, err) + stat, ok := info.Sys().(*syscall.Stat_t) + require.True(t, ok) + require.Equal(t, uint32(uid), stat.Uid) + require.Equal(t, uint32(gid), stat.Gid) + require.Equal(t, os.FileMode(0o600), info.Mode().Perm()) +} + +func TestSFTPRequestedUserOwnsFilesAndCannotReadRootPrivatePath(t *testing.T) { + if os.Geteuid() != 0 { + t.Skip("switching to a separate account requires root") + } + nobody, err := user.Lookup("nobody") + if err != nil { + t.Skipf("nobody account unavailable: %v", err) + } + if nobody.Uid == "0" { + t.Skip("nobody account has root UID") + } + configureSFTPTestChild(t) + dir := sftpRootTempDir(t) + uid, err := strconv.Atoi(nobody.Uid) + require.NoError(t, err) + gid, err := strconv.Atoi(nobody.Gid) + require.NoError(t, err) + require.NoError(t, os.Chown(dir, uid, gid)) + writable := filepath.Join(dir, "writable") + require.NoError(t, os.Mkdir(writable, 0o700)) + require.NoError(t, os.Chown(writable, uid, gid)) + private := filepath.Join(dir, "root-private") + require.NoError(t, os.Mkdir(private, 0o700)) + secret := filepath.Join(private, "secret") + require.NoError(t, os.WriteFile(secret, []byte("root only"), 0o600)) + + stdout, stdin := openSFTPTestSession(t, nobody.Username) + client := newSFTPTestClient(t, stdout, stdin) + assertSFTPFileIdentity(t, client, writable, nobody) + _, err = client.Open(secret) + require.ErrorIs(t, err, os.ErrPermission) + require.NoError(t, client.Close()) +} + +func TestSFTPCurrentUserAndInvalidIdentity(t *testing.T) { + current, err := user.Current() + require.NoError(t, err) + configureSFTPTestChild(t) + + t.Run("current user", func(t *testing.T) { + stdout, stdin := openSFTPTestSession(t, current.Username) + client := newSFTPTestClient(t, stdout, stdin) + assertSFTPFileIdentity(t, client, t.TempDir(), current) + if info, err := os.Stat(current.HomeDir); err == nil && info.IsDir() { + workingDir, err := client.Getwd() + require.NoError(t, err) + require.Equal(t, current.HomeDir, workingDir) + } + require.NoError(t, client.Close()) + }) + + t.Run("unknown account", func(t *testing.T) { + invalid := fmt.Sprintf("devsy-sftp-missing-%d", os.Getpid()) + _, err := user.Lookup(invalid) + require.Error(t, err) + stdout, stdin := openSFTPTestSession(t, invalid) + _, err = sftp.NewClientPipe(stdout, stdin) + require.Error(t, err) + }) + + if os.Geteuid() != 0 { + t.Run("non-switchable account", func(t *testing.T) { + stdout, stdin := openSFTPTestSession(t, "root") + _, err := sftp.NewClientPipe(stdout, stdin) + require.Error(t, err) + }) + } +} + +func TestSFTPCurrentUserWithExtraGroups(t *testing.T) { + if os.Getenv(sftpExtraGroupChildEnvironment) == "1" { + current, err := user.Current() + require.NoError(t, err) + accountGroups, err := current.GroupIds() + require.NoError(t, err) + require.NotContains(t, accountGroups, "0") + processGroups, err := os.Getgroups() + require.NoError(t, err) + require.Contains(t, processGroups, 0) + configureSFTPTestChild(t) + stdout, stdin := openSFTPTestSession(t, current.Username) + client := newSFTPTestClient(t, stdout, stdin) + assertSFTPFileIdentity(t, client, t.TempDir(), current) + require.NoError(t, client.Close()) + return + } + + if os.Geteuid() != 0 { + t.Skip("adding an extra group requires root") + } + nobody, err := user.Lookup("nobody") + if err != nil { + t.Skipf("nobody account unavailable: %v", err) + } + uid, err := strconv.ParseUint(nobody.Uid, 10, 32) + require.NoError(t, err) + gid, err := strconv.ParseUint(nobody.Gid, 10, 32) + require.NoError(t, err) + cmd := &exec.Cmd{ + Path: sftpTestExecutable, + Args: []string{sftpTestExecutable, "-test.run=^TestSFTPCurrentUserWithExtraGroups$"}, + Env: append(os.Environ(), "TMPDIR=/tmp", sftpExtraGroupChildEnvironment+"=1"), + Dir: "/", + SysProcAttr: &syscall.SysProcAttr{ + Credential: &syscall.Credential{ + Uid: uint32(uid), Gid: uint32(gid), Groups: []uint32{0}, + }, + }, + } + output, err := cmd.CombinedOutput() + require.NoError(t, err, string(output)) +} diff --git a/pkg/ssh/server/ssh.go b/pkg/ssh/server/ssh.go index 74ec33860..b897bdb95 100644 --- a/pkg/ssh/server/ssh.go +++ b/pkg/ssh/server/ssh.go @@ -179,7 +179,7 @@ func NewServer(addr string, opts ServerOptions) (Server, error) { workdir: opts.Workdir, reuseSock: opts.ReuseSock, currentUser: currentUser.Username, - sshServer: buildSSHServer(addr, currentUser.Username), + sshServer: buildSSHServer(addr), } if len(opts.Keys) > 0 { @@ -204,7 +204,7 @@ func NewServer(addr string, opts ServerOptions) (Server, error) { // buildSSHServer constructs the underlying ssh.Server with its port // forwarding, channel, request, and SFTP subsystem handlers wired up. -func buildSSHServer(addr, currentUsername string) ssh.Server { +func buildSSHServer(addr string) ssh.Server { forwardHandler := &ssh.ForwardedTCPHandler{} forwardedUnixHandler := &ssh.ForwardedUnixHandler{} keepAliveInterval, keepAliveCountMax := keepAliveConfig() @@ -235,7 +235,7 @@ func buildSSHServer(addr, currentUsername string) ssh.Server { }, SubsystemHandlers: map[string]ssh.SubsystemHandler{ "sftp": func(s ssh.Session) { - sftpHandler(s, currentUsername) + sftpHandler(s) }, }, } diff --git a/pkg/ssh/server/ssh_container.go b/pkg/ssh/server/ssh_container.go index 6af95c2a7..f69dca45c 100644 --- a/pkg/ssh/server/ssh_container.go +++ b/pkg/ssh/server/ssh_container.go @@ -55,7 +55,7 @@ func NewContainerServer(addr string, workdir string) (Server, error) { }, SubsystemHandlers: map[string]ssh.SubsystemHandler{ "sftp": func(s ssh.Session) { - sftpHandler(s, "") + sftpHandler(s) }, }, }, diff --git a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/connect-to-a-workspace.mdx b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/connect-to-a-workspace.mdx index d842340ac..05f6f13af 100644 --- a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/connect-to-a-workspace.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/connect-to-a-workspace.mdx @@ -69,6 +69,12 @@ devsy workspace ssh my-workspace devsy workspace ssh my-workspace --command "echo Hello World" ``` +SFTP uses the SSH username for file operations inside the workspace. Files you upload belong to that user, and access follows that user's permissions: + +```sh +sftp USER@WORKSPACE_NAME.devsy +``` + If you are already inside a Devsy container and need the host and remote user for a manually configured IDE connection, run this from the container terminal: ```sh