2024-04-28 15:42:10 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
//go:build integrationtest
|
|
|
|
// +build integrationtest
|
|
|
|
|
|
|
|
package tailssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-06-14 18:28:39 +00:00
|
|
|
"bytes"
|
2024-05-29 17:51:50 +00:00
|
|
|
"context"
|
2024-04-28 15:42:10 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2024-06-14 18:28:39 +00:00
|
|
|
"errors"
|
2024-04-28 15:42:10 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-06-14 18:28:39 +00:00
|
|
|
"path/filepath"
|
2024-04-28 15:42:10 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
"github.com/bramvdbogaerde/go-scp"
|
2024-04-28 15:42:10 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
gossh "github.com/tailscale/golang-x-crypto/ssh"
|
|
|
|
"golang.org/x/crypto/ssh"
|
2024-06-14 18:28:39 +00:00
|
|
|
"golang.org/x/crypto/ssh/agent"
|
2024-04-28 15:42:10 +00:00
|
|
|
"tailscale.com/net/tsdial"
|
|
|
|
"tailscale.com/tailcfg"
|
2024-06-14 18:28:39 +00:00
|
|
|
glider "tailscale.com/tempfork/gliderlabs/ssh"
|
2024-04-28 15:42:10 +00:00
|
|
|
"tailscale.com/types/key"
|
|
|
|
"tailscale.com/types/netmap"
|
2024-05-29 17:51:50 +00:00
|
|
|
"tailscale.com/util/set"
|
2024-04-28 15:42:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains integration tests of the SSH functionality. These tests
|
|
|
|
// exercise everything except for the authentication logic.
|
|
|
|
//
|
|
|
|
// The tests make the following assumptions about the environment:
|
|
|
|
//
|
|
|
|
// - OS is one of MacOS or Linux
|
|
|
|
// - Test is being run as root (e.g. go test -tags integrationtest -c . && sudo ./tailssh.test -test.run TestIntegration)
|
|
|
|
// - TAILSCALED_PATH environment variable points at tailscaled binary
|
|
|
|
// - User "testuser" exists
|
|
|
|
// - "testuser" is in groups "groupone" and "grouptwo"
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// Create our log file.
|
|
|
|
file, err := os.OpenFile("/tmp/tailscalessh.log", os.O_CREATE|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
// Tail our log file.
|
2024-05-29 17:51:50 +00:00
|
|
|
cmd := exec.Command("tail", "-F", "/tmp/tailscalessh.log")
|
2024-04-28 15:42:10 +00:00
|
|
|
|
|
|
|
r, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
go func() {
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
log.Println(line)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-05-29 17:51:50 +00:00
|
|
|
defer func() {
|
|
|
|
// tail -f has a default sleep interval of 1 second, so it takes a
|
|
|
|
// moment for it to finish reading our log file after we've terminated.
|
|
|
|
// So, wait a bit to let it catch up.
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}()
|
2024-04-28 15:42:10 +00:00
|
|
|
|
|
|
|
m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegrationSSH(t *testing.T) {
|
|
|
|
debugTest.Store(true)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
debugTest.Store(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
homeDir := "/home/testuser"
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
homeDir = "/Users/testuser"
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2024-05-29 17:51:50 +00:00
|
|
|
cmd string
|
|
|
|
want []string
|
|
|
|
forceV1Behavior bool
|
|
|
|
skip bool
|
2024-10-01 03:47:45 +00:00
|
|
|
allowSendEnv bool
|
2024-04-28 15:42:10 +00:00
|
|
|
}{
|
|
|
|
{
|
2024-05-29 17:51:50 +00:00
|
|
|
cmd: "id",
|
|
|
|
want: []string{"testuser", "groupone", "grouptwo"},
|
|
|
|
forceV1Behavior: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cmd: "id",
|
|
|
|
want: []string{"testuser", "groupone", "grouptwo"},
|
|
|
|
forceV1Behavior: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cmd: "pwd",
|
|
|
|
want: []string{homeDir},
|
2024-08-16 22:09:33 +00:00
|
|
|
skip: os.Getenv("SKIP_FILE_OPS") == "1" || !fallbackToSUAvailable(),
|
2024-05-29 17:51:50 +00:00
|
|
|
forceV1Behavior: false,
|
2024-04-28 15:42:10 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-29 17:51:50 +00:00
|
|
|
cmd: "echo 'hello'",
|
|
|
|
want: []string{"hello"},
|
2024-08-16 22:09:33 +00:00
|
|
|
skip: os.Getenv("SKIP_FILE_OPS") == "1" || !fallbackToSUAvailable(),
|
2024-05-29 17:51:50 +00:00
|
|
|
forceV1Behavior: false,
|
2024-04-28 15:42:10 +00:00
|
|
|
},
|
2024-10-01 03:47:45 +00:00
|
|
|
{
|
|
|
|
cmd: `echo "${GIT_ENV_VAR:-unset1} ${EXACT_MATCH:-unset2} ${TESTING:-unset3} ${NOT_ALLOWED:-unset4}"`,
|
|
|
|
want: []string{"working1 working2 working3 unset4"},
|
|
|
|
forceV1Behavior: false,
|
|
|
|
allowSendEnv: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cmd: `echo "${GIT_ENV_VAR:-unset1} ${EXACT_MATCH:-unset2} ${TESTING:-unset3} ${NOT_ALLOWED:-unset4}"`,
|
|
|
|
want: []string{"unset1 unset2 unset3 unset4"},
|
|
|
|
forceV1Behavior: false,
|
|
|
|
allowSendEnv: false,
|
|
|
|
},
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2024-05-29 17:51:50 +00:00
|
|
|
if test.skip {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-28 15:42:10 +00:00
|
|
|
// run every test both without and with a shell
|
|
|
|
for _, shell := range []bool{false, true} {
|
|
|
|
shellQualifier := "no_shell"
|
|
|
|
if shell {
|
|
|
|
shellQualifier = "shell"
|
|
|
|
}
|
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
versionQualifier := "v2"
|
|
|
|
if test.forceV1Behavior {
|
|
|
|
versionQualifier = "v1"
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(fmt.Sprintf("%s_%s_%s", test.cmd, shellQualifier, versionQualifier), func(t *testing.T) {
|
2024-10-01 03:47:45 +00:00
|
|
|
sendEnv := map[string]string{
|
|
|
|
"GIT_ENV_VAR": "working1",
|
|
|
|
"EXACT_MATCH": "working2",
|
|
|
|
"TESTING": "working3",
|
|
|
|
"NOT_ALLOWED": "working4",
|
|
|
|
}
|
|
|
|
s := testSession(t, test.forceV1Behavior, test.allowSendEnv, sendEnv)
|
2024-04-28 15:42:10 +00:00
|
|
|
|
|
|
|
if shell {
|
|
|
|
err := s.RequestPty("xterm", 40, 80, ssh.TerminalModes{
|
|
|
|
ssh.ECHO: 1,
|
|
|
|
ssh.TTY_OP_ISPEED: 14400,
|
|
|
|
ssh.TTY_OP_OSPEED: 14400,
|
|
|
|
})
|
2024-05-29 17:51:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to request PTY: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Shell()
|
2024-04-28 15:42:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to request shell: %s", err)
|
|
|
|
}
|
2024-05-29 17:51:50 +00:00
|
|
|
|
|
|
|
// Read the shell prompt
|
|
|
|
s.read()
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
got := s.run(t, test.cmd, shell)
|
2024-04-28 15:42:10 +00:00
|
|
|
for _, want := range test.want {
|
|
|
|
if !strings.Contains(got, want) {
|
|
|
|
t.Errorf("%q does not contain %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegrationSFTP(t *testing.T) {
|
|
|
|
debugTest.Store(true)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
debugTest.Store(false)
|
|
|
|
})
|
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
for _, forceV1Behavior := range []bool{false, true} {
|
|
|
|
name := "v2"
|
|
|
|
if forceV1Behavior {
|
|
|
|
name = "v1"
|
|
|
|
}
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
filePath := "/home/testuser/sftptest.dat"
|
|
|
|
if forceV1Behavior || !fallbackToSUAvailable() {
|
|
|
|
filePath = "/tmp/sftptest.dat"
|
|
|
|
}
|
|
|
|
wantText := "hello world"
|
2024-04-28 15:42:10 +00:00
|
|
|
|
2024-10-01 03:47:45 +00:00
|
|
|
cl := testClient(t, forceV1Behavior, false)
|
2024-05-29 17:51:50 +00:00
|
|
|
scl, err := sftp.NewClient(cl)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't get sftp client: %s", err)
|
|
|
|
}
|
2024-04-28 15:42:10 +00:00
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
file, err := scl.Create(filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't create file: %s", err)
|
|
|
|
}
|
|
|
|
_, err = file.Write([]byte(wantText))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't write to file: %s", err)
|
|
|
|
}
|
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't close file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err = scl.OpenFile(filePath, os.O_RDONLY)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't open file: %s", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
gotText, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't read file: %s", err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(string(gotText), wantText); diff != "" {
|
|
|
|
t.Fatalf("unexpected file contents (-got +want):\n%s", diff)
|
|
|
|
}
|
|
|
|
|
2024-10-01 03:47:45 +00:00
|
|
|
s := testSessionFor(t, cl, nil)
|
2024-05-29 17:51:50 +00:00
|
|
|
got := s.run(t, "ls -l "+filePath, false)
|
|
|
|
if !strings.Contains(got, "testuser") {
|
|
|
|
t.Fatalf("unexpected file owner user: %s", got)
|
|
|
|
} else if !strings.Contains(got, "testuser") {
|
|
|
|
t.Fatalf("unexpected file owner group: %s", got)
|
|
|
|
}
|
|
|
|
})
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
2024-05-29 17:51:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegrationSCP(t *testing.T) {
|
|
|
|
debugTest.Store(true)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
debugTest.Store(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, forceV1Behavior := range []bool{false, true} {
|
|
|
|
name := "v2"
|
|
|
|
if forceV1Behavior {
|
|
|
|
name = "v1"
|
|
|
|
}
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
filePath := "/home/testuser/scptest.dat"
|
|
|
|
if !fallbackToSUAvailable() {
|
|
|
|
filePath = "/tmp/scptest.dat"
|
|
|
|
}
|
|
|
|
wantText := "hello world"
|
|
|
|
|
2024-10-01 03:47:45 +00:00
|
|
|
cl := testClient(t, forceV1Behavior, false)
|
2024-05-29 17:51:50 +00:00
|
|
|
scl, err := scp.NewClientBySSH(cl)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't get sftp client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scl.Copy(context.Background(), strings.NewReader(wantText), filePath, "0644", int64(len(wantText)))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't create file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
outfile, err := os.CreateTemp("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't create temp file: %s", err)
|
|
|
|
}
|
|
|
|
err = scl.CopyFromRemote(context.Background(), outfile, filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't copy file from remote: %s", err)
|
|
|
|
}
|
|
|
|
outfile.Close()
|
|
|
|
|
|
|
|
gotText, err := os.ReadFile(outfile.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("can't read file: %s", err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(string(gotText), wantText); diff != "" {
|
|
|
|
t.Fatalf("unexpected file contents (-got +want):\n%s", diff)
|
|
|
|
}
|
|
|
|
|
2024-10-01 03:47:45 +00:00
|
|
|
s := testSessionFor(t, cl, nil)
|
2024-05-29 17:51:50 +00:00
|
|
|
got := s.run(t, "ls -l "+filePath, false)
|
|
|
|
if !strings.Contains(got, "testuser") {
|
|
|
|
t.Fatalf("unexpected file owner user: %s", got)
|
|
|
|
} else if !strings.Contains(got, "testuser") {
|
|
|
|
t.Fatalf("unexpected file owner group: %s", got)
|
|
|
|
}
|
|
|
|
})
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
2024-05-29 17:51:50 +00:00
|
|
|
}
|
2024-04-28 15:42:10 +00:00
|
|
|
|
2024-06-14 18:28:39 +00:00
|
|
|
func TestSSHAgentForwarding(t *testing.T) {
|
|
|
|
debugTest.Store(true)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
debugTest.Store(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create a client SSH key
|
|
|
|
tmpDir, err := os.MkdirTemp("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = os.RemoveAll(tmpDir)
|
|
|
|
})
|
|
|
|
pkFile := filepath.Join(tmpDir, "pk")
|
|
|
|
clientKey, clientKeyRSA := generateClientKey(t, pkFile)
|
|
|
|
|
|
|
|
// Start upstream SSH server
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to listen for SSH: %s", err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = l.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
// Run an SSH server that accepts connections from that client SSH key.
|
|
|
|
gs := glider.Server{
|
|
|
|
Handler: func(s glider.Session) {
|
|
|
|
io.WriteString(s, "Hello world\n")
|
|
|
|
},
|
|
|
|
PublicKeyHandler: func(ctx glider.Context, key glider.PublicKey) error {
|
|
|
|
// Note - this is not meant to be cryptographically secure, it's
|
|
|
|
// just checking that SSH agent forwarding is forwarding the right
|
|
|
|
// key.
|
|
|
|
a := key.Marshal()
|
|
|
|
b := clientKey.PublicKey().Marshal()
|
|
|
|
if !bytes.Equal(a, b) {
|
|
|
|
return errors.New("key mismatch")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
go gs.Serve(l)
|
|
|
|
|
|
|
|
// Run tailscale SSH server and connect to it
|
|
|
|
username := "testuser"
|
2024-10-01 03:47:45 +00:00
|
|
|
tailscaleAddr := testServer(t, username, false, false)
|
2024-06-14 18:28:39 +00:00
|
|
|
tcl, err := ssh.Dial("tcp", tailscaleAddr, &ssh.ClientConfig{
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() { tcl.Close() })
|
|
|
|
|
|
|
|
s, err := tcl.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up SSH agent forwarding on the client
|
|
|
|
err = agent.RequestAgentForwarding(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyring := agent.NewKeyring()
|
|
|
|
keyring.Add(agent.AddedKey{
|
|
|
|
PrivateKey: clientKeyRSA,
|
|
|
|
})
|
|
|
|
err = agent.ForwardToAgent(tcl, keyring)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to SSH to the upstream test server using the forwarded SSH key
|
|
|
|
// and run the "true" command.
|
|
|
|
upstreamHost, upstreamPort, err := net.SplitHostPort(l.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
o, err := s.CombinedOutput(fmt.Sprintf(`ssh -T -o StrictHostKeyChecking=no -p %s upstreamuser@%s "true"`, upstreamPort, upstreamHost))
|
|
|
|
if err != nil {
|
2024-08-05 22:09:34 +00:00
|
|
|
t.Fatalf("unable to call true command: %s\n%s\n-------------------------", err, o)
|
2024-06-14 18:28:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
func fallbackToSUAvailable() bool {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return false
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
2024-05-29 17:51:50 +00:00
|
|
|
|
|
|
|
_, err := exec.LookPath("su")
|
2024-04-28 15:42:10 +00:00
|
|
|
if err != nil {
|
2024-05-29 17:51:50 +00:00
|
|
|
return false
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
// Some operating systems like Fedora seem to require login to be present
|
|
|
|
// in order for su to work.
|
|
|
|
_, err = exec.LookPath("login")
|
|
|
|
return err == nil
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type session struct {
|
|
|
|
*ssh.Session
|
|
|
|
|
|
|
|
stdin io.WriteCloser
|
|
|
|
stdout io.ReadCloser
|
|
|
|
stderr io.ReadCloser
|
|
|
|
}
|
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
func (s *session) run(t *testing.T, cmdString string, shell bool) string {
|
2024-04-28 15:42:10 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
if shell {
|
|
|
|
_, err := s.stdin.Write([]byte(fmt.Sprintf("%s\n", cmdString)))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to send command to shell: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := s.Start(cmdString)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to start command: %s", err)
|
|
|
|
}
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 17:51:50 +00:00
|
|
|
return s.read()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *session) read() string {
|
2024-04-28 15:42:10 +00:00
|
|
|
ch := make(chan []byte)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
b := make([]byte, 1)
|
|
|
|
n, err := s.stdout.Read(b)
|
|
|
|
if n > 0 {
|
|
|
|
ch <- b
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Read first byte in blocking fashion.
|
|
|
|
_got := <-ch
|
|
|
|
|
|
|
|
// Read subsequent bytes in non-blocking fashion.
|
|
|
|
readLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case b := <-ch:
|
|
|
|
_got = append(_got, b...)
|
2024-05-29 17:51:50 +00:00
|
|
|
case <-time.After(1 * time.Second):
|
2024-04-28 15:42:10 +00:00
|
|
|
break readLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(_got)
|
|
|
|
}
|
|
|
|
|
2024-10-01 03:47:45 +00:00
|
|
|
func testClient(t *testing.T, forceV1Behavior bool, allowSendEnv bool, authMethods ...ssh.AuthMethod) *ssh.Client {
|
2024-04-28 15:42:10 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
username := "testuser"
|
2024-10-01 03:47:45 +00:00
|
|
|
addr := testServer(t, username, forceV1Behavior, allowSendEnv)
|
2024-06-14 18:28:39 +00:00
|
|
|
|
|
|
|
cl, err := ssh.Dial("tcp", addr, &ssh.ClientConfig{
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
|
Auth: authMethods,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() { cl.Close() })
|
|
|
|
|
|
|
|
return cl
|
|
|
|
}
|
|
|
|
|
2024-10-01 03:47:45 +00:00
|
|
|
func testServer(t *testing.T, username string, forceV1Behavior bool, allowSendEnv bool) string {
|
2024-04-28 15:42:10 +00:00
|
|
|
srv := &server{
|
2024-10-01 03:47:45 +00:00
|
|
|
lb: &testBackend{localUser: username, forceV1Behavior: forceV1Behavior, allowSendEnv: allowSendEnv},
|
2024-04-28 15:42:10 +00:00
|
|
|
logf: log.Printf,
|
|
|
|
tailscaledPath: os.Getenv("TAILSCALED_PATH"),
|
|
|
|
timeNow: time.Now,
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Cleanup(func() { l.Close() })
|
|
|
|
|
|
|
|
go func() {
|
2024-06-14 18:28:39 +00:00
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err == nil {
|
|
|
|
go srv.HandleSSHConn(&addressFakingConn{conn})
|
|
|
|
}
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-06-14 18:28:39 +00:00
|
|
|
return l.Addr().String()
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-01 03:47:45 +00:00
|
|
|
func testSession(t *testing.T, forceV1Behavior bool, allowSendEnv bool, sendEnv map[string]string) *session {
|
|
|
|
cl := testClient(t, forceV1Behavior, allowSendEnv)
|
|
|
|
return testSessionFor(t, cl, sendEnv)
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-01 03:47:45 +00:00
|
|
|
func testSessionFor(t *testing.T, cl *ssh.Client, sendEnv map[string]string) *session {
|
2024-04-28 15:42:10 +00:00
|
|
|
s, err := cl.NewSession()
|
|
|
|
if err != nil {
|
2024-06-14 18:28:39 +00:00
|
|
|
t.Fatal(err)
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
2024-10-01 03:47:45 +00:00
|
|
|
for k, v := range sendEnv {
|
|
|
|
s.Setenv(k, v)
|
|
|
|
}
|
|
|
|
|
2024-04-28 15:42:10 +00:00
|
|
|
t.Cleanup(func() { s.Close() })
|
|
|
|
|
|
|
|
stdinReader, stdinWriter := io.Pipe()
|
|
|
|
stdoutReader, stdoutWriter := io.Pipe()
|
|
|
|
stderrReader, stderrWriter := io.Pipe()
|
|
|
|
s.Stdin = stdinReader
|
|
|
|
s.Stdout = io.MultiWriter(stdoutWriter, os.Stdout)
|
|
|
|
s.Stderr = io.MultiWriter(stderrWriter, os.Stderr)
|
|
|
|
return &session{
|
|
|
|
Session: s,
|
|
|
|
stdin: stdinWriter,
|
|
|
|
stdout: stdoutReader,
|
|
|
|
stderr: stderrReader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-14 18:28:39 +00:00
|
|
|
func generateClientKey(t *testing.T, privateKeyFile string) (ssh.Signer, *rsa.PrivateKey) {
|
|
|
|
t.Helper()
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
mk, err := x509.MarshalPKCS8PrivateKey(priv)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
privateKey := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: mk})
|
|
|
|
if privateKey == nil {
|
|
|
|
t.Fatal("failed to encoded private key")
|
|
|
|
}
|
|
|
|
err = os.WriteFile(privateKeyFile, privateKey, 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
signer, err := ssh.ParsePrivateKey(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return signer, priv
|
|
|
|
}
|
|
|
|
|
2024-04-28 15:42:10 +00:00
|
|
|
// testBackend implements ipnLocalBackend
|
|
|
|
type testBackend struct {
|
2024-05-29 17:51:50 +00:00
|
|
|
localUser string
|
|
|
|
forceV1Behavior bool
|
2024-10-01 03:47:45 +00:00
|
|
|
allowSendEnv bool
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *testBackend) GetSSH_HostKeys() ([]gossh.Signer, error) {
|
|
|
|
var result []gossh.Signer
|
2024-06-14 18:28:39 +00:00
|
|
|
var priv any
|
|
|
|
var err error
|
|
|
|
const keySize = 2048
|
|
|
|
priv, err = rsa.GenerateKey(rand.Reader, keySize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mk, err := x509.MarshalPKCS8PrivateKey(priv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hostKey := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: mk})
|
|
|
|
signer, err := gossh.ParsePrivateKey(hostKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
2024-06-14 18:28:39 +00:00
|
|
|
result = append(result, signer)
|
2024-04-28 15:42:10 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *testBackend) ShouldRunSSH() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *testBackend) NetMap() *netmap.NetworkMap {
|
2024-05-29 17:51:50 +00:00
|
|
|
capMap := make(set.Set[tailcfg.NodeCapability])
|
|
|
|
if tb.forceV1Behavior {
|
|
|
|
capMap[tailcfg.NodeAttrSSHBehaviorV1] = struct{}{}
|
|
|
|
}
|
2024-10-01 03:47:45 +00:00
|
|
|
if tb.allowSendEnv {
|
|
|
|
capMap[tailcfg.NodeAttrSSHEnvironmentVariables] = struct{}{}
|
|
|
|
}
|
2024-04-28 15:42:10 +00:00
|
|
|
return &netmap.NetworkMap{
|
|
|
|
SSHPolicy: &tailcfg.SSHPolicy{
|
|
|
|
Rules: []*tailcfg.SSHRule{
|
2024-05-29 17:51:50 +00:00
|
|
|
{
|
2024-04-28 15:42:10 +00:00
|
|
|
Principals: []*tailcfg.SSHPrincipal{{Any: true}},
|
2024-06-14 18:28:39 +00:00
|
|
|
Action: &tailcfg.SSHAction{Accept: true, AllowAgentForwarding: true},
|
2024-04-28 15:42:10 +00:00
|
|
|
SSHUsers: map[string]string{"*": tb.localUser},
|
2024-10-01 03:47:45 +00:00
|
|
|
AcceptEnv: []string{"GIT_*", "EXACT_MATCH", "TEST?NG"},
|
2024-04-28 15:42:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-05-29 17:51:50 +00:00
|
|
|
AllCaps: capMap,
|
2024-04-28 15:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-20 18:30:19 +00:00
|
|
|
func (tb *testBackend) WhoIs(_ string, ipp netip.AddrPort) (n tailcfg.NodeView, u tailcfg.UserProfile, ok bool) {
|
2024-04-28 15:42:10 +00:00
|
|
|
return (&tailcfg.Node{}).View(), tailcfg.UserProfile{
|
|
|
|
LoginName: tb.localUser + "@example.com",
|
|
|
|
}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *testBackend) DoNoiseRequest(req *http.Request) (*http.Response, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *testBackend) Dialer() *tsdial.Dialer {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *testBackend) TailscaleVarRoot() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *testBackend) NodeKey() key.NodePublic {
|
|
|
|
return key.NodePublic{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type addressFakingConn struct {
|
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *addressFakingConn) LocalAddr() net.Addr {
|
|
|
|
return &net.TCPAddr{
|
|
|
|
IP: net.ParseIP("100.100.100.101"),
|
|
|
|
Port: 22,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *addressFakingConn) RemoteAddr() net.Addr {
|
|
|
|
return &net.TCPAddr{
|
|
|
|
IP: net.ParseIP("100.100.100.102"),
|
|
|
|
Port: 10002,
|
|
|
|
}
|
|
|
|
}
|