1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
10
gc "launchpad.net/gocheck"
12
"launchpad.net/juju-core/testing"
13
jc "launchpad.net/juju-core/testing/checkers"
14
"launchpad.net/juju-core/testing/testbase"
15
"launchpad.net/juju-core/utils"
16
"launchpad.net/juju-core/utils/ssh"
19
type ClientKeysSuite struct {
23
var _ = gc.Suite(&ClientKeysSuite{})
25
func (s *ClientKeysSuite) SetUpTest(c *gc.C) {
26
s.LoggingSuite.SetUpTest(c)
27
fakeHome := testing.MakeEmptyFakeHome(c)
28
s.AddCleanup(func(*gc.C) { fakeHome.Restore() })
29
s.AddCleanup(func(*gc.C) { ssh.ClearClientKeys() })
32
func checkFiles(c *gc.C, obtained, expected []string) {
34
for i, e := range expected {
35
expected[i], err = utils.NormalizePath(e)
36
c.Assert(err, gc.IsNil)
38
c.Assert(obtained, jc.SameContents, expected)
41
func checkPublicKeyFiles(c *gc.C, expected ...string) {
42
keys := ssh.PublicKeyFiles()
43
checkFiles(c, keys, expected)
46
func checkPrivateKeyFiles(c *gc.C, expected ...string) {
47
keys := ssh.PrivateKeyFiles()
48
checkFiles(c, keys, expected)
51
func (s *ClientKeysSuite) TestPublicKeyFiles(c *gc.C) {
52
// LoadClientKeys will create the specified directory
53
// and populate it with a key pair.
54
err := ssh.LoadClientKeys("~/.juju/ssh")
55
c.Assert(err, gc.IsNil)
56
checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub")
57
// All files ending with .pub in the client key dir get picked up.
58
priv, pub, err := ssh.GenerateKey("whatever")
59
c.Assert(err, gc.IsNil)
60
err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600)
61
c.Assert(err, gc.IsNil)
62
err = ssh.LoadClientKeys("~/.juju/ssh")
63
c.Assert(err, gc.IsNil)
64
// The new public key won't be observed until the
65
// corresponding private key exists.
66
checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub")
67
err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600)
68
c.Assert(err, gc.IsNil)
69
err = ssh.LoadClientKeys("~/.juju/ssh")
70
c.Assert(err, gc.IsNil)
71
checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub")
74
func (s *ClientKeysSuite) TestPrivateKeyFiles(c *gc.C) {
75
// Create/load client keys. They will be cached in memory:
76
// any files added to the directory will not be considered
77
// unless LoadClientKeys is called again.
78
err := ssh.LoadClientKeys("~/.juju/ssh")
79
c.Assert(err, gc.IsNil)
80
checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
81
priv, pub, err := ssh.GenerateKey("whatever")
82
c.Assert(err, gc.IsNil)
83
err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever"), []byte(priv), 0600)
84
c.Assert(err, gc.IsNil)
85
err = ssh.LoadClientKeys("~/.juju/ssh")
86
c.Assert(err, gc.IsNil)
87
// The new private key won't be observed until the
88
// corresponding public key exists.
89
checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
90
err = ioutil.WriteFile(testing.HomePath(".juju", "ssh", "whatever.pub"), []byte(pub), 0600)
91
c.Assert(err, gc.IsNil)
92
// new keys won't be reported until we call LoadClientKeys again
93
checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub")
94
checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")
95
err = ssh.LoadClientKeys("~/.juju/ssh")
96
c.Assert(err, gc.IsNil)
97
checkPublicKeyFiles(c, "~/.juju/ssh/juju_id_rsa.pub", "~/.juju/ssh/whatever.pub")
98
checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa", "~/.juju/ssh/whatever")
101
func (s *ClientKeysSuite) TestLoadClientKeysDirExists(c *gc.C) {
102
err := os.MkdirAll(testing.HomePath(".juju", "ssh"), 0755)
103
c.Assert(err, gc.IsNil)
104
err = ssh.LoadClientKeys("~/.juju/ssh")
105
c.Assert(err, gc.IsNil)
106
checkPrivateKeyFiles(c, "~/.juju/ssh/juju_id_rsa")