SFTP implementation of vfs.FS on a conn.Lease: passes the conformance suite on the in-process stand (and on a real sshd under the integration tag), including RemoveTree on a symlink to a directory (N-P0-3). Volume is the host key fingerprint (N-P0-5); metadata calls return on ctx cancel, a watchdog probes hung calls; io.Copy uses concurrent reads/writes (N-P1-6). Connection errors map to vfs.ErrConnLost. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
163 lines
4.4 KiB
Go
163 lines
4.4 KiB
Go
//go:build integration
|
|
|
|
package sftpfs
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"encoding/pem"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
"golang.org/x/crypto/ssh/knownhosts"
|
|
|
|
"vcom/internal/ssh/conn"
|
|
"vcom/internal/vfs"
|
|
"vcom/internal/vfs/vfstest"
|
|
)
|
|
|
|
const sshdPath = "/usr/sbin/sshd"
|
|
|
|
// realSSHD starts the system sshd as the current user on a free port and
|
|
// returns an FS connected to it. The test is skipped when that is not
|
|
// possible (no sshd, sandbox without ports, ...).
|
|
func realSSHD(t *testing.T) *FS {
|
|
t.Helper()
|
|
if _, err := os.Stat(sshdPath); err != nil {
|
|
t.Skip("no " + sshdPath)
|
|
}
|
|
u, err := user.Current()
|
|
if err != nil {
|
|
t.Skip(err)
|
|
}
|
|
dir, err := os.MkdirTemp("", "vcom-sshd")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.RemoveAll(dir) })
|
|
writeKey := func(name string) ssh.PublicKey {
|
|
_, priv, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
blk, err := ssh.MarshalPrivateKey(priv, name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, name), pem.EncodeToMemory(blk), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, _ := ssh.NewSignerFromKey(priv)
|
|
return s.PublicKey()
|
|
}
|
|
hostPub := writeKey("host_key")
|
|
clientPub := writeKey("client_key")
|
|
if err := os.WriteFile(filepath.Join(dir, "authorized_keys"), ssh.MarshalAuthorizedKey(clientPub), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Skip(err)
|
|
}
|
|
port := ln.Addr().(*net.TCPAddr).Port
|
|
ln.Close()
|
|
cfg := fmt.Sprintf(`Port %d
|
|
ListenAddress 127.0.0.1
|
|
HostKey %[2]s/host_key
|
|
PidFile %[2]s/sshd.pid
|
|
AuthorizedKeysFile %[2]s/authorized_keys
|
|
StrictModes no
|
|
UsePAM no
|
|
PasswordAuthentication no
|
|
KbdInteractiveAuthentication no
|
|
PubkeyAuthentication yes
|
|
Subsystem sftp internal-sftp
|
|
`, port, dir)
|
|
if err := os.WriteFile(filepath.Join(dir, "sshd_config"), []byte(cfg), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out bytes.Buffer
|
|
cmd := exec.Command(sshdPath, "-D", "-e", "-f", filepath.Join(dir, "sshd_config"))
|
|
cmd.Stdout, cmd.Stderr = &out, &out
|
|
if err := cmd.Start(); err != nil {
|
|
t.Skip(err)
|
|
}
|
|
t.Cleanup(func() { cmd.Process.Kill(); cmd.Wait() })
|
|
addr := net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for {
|
|
c, err := net.DialTimeout("tcp", addr, 100*time.Millisecond)
|
|
if err == nil {
|
|
c.Close()
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Skipf("sshd did not start: %s", out.String())
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
kh := filepath.Join(dir, "known_hosts")
|
|
if err := os.WriteFile(kh, []byte(knownhosts.Line([]string{knownhosts.Normalize(addr)}, hostPub)+"\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pool := conn.NewPool(conn.PoolOptions{DialOptions: conn.DialOptions{
|
|
Home: dir, AgentSocket: "-", KnownHostsFiles: []string{kh}, DefaultIdentityFiles: []string{},
|
|
}})
|
|
t.Cleanup(func() { pool.Close() })
|
|
l, err := pool.Acquire(context.Background(), conn.Endpoint{
|
|
Alias: "sshd", User: u.Username, Host: "127.0.0.1", Port: port,
|
|
IdentityFiles: []string{filepath.Join(dir, "client_key")}, IdentitiesOnly: true,
|
|
})
|
|
if err != nil {
|
|
t.Skipf("cannot log in to the test sshd: %v\n%s", err, out.String())
|
|
}
|
|
f := New(l)
|
|
t.Cleanup(f.Release)
|
|
return f
|
|
}
|
|
|
|
// SSH_FXP_SYMLINK argument order differs between the draft and OpenSSH; the
|
|
// link must point where we asked.
|
|
func TestSymlinkOrderOpenSSH(t *testing.T) {
|
|
f := realSSHD(t)
|
|
ctx := context.Background()
|
|
dir := t.TempDir()
|
|
if err := f.Symlink(ctx, "target-name", dir+"/link"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := os.Readlink(filepath.Join(dir, "link"))
|
|
if err != nil || got != "target-name" {
|
|
t.Fatalf("link -> %q (%v), want target-name", got, err)
|
|
}
|
|
if t2, err := f.ReadLink(ctx, dir+"/link"); err != nil || t2 != "target-name" {
|
|
t.Errorf("ReadLink = %q, %v", t2, err)
|
|
}
|
|
c := f.Caps()
|
|
if !c.NoReplace || !c.AtomicReplace {
|
|
t.Errorf("OpenSSH caps = %+v", c)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "a"), []byte("a"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "b"), []byte("b"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.Rename(ctx, dir+"/a", dir+"/b", vfs.NoReplace); !errors.Is(err, vfs.ErrExist) {
|
|
t.Errorf("Rename(NoReplace) onto existing: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestConformanceOpenSSH(t *testing.T) {
|
|
vfstest.Run(t, func(t *testing.T) (vfs.FS, string) { return realSSHD(t), t.TempDir() })
|
|
}
|