github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/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 jc "github.com/juju/testing/checkers" 13 "github.com/juju/utils" 14 "github.com/juju/utils/ssh" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/environs/config" 18 "github.com/juju/juju/testing" 19 ) 20 21 type AuthKeysSuite struct { 22 testing.BaseSuite 23 dotssh string // ~/.ssh 24 } 25 26 var _ = gc.Suite(&AuthKeysSuite{}) 27 28 func (s *AuthKeysSuite) SetUpTest(c *gc.C) { 29 s.BaseSuite.SetUpTest(c) 30 old := utils.Home() 31 newhome := c.MkDir() 32 err := utils.SetHome(newhome) 33 c.Assert(err, jc.ErrorIsNil) 34 s.AddCleanup(func(*gc.C) { 35 ssh.ClearClientKeys() 36 err := utils.SetHome(old) 37 c.Assert(err, jc.ErrorIsNil) 38 }) 39 40 s.dotssh = filepath.Join(newhome, ".ssh") 41 err = os.Mkdir(s.dotssh, 0755) 42 c.Assert(err, jc.ErrorIsNil) 43 } 44 45 func (s *AuthKeysSuite) TestReadAuthorizedKeysErrors(c *gc.C) { 46 _, err := config.ReadAuthorizedKeys("") 47 c.Assert(err, gc.ErrorMatches, "no public ssh keys found") 48 c.Assert(err, gc.Equals, config.ErrNoAuthorizedKeys) 49 _, err = config.ReadAuthorizedKeys(filepath.Join(s.dotssh, "notthere.pub")) 50 c.Assert(err, gc.ErrorMatches, "no public ssh keys found") 51 c.Assert(err, gc.Equals, config.ErrNoAuthorizedKeys) 52 } 53 54 func writeFile(c *gc.C, filename string, contents string) { 55 err := ioutil.WriteFile(filename, []byte(contents), 0644) 56 c.Assert(err, jc.ErrorIsNil) 57 } 58 59 func (s *AuthKeysSuite) TestReadAuthorizedKeys(c *gc.C) { 60 writeFile(c, filepath.Join(s.dotssh, "id_rsa.pub"), "id_rsa") 61 writeFile(c, filepath.Join(s.dotssh, "identity.pub"), "identity") 62 writeFile(c, filepath.Join(s.dotssh, "test.pub"), "test") 63 keys, err := config.ReadAuthorizedKeys("") 64 c.Assert(err, jc.ErrorIsNil) 65 c.Assert(keys, gc.Equals, "id_rsa\nidentity\n") 66 keys, err = config.ReadAuthorizedKeys("test.pub") // relative to ~/.ssh 67 c.Assert(err, jc.ErrorIsNil) 68 c.Assert(keys, gc.Equals, "test\n") 69 } 70 71 func (s *AuthKeysSuite) TestReadAuthorizedKeysClientKeys(c *gc.C) { 72 keydir := filepath.Join(s.dotssh, "juju") 73 err := ssh.LoadClientKeys(keydir) // auto-generates a key pair 74 c.Assert(err, jc.ErrorIsNil) 75 pubkeyFiles := ssh.PublicKeyFiles() 76 c.Assert(pubkeyFiles, gc.HasLen, 1) 77 data, err := ioutil.ReadFile(pubkeyFiles[0]) 78 c.Assert(err, jc.ErrorIsNil) 79 prefix := strings.Trim(string(data), "\n") + "\n" 80 81 writeFile(c, filepath.Join(s.dotssh, "id_rsa.pub"), "id_rsa") 82 writeFile(c, filepath.Join(s.dotssh, "test.pub"), "test") 83 keys, err := config.ReadAuthorizedKeys("") 84 c.Assert(err, jc.ErrorIsNil) 85 c.Assert(keys, gc.Equals, prefix+"id_rsa\n") 86 keys, err = config.ReadAuthorizedKeys("test.pub") 87 c.Assert(err, jc.ErrorIsNil) 88 c.Assert(keys, gc.Equals, prefix+"test\n") 89 keys, err = config.ReadAuthorizedKeys("notthere.pub") 90 c.Assert(err, jc.ErrorIsNil) 91 c.Assert(keys, gc.Equals, prefix) 92 } 93 94 func (s *AuthKeysSuite) TestConcatAuthKeys(c *gc.C) { 95 for _, test := range []struct{ a, b, result string }{ 96 {"a", "", "a"}, 97 {"", "b", "b"}, 98 {"a", "b", "a\nb"}, 99 {"a\n", "b", "a\nb"}, 100 } { 101 c.Check(config.ConcatAuthKeys(test.a, test.b), gc.Equals, test.result) 102 } 103 }