forked from coreos/ignition
/
tls.go
226 lines (209 loc) · 5.96 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
// 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"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"github.com/coreos/ignition/tests/register"
"github.com/coreos/ignition/tests/types"
)
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.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
customCAServer.StartTLS()
register.Register(register.NegativeTest, AppendConfigCustomCert())
register.Register(register.NegativeTest, FetchFileCustomCert())
register.Register(register.NegativeTest, AppendConfigCustomCertHTTP())
register.Register(register.NegativeTest, AppendConfigCustomCertInvalidHeaderHTTP())
}
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"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := fmt.Sprintf(`{
"ignition": {
"version": "$version",
"config": {
"append": [{
"source": %q
}]
},
"timeouts": {
"httpTotal": 5
}
}
}`, customCAServer.URL)
configMinVersion := "2.1.0"
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",
"timeouts": {
"httpTotal": 5
}
},
"storage": {
"files": [{
"filesystem": "root",
"path": "/foo/bar",
"contents": {
"source": %q
}
}]
}
}`, customCAServer.URL)
configMinVersion := "2.1.0"
return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}
func AppendConfigCustomCertHTTP() types.Test {
name := "Fetch Certificate from Invalid Address"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := fmt.Sprintf(`{
"ignition": {
"version": "$version",
"security": {
"tls": {
"certificateAuthorities": [{
"source": "http://127.0.0.1:8080/asdf"
}]
}
}
},
"storage": {
"files": [{
"filesystem": "root",
"path": "/foo/bar",
"contents": {
"source": %q
}
}]
}
}`, customCAServer.URL)
configMinVersion := "2.2.0"
return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}
func AppendConfigCustomCertInvalidHeaderHTTP() types.Test {
name := "Fetch Certificate with Invalid Header - HTTP"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := fmt.Sprintf(`{
"ignition": {
"version": "$version",
"security": {
"tls": {
"certificateAuthorities": [{
"httpHeaders": [{"name": "X-Auth", "value": "INVALID"}, {"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"
return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}