| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "flag" |
| 8 | "fmt" |
| 9 | |
| Tim Windelschmidt | 5f5f330 | 2024-02-22 23:50:24 +0100 | [diff] [blame] | 10 | xssh "golang.org/x/crypto/ssh" |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 11 | "k8s.io/klog/v2" |
| 12 | |
| 13 | "source.monogon.dev/cloud/shepherd/manager" |
| Tim Windelschmidt | 5f5f330 | 2024-02-22 23:50:24 +0100 | [diff] [blame] | 14 | "source.monogon.dev/go/net/ssh" |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | type sshConfig struct { |
| 18 | User string |
| 19 | Pass string |
| 20 | SSHKey manager.SSHKey |
| 21 | } |
| 22 | |
| 23 | func (sc *sshConfig) check() error { |
| 24 | if sc.User == "" { |
| 25 | return fmt.Errorf("-ssh_user must be set") |
| 26 | } |
| 27 | |
| 28 | if sc.Pass == "" && sc.SSHKey.KeyPersistPath == "" { |
| 29 | //TODO: The flag name -ssh_key_path could change, which would make this |
| 30 | // error very confusing. |
| 31 | return fmt.Errorf("-ssh_pass or -ssh_key_path must be set") |
| 32 | } |
| 33 | |
| 34 | return nil |
| 35 | } |
| 36 | |
| 37 | func (sc *sshConfig) RegisterFlags() { |
| 38 | flag.StringVar(&sc.User, "ssh_user", "", "SSH username to log into the machines") |
| 39 | flag.StringVar(&sc.Pass, "ssh_pass", "", "SSH password to log into the machines") |
| 40 | sc.SSHKey.RegisterFlags() |
| 41 | } |
| 42 | |
| Tim Windelschmidt | 5f5f330 | 2024-02-22 23:50:24 +0100 | [diff] [blame] | 43 | func (sc *sshConfig) NewClient() (*ssh.DirectClient, error) { |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 44 | if err := sc.check(); err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| Tim Windelschmidt | 5f5f330 | 2024-02-22 23:50:24 +0100 | [diff] [blame] | 48 | c := ssh.DirectClient{ |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 49 | Username: sc.User, |
| 50 | } |
| 51 | |
| 52 | switch { |
| 53 | case sc.Pass != "": |
| Tim Windelschmidt | d0e39cb | 2024-09-16 16:14:00 +0200 | [diff] [blame] | 54 | c.AuthMethods = []xssh.AuthMethod{xssh.Password(sc.Pass)} |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 55 | case sc.SSHKey.KeyPersistPath != "": |
| 56 | signer, err := sc.SSHKey.Signer() |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | |
| 61 | pubKey, err := sc.SSHKey.PublicKey() |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | klog.Infof("Using ssh key auth with public key: %s", pubKey) |
| 67 | |
| Tim Windelschmidt | d0e39cb | 2024-09-16 16:14:00 +0200 | [diff] [blame] | 68 | c.AuthMethods = []xssh.AuthMethod{xssh.PublicKeys(signer)} |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 69 | } |
| 70 | return &c, nil |
| 71 | } |