Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

user: ask the OS about the home of the current user

This is more reliable than getting the environment variable. It's
important to notice that the function falls back to the environment
variable, if it cannot get the home directory from the operating system
(when cgo is disabled and we're not on Windows, for instance).
  • Loading branch information...
commit 0e49bca532b1079113ccdb0e7675eb1156f74c4d 1 parent 9bd973d
@fsouza fsouza authored
Showing with 21 additions and 12 deletions.
  1. +13 −4 user/key.go
  2. +7 −7 user/key_test.go
  3. +1 −1  user/user_test.go
View
17 user/key.go
@@ -10,18 +10,27 @@ import (
"github.com/globocom/gandalf/fs"
"io/ioutil"
"os"
+ "os/user"
"path"
"strings"
)
-// file to write user's keys
-var authKey string = path.Join(os.Getenv("HOME"), ".ssh", "authorized_keys")
+// authKey returns the file to write user's keys.
+func authKey() string {
+ var home string
+ if current, err := user.Current(); err == nil {
+ home = current.HomeDir
+ } else {
+ home = os.ExpandEnv("$HOME")
+ }
+ return path.Join(home, ".ssh", "authorized_keys")
+}
// Writes `key` in authorized_keys file (from current user)
// It does not writes in the database, there is no need for that since the key
// object is embedded on the user's document
func addKey(k, username string) error {
- file, err := fs.Filesystem().OpenFile(authKey, os.O_RDWR|os.O_EXCL, 0755)
+ file, err := fs.Filesystem().OpenFile(authKey(), os.O_RDWR|os.O_EXCL, 0755)
defer file.Close()
if err != nil {
return err
@@ -68,7 +77,7 @@ func removeKeys(keys map[string]string, username string) error {
// removes a key from auhtKey file
func removeKey(key, username string) error {
- file, err := fs.Filesystem().OpenFile(authKey, os.O_RDWR|os.O_EXCL, 0755)
+ file, err := fs.Filesystem().OpenFile(authKey(), os.O_RDWR|os.O_EXCL, 0755)
defer file.Close()
if err != nil {
return err
View
14 user/key_test.go
@@ -16,7 +16,7 @@ import (
func (s *S) TestAuthKeysShouldBeAbsolutePathToUsersAuthorizedKeysByDefault(c *C) {
home := os.Getenv("HOME")
expected := path.Join(home, ".ssh", "authorized_keys")
- c.Assert(authKey, Equals, expected)
+ c.Assert(authKey(), Equals, expected)
}
func (s *S) TestShouldAddKeyWithoutError(c *C) {
@@ -28,7 +28,7 @@ func (s *S) TestShouldWriteKeyInFile(c *C) {
key := "somekey blaaaaaaa r2d2@host"
err := addKey(key, "someuser")
c.Assert(err, IsNil)
- f, err := s.rfs.OpenFile(authKey, os.O_RDWR, 0755)
+ f, err := s.rfs.OpenFile(authKey(), os.O_RDWR, 0755)
b, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
got := string(b)
@@ -42,7 +42,7 @@ func (s *S) TestShouldAppendKeyInFile(c *C) {
key2 := "somekey foo r2d2@host"
err = addKey(key2, "someuser")
c.Assert(err, IsNil)
- f, err := s.rfs.OpenFile(authKey, os.O_RDWR, 0755)
+ f, err := s.rfs.OpenFile(authKey(), os.O_RDWR, 0755)
c.Assert(err, IsNil)
b, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
@@ -56,7 +56,7 @@ func (s *S) TestAddShouldWrapKeyWithRestrictions(c *C) {
expected := fmt.Sprintf("no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,command=.* %s", key)
err := addKey(key, "someuser")
c.Assert(err, IsNil)
- f, err := s.rfs.OpenFile(authKey, os.O_RDWR, 0755)
+ f, err := s.rfs.OpenFile(authKey(), os.O_RDWR, 0755)
c.Assert(err, IsNil)
b, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
@@ -97,7 +97,7 @@ func (s *S) TestRemoveKey(c *C) {
err = addKey(key2, "someuser")
c.Assert(err, IsNil)
err = removeKey(key1, "someuser")
- f, err := s.rfs.OpenFile(authKey, os.O_RDWR, 0755)
+ f, err := s.rfs.OpenFile(authKey(), os.O_RDWR, 0755)
c.Assert(err, IsNil)
b, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
@@ -111,7 +111,7 @@ func (s *S) TestRemoveKey(c *C) {
func (s *S) TestRemoveWhenKeyDoesNotExists(c *C) {
err := removeKey("somekey blaaaaaaa r2d2@host", "anotheruser")
c.Assert(err, IsNil)
- f, err := s.rfs.OpenFile(authKey, os.O_RDWR, 0755)
+ f, err := s.rfs.OpenFile(authKey(), os.O_RDWR, 0755)
c.Assert(err, IsNil)
b, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
@@ -125,7 +125,7 @@ func (s *S) TestRemoveWhenExistsOnlyOneKey(c *C) {
c.Assert(err, IsNil)
err = removeKey(key, "someuser")
c.Assert(err, IsNil)
- f, err := s.rfs.OpenFile(authKey, os.O_RDWR, 0755)
+ f, err := s.rfs.OpenFile(authKey(), os.O_RDWR, 0755)
c.Assert(err, IsNil)
b, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
View
2  user/user_test.go
@@ -36,7 +36,7 @@ func (s *S) authKeysContent(c *C) string {
}
func (s *S) clearAuthKeyFile() bool {
- f, err := s.rfs.OpenFile(authKey, os.O_RDWR, 0755)
+ f, err := s.rfs.OpenFile(authKey(), os.O_RDWR, 0755)
if err != nil {
return false
}
Please sign in to comment.
Something went wrong with that request. Please try again.