-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevcert_test.go
More file actions
208 lines (193 loc) · 6.04 KB
/
Copy pathdevcert_test.go
File metadata and controls
208 lines (193 loc) · 6.04 KB
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
package main
import (
"crypto/ecdsa"
"crypto/tls"
"crypto/x509"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
// writeTestKeypair stands in for an operator-supplied certificate on disk.
func writeTestKeypair(t *testing.T) (certPath, keyPath string) {
t.Helper()
certPEM, keyPEM, err := generateSelfSignedPEM()
if err != nil {
t.Fatalf("generate: %v", err)
}
dir := t.TempDir()
certPath = filepath.Join(dir, "tls.crt")
keyPath = filepath.Join(dir, "tls.key")
if err := os.WriteFile(certPath, certPEM, 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(keyPath, keyPEM, 0o600); err != nil {
t.Fatal(err)
}
return certPath, keyPath
}
func TestGenerateSelfSignedCert_CompletesAHandshake(t *testing.T) {
cert, err := generateSelfSignedCert()
if err != nil {
t.Fatalf("generate: %v", err)
}
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}))
srv.TLS = &tls.Config{Certificates: []tls.Certificate{cert}}
srv.StartTLS()
defer srv.Close()
leaf, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
t.Fatalf("parse: %v", err)
}
pool := x509.NewCertPool()
pool.AddCert(leaf)
client := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool}}}
resp, err := client.Get(srv.URL)
if err != nil {
t.Fatalf("handshake failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("status = %d", resp.StatusCode)
}
}
func TestGenerateSelfSignedCert_DiffersEveryCall(t *testing.T) {
first, err := generateSelfSignedCert()
if err != nil {
t.Fatal(err)
}
second, err := generateSelfSignedCert()
if err != nil {
t.Fatal(err)
}
a, err := x509.ParseCertificate(first.Certificate[0])
if err != nil {
t.Fatal(err)
}
b, err := x509.ParseCertificate(second.Certificate[0])
if err != nil {
t.Fatal(err)
}
if a.SerialNumber.Cmp(b.SerialNumber) == 0 {
t.Error("two generated certificates share a serial number")
}
firstKey, ok := a.PublicKey.(*ecdsa.PublicKey)
if !ok {
t.Fatalf("unexpected key type %T", a.PublicKey)
}
if firstKey.Equal(b.PublicKey) {
t.Error("two generated certificates share a public key")
}
}
func TestGenerateSelfSignedCert_HasLocalhostSANs(t *testing.T) {
cert, err := generateSelfSignedCert()
if err != nil {
t.Fatal(err)
}
leaf, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
t.Fatal(err)
}
if err := leaf.VerifyHostname("localhost"); err != nil {
t.Errorf("localhost is not covered: %v", err)
}
if err := leaf.VerifyHostname("127.0.0.1"); err != nil {
t.Errorf("127.0.0.1 is not covered: %v", err)
}
}
func TestResolveServerCertificate_UsesConfiguredKeypair(t *testing.T) {
certPath, keyPath := writeTestKeypair(t)
got, err := resolveServerCertificate(ListenConfig{Cert: certPath, Key: keyPath})
if err != nil {
t.Fatalf("resolve: %v", err)
}
want, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
t.Fatal(err)
}
if string(got.Certificate[0]) != string(want.Certificate[0]) {
t.Error("resolved certificate is not the configured one")
}
}
func TestResolveServerCertificate_ConfiguredKeypairBeatsDevFlag(t *testing.T) {
certPath, keyPath := writeTestKeypair(t)
got, err := resolveServerCertificate(ListenConfig{Cert: certPath, Key: keyPath, DevSelfSignedCert: true})
if err != nil {
t.Fatalf("resolve: %v", err)
}
want, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
t.Fatal(err)
}
if string(got.Certificate[0]) != string(want.Certificate[0]) {
t.Error("the dev flag overrode an explicitly configured certificate")
}
}
// Falling back to a generated certificate here would silently serve an
// untrusted one in place of the certificate the operator asked for.
func TestResolveServerCertificate_UnreadableKeypairIsFatalEvenWithDevFlag(t *testing.T) {
_, err := resolveServerCertificate(ListenConfig{
Cert: "/nonexistent/tls.crt",
Key: "/nonexistent/tls.key",
DevSelfSignedCert: true,
})
if err == nil {
t.Fatal("expected an unreadable keypair to fail rather than fall back to generation")
}
if !strings.Contains(err.Error(), "listen.cert") {
t.Errorf("error should name the config field, got %v", err)
}
}
func TestResolveServerCertificate_MissingFileWithFlagOffIsFatal(t *testing.T) {
_, err := resolveServerCertificate(ListenConfig{
Cert: "/nonexistent/tls.crt",
Key: "/nonexistent/tls.key",
})
if err == nil {
t.Fatal("expected a missing keypair to fail startup")
}
for _, want := range []string{"listen.cert", "listen.key", "installation guide"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("error should mention %s, got %v", want, err)
}
}
}
func TestResolveServerCertificate_GeneratesWhenOnlyDevFlagSet(t *testing.T) {
cert, err := resolveServerCertificate(ListenConfig{DevSelfSignedCert: true})
if err != nil {
t.Fatalf("resolve: %v", err)
}
leaf, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
t.Fatal(err)
}
if leaf.Subject.CommonName != "philter-ai-proxy" {
t.Errorf("unexpected subject %q", leaf.Subject.CommonName)
}
}
func TestResolveServerCertificate_NoCertConfiguredFailsWithGuidance(t *testing.T) {
_, err := resolveServerCertificate(ListenConfig{})
if err == nil {
t.Fatal("expected startup to fail with no certificate configured")
}
for _, want := range []string{"listen.cert", "listen.key", "listen.devSelfSignedCert", "installation guide"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("error should mention %s, got %v", want, err)
}
}
}
func TestResolveServerCertificate_HalfConfiguredKeypairIsRejected(t *testing.T) {
certPath, _ := writeTestKeypair(t)
_, err := resolveServerCertificate(ListenConfig{Cert: certPath})
if err == nil || !strings.Contains(err.Error(), "listen.key") {
t.Errorf("expected a missing listen.key to be reported, got %v", err)
}
_, err = resolveServerCertificate(ListenConfig{Key: certPath})
if err == nil || !strings.Contains(err.Error(), "listen.cert") {
t.Errorf("expected a missing listen.cert to be reported, got %v", err)
}
}