Resolve host aliases through ~/.ssh/config with kevinburke/ssh_config: HostName defaults to the alias, key=value syntax, Host * defaults, Include, quotes, several IdentityFile, '#' inside values (N-P1-5). Endpoint.Key includes the user (N-P2-3). Port the hosts.dat store with atomic writes and a base64 password field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
package sshconfig
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestStoreRoundTrip(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "vcom")
|
|
st, err := OpenStore(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := StoredHost{Name: "box", HostName: "10.0.0.5", Port: 2200, User: "root", Password: "hunter2", IdentityFile: "/k"}
|
|
if err := st.Put(h); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := os.ReadFile(filepath.Join(dir, HostsFile))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if bytes.Contains(raw, []byte("hunter2")) || bytes.Contains(raw, []byte("10.0.0.5")) {
|
|
t.Fatal("hosts.dat must be encrypted")
|
|
}
|
|
for _, name := range []string{HostsFile, KeyFile} {
|
|
fi, err := os.Stat(filepath.Join(dir, name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fi.Mode().Perm() != 0o600 {
|
|
t.Errorf("%s mode = %v", name, fi.Mode().Perm())
|
|
}
|
|
}
|
|
|
|
st2, err := OpenStore(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, ok := st2.Find("BOX")
|
|
if !ok || got != h {
|
|
t.Fatalf("reloaded = %+v (ok=%v), want %+v", got, ok, h)
|
|
}
|
|
|
|
h.Password = "changed"
|
|
if err := st2.Put(h); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hs := st2.Hosts(); len(hs) != 1 || hs[0].Password != "changed" {
|
|
t.Errorf("Put must replace by name: %+v", hs)
|
|
}
|
|
if err := st2.Remove("box"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st2.Remove("box"); !errors.Is(err, ErrHostNotFound) {
|
|
t.Errorf("Remove unknown: %v", err)
|
|
}
|
|
ents, _ := os.ReadDir(dir)
|
|
for _, e := range ents {
|
|
if e.Name() != HostsFile && e.Name() != KeyFile {
|
|
t.Errorf("leftover file %s (atomic write must clean up)", e.Name())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStoreDamagedKeyNotReplaced(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writeFile(t, filepath.Join(dir, KeyFile), "not base64 !!")
|
|
if _, err := OpenStore(dir); err == nil {
|
|
t.Fatal("damaged key must be an error")
|
|
}
|
|
b, _ := os.ReadFile(filepath.Join(dir, KeyFile))
|
|
if string(b) != "not base64 !!" {
|
|
t.Fatal("damaged key was overwritten")
|
|
}
|
|
}
|
|
|
|
func TestStoreSaveFailureKeepsState(t *testing.T) {
|
|
if os.Geteuid() == 0 {
|
|
t.Skip("root ignores directory permissions")
|
|
}
|
|
dir := t.TempDir()
|
|
st, err := OpenStore(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.Put(StoredHost{Name: "a"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(dir, 0o500); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Chmod(dir, 0o700)
|
|
if err := st.Put(StoredHost{Name: "b"}); err == nil {
|
|
t.Fatal("Put into read-only dir must fail")
|
|
}
|
|
if hs := st.Hosts(); len(hs) != 1 || hs[0].Name != "a" {
|
|
t.Errorf("failed save changed state: %+v", hs)
|
|
}
|
|
}
|