Skip to content

Commit 0dfd10d

Browse files
committed
Include private keys from ~/.ssh for backwards compatibility
Otherwise instances created with the previous release of lima would become inaccessible by the `limactl shell` command. Signed-off-by: Jan Dubois <[email protected]>
1 parent 7c4ce9e commit 0dfd10d

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

pkg/sshutil/sshutil.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,31 @@ func SSHArgs(instDir string) ([]string, error) {
124124
if err != nil {
125125
return nil, err
126126
}
127-
args := []string{
128-
"-i", privateKeyPath,
127+
args := []string{"-i", privateKeyPath}
128+
129+
// Append all private keys corresponding to ~/.ssh/*.pub to keep old instances workin
130+
// that had been created before lima started using an internal identity.
131+
homeDir, err := os.UserHomeDir()
132+
if err != nil {
133+
return nil, err
134+
}
135+
files, err := filepath.Glob(filepath.Join(homeDir, ".ssh/*.pub"))
136+
if err != nil {
137+
panic(err) // Only possible error is ErrBadPattern, so this should be unreachable.
138+
}
139+
for _, f := range files {
140+
if !strings.HasSuffix(f, ".pub") {
141+
panic(errors.Errorf("unexpected ssh public key filename %q", f))
142+
}
143+
privateKeyPath := strings.TrimSuffix(f, ".pub")
144+
_, err = os.Stat(privateKeyPath)
145+
if err != nil {
146+
return nil, err
147+
}
148+
args = append(args, "-i", privateKeyPath)
149+
}
150+
151+
args = append(args,
129152
"-l", u.Username, // guest and host have the same username, but we should specify the username explicitly (#85)
130153
"-o", "ControlMaster=auto",
131154
"-o", "ControlPath=" + controlSock,
@@ -136,6 +159,6 @@ func SSHArgs(instDir string) ([]string, error) {
136159
"-o", "PreferredAuthentications=publickey",
137160
"-o", "Compression=no",
138161
"-o", "BatchMode=yes",
139-
}
162+
)
140163
return args, nil
141164
}

0 commit comments

Comments
 (0)