Skip to content

Commit 072e83d

Browse files
authored
Merge pull request #818 from eirslett/feature/ssl-inline
Support inline SSL certificates
2 parents 4604d39 + 1467baf commit 072e83d

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

ssl.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func ssl(o values) (func(net.Conn) (net.Conn, error), error) {
5959
return nil, err
6060
}
6161

62+
// This pseudo-parameter is not recognized by the PostgreSQL server, so let's delete it after use.
63+
delete(o, "sslinline")
64+
6265
// Accept renegotiation requests initiated by the backend.
6366
//
6467
// Renegotiation was deprecated then removed from PostgreSQL 9.5, but
@@ -83,6 +86,19 @@ func ssl(o values) (func(net.Conn) (net.Conn, error), error) {
8386
// in the user's home directory. The configured files must exist and have
8487
// the correct permissions.
8588
func sslClientCertificates(tlsConf *tls.Config, o values) error {
89+
sslinline := o["sslinline"]
90+
if sslinline == "true" {
91+
cert, err := tls.X509KeyPair([]byte(o["sslcert"]), []byte(o["sslkey"]))
92+
// Clear out these params, in case they were to be sent to the PostgreSQL server by mistake
93+
o["sslcert"] = ""
94+
o["sslkey"] = ""
95+
if err != nil {
96+
return err
97+
}
98+
tlsConf.Certificates = []tls.Certificate{cert}
99+
return nil
100+
}
101+
86102
// user.Current() might fail when cross-compiling. We have to ignore the
87103
// error and continue without home directory defaults, since we wouldn't
88104
// know from where to load them.
@@ -137,9 +153,19 @@ func sslCertificateAuthority(tlsConf *tls.Config, o values) error {
137153
if sslrootcert := o["sslrootcert"]; len(sslrootcert) > 0 {
138154
tlsConf.RootCAs = x509.NewCertPool()
139155

140-
cert, err := ioutil.ReadFile(sslrootcert)
141-
if err != nil {
142-
return err
156+
sslinline := o["sslinline"]
157+
158+
var cert []byte
159+
if sslinline == "true" {
160+
// // Clear out this param, in case it were to be sent to the PostgreSQL server by mistake
161+
o["sslrootcert"] = ""
162+
cert = []byte(sslrootcert)
163+
} else {
164+
var err error
165+
cert, err = ioutil.ReadFile(sslrootcert)
166+
if err != nil {
167+
return err
168+
}
143169
}
144170

145171
if !tlsConf.RootCAs.AppendCertsFromPEM(cert) {

url.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ func ParseURL(url string) (string, error) {
4040
}
4141

4242
var kvs []string
43-
escaper := strings.NewReplacer(` `, `\ `, `'`, `\'`, `\`, `\\`)
43+
escaper := strings.NewReplacer(`'`, `\'`, `\`, `\\`)
4444
accrue := func(k, v string) {
4545
if v != "" {
46-
kvs = append(kvs, k+"="+escaper.Replace(v))
46+
kvs = append(kvs, k+"='"+escaper.Replace(v)+"'")
4747
}
4848
}
4949

url_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
func TestSimpleParseURL(t *testing.T) {
8-
expected := "host=hostname.remote"
8+
expected := "host='hostname.remote'"
99
str, err := ParseURL("postgres://hostname.remote")
1010
if err != nil {
1111
t.Fatal(err)
@@ -17,7 +17,7 @@ func TestSimpleParseURL(t *testing.T) {
1717
}
1818

1919
func TestIPv6LoopbackParseURL(t *testing.T) {
20-
expected := "host=::1 port=1234"
20+
expected := "host='::1' port='1234'"
2121
str, err := ParseURL("postgres://[::1]:1234")
2222
if err != nil {
2323
t.Fatal(err)
@@ -29,7 +29,7 @@ func TestIPv6LoopbackParseURL(t *testing.T) {
2929
}
3030

3131
func TestFullParseURL(t *testing.T) {
32-
expected := `dbname=database host=hostname.remote password=top\ secret port=1234 user=username`
32+
expected := `dbname='database' host='hostname.remote' password='top secret' port='1234' user='username'`
3333
str, err := ParseURL("postgres://username:top%[email protected]:1234/database")
3434
if err != nil {
3535
t.Fatal(err)

0 commit comments

Comments
 (0)