-
-
Notifications
You must be signed in to change notification settings - Fork 945
Expand file tree
/
Copy pathssl_test.go
More file actions
380 lines (333 loc) · 10.8 KB
/
ssl_test.go
File metadata and controls
380 lines (333 loc) · 10.8 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package pq
import (
"bytes"
_ "crypto/sha256"
"crypto/tls"
"database/sql"
"fmt"
"io"
"net"
"os"
"testing"
"time"
"github.com/lib/pq/internal/pqtest"
"github.com/lib/pq/internal/proto"
)
func openSSLConn(t *testing.T, conninfo ...string) (*sql.DB, error) {
db := pqtest.MustDB(t, conninfo...)
// Do something with the connection to see whether it's working or not.
return db, db.Ping()
}
// Environment sanity check: should fail without SSL
func startSSLTest(t *testing.T, user string) {
t.Parallel()
wantErr := `invalid_authorization_specification`
if pqtest.Pgbouncer() {
wantErr = "protocol_violation"
} else if pqtest.Pgpool() {
wantErr = "internal_error"
}
_, err := openSSLConn(t, "sslmode=disable user="+user)
pqErr := pqError(t, err)
if pqErr.Code.Name() != wantErr {
t.Fatalf("wrong error code %q", pqErr.Code.Name())
}
}
func TestSSLMode(t *testing.T) {
f := pqtest.NewFake(t, func(f pqtest.Fake, cn net.Conn) {
f.Startup(cn, nil)
for {
code, _, ok := f.ReadMsg(cn)
if !ok {
return
}
switch code {
case proto.Query:
f.WriteMsg(cn, proto.EmptyQueryResponse, "")
f.WriteMsg(cn, proto.ReadyForQuery, "I")
case proto.Terminate:
cn.Close()
return
}
}
})
tests := []struct {
connect string
wantErr string
}{
// Default (prefer) should work both with and without ssl.
{"user=pqgossl", ""},
{f.DSN(), ""},
// sslmode=require: require SSL, but don't verify certificate.
{"sslmode=require user=pqgossl", ""},
{"sslmode=require " + f.DSN(), "pq: SSL is not enabled on the server"},
// sslmode=verify-ca: verify that the certificate was signed by a trusted CA
{"host=postgres sslmode=verify-ca user=pqgossl", "invalid-cert"},
{"host=postgres sslmode=verify-ca user=pqgossl sslrootcert=''", "invalid-cert"},
{"sslrootcert=testdata/init/root.crt sslmode=verify-ca user=pqgossl host=127.0.0.1", ""},
{"sslrootcert=testdata/init/root.crt sslmode=verify-ca user=pqgossl host=postgres-invalid", ""},
{"sslrootcert=testdata/init/root.crt sslmode=verify-ca user=pqgossl host=postgres", ""},
// sslmode=verify-full: verify that the certification was signed by a trusted CA and the host matches
{"sslmode=verify-full user=pqgossl host=postgres", "invalid-cert"},
{"sslrootcert=testdata/init/root.crt sslmode=verify-full user=pqgossl host=127.0.0.1", "invalid-cert"},
{"sslrootcert=testdata/init/root.crt sslmode=verify-full user=pqgossl host=postgres-invalid", "invalid-cert"},
{"sslrootcert=testdata/init/root.crt sslmode=verify-full user=pqgossl host=postgres", ""},
// With root cert
{"sslrootcert=testdata/init/bogus_root.crt host=postgres sslmode=require user=pqgossl", "invalid-cert"},
{"sslrootcert=testdata/init/non_existent.crt host=127.0.0.1 sslmode=require user=pqgossl", ""},
{"sslrootcert=testdata/init/root.crt host=127.0.0.1 sslmode=require user=pqgossl", ""},
{"sslrootcert=testdata/init/root.crt host=postgres sslmode=require user=pqgossl", ""},
{"sslrootcert=testdata/init/root.crt host=postgres-invalid sslmode=require user=pqgossl", ""},
// sslmode=prefer
{"sslmode=prefer user=pqgossl", ""},
{"sslmode=prefer", ""},
{"sslmode=prefer user=pqgossl " + f.DSN(), ""}, // Doesn't support SSL, so try again without.
// sslmode=allow
{"sslmode=allow user=pqgossl", ""}, // Requires SSL, so will try again
{"sslmode=allow", ""}, // Doesn't need SSL, should just work.
{"sslmode=allow " + f.DSN(), ""}, // Idem
// sslmode=disable
{"sslmode=disable user=pqgossl", "no encryption"},
// sslnegotiation=direct should fail if ssl isn't required, like libpq:
// psql: error: weak sslmode "allow" may not be used with sslnegotiation=direct (use "require", "verify-ca", or "verify-full")
{"sslmode=disable sslnegotiation=direct", "weak sslmode"},
{"sslmode=allow sslnegotiation=direct", "weak sslmode"},
{"sslmode=prefer sslnegotiation=direct", "weak sslmode"},
}
startSSLTest(t, "pqgossl")
for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
t.Parallel()
if tt.wantErr == "no encryption" && pqtest.Pgbouncer() {
// PostgreSQL repsonds with:
// pq: pg_hba.conf rejects connection for host "172.18.0.1", user "pqgossl", database "pqgo", no encryption (28000)
//
// But pgbouncer has a different message and code:
// pq: login rejected (08P01)
tt.wantErr = "login rejected"
}
_, err := openSSLConn(t, tt.connect)
t.Log(tt.connect)
switch {
case tt.wantErr == "" && err != nil:
t.Fatalf("\nfailed for %q\n%s", tt.connect, err)
case tt.wantErr == "invalid-cert":
if !pqtest.InvalidCertificate(err) {
t.Fatalf("wrong error type %T: %[1]s", err)
}
case !pqtest.ErrorContains(err, tt.wantErr):
t.Fatalf("wrong error\nwant: %s\nhave: %s", tt.wantErr, err)
}
})
}
}
// Authenticate over SSL using client certificates
func TestSSLClientCertificates(t *testing.T) {
pqtest.SkipPgpool(t) // TODO: can't get it to work.
pqtest.SkipPgbouncer(t) // TODO: can't get it to work.
startSSLTest(t, "pqgosslcert")
// Make sure the permissions of the keyfile are correct, or it won't load.
// TODO: will probably fail on Windows? Dunno.
err := os.Chmod("testdata/init/postgresql.key", 0o600)
if err != nil {
t.Fatal(err)
}
tests := []struct {
connect string
wantErr string
}{
{"sslmode=require user=pqgosslcert", "requires a valid client certificate (28000)"},
{"sslmode=require user=pqgosslcert sslcert=''", "requires a valid client certificate (28000)"},
{"sslmode=require user=pqgosslcert sslcert=/tmp/filedoesnotexist", "requires a valid client certificate (28000)"},
{"sslmode=require user=pqgosslcert sslcert=testdata/init/postgresql.crt", "directory"},
{"sslmode=require user=pqgosslcert sslcert=testdata/init/postgresql.crt sslkey=''", "directory"},
{"sslmode=require user=pqgosslcert sslcert=testdata/init/postgresql.crt sslkey=/tmp/filedoesnotexist", "no such file or directory"},
{"sslmode=require user=pqgosslcert sslcert=testdata/init/postgresql.crt sslkey=testdata/init/postgresql.crt", "has world access"},
{"sslmode=require user=pqgosslcert sslcert=testdata/init/postgresql.crt sslkey=testdata/init/postgresql.key", ""},
{fmt.Sprintf("sslmode=require user=pqgosslcert sslinline=true sslcert='%s' sslkey='%s'",
pqtest.Read(t, "testdata/init/postgresql.crt"),
pqtest.Read(t, "testdata/init/postgresql.key")),
""},
}
for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
t.Parallel()
db, err := openSSLConn(t, tt.connect)
if !pqtest.ErrorContains(err, tt.wantErr) {
t.Fatalf("wrong error\nwant: %s\nhave: %s", tt.wantErr, err)
}
if err == nil {
rows, err := db.Query("select 1")
if err != nil {
t.Fatal(err)
}
if err := rows.Close(); err != nil {
t.Fatal(err)
}
}
})
}
}
// Check that clint sends SNI data when sslsni is not disabled
func TestSSLSNI(t *testing.T) {
startSSLTest(t, "pqgosslcert")
tests := []struct {
name string
connect string
hostname string
wantSNI string
direct bool
}{
{
name: "SNI is set by default",
connect: "sslmode=require",
hostname: "localhost",
wantSNI: "localhost",
},
{
name: "SNI is passed when asked for",
connect: "sslmode=require sslsni=1",
hostname: "localhost",
wantSNI: "localhost",
},
{
name: "SNI is not passed when disabled",
connect: "sslmode=require sslsni=0",
hostname: "localhost",
wantSNI: "",
},
{
name: "SNI is not set for IPv4",
connect: "sslmode=require",
hostname: "127.0.0.1",
wantSNI: "",
},
{
name: "SNI is set for when CN doesn't match",
connect: "sslmode=require",
hostname: "postgres-invalid",
wantSNI: "postgres-invalid",
},
{
name: "SNI is set for negotiated ssl",
connect: "sslmode=require sslnegotiation=postgres",
hostname: "localhost",
wantSNI: "localhost",
},
{
name: "SNI is set for direct ssl",
connect: "sslmode=require sslnegotiation=direct",
hostname: "localhost",
wantSNI: "localhost",
direct: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
port, nameCh, errCh := mockPostgresSSL(t, tt.direct)
// We are okay to skip this error as we are polling errCh and we'll
// get an error or timeout from the server side in case of problems
// here.
db, _ := sql.Open("postgres", fmt.Sprintf("host=%s port=%s %s", tt.hostname, port, tt.connect))
_, _ = db.Exec("select 1")
// Check SNI data
select {
case <-time.After(time.Second):
t.Fatal("exceeded connection timeout without erroring out")
case err := <-errCh:
t.Fatal(err)
case name := <-nameCh:
if name != tt.wantSNI {
t.Fatalf("have: %q\nwant: %q", name, tt.wantSNI)
}
}
})
}
}
func TestUnreadableHome(t *testing.T) {
// Ignore HOME being unset or not a directory
for _, h := range []string{"", "/dev/null"} {
os.Setenv("HOME", h)
err := sslClientCertificates(&tls.Config{}, Config{})
if err != nil {
t.Fatal(err)
}
}
}
// Make a postgres mock server to test TLS SNI
//
// Accepts postgres StartupMessage and handles TLS clientHello, then closes a
// connection. While reading clientHello catch passed SNI data and report it to
// nameChan.
func mockPostgresSSL(t *testing.T, direct bool) (string, chan string, chan error) {
l, err := net.Listen("tcp", "127.0.0.1:")
if err != nil {
t.Fatal(err)
return "", nil, nil
}
_, port, err := net.SplitHostPort(l.Addr().String())
if err != nil {
t.Fatal(err)
return "", nil, nil
}
var (
nameCh = make(chan string, 1)
errCh = make(chan error, 1)
)
go func() {
conn, err := l.Accept()
if err != nil {
errCh <- err
return
}
defer conn.Close()
t.Cleanup(func() {
close(errCh)
close(nameCh)
l.Close()
})
err = conn.SetDeadline(time.Now().Add(time.Second))
if err != nil {
errCh <- err
return
}
if !direct {
// Receive StartupMessage with SSL Request
startupMessage := make([]byte, 8)
_, err := io.ReadFull(conn, startupMessage)
if err != nil {
errCh <- err
return
}
// StartupMessage: first four bytes -- total len = 8, last four bytes SslRequestNumber
if !bytes.Equal(startupMessage, []byte{0, 0, 0, 0x8, 0x4, 0xd2, 0x16, 0x2f}) {
errCh <- fmt.Errorf("unexpected startup message: %#v", startupMessage)
return
}
// Respond with SSLOk
_, err = conn.Write([]byte("S"))
if err != nil {
errCh <- err
return
}
}
// Set up TLS context to catch clientHello. It will always error out during
// handshake as no certificate is set.
var sniHost string
srv := tls.Server(conn, &tls.Config{
GetConfigForClient: func(argHello *tls.ClientHelloInfo) (*tls.Config, error) {
sniHost = argHello.ServerName
return nil, nil
},
})
defer srv.Close()
// Do the TLS handshake ignoring errors
_ = srv.Handshake()
nameCh <- sniHost
}()
return port, nameCh, errCh
}