|
| 1 | +// @spec system-connection-profile |
| 2 | +// |
| 3 | +// AC traceability (this file): |
| 4 | +// |
| 5 | +// AC-08 TestSSHTransportProd_AuthLearning |
| 6 | +// |
| 7 | +// The dial seam is stubbed so the auth-learning wiring (PreferAuth in, |
| 8 | +// ObservedAuth out, RecordSSHAuth on success) is exercised without a real |
| 9 | +// SSH server. The shared discovery prod transport is the one path both OS |
| 10 | +// discovery and OS intelligence (collector) dial through. |
| 11 | + |
| 12 | +package discovery |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + "errors" |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/google/uuid" |
| 20 | + |
| 21 | + "github.com/Hanalyx/openwatch/internal/connprofile" |
| 22 | + "github.com/Hanalyx/openwatch/internal/credential" |
| 23 | + owssh "github.com/Hanalyx/openwatch/internal/ssh" |
| 24 | +) |
| 25 | + |
| 26 | +// recordingProfiles is an in-memory connprofile store for the test. |
| 27 | +type recordingProfiles struct { |
| 28 | + prefer connprofile.SSHAuthMethod |
| 29 | + getErr error |
| 30 | + gotID uuid.UUID |
| 31 | + recorded connprofile.SSHAuthMethod |
| 32 | +} |
| 33 | + |
| 34 | +func (p *recordingProfiles) Get(_ context.Context, hostID uuid.UUID) (connprofile.Profile, error) { |
| 35 | + p.gotID = hostID |
| 36 | + if p.getErr != nil { |
| 37 | + return connprofile.Profile{}, p.getErr |
| 38 | + } |
| 39 | + return connprofile.Profile{SSHAuthMethod: p.prefer}, nil |
| 40 | +} |
| 41 | + |
| 42 | +func (p *recordingProfiles) RecordSSHAuth(_ context.Context, _ uuid.UUID, m connprofile.SSHAuthMethod) error { |
| 43 | + p.recorded = m |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func TestSSHTransportProd_AuthLearning(t *testing.T) { |
| 48 | + cred := &credential.Credential{Username: "u", AuthMethod: credential.AuthBoth, Password: "p"} |
| 49 | + |
| 50 | + // stubDial captures the PreferAuth handed down and simulates the |
| 51 | + // observed method crypto/ssh would report on a successful handshake. |
| 52 | + newStubDial := func(gotPrefer *string, simulateObserved string) func(context.Context, string, int, *credential.Credential, owssh.DialOptions) (SSHSession, error) { |
| 53 | + return func(_ context.Context, _ string, _ int, _ *credential.Credential, opts owssh.DialOptions) (SSHSession, error) { |
| 54 | + *gotPrefer = opts.PreferAuth |
| 55 | + if opts.ObservedAuth != nil { |
| 56 | + *opts.ObservedAuth = simulateObserved |
| 57 | + } |
| 58 | + return learnStubSession{}, nil |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + t.Run("system-connection-profile/AC-08", func(t *testing.T) { |
| 63 | + hostID := uuid.Must(uuid.NewV7()) |
| 64 | + profiles := &recordingProfiles{prefer: connprofile.AuthPassword} |
| 65 | + var gotPrefer string |
| 66 | + |
| 67 | + tr := NewSSHTransport(owssh.ModeTOFU, owssh.NewMemoryStore()).WithProfiles(profiles) |
| 68 | + tr.dial = newStubDial(&gotPrefer, "password") |
| 69 | + |
| 70 | + ctx := connprofile.WithHostID(context.Background(), hostID) |
| 71 | + if _, err := tr.Dial(ctx, "192.0.2.1", 22, cred); err != nil { |
| 72 | + t.Fatalf("dial: %v", err) |
| 73 | + } |
| 74 | + if gotPrefer != "password" { |
| 75 | + t.Errorf("lead-with: want PreferAuth=password, got %q", gotPrefer) |
| 76 | + } |
| 77 | + if profiles.gotID != hostID { |
| 78 | + t.Errorf("lookup: want Get(%s), got Get(%s)", hostID, profiles.gotID) |
| 79 | + } |
| 80 | + if profiles.recorded != connprofile.AuthPassword { |
| 81 | + t.Errorf("record: want recorded=password, got %q", profiles.recorded) |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("no host id on ctx: no learning", func(t *testing.T) { |
| 86 | + profiles := &recordingProfiles{prefer: connprofile.AuthPassword} |
| 87 | + var gotPrefer string |
| 88 | + |
| 89 | + tr := NewSSHTransport(owssh.ModeTOFU, owssh.NewMemoryStore()).WithProfiles(profiles) |
| 90 | + tr.dial = newStubDial(&gotPrefer, "key") |
| 91 | + |
| 92 | + if _, err := tr.Dial(context.Background(), "192.0.2.1", 22, cred); err != nil { |
| 93 | + t.Fatalf("dial: %v", err) |
| 94 | + } |
| 95 | + if gotPrefer != "" { |
| 96 | + t.Errorf("no host id: want empty PreferAuth, got %q", gotPrefer) |
| 97 | + } |
| 98 | + if profiles.recorded != "" { |
| 99 | + t.Errorf("no host id: want no record, got %q", profiles.recorded) |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("no store wired: no learning", func(t *testing.T) { |
| 104 | + var gotPrefer string |
| 105 | + tr := NewSSHTransport(owssh.ModeTOFU, owssh.NewMemoryStore()) |
| 106 | + tr.dial = newStubDial(&gotPrefer, "key") |
| 107 | + |
| 108 | + ctx := connprofile.WithHostID(context.Background(), uuid.Must(uuid.NewV7())) |
| 109 | + if _, err := tr.Dial(ctx, "192.0.2.1", 22, cred); err != nil { |
| 110 | + t.Fatalf("dial: %v", err) |
| 111 | + } |
| 112 | + if gotPrefer != "" { |
| 113 | + t.Errorf("no store: want empty PreferAuth, got %q", gotPrefer) |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + t.Run("profile lookup error is non-fatal", func(t *testing.T) { |
| 118 | + profiles := &recordingProfiles{getErr: errors.New("db down")} |
| 119 | + var gotPrefer string |
| 120 | + |
| 121 | + tr := NewSSHTransport(owssh.ModeTOFU, owssh.NewMemoryStore()).WithProfiles(profiles) |
| 122 | + tr.dial = newStubDial(&gotPrefer, "password") |
| 123 | + |
| 124 | + ctx := connprofile.WithHostID(context.Background(), uuid.Must(uuid.NewV7())) |
| 125 | + if _, err := tr.Dial(ctx, "192.0.2.1", 22, cred); err != nil { |
| 126 | + t.Fatalf("dial: want success despite lookup error, got %v", err) |
| 127 | + } |
| 128 | + if gotPrefer != "" { |
| 129 | + t.Errorf("lookup error: want default order (empty PreferAuth), got %q", gotPrefer) |
| 130 | + } |
| 131 | + // observed still recorded — learning continues even when the |
| 132 | + // lead-with hint was unavailable. |
| 133 | + if profiles.recorded != connprofile.AuthPassword { |
| 134 | + t.Errorf("record: want recorded=password, got %q", profiles.recorded) |
| 135 | + } |
| 136 | + }) |
| 137 | +} |
| 138 | + |
| 139 | +// learnStubSession is a no-op SSHSession for the dial-seam tests. |
| 140 | +type learnStubSession struct{} |
| 141 | + |
| 142 | +func (learnStubSession) Run(context.Context, string) ([]byte, int, error) { return nil, 0, nil } |
| 143 | +func (learnStubSession) RunWithStdin(context.Context, string, []byte) ([]byte, int, error) { |
| 144 | + return nil, 0, nil |
| 145 | +} |
| 146 | +func (learnStubSession) Close() error { return nil } |
0 commit comments