()
| 201 | } |
| 202 | |
| 203 | func (c *testContext) LoginViaSSH() { |
| 204 | t := c.T |
| 205 | t.Helper() |
| 206 | c.lock.Lock() |
| 207 | defer c.lock.Unlock() |
| 208 | if c.sshConn != nil { |
| 209 | t.Fatalf("Already logged in via SSH.") |
| 210 | } |
| 211 | |
| 212 | t.Logf("Logging in via SSH...") |
| 213 | |
| 214 | cfg := ssh.ClientConfig{} |
| 215 | cfg.SetDefaults() |
| 216 | username := c.T.Name() |
| 217 | user := c.users.AddUser(username) |
| 218 | password := "test-login" |
| 219 | user.SetPassword(password) |
| 220 | cfg.Auth = append(cfg.Auth, ssh.Password(password)) |
| 221 | |
| 222 | hostKeys, err := c.cfg.SSH.LoadHostKeys() |
| 223 | if err != nil { |
| 224 | t.Fatalf("Failed to read back the host keys (%v)", err) |
| 225 | } |
| 226 | hostKeyAlgorithms := make([]string, len(hostKeys)) |
| 227 | marshalledHostKeys := make([]string, len(hostKeys)) |
| 228 | for i, hostKey := range hostKeys { |
| 229 | hostKeyAlgorithms[i] = hostKey.PublicKey().Type() |
| 230 | marshalledHostKeys[i] = string(ssh.MarshalAuthorizedKey(hostKey.PublicKey())) |
| 231 | } |
| 232 | cfg.HostKeyAlgorithms = hostKeyAlgorithms |
| 233 | cfg.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error { |
| 234 | marshalledHostKey := string(ssh.MarshalAuthorizedKey(key)) |
| 235 | for _, hostKey := range marshalledHostKeys { |
| 236 | if hostKey == marshalledHostKey { |
| 237 | return nil |
| 238 | } |
| 239 | } |
| 240 | return fmt.Errorf("invalid host key: %s", marshalledHostKey) |
| 241 | } |
| 242 | cfg.User = username |
| 243 | |
| 244 | sshConn, err := ssh.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", c.sshPort), &cfg) |
| 245 | if err != nil { |
| 246 | t.Fatalf("Failed to log in via SSH (%v)", err) |
| 247 | } |
| 248 | c.sshConn = sshConn |
| 249 | |
| 250 | t.Logf("SSH login successful.") |
| 251 | } |
| 252 | |
| 253 | func (c *testContext) StartSessionChannel() { |
| 254 | t := c.T |
nothing calls this directly
no test coverage detected