github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/utils/ssh/clientkeys_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package ssh_test 5 6 import ( 7 "io/ioutil" 8 "os" 9 10 gitjujutesting "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 "github.com/juju/utils" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/utils/ssh" 16 ) 17 18 type ClientKeysSuite struct { 19 gitjujutesting.FakeHomeSuite 20 } 21 22 var _ = gc.Suite(&ClientKeysSuite{}) 23 24 func (s *ClientKeysSuite) SetUpTest(c *gc.C) { 25 s.FakeHomeSuite.SetUpTest(c) 26 s.AddCleanup(func(*gc.C) { ssh.ClearClientKeys() }) 27 generateKeyRestorer := overrideGenerateKey(c) 28 s.AddCleanup(func(*gc.C) { generateKeyRestorer.Restore() }) 29 } 30 31 func checkFiles(c *gc.C, obtained, expected []string) { 32 var err error 33 for i, e := range expected { 34 expected[i], err = utils.NormalizePath(e) 35 c.Assert(err, jc.ErrorIsNil) 36 } 37 c.Assert(obtained, jc.SameContents, expected) 38 } 39 40 func checkPublicKeyFiles(c *gc.C, expected ...string) { 41 keys := ssh.PublicKeyFiles() 42 checkFiles(c, keys, expected) 43 } 44 45 func checkPrivateKeyFiles(c *gc.C, expected ...string) { 46 keys := ssh.PrivateKeyFiles() 47 checkFiles(c, keys, expected) 48 } 49 50 func (s *ClientKeysSuite) TestPublicKeyFiles(c *gc.C) { 51 // LoadClientKeys will create the specified directory 52 // and populate it with a key pair. 53 err := ssh.LoadClientKeys("~/.juju/ssh") 54 c.Assert(err, jc.ErrorIsNil) 55 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub") 56 // All files ending with .pub in the client key dir get picked up. 57 priv, pub, err := ssh.GenerateKey("whatever") 58 c.Assert(err, jc.ErrorIsNil) 59 err = ioutil.WriteFile(gitjujutesting.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600) 60 c.Assert(err, jc.ErrorIsNil) 61 err = ssh.LoadClientKeys("~/.juju/ssh") 62 c.Assert(err, jc.ErrorIsNil) 63 // The new public key won't be observed until the 64 // corresponding private key exists. 65 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub") 66 err = ioutil.WriteFile(gitjujutesting.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600) 67 c.Assert(err, jc.ErrorIsNil) 68 err = ssh.LoadClientKeys("~/.juju/ssh") 69 c.Assert(err, jc.ErrorIsNil) 70 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub") 71 } 72 73 func (s *ClientKeysSuite) TestPrivateKeyFiles(c *gc.C) { 74 // Create/load client keys. They will be cached in memory: 75 // any files added to the directory will not be considered 76 // unless LoadClientKeys is called again. 77 err := ssh.LoadClientKeys("~/.juju/ssh") 78 c.Assert(err, jc.ErrorIsNil) 79 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa") 80 priv, pub, err := ssh.GenerateKey("whatever") 81 c.Assert(err, jc.ErrorIsNil) 82 err = ioutil.WriteFile(gitjujutesting.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600) 83 c.Assert(err, jc.ErrorIsNil) 84 err = ssh.LoadClientKeys("~/.juju/ssh") 85 c.Assert(err, jc.ErrorIsNil) 86 // The new private key won't be observed until the 87 // corresponding public key exists. 88 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa") 89 err = ioutil.WriteFile(gitjujutesting.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600) 90 c.Assert(err, jc.ErrorIsNil) 91 // new keys won't be reported until we call LoadClientKeys again 92 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub") 93 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa") 94 err = ssh.LoadClientKeys("~/.juju/ssh") 95 c.Assert(err, jc.ErrorIsNil) 96 checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub") 97 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa", "~/.juju/ssh/whatever") 98 } 99 100 func (s *ClientKeysSuite) TestLoadClientKeysDirExists(c *gc.C) { 101 err := os.MkdirAll(gitjujutesting.HomePath(".juju", "ssh"), 0755) 102 c.Assert(err, jc.ErrorIsNil) 103 err = ssh.LoadClientKeys("~/.juju/ssh") 104 c.Assert(err, jc.ErrorIsNil) 105 checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa") 106 }