Skip to content
This repository was archived by the owner on Feb 9, 2025. It is now read-only.

Commit 5f64cbe

Browse files
committed
Minor fixes
- remove `os/user` dependency - not need `CGO`
1 parent 2be4b58 commit 5f64cbe

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

ssl.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"crypto/x509"
88
"io/ioutil"
99
"os"
10-
"os/user"
1110
"path/filepath"
1211

1312
"github.com/pkg/errors"
@@ -78,20 +77,14 @@ func ssl(o values) (*tls.Config, error) {
7877
// in the user's home directory. The configured files must exist and have
7978
// the correct permissions.
8079
func sslClientCertificates(tlsConf *tls.Config, o values) error {
81-
// usr.Current() might fail when cross-compiling. We have to ignore the
82-
// error and continue without home directory defaults, since we wouldn't
83-
// know from where to load them.
84-
usr, err := user.Current()
85-
if err != nil {
86-
return err
87-
}
80+
home := userHomeDir()
8881

8982
// In libpq, the client certificate is only loaded if the setting is not blank.
9083
//
9184
// https://github.com/postgres/postgres/blob/REL9_6_2/src/interfaces/libpq/fe-secure-openssl.c#L1036-L1037
9285
sslcert := o["sslcert"]
93-
if len(sslcert) == 0 && usr != nil {
94-
sslcert = filepath.Join(usr.HomeDir, ".postgresql", "postgresql.crt")
86+
if len(sslcert) == 0 && home != "" {
87+
sslcert = filepath.Join(home, ".postgresql", "postgresql.crt")
9588
}
9689
// https://github.com/postgres/postgres/blob/REL9_6_2/src/interfaces/libpq/fe-secure-openssl.c#L1045
9790
if len(sslcert) == 0 {
@@ -108,8 +101,8 @@ func sslClientCertificates(tlsConf *tls.Config, o values) error {
108101
//
109102
// https://github.com/postgres/postgres/blob/REL9_6_2/src/interfaces/libpq/fe-secure-openssl.c#L1123-L1222
110103
sslkey := o["sslkey"]
111-
if len(sslkey) == 0 && usr != nil {
112-
sslkey = filepath.Join(usr.HomeDir, ".postgresql", "postgresql.key")
104+
if len(sslkey) == 0 && home != "" {
105+
sslkey = filepath.Join(home, ".postgresql", "postgresql.key")
113106
}
114107

115108
if len(sslkey) > 0 {

ssl_user.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build !windows
2+
3+
package postgres
4+
5+
import "os"
6+
7+
func userHomeDir() string { return os.Getenv("HOME") }

ssl_user_windows.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// +build windows
2+
3+
package postgres
4+
5+
import (
6+
"os"
7+
)
8+
9+
func userHomeDir() string {
10+
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
11+
if home == "" {
12+
home = os.Getenv("USERPROFILE")
13+
}
14+
return home
15+
}

0 commit comments

Comments
 (0)