~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/utils/ssh/clientkeys_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

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