Index: utils/ssh/authorisedkeys.go |
=== modified file 'utils/ssh/authorisedkeys.go' |
--- utils/ssh/authorisedkeys.go 2014-01-30 06:08:57 +0000 |
+++ utils/ssh/authorisedkeys.go 2014-03-05 16:30:01 +0000 |
@@ -114,22 +114,16 @@ |
if err == nil { |
perms = info.Mode().Perm() |
} |
- // Write the data to a temp file |
- tempDir, err := ioutil.TempDir(keyDir, "") |
- if err != nil { |
- return err |
- } |
- tempFile := filepath.Join(tempDir, "newkeyfile") |
- defer os.RemoveAll(tempDir) |
- err = ioutil.WriteFile(tempFile, []byte(keyData), perms) |
- if err != nil { |
- return err |
- } |
- // Rename temp file to the final location and ensure its owner |
- // is set correctly. |
logger.Debugf("writing authorised keys file %s", sshKeyFile) |
+ err = utils.AtomicWriteFile(sshKeyFile, []byte(keyData), perms) |
+ if err != nil { |
+ return err |
+ } |
+ |
// TODO (wallyworld) - what to do on windows (if anything) |
+ // TODO(dimitern) - no need to use user.Current() if username |
+ // is "" - it will use the current user anyway. |
if runtime.GOOS != "windows" { |
// Ensure the resulting authorised keys file has its ownership |
// set to the specified username. |
@@ -151,12 +145,12 @@ |
if err != nil { |
return err |
} |
- err = os.Chown(tempFile, uid, gid) |
+ err = os.Chown(sshKeyFile, uid, gid) |
if err != nil { |
return err |
} |
} |
- return os.Rename(tempFile, sshKeyFile) |
+ return nil |
} |
// We need a mutex because updates to the authorised keys file are done by |