dashboard / erock/pico / chore: rsync test #47 rss

accepted · opened on 2025-02-21T21:07:49Z by erock
Help
checkout latest patchset:
ssh pr.pico.sh print pr-47 | git am -3
checkout any patchset in a patch request:
ssh pr.pico.sh print ps-X | git am -3
add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 47
add review to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add --review 47
accept PR:
ssh pr.pico.sh pr accept 47
close PR:
ssh pr.pico.sh pr close 47

Logs

erock created pr with ps-100 on 2025-02-21T21:07:49Z
erock added ps-101 on 2025-02-21T21:21:15Z
erock added ps-102 on 2025-02-21T21:21:56Z
erock added ps-103 on 2025-02-21T21:22:20Z
erock added ps-104 on 2025-02-22T02:12:03Z
erock changed status on 2025-03-13T14:53:20Z {"status":"accepted"}

Patchsets

ps-100 by erock on 2025-02-21T21:07:49Z
Range Diff ↕ rd-101
1: 0306c3f < -: ------- chore: rsync test
2: 971b354 ! 1: 745b513 chore: add `rsync --delete` test
ps-101 by erock on 2025-02-21T21:21:15Z
Range Diff ↕ rd-102
1: 745b513 ! 1: 76a6734 chore: add `rsync --delete` test
ps-102 by erock on 2025-02-21T21:21:56Z
Range Diff ↕ rd-103
1: 76a6734 ! 1: 57bb60c chore: add `rsync --delete` test
ps-103 by erock on 2025-02-21T21:22:20Z
Range Diff ↕ rd-104
1: 57bb60c = 1: 57bb60c chore: add `rsync --delete` test
-: ------- > 2: 5c8d929 chore: test is failing on `--delete`
ps-104 by erock on 2025-02-22T02:12:03Z

Patchset ps-101

chore: add `rsync --delete` test

Eric Bower
2025-02-17T15:36:03Z
pgs/ssh_test.go
+153 -5
Back to top

chore: add `rsync --delete` test

I can't figure out how to format the `exec.Command` for rsync with `-e`
flag

Current error:

```
[rsync -rv -e "ssh -p 2222 -o IdentitiesOnly=yes -i /tmp/rsync-3246995037/id_ed25519 -o StrictHostKeyChecking=no" /tmp/rsync-3246995037/ localhost:/test]
rsync: [sender] Failed to exec ssh -p 2222 -o IdentitiesOnly=yes -i /tmp/rsync-3246995037/id_ed25519 -o StrictHostKeyChecking=no: No such file or directory (2)
rsync error: error in IPC code (code 14) at pipe.c(85) [sender=3.4.0]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in IPC code (code 14) at io.c(232) [sender=3.4.0]
 exit status 14
--- FAIL: TestSshServerRsync (0.10s)
    ssh_test.go:152: exit status 14
```
pgs/ssh_test.go link
+153 -5
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
diff --git a/pgs/ssh_test.go b/pgs/ssh_test.go
index d30fa31..bccd375 100644
--- a/pgs/ssh_test.go
+++ b/pgs/ssh_test.go
@@ -3,9 +3,14 @@ package pgs
 import (
 	"crypto/ed25519"
 	"crypto/rand"
+	"crypto/x509"
+	"encoding/pem"
+	"fmt"
 	"io"
 	"log/slog"
 	"os"
+	"os/exec"
+	"path/filepath"
 	"strings"
 	"testing"
 	"time"
@@ -18,7 +23,7 @@ import (
 	"golang.org/x/crypto/ssh"
 )
 
-func TestSshServer(t *testing.T) {
+func TestSshServerSftp(t *testing.T) {
 	logger := slog.Default()
 	dbpool := pgsdb.NewDBMemory(logger)
 	// setup test data
@@ -57,9 +62,142 @@ func TestSshServer(t *testing.T) {
 	done <- nil
 }
 
+func TestSshServerRsync(t *testing.T) {
+	logger := slog.Default()
+	dbpool := pgsdb.NewDBMemory(logger)
+	// setup test data
+	dbpool.SetupTestData()
+	st, err := storage.NewStorageMemory(map[string]map[string]string{})
+	if err != nil {
+		panic(err)
+	}
+	cfg := NewPgsConfig(logger, dbpool, st)
+	done := make(chan error)
+	go StartSshServer(cfg, done)
+	// Hack to wait for startup
+	time.Sleep(time.Millisecond * 100)
+
+	user := GenerateUser()
+	key := utils.KeyForKeyText(user.signer.PublicKey())
+	// add user's pubkey to the default test account
+	dbpool.Pubkeys = append(dbpool.Pubkeys, &db.PublicKey{
+		ID:     "nice-pubkey",
+		UserID: dbpool.Users[0].ID,
+		Key:    key,
+	})
+
+	conn, err := user.NewClient()
+	if err != nil {
+		t.Error(err)
+		return
+	}
+	defer conn.Close()
+
+	// open an SFTP session over an existing ssh connection.
+	client, err := sftp.NewClient(conn)
+	if err != nil {
+		cfg.Logger.Error("could not create sftp client", "err", err)
+		panic(err)
+	}
+	defer client.Close()
+
+	name, err := os.MkdirTemp("", "rsync-")
+	if err != nil {
+		panic(err)
+	}
+
+	// remove the temporary directory at the end of the program
+	defer os.RemoveAll(name)
+
+	block := &pem.Block{
+		Type:  "PRIVATE KEY",
+		Bytes: user.privateKey,
+	}
+	keyFile := filepath.Join(name, "id_ed25519")
+	err = os.WriteFile(
+		keyFile,
+		pem.EncodeToMemory(block), 0600,
+	)
+
+	index := "<!doctype html><html><body>index</body></html>"
+	err = os.WriteFile(
+		filepath.Join(name, "index.html"),
+		[]byte(index), 0666,
+	)
+
+	about := "<!doctype html><html><body>about</body></html>"
+	aboutFile := filepath.Join(name, "about.html")
+	err = os.WriteFile(
+		aboutFile,
+		[]byte(about), 0666,
+	)
+
+	contact := "<!doctype html><html><body>contact</body></html>"
+	err = os.WriteFile(
+		filepath.Join(name, "contact.html"),
+		[]byte(contact), 0666,
+	)
+
+	eCmd := fmt.Sprintf(
+		`"ssh -p 2222 -o IdentitiesOnly=yes -i %s -o StrictHostKeyChecking=no"`,
+		keyFile,
+	)
+
+	// copy files
+	cmd := exec.Command("rsync", "-rv", "-e", eCmd, name+"/", "localhost:/test")
+	fmt.Println(cmd.Args)
+	result, err := cmd.CombinedOutput()
+	if err != nil {
+		fmt.Println(string(result), err)
+		t.Error(err)
+		return
+	}
+
+	// check it's there
+	fi, err := client.Lstat("about.html")
+	if err != nil {
+		cfg.Logger.Error("could not get stat for file", "err", err)
+		t.Error("about.html not found")
+		return
+	}
+	if fi.Size() != 0 {
+		cfg.Logger.Error("about.html wrong size", "size", fi.Size())
+		t.Error("about.html wrong size")
+		return
+	}
+
+	// remove about file
+	os.Remove(aboutFile)
+
+	// copy files with delete
+	delCmd := exec.Command("rsync", "-rv", "--delete", "-e", eCmd, name+"/", "localhost:/test")
+	err = delCmd.Run()
+	if err != nil {
+		t.Error(err)
+		return
+	}
+
+	done <- nil
+}
+
+func createTmpFile(name, contents, ext string) *os.File {
+	file, err := os.CreateTemp("tmp", fmt.Sprintf("%s-*.%s", name, ext))
+	if err != nil {
+		panic(err)
+	}
+
+	data := []byte(contents)
+	if _, err := file.Write(data); err != nil {
+		panic(err)
+	}
+
+	return file
+}
+
 type UserSSH struct {
-	username string
-	signer   ssh.Signer
+	username   string
+	signer     ssh.Signer
+	privateKey []byte
 }
 
 func NewUserSSH(username string, signer ssh.Signer) *UserSSH {
@@ -146,17 +284,27 @@ func GenerateUser() UserSSH {
 		panic(err)
 	}
 
+	b, err := x509.MarshalPKCS8PrivateKey(userKey)
+	if err != nil {
+		panic(err)
+	}
+
 	userSigner, err := ssh.NewSignerFromKey(userKey)
 	if err != nil {
 		panic(err)
 	}
 
 	return UserSSH{
-		username: "testuser",
-		signer:   userSigner,
+		username:   "testuser",
+		signer:     userSigner,
+		privateKey: b,
 	}
 }
 
+func WriteFilesWithRsync(cfg *PgsConfig, conn *ssh.Client, files []string) (*os.FileInfo, error) {
+	return nil, nil
+}
+
 func WriteFileWithSftp(cfg *PgsConfig, conn *ssh.Client) (*os.FileInfo, error) {
 	// open an SFTP session over an existing ssh connection.
 	client, err := sftp.NewClient(conn)
shared/ssh.go link
+2 -0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
diff --git a/shared/ssh.go b/shared/ssh.go
index 895ae02..7865e08 100644
--- a/shared/ssh.go
+++ b/shared/ssh.go
@@ -1,6 +1,7 @@
 package shared
 
 import (
+	"fmt"
 	"log/slog"
 
 	"github.com/charmbracelet/ssh"
@@ -26,6 +27,7 @@ func NewSshAuthHandler(dbh AuthFindUser, logger *slog.Logger) *SshAuthHandler {
 
 func (r *SshAuthHandler) PubkeyAuthHandler(ctx ssh.Context, key ssh.PublicKey) bool {
 	pubkey := utils.KeyForKeyText(key)
+	fmt.Println("ACTUAL", pubkey)
 	user, err := r.DB.FindUserByPubkey(pubkey)
 	if err != nil {
 		r.Logger.Error(