Skip to content

Commit 014845a

Browse files
authored
feat(vm_ssh): support custom identity-file in 'vm ssh' command (#522)
Signed-off-by: Kyle Squizzato <[email protected]>
1 parent 07cae9b commit 014845a

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

cli/cmd/vm_ssh.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cmd
22

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
57

68
"github.com/manifoldco/promptui"
79
"github.com/pkg/errors"
@@ -10,7 +12,10 @@ import (
1012
)
1113

1214
func (r *runners) InitVMSSH(parent *cobra.Command) *cobra.Command {
13-
var sshUser string
15+
var (
16+
sshUser string
17+
sshIdentityFile string
18+
)
1419

1520
cmd := &cobra.Command{
1621
Use: "ssh [VM_ID]",
@@ -29,6 +34,9 @@ Note: Only running VMs can be connected to via SSH.`,
2934
Example: `# SSH into a specific VM by ID
3035
replicated vm ssh <id>
3136
37+
# SSH with a specified identity file
38+
replicated vm ssh <id> -i ~/.ssh/id_rsa
39+
3240
# SSH into a VM with a specific user
3341
replicated vm ssh <id> -u myuser
3442
@@ -41,7 +49,7 @@ replicated vm ssh`,
4149
parent.AddCommand(cmd)
4250

4351
cmd.Flags().StringVarP(&sshUser, "user", "u", "", "SSH user to connect with")
44-
52+
cmd.Flags().StringVarP(&sshIdentityFile, "identity-file", "i", "", "SSH identity file to use")
4553
return cmd
4654
}
4755

@@ -56,6 +64,18 @@ func (r *runners) sshVM(cmd *cobra.Command, args []string) error {
5664
}
5765

5866
sshUser, _ := cmd.Flags().GetString("user")
67+
sshIdentityFile, _ := cmd.Flags().GetString("identity-file")
68+
69+
if sshIdentityFile != "" {
70+
sshIdentityFile, err := filepath.Abs(sshIdentityFile)
71+
if err != nil {
72+
return errors.Wrap(err, "failed to get absolute path for identity file")
73+
}
74+
_, err = os.Stat(sshIdentityFile)
75+
if err != nil {
76+
return errors.Wrap(err, "identity file does not exist or is not accessible")
77+
}
78+
}
5979

6080
// Get VM ID - either directly provided or selected
6181
var vmID string
@@ -96,7 +116,7 @@ func (r *runners) sshVM(cmd *cobra.Command, args []string) error {
96116
vmID = selectedVM.ID
97117
}
98118

99-
return r.kotsAPI.SSHIntoVM(vmID, sshUser)
119+
return r.kotsAPI.SSHIntoVM(vmID, sshUser, sshIdentityFile)
100120
}
101121

102122
// handleNoRunningVMs handles the case when no running VMs are found

pkg/kotsclient/vm_ssh.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
// SSHIntoVM connects to a VM via SSH using the provided ID and optional user flag
10-
func (c *VendorV3Client) SSHIntoVM(vmID string, sshUserFlag string) error {
10+
func (c *VendorV3Client) SSHIntoVM(vmID string, sshUserFlag string, identityFile string) error {
1111
connInfo, err := c.GetSSHConnectionInfo(vmID, sshUserFlag)
1212
if err != nil {
1313
return err
@@ -18,7 +18,13 @@ func (c *VendorV3Client) SSHIntoVM(vmID string, sshUserFlag string) error {
1818
sshEndpoint = fmt.Sprintf("%s@%s", connInfo.User, connInfo.Endpoint)
1919
}
2020

21-
cmd := exec.Command("ssh", sshEndpoint, "-p", fmt.Sprintf("%d", connInfo.Port))
21+
var cmd *exec.Cmd
22+
if identityFile != "" {
23+
cmd = exec.Command("ssh", sshEndpoint, "-p", fmt.Sprintf("%d", connInfo.Port), "-i", identityFile)
24+
} else {
25+
cmd = exec.Command("ssh", sshEndpoint, "-p", fmt.Sprintf("%d", connInfo.Port))
26+
}
27+
2228
cmd.Stdin = os.Stdin
2329
cmd.Stdout = os.Stdout
2430
cmd.Stderr = os.Stderr

0 commit comments

Comments
 (0)