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
|
diff --git a/pgs/ssh_test.go b/pgs/ssh_test.go
index bf1c27e..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"
@@ -73,39 +78,126 @@ func TestSshServerRsync(t *testing.T) {
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: utils.KeyForKeyText(user.signer.PublicKey()),
+ Key: key,
})
- /* client, err := user.NewClient()
+ 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()
- _, err = WriteFilesWithRsync(cfg, client, []string{"index.html", "about.html", "favicon.png", "profile.jpg"})
+ 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
}
- // test `--delete` functionality
- _, err = WriteFilesWithRsync(cfg, client, []string{"index.html", "profile.jpg"})
+ // 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 {
@@ -192,14 +284,20 @@ 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,
}
}
|