forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
215 lines (180 loc) · 6.89 KB
/
config_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package bootstrap_test
import (
"os"
"time"
jujutesting "github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/internal/testing"
"github.com/juju/juju/juju/osenv"
)
type ConfigSuite struct {
testing.FakeJujuXDGDataHomeSuite
}
var _ = gc.Suite(&ConfigSuite{})
func (*ConfigSuite) TestDefaultConfig(c *gc.C) {
cfg, err := bootstrap.NewConfig(nil)
c.Assert(err, jc.ErrorIsNil)
// These three things are generated.
c.Assert(cfg.AdminSecret, gc.Not(gc.HasLen), 0)
c.Assert(cfg.CACert, gc.Not(gc.HasLen), 0)
c.Assert(cfg.CAPrivateKey, gc.Not(gc.HasLen), 0)
c.Assert(cfg.BootstrapTimeout, gc.Equals, time.Second*1200)
c.Assert(cfg.BootstrapRetryDelay, gc.Equals, time.Second*5)
c.Assert(cfg.BootstrapAddressesDelay, gc.Equals, time.Second*10)
}
func (*ConfigSuite) TestConfigValuesSpecified(c *gc.C) {
for _, serviceType := range []string{"external", "loadbalancer"} {
externalIps := []string{"10.0.0.1", "10.0.0.2"}
if serviceType == "loadbalancer" {
externalIps = externalIps[:1]
}
cfg, err := bootstrap.NewConfig(map[string]interface{}{
"admin-secret": "sekrit",
"ca-cert": testing.CACert,
"ca-private-key": testing.CAKey,
"bootstrap-timeout": 1,
"bootstrap-retry-delay": 2,
"bootstrap-addresses-delay": 3,
"controller-service-type": serviceType,
"controller-external-name": "externalName",
"controller-external-ips": externalIps,
})
c.Assert(err, jc.ErrorIsNil)
c.Assert(cfg, jc.DeepEquals, bootstrap.Config{
AdminSecret: "sekrit",
CACert: testing.CACert,
CAPrivateKey: testing.CAKey,
BootstrapTimeout: time.Second * 1,
BootstrapRetryDelay: time.Second * 2,
BootstrapAddressesDelay: time.Second * 3,
ControllerServiceType: serviceType,
ControllerExternalName: "externalName",
ControllerExternalIPs: externalIps,
})
}
}
func (s *ConfigSuite) addFiles(c *gc.C, files ...jujutesting.TestFile) {
for _, f := range files {
err := os.WriteFile(osenv.JujuXDGDataHomePath(f.Name), []byte(f.Data), 0666)
c.Assert(err, gc.IsNil)
}
}
func (s *ConfigSuite) TestDefaultConfigReadsDefaultCACertKeyFiles(c *gc.C) {
s.addFiles(c, []jujutesting.TestFile{
{"ca-cert.pem", testing.CACert},
{"ca-private-key.pem", testing.CAKey},
}...)
cfg, err := bootstrap.NewConfig(nil)
c.Assert(err, jc.ErrorIsNil)
c.Assert(cfg.CACert, gc.Equals, testing.CACert)
c.Assert(cfg.CAPrivateKey, gc.Equals, testing.CAKey)
}
func (s *ConfigSuite) TestConfigReadsCACertKeyFilesFromPaths(c *gc.C) {
s.addFiles(c, []jujutesting.TestFile{
{"ca-cert-2.pem", testing.OtherCACert},
{"ca-private-key-2.pem", testing.OtherCAKey},
}...)
cfg, err := bootstrap.NewConfig(map[string]interface{}{
"ca-cert-path": "ca-cert-2.pem",
"ca-private-key-path": "ca-private-key-2.pem",
})
c.Assert(err, jc.ErrorIsNil)
c.Assert(cfg.CACert, gc.Equals, testing.OtherCACert)
c.Assert(cfg.CAPrivateKey, gc.Equals, testing.OtherCAKey)
}
func (s *ConfigSuite) TestConfigNonExistentPath(c *gc.C) {
s.testConfigError(c, map[string]interface{}{
"ca-cert-path": "not/there",
}, `reading "ca-cert" from file: "ca-cert" not set, and could not read from "not/there": .*`)
}
func (s *ConfigSuite) TestConfigInvalidCACert(c *gc.C) {
s.testConfigError(c, map[string]interface{}{
"ca-cert": invalidCACert,
"ca-private-key": testing.CAKey,
}, "validating ca-cert and ca-private-key: x509: malformed certificate")
}
func (s *ConfigSuite) TestConfigInvalidCAKey(c *gc.C) {
s.testConfigError(c, map[string]interface{}{
"ca-cert": testing.CACert,
"ca-private-key": invalidCAKey,
}, "validating ca-cert and ca-private-key: (crypto/)?tls: failed to parse private key")
}
func (s *ConfigSuite) TestConfigCACertKeyMismatch(c *gc.C) {
s.testConfigError(c, map[string]interface{}{
"ca-cert": testing.CACert,
"ca-private-key": testing.OtherCAKey,
}, "validating ca-cert and ca-private-key: (crypto/)?tls: private key does not match public key")
}
func (s *ConfigSuite) TestConfigCACertWithEmptyKey(c *gc.C) {
s.testConfigError(c, map[string]interface{}{
"ca-cert": testing.CACert,
}, "validating ca-cert and ca-private-key: (crypto/)?tls: failed to find any PEM data in key input")
}
func (s *ConfigSuite) TestConfigEmptyCACertWithKey(c *gc.C) {
s.testConfigError(c, map[string]interface{}{
"ca-private-key": testing.CAKey,
}, "validating ca-cert and ca-private-key: (crypto/)?tls: failed to find any PEM data in certificate input")
}
func (*ConfigSuite) testConfigError(c *gc.C, attrs map[string]interface{}, expect string) {
_, err := bootstrap.NewConfig(attrs)
c.Assert(err, gc.ErrorMatches, expect)
}
func (*ConfigSuite) TestValidate(c *gc.C) {
c.Assert(validConfig().Validate(), jc.ErrorIsNil)
}
func (*ConfigSuite) TestValidateAdminSecret(c *gc.C) {
cfg := validConfig()
cfg.AdminSecret = ""
c.Assert(cfg.Validate(), gc.ErrorMatches, "empty admin-secret not valid")
}
func (*ConfigSuite) TestValidateBootstrapTimeout(c *gc.C) {
cfg := validConfig()
cfg.BootstrapTimeout = 0
c.Assert(cfg.Validate(), gc.ErrorMatches, "bootstrap-timeout of 0s? not valid")
}
func (*ConfigSuite) TestValidateBootstrapRetryDelay(c *gc.C) {
cfg := validConfig()
cfg.BootstrapRetryDelay = -1 * time.Second
c.Assert(cfg.Validate(), gc.ErrorMatches, "bootstrap-retry-delay of -1s not valid")
}
func (*ConfigSuite) TestValidateBootstrapAddressesDelay(c *gc.C) {
cfg := validConfig()
cfg.BootstrapAddressesDelay = -2 * time.Minute
c.Assert(cfg.Validate(), gc.ErrorMatches, "bootstrap-addresses-delay of -2m0s not valid")
}
func (*ConfigSuite) TestValidateExternalIpsAndServiceType(c *gc.C) {
cfg := validConfig()
cfg.ControllerServiceType = "cluster"
c.Assert(cfg.Validate(), gc.ErrorMatches, `external IPs require a service type of "external" or "loadbalancer"`)
}
func (*ConfigSuite) TestValidateExternalIpsAndLoadBalancer(c *gc.C) {
cfg := validConfig()
cfg.ControllerServiceType = "loadbalancer"
c.Assert(cfg.Validate(), gc.ErrorMatches, `only 1 external IP is allowed with service type "loadbalancer"`)
}
func validConfig() bootstrap.Config {
return bootstrap.Config{
AdminSecret: "sekrit",
CACert: testing.CACert,
CAPrivateKey: testing.CAKey,
BootstrapTimeout: time.Second * 1,
BootstrapRetryDelay: time.Second * 2,
BootstrapAddressesDelay: time.Second * 3,
ControllerServiceType: "external",
ControllerExternalIPs: []string{"10.0.0.1", "10.0.0.2"},
}
}
var invalidCAKey = `
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw
-----END RSA PRIVATE KEY-----
`[1:]
var invalidCACert = `
-----BEGIN CERTIFICATE-----
MIIBOgIBAAJAZabKgKInuOxj5vDWLwHHQtK3/45KB+32D15w94Nt83BmuGxo90lw
-----END CERTIFICATE-----
`[1:]