Skip to content

Commit 1f27021

Browse files
authored
Merge pull request #105 from AkihiroSuda/dev-allow-exclude-home-ssh
ssh: allow `ssh.loadDotSSHPubKeys = false`
2 parents b16b3f8 + 3767cf3 commit 1f27021

File tree

9 files changed

+51
-29
lines changed

9 files changed

+51
-29
lines changed

cmd/limactl/copy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func copyAction(clicontext *cli.Context) error {
3636
return err
3737
}
3838

39-
args, err := sshutil.CommonArgs()
39+
const useDotSSH = true
40+
args, err := sshutil.CommonArgs(useDotSSH)
4041
if err != nil {
4142
return err
4243
}

cmd/limactl/shell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func shellAction(clicontext *cli.Context) error {
103103
return err
104104
}
105105

106-
args, err := sshutil.SSHArgs(inst.Dir)
106+
args, err := sshutil.SSHArgs(inst.Dir, *y.SSH.LoadDotSSHPubKeys)
107107
if err != nil {
108108
return err
109109
}

pkg/cidata/cidata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
4141
Containerd: Containerd{System: *y.Containerd.System, User: *y.Containerd.User},
4242
}
4343

44-
pubKeys, err := sshutil.DefaultPubKeys()
44+
pubKeys, err := sshutil.DefaultPubKeys(*y.SSH.LoadDotSSHPubKeys)
4545
if err != nil {
4646
return err
4747
}

pkg/hostagent/hostagent.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func New(instName string, stdout, stderr io.Writer, sigintCh chan os.Signal) (*H
6666
if err != nil {
6767
return nil, err
6868
}
69+
// y is loaded with FillDefault() already, so no need to care about nil pointers.
6970

7071
qCfg := qemu.Config{
7172
Name: instName,
@@ -77,7 +78,7 @@ func New(instName string, stdout, stderr io.Writer, sigintCh chan os.Signal) (*H
7778
return nil, err
7879
}
7980

80-
sshArgs, err := sshutil.SSHArgs(inst.Dir)
81+
sshArgs, err := sshutil.SSHArgs(inst.Dir, *y.SSH.LoadDotSSHPubKeys)
8182
if err != nil {
8283
return nil, err
8384
}

pkg/limayaml/default.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ ssh:
4646
# Currently, this port number has to be specified manually.
4747
# Default: none
4848
localPort: 60022
49+
# Load ~/.ssh/*.pub in addition to $LIMA_HOME/_config/user.pub .
50+
# This option is useful when you want to use other SSH-based
51+
# applications such as rsync with the Lima instance.
52+
# If you have an insecure key under ~/.ssh, do not use this option.
53+
# Default: true
54+
loadDotSSHPubKeys: true
4955

5056
firmware:
5157
# Use legacy BIOS instead of UEFI.

pkg/limayaml/defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func FillDefault(y *LimaYAML) {
2525
if y.Video.Display == "" {
2626
y.Video.Display = "none"
2727
}
28+
if y.SSH.LoadDotSSHPubKeys == nil {
29+
y.SSH.LoadDotSSHPubKeys = &[]bool{true}[0]
30+
}
2831
for i := range y.Provision {
2932
provision := &y.Provision[i]
3033
if provision.Mode == "" {

pkg/limayaml/limayaml.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type Mount struct {
3434

3535
type SSH struct {
3636
LocalPort int `yaml:"localPort,omitempty"` // REQUIRED (FIXME: auto assign)
37+
38+
// LoadDotSSHPubKeys loads ~/.ssh/*.pub in addition to $LIMA_HOME/_config/user.pub .
39+
// Default: true
40+
LoadDotSSHPubKeys *bool `yaml:"loadDotSSHPubKeys,omitempty"`
3741
}
3842

3943
type Firmware struct {
@@ -60,8 +64,8 @@ type Provision struct {
6064
}
6165

6266
type Containerd struct {
63-
System *bool `yaml:"system,omitempty"`
64-
User *bool `yaml:"user,omitempty"`
67+
System *bool `yaml:"system,omitempty"` // default: false
68+
User *bool `yaml:"user,omitempty"` // default: true
6569
}
6670

6771
type ProbeMode = string

pkg/sshutil/sshutil.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ func readPublicKey(f string) (PubKey, error) {
3535
}
3636

3737
// DefaultPubKeys returns the public key from $LIMA_HOME/_config/user.pub.
38-
// The key will be created if it does not yet exist. All public keys
39-
// ~/.ssh/*.pub will be appended to make the VM accessible without specifying
40-
// and identity explicitly.
41-
func DefaultPubKeys() ([]PubKey, error) {
38+
// The key will be created if it does not yet exist.
39+
//
40+
// When loadDotSSH is true, ~/.ssh/*.pub will be appended to make the VM accessible without specifying
41+
// an identity explicitly.
42+
func DefaultPubKeys(loadDotSSH bool) ([]PubKey, error) {
4243
// Read $LIMA_HOME/_config/user.pub
4344
configDir, err := store.LimaConfigDir()
4445
if err != nil {
@@ -70,6 +71,10 @@ func DefaultPubKeys() ([]PubKey, error) {
7071
}
7172
res := []PubKey{entry}
7273

74+
if !loadDotSSH {
75+
return res, nil
76+
}
77+
7378
// Append all of ~/.ssh/*.pub
7479
homeDir, err := os.UserHomeDir()
7580
if err != nil {
@@ -112,7 +117,7 @@ func RemoveKnownHostEntries(sshLocalPort int) error {
112117
return nil
113118
}
114119

115-
func CommonArgs() ([]string, error) {
120+
func CommonArgs(useDotSSH bool) ([]string, error) {
116121
configDir, err := store.LimaConfigDir()
117122
if err != nil {
118123
return nil, err
@@ -126,24 +131,26 @@ func CommonArgs() ([]string, error) {
126131

127132
// Append all private keys corresponding to ~/.ssh/*.pub to keep old instances workin
128133
// that had been created before lima started using an internal identity.
129-
homeDir, err := os.UserHomeDir()
130-
if err != nil {
131-
return nil, err
132-
}
133-
files, err := filepath.Glob(filepath.Join(homeDir, ".ssh/*.pub"))
134-
if err != nil {
135-
panic(err) // Only possible error is ErrBadPattern, so this should be unreachable.
136-
}
137-
for _, f := range files {
138-
if !strings.HasSuffix(f, ".pub") {
139-
panic(errors.Errorf("unexpected ssh public key filename %q", f))
140-
}
141-
privateKeyPath := strings.TrimSuffix(f, ".pub")
142-
_, err = os.Stat(privateKeyPath)
134+
if useDotSSH {
135+
homeDir, err := os.UserHomeDir()
143136
if err != nil {
144137
return nil, err
145138
}
146-
args = append(args, "-i", privateKeyPath)
139+
files, err := filepath.Glob(filepath.Join(homeDir, ".ssh/*.pub"))
140+
if err != nil {
141+
panic(err) // Only possible error is ErrBadPattern, so this should be unreachable.
142+
}
143+
for _, f := range files {
144+
if !strings.HasSuffix(f, ".pub") {
145+
panic(errors.Errorf("unexpected ssh public key filename %q", f))
146+
}
147+
privateKeyPath := strings.TrimSuffix(f, ".pub")
148+
_, err = os.Stat(privateKeyPath)
149+
if err != nil {
150+
return nil, err
151+
}
152+
args = append(args, "-i", privateKeyPath)
153+
}
147154
}
148155

149156
args = append(args,
@@ -157,7 +164,7 @@ func CommonArgs() ([]string, error) {
157164
return args, nil
158165
}
159166

160-
func SSHArgs(instDir string) ([]string, error) {
167+
func SSHArgs(instDir string, useDotSSH bool) ([]string, error) {
161168
controlSock := filepath.Join(instDir, filenames.SSHSock)
162169
if len(controlSock) >= osutil.UnixPathMax {
163170
return nil, errors.Errorf("socket path %q is too long: >= UNIX_PATH_MAX=%d", controlSock, osutil.UnixPathMax)
@@ -166,7 +173,7 @@ func SSHArgs(instDir string) ([]string, error) {
166173
if err != nil {
167174
return nil, err
168175
}
169-
args, err := CommonArgs()
176+
args, err := CommonArgs(useDotSSH)
170177
if err != nil {
171178
return nil, err
172179
}

pkg/sshutil/sshutil_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package sshutil
33
import "testing"
44

55
func TestDefaultPubKeys(t *testing.T) {
6-
keys, _ := DefaultPubKeys()
6+
keys, _ := DefaultPubKeys(true)
77
t.Logf("found %d public keys", len(keys))
88
for _, key := range keys {
99
t.Logf("%s: %q", key.Filename, key.Content)

0 commit comments

Comments
 (0)