-
Notifications
You must be signed in to change notification settings - Fork 15
/
tls.go
321 lines (296 loc) · 7.97 KB
/
tls.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2018 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package security
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httptest"
"github.com/flatcar/ignition/tests/register"
"github.com/flatcar/ignition/tests/types"
"github.com/vincent-petithory/dataurl"
)
func init() {
cer, err := tls.X509KeyPair(publicKey, privateKey)
if err != nil {
panic(fmt.Sprintf("error loading x509 keypair: %v", err))
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
customCAServer.TLS = config
customCAServer.StartTLS()
register.Register(register.PositiveTest, AppendConfigCustomCert())
register.Register(register.PositiveTest, FetchFileCustomCert())
register.Register(register.PositiveTest, FetchFileCustomCertHTTP())
register.Register(register.PositiveTest, FetchFileCustomCertHTTPUsingHeaders())
register.Register(register.PositiveTest, FetchFileCustomCertHTTPRedirectHeaders())
}
var (
// generated via:
// openssl ecparam -genkey -name secp384r1 -out server.key
privateKey = []byte(`-----BEGIN EC PARAMETERS-----
BgUrgQQAIg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDB6yW6RIYfTXdYVuPY0V0L6EtZ6vZD86vgbsw52Y3/U5nZ2JE++JrKu
tt2Xt/NMzG6gBwYFK4EEACKhZANiAAQDEhfHEulYKlANw9eR5l455gwzAIQuraa0
49RhvM7PPywaiD8DobteQmE8wn7cJSzOYw6GLvrL4Q1BO5EFUXknkW50t8lfnUeH
veCNsqvm82F1NVevVoExAUhDYmMREa4=
-----END EC PRIVATE KEY-----`)
// generated via:
// openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
publicKey = []byte(`-----BEGIN CERTIFICATE-----
MIICzTCCAlKgAwIBAgIJALTP0pfNBMzGMAoGCCqGSM49BAMCMIGZMQswCQYDVQQG
EwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNj
bzETMBEGA1UECgwKQ29yZU9TIEluYzEUMBIGA1UECwwLRW5naW5lZXJpbmcxEzAR
BgNVBAMMCmNvcmVvcy5jb20xHTAbBgkqhkiG9w0BCQEWDm9lbUBjb3Jlb3MuY29t
MB4XDTE4MDEyNTAwMDczOVoXDTI4MDEyMzAwMDczOVowgZkxCzAJBgNVBAYTAlVT
MRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1TYW4gRnJhbmNpc2NvMRMw
EQYDVQQKDApDb3JlT1MgSW5jMRQwEgYDVQQLDAtFbmdpbmVlcmluZzETMBEGA1UE
AwwKY29yZW9zLmNvbTEdMBsGCSqGSIb3DQEJARYOb2VtQGNvcmVvcy5jb20wdjAQ
BgcqhkjOPQIBBgUrgQQAIgNiAAQDEhfHEulYKlANw9eR5l455gwzAIQuraa049Rh
vM7PPywaiD8DobteQmE8wn7cJSzOYw6GLvrL4Q1BO5EFUXknkW50t8lfnUeHveCN
sqvm82F1NVevVoExAUhDYmMREa6jZDBiMA8GA1UdEQQIMAaHBH8AAAEwHQYDVR0O
BBYEFEbFy0SPiF1YXt+9T3Jig2rNmBtpMB8GA1UdIwQYMBaAFEbFy0SPiF1YXt+9
T3Jig2rNmBtpMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDaQAwZgIxAOul
t3MhI02IONjTDusl2YuCxMgpy2uy0MPkEGUHnUOsxmPSG0gEBCNHyeKVeTaPUwIx
AKbyaAqbChEy9CvDgyv6qxTYU+eeBImLKS3PH2uW5etc/69V/sDojqpH3hEffsOt
9g==
-----END CERTIFICATE-----`)
customCAServerFile = []byte(`{
"ignition": { "version": "2.0.0" },
"storage": {
"files": [{
"filesystem": "root",
"path": "/foo/bar",
"contents": { "source": "data:,example%20file%0A" }
}]
}
}`)
customCAServer = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(customCAServerFile)
}))
)
func AppendConfigCustomCert() types.Test {
name := "Append config with custom tls cert_positive"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := fmt.Sprintf(`{
"ignition": {
"version": "$version",
"config": {
"append": [{
"source": %q
}]
},
"security": {
"tls": {
"certificateAuthorities": [{
"source": %q
}]
}
}
}
}`, customCAServer.URL, dataurl.EncodeBytes(publicKey))
configMinVersion := "2.2.0"
out[0].Partitions.AddFiles("ROOT", []types.File{
{
Node: types.Node{
Name: "bar",
Directory: "foo",
},
Contents: "example file\n",
},
})
return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}
func FetchFileCustomCert() types.Test {
name := "Fetch file with custom tls cert"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := fmt.Sprintf(`{
"ignition": {
"version": "$version",
"security": {
"tls": {
"certificateAuthorities": [{
"source": %q
}]
}
}
},
"storage": {
"files": [{
"filesystem": "root",
"path": "/foo/bar",
"contents": {
"source": %q
}
}]
}
}`, dataurl.EncodeBytes(publicKey), customCAServer.URL)
configMinVersion := "2.2.0"
out[0].Partitions.AddFiles("ROOT", []types.File{
{
Node: types.Node{
Directory: "foo",
Name: "bar",
},
Contents: string(customCAServerFile),
},
})
return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}
func FetchFileCustomCertHTTP() types.Test {
name := "Fetch file with custom tls cert from HTTP URL"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := fmt.Sprintf(`{
"ignition": {
"version": "$version",
"security": {
"tls": {
"certificateAuthorities": [{
"source": "http://127.0.0.1:8080/certificates"
}]
}
}
},
"storage": {
"files": [{
"filesystem": "root",
"path": "/foo/bar",
"contents": {
"source": %q
}
}]
}
}`, customCAServer.URL)
configMinVersion := "2.2.0"
out[0].Partitions.AddFiles("ROOT", []types.File{
{
Node: types.Node{
Directory: "foo",
Name: "bar",
},
Contents: string(customCAServerFile),
},
})
return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}
func FetchFileCustomCertHTTPUsingHeaders() types.Test {
name := "Fetch file with custom tls cert from HTTP URL using HTTP headers"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := fmt.Sprintf(`{
"ignition": {
"version": "$version",
"security": {
"tls": {
"certificateAuthorities": [{
"httpHeaders": [{"name": "X-Auth", "value": "r8ewap98gfh4d8"}, {"name": "Keep-Alive", "value": "300"}],
"source": "http://127.0.0.1:8080/certificates_headers"
}]
}
}
},
"storage": {
"files": [{
"filesystem": "root",
"path": "/foo/bar",
"contents": {
"source": %q
}
}]
}
}`, customCAServer.URL)
configMinVersion := "2.4.0-experimental"
out[0].Partitions.AddFiles("ROOT", []types.File{
{
Node: types.Node{
Directory: "foo",
Name: "bar",
},
Contents: string(customCAServerFile),
},
})
return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}
func FetchFileCustomCertHTTPRedirectHeaders() types.Test {
name := "Fetch file with custom tls cert from HTTP URL using HTTP headers with redirect"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := fmt.Sprintf(`{
"ignition": {
"version": "$version",
"security": {
"tls": {
"certificateAuthorities": [{
"httpHeaders": [{"name": "X-Auth", "value": "r8ewap98gfh4d8"}, {"name": "Keep-Alive", "value": "300"}],
"source": "http://127.0.0.1:8080/certificates_headers_redirect"
}]
}
}
},
"storage": {
"files": [{
"filesystem": "root",
"path": "/foo/bar",
"contents": {
"source": %q
}
}]
}
}`, customCAServer.URL)
configMinVersion := "2.4.0-experimental"
out[0].Partitions.AddFiles("ROOT", []types.File{
{
Node: types.Node{
Directory: "foo",
Name: "bar",
},
Contents: string(customCAServerFile),
},
})
return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}