Skip to content

Commit 0082837

Browse files
Merge pull request docker-archive-public#3330 from bamarni/issue-3268
[Generic] Copy the public key to machine directory (fixes docker-archive-public#3268)
2 parents ee17cf4 + 9f8f133 commit 0082837

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

drivers/generic/generic.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net"
77
"os"
8+
"path"
89
"strconv"
910
"time"
1011

@@ -87,13 +88,6 @@ func (d *Driver) GetSSHUsername() string {
8788
}
8889

8990
func (d *Driver) GetSSHKeyPath() string {
90-
if d.SSHKey == "" {
91-
return ""
92-
}
93-
94-
if d.SSHKeyPath == "" {
95-
d.SSHKeyPath = d.ResolveStorePath("id_rsa")
96-
}
9791
return d.SSHKeyPath
9892
}
9993

@@ -114,8 +108,10 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
114108
func (d *Driver) PreCreateCheck() error {
115109
if d.SSHKey != "" {
116110
if _, err := os.Stat(d.SSHKey); os.IsNotExist(err) {
117-
return fmt.Errorf("Ssh key does not exist: %q", d.SSHKey)
111+
return fmt.Errorf("SSH key does not exist: %q", d.SSHKey)
118112
}
113+
114+
// TODO: validate the key is a valid key
119115
}
120116

121117
return nil
@@ -126,13 +122,14 @@ func (d *Driver) Create() error {
126122
log.Info("No SSH key specified. Assuming an existing key at the default location.")
127123
} else {
128124
log.Info("Importing SSH key...")
129-
// TODO: validate the key is a valid key
130-
if err := mcnutils.CopyFile(d.SSHKey, d.GetSSHKeyPath()); err != nil {
131-
return fmt.Errorf("unable to copy ssh key: %s", err)
125+
126+
d.SSHKeyPath = d.ResolveStorePath(path.Base(d.SSHKey))
127+
if err := copySSHKey(d.SSHKey, d.SSHKeyPath); err != nil {
128+
return err
132129
}
133130

134-
if err := os.Chmod(d.GetSSHKeyPath(), 0600); err != nil {
135-
return fmt.Errorf("unable to set permissions on the ssh key: %s", err)
131+
if err := copySSHKey(d.SSHKey+".pub", d.SSHKeyPath+".pub"); err != nil {
132+
log.Infof("Couldn't copy SSH public key : %s", err)
136133
}
137134
}
138135

@@ -185,3 +182,15 @@ func (d *Driver) Kill() error {
185182
func (d *Driver) Remove() error {
186183
return nil
187184
}
185+
186+
func copySSHKey(src, dst string) error {
187+
if err := mcnutils.CopyFile(src, dst); err != nil {
188+
return fmt.Errorf("unable to copy ssh key: %s", err)
189+
}
190+
191+
if err := os.Chmod(dst, 0600); err != nil {
192+
return fmt.Errorf("unable to set permissions on the ssh key: %s", err)
193+
}
194+
195+
return nil
196+
}

libmachine/mcnutils/utils.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ func CopyFile(src, dst string) error {
6363
return err
6464
}
6565

66-
if err := os.Chmod(dst, fi.Mode()); err != nil {
67-
return err
68-
}
69-
70-
return nil
66+
return os.Chmod(dst, fi.Mode())
7167
}
7268

7369
func WaitForSpecificOrError(f func() (bool, error), maxAttempts int, waitInterval time.Duration) error {

0 commit comments

Comments
 (0)