-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertpool_test.go
150 lines (121 loc) · 4.54 KB
/
certpool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api_test
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/juju/loggo"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/api"
"github.com/juju/juju/pki"
"github.com/juju/juju/testing"
)
type certPoolSuite struct {
testing.BaseSuite
logs *certLogs
}
var _ = gc.Suite(&certPoolSuite{})
func (s *certPoolSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)
s.logs = &certLogs{}
loggo.GetLogger("juju.api").SetLogLevel(loggo.TRACE)
loggo.RegisterWriter("api-certs", s.logs)
}
func (*certPoolSuite) TestCreateCertPoolNoCert(c *gc.C) {
pool, err := api.CreateCertPool("")
c.Assert(err, jc.ErrorIsNil)
c.Assert(pool.Subjects(), gc.HasLen, 0)
}
func (*certPoolSuite) TestCreateCertPoolTestCert(c *gc.C) {
pool, err := api.CreateCertPool(testing.CACert)
c.Assert(err, jc.ErrorIsNil)
c.Assert(pool.Subjects(), gc.HasLen, 1)
}
func (s *certPoolSuite) TestCreateCertPoolNoDir(c *gc.C) {
certDir := filepath.Join(c.MkDir(), "missing")
s.PatchValue(api.CertDir, certDir)
pool, err := api.CreateCertPool("")
c.Assert(err, jc.ErrorIsNil)
c.Assert(pool.Subjects(), gc.HasLen, 0)
c.Assert(s.logs.messages, gc.HasLen, 1)
// The directory not existing is likely to happen a lot, so it is only
// logged out at trace to help be explicit in the case where detailed
// debugging is needed.
c.Assert(s.logs.messages[0], gc.Matches, `TRACE cert dir ".*" does not exist`)
}
func (s *certPoolSuite) TestCreateCertPoolNotADir(c *gc.C) {
certDir := filepath.Join(c.MkDir(), "missing")
s.PatchValue(api.CertDir, certDir)
// Make the certDir a file instead...
c.Assert(os.WriteFile(certDir, []byte("blah"), 0644), jc.ErrorIsNil)
pool, err := api.CreateCertPool("")
c.Assert(err, jc.ErrorIsNil)
c.Assert(pool.Subjects(), gc.HasLen, 0)
c.Assert(s.logs.messages, gc.HasLen, 1)
c.Assert(s.logs.messages[0], gc.Matches, `INFO cert dir ".*" is not a directory`)
}
func (s *certPoolSuite) TestCreateCertPoolEmptyDir(c *gc.C) {
certDir := c.MkDir()
s.PatchValue(api.CertDir, certDir)
pool, err := api.CreateCertPool("")
c.Assert(err, jc.ErrorIsNil)
c.Assert(pool.Subjects(), gc.HasLen, 0)
c.Assert(s.logs.messages, gc.HasLen, 1)
c.Assert(s.logs.messages[0], gc.Matches, `DEBUG added 0 certs to the pool from .*`)
}
func (s *certPoolSuite) TestCreateCertPoolLoadsPEMFiles(c *gc.C) {
certDir := c.MkDir()
s.PatchValue(api.CertDir, certDir)
s.addCert(c, filepath.Join(certDir, "first.pem"))
s.addCert(c, filepath.Join(certDir, "second.pem"))
s.addCert(c, filepath.Join(certDir, "third.pem"))
pool, err := api.CreateCertPool("")
c.Assert(err, jc.ErrorIsNil)
c.Assert(pool.Subjects(), gc.HasLen, 3)
c.Assert(s.logs.messages, gc.HasLen, 1)
c.Assert(s.logs.messages[0], gc.Matches, `DEBUG added 3 certs to the pool from .*`)
}
func (s *certPoolSuite) TestCreateCertPoolLoadsOnlyPEMFiles(c *gc.C) {
certDir := c.MkDir()
s.PatchValue(api.CertDir, certDir)
s.addCert(c, filepath.Join(certDir, "first.pem"))
c.Assert(os.WriteFile(filepath.Join(certDir, "second.cert"), []byte("blah"), 0644), jc.ErrorIsNil)
pool, err := api.CreateCertPool("")
c.Assert(err, jc.ErrorIsNil)
c.Assert(pool.Subjects(), gc.HasLen, 1)
c.Assert(s.logs.messages, gc.HasLen, 1)
c.Assert(s.logs.messages[0], gc.Matches, `DEBUG added 1 certs to the pool from .*`)
}
func (s *certPoolSuite) TestCreateCertPoolLogsBadCerts(c *gc.C) {
certDir := c.MkDir()
s.PatchValue(api.CertDir, certDir)
c.Assert(os.WriteFile(filepath.Join(certDir, "broken.pem"), []byte("blah"), 0644), jc.ErrorIsNil)
pool, err := api.CreateCertPool("")
c.Assert(err, jc.ErrorIsNil)
c.Assert(pool.Subjects(), gc.HasLen, 0)
c.Assert(s.logs.messages, gc.HasLen, 2)
c.Assert(s.logs.messages[0], gc.Matches, `INFO error parsing cert ".*broken.pem": .*`)
c.Assert(s.logs.messages[1], gc.Matches, `DEBUG added 0 certs to the pool from .*`)
}
func (s *certPoolSuite) addCert(c *gc.C, filename string) {
signer, err := pki.DefaultKeyProfile()
c.Assert(err, jc.ErrorIsNil)
caCert, err := pki.NewCA("random model name", signer)
c.Assert(err, jc.ErrorIsNil)
caCertPem, err := pki.CertificateToPemString(pki.DefaultPemHeaders, caCert)
c.Assert(err, jc.ErrorIsNil)
c.Assert(err, jc.ErrorIsNil)
err = os.WriteFile(filename, []byte(caCertPem), 0644)
c.Assert(err, jc.ErrorIsNil)
}
type certLogs struct {
messages []string
}
func (c *certLogs) Write(entry loggo.Entry) {
if strings.HasSuffix(entry.Filename, "certpool.go") {
c.messages = append(c.messages, fmt.Sprintf("%s %s", entry.Level, entry.Message))
}
}