launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/environs/config/authkeys_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package config_test 5 6 import ( 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "strings" 11 12 gc "launchpad.net/gocheck" 13 14 "launchpad.net/juju-core/environs/config" 15 "launchpad.net/juju-core/juju/osenv" 16 "launchpad.net/juju-core/testing/testbase" 17 "launchpad.net/juju-core/utils/ssh" 18 ) 19 20 type AuthKeysSuite struct { 21 testbase.LoggingSuite 22 dotssh string // ~/.ssh 23 } 24 25 var _ = gc.Suite(&AuthKeysSuite{}) 26 27 func (s *AuthKeysSuite) SetUpTest(c *gc.C) { 28 s.LoggingSuite.SetUpTest(c) 29 old := osenv.Home() 30 newhome := c.MkDir() 31 osenv.SetHome(newhome) 32 s.AddCleanup(func(*gc.C) { osenv.SetHome(old) }) 33 s.dotssh = filepath.Join(newhome, ".ssh") 34 err := os.Mkdir(s.dotssh, 0755) 35 c.Assert(err, gc.IsNil) 36 } 37 38 func (s *AuthKeysSuite) TearDownTest(c *gc.C) { 39 ssh.ClearClientKeys() 40 s.LoggingSuite.TearDownTest(c) 41 } 42 43 func (s *AuthKeysSuite) TestReadAuthorizedKeysErrors(c *gc.C) { 44 _, err := config.ReadAuthorizedKeys("") 45 c.Assert(err, gc.ErrorMatches, "no public ssh keys found") 46 _, err = config.ReadAuthorizedKeys(filepath.Join(s.dotssh, "notthere.pub")) 47 c.Assert(err, gc.ErrorMatches, "no public ssh keys found") 48 } 49 50 func writeFile(c *gc.C, filename string, contents string) { 51 err := ioutil.WriteFile(filename, []byte(contents), 0644) 52 c.Assert(err, gc.IsNil) 53 } 54 55 func (s *AuthKeysSuite) TestReadAuthorizedKeys(c *gc.C) { 56 writeFile(c, filepath.Join(s.dotssh, "id_rsa.pub"), "id_rsa") 57 writeFile(c, filepath.Join(s.dotssh, "identity.pub"), "identity") 58 writeFile(c, filepath.Join(s.dotssh, "test.pub"), "test") 59 keys, err := config.ReadAuthorizedKeys("") 60 c.Assert(err, gc.IsNil) 61 c.Assert(keys, gc.Equals, "id_rsa\nidentity\n") 62 keys, err = config.ReadAuthorizedKeys("test.pub") // relative to ~/.ssh 63 c.Assert(err, gc.IsNil) 64 c.Assert(keys, gc.Equals, "test\n") 65 } 66 67 func (s *AuthKeysSuite) TestReadAuthorizedKeysClientKeys(c *gc.C) { 68 keydir := filepath.Join(s.dotssh, "juju") 69 err := ssh.LoadClientKeys(keydir) // auto-generates a key pair 70 c.Assert(err, gc.IsNil) 71 pubkeyFiles := ssh.PublicKeyFiles() 72 c.Assert(pubkeyFiles, gc.HasLen, 1) 73 data, err := ioutil.ReadFile(pubkeyFiles[0]) 74 c.Assert(err, gc.IsNil) 75 prefix := strings.Trim(string(data), "\n") + "\n" 76 77 writeFile(c, filepath.Join(s.dotssh, "id_rsa.pub"), "id_rsa") 78 writeFile(c, filepath.Join(s.dotssh, "test.pub"), "test") 79 keys, err := config.ReadAuthorizedKeys("") 80 c.Assert(err, gc.IsNil) 81 c.Assert(keys, gc.Equals, prefix+"id_rsa\n") 82 keys, err = config.ReadAuthorizedKeys("test.pub") 83 c.Assert(err, gc.IsNil) 84 c.Assert(keys, gc.Equals, prefix+"test\n") 85 keys, err = config.ReadAuthorizedKeys("notthere.pub") 86 c.Assert(err, gc.IsNil) 87 c.Assert(keys, gc.Equals, prefix) 88 }