2015-04-30 19:47:07 +03:00
|
|
|
package connection
|
2014-12-18 06:32:50 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-08-16 05:11:09 +03:00
|
|
|
neturl "net/url"
|
2015-01-10 07:55:00 +03:00
|
|
|
"os"
|
2014-12-18 06:32:50 +03:00
|
|
|
"os/user"
|
|
|
|
"strings"
|
2015-04-30 19:47:07 +03:00
|
|
|
|
|
|
|
"github.com/sosedoff/pgweb/pkg/command"
|
2014-12-18 06:32:50 +03:00
|
|
|
)
|
|
|
|
|
2019-01-28 23:03:45 +03:00
|
|
|
// Common errors
|
2018-09-14 06:25:15 +03:00
|
|
|
var (
|
2019-01-28 23:03:45 +03:00
|
|
|
errCantDetectUser = errors.New("Could not detect default username")
|
|
|
|
errInvalidURLFormat = errors.New("Invalid URL. Valid format: postgres://user:password@host:port/db?sslmode=mode")
|
2018-09-14 06:25:15 +03:00
|
|
|
)
|
|
|
|
|
2019-01-28 23:03:45 +03:00
|
|
|
// currentUser returns a current user name
|
2015-01-10 07:55:00 +03:00
|
|
|
func currentUser() (string, error) {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err == nil {
|
|
|
|
return u.Username, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name := os.Getenv("USER")
|
|
|
|
if name != "" {
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2019-01-28 23:03:45 +03:00
|
|
|
return "", errCantDetectUser
|
2015-01-10 07:55:00 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 06:25:15 +03:00
|
|
|
// Check if connection url has a correct postgres prefix
|
|
|
|
func hasValidPrefix(str string) bool {
|
|
|
|
return strings.HasPrefix(str, "postgres://") || strings.HasPrefix(str, "postgresql://")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract all query vals and return as a map
|
|
|
|
func valsFromQuery(vals neturl.Values) map[string]string {
|
|
|
|
result := map[string]string{}
|
|
|
|
for k, v := range vals {
|
|
|
|
result[strings.ToLower(k)] = v[0]
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-01-28 23:03:45 +03:00
|
|
|
// FormatURL reformats the existing connection string
|
|
|
|
func FormatURL(opts command.Options) (string, error) {
|
2019-11-02 21:00:23 +03:00
|
|
|
url := opts.URL
|
2014-12-18 06:32:50 +03:00
|
|
|
|
2018-09-14 06:25:15 +03:00
|
|
|
// Validate connection string prefix
|
|
|
|
if !hasValidPrefix(url) {
|
2019-01-28 23:03:45 +03:00
|
|
|
return "", errInvalidURLFormat
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 06:25:15 +03:00
|
|
|
// Validate the URL
|
|
|
|
uri, err := neturl.Parse(url)
|
|
|
|
if err != nil {
|
2019-01-28 23:03:45 +03:00
|
|
|
return "", errInvalidURLFormat
|
2018-09-14 06:25:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get query params
|
|
|
|
params := valsFromQuery(uri.Query())
|
|
|
|
|
|
|
|
// Determine if we need to specify sslmode if it's missing
|
|
|
|
if params["sslmode"] == "" {
|
2022-11-24 01:21:30 +03:00
|
|
|
if opts.SSLMode == "" {
|
2018-09-14 06:25:15 +03:00
|
|
|
// Only modify sslmode for local connections
|
|
|
|
if strings.Contains(uri.Host, "localhost") || strings.Contains(uri.Host, "127.0.0.1") {
|
|
|
|
params["sslmode"] = "disable"
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|
2018-09-14 06:25:15 +03:00
|
|
|
} else {
|
2022-11-24 01:21:30 +03:00
|
|
|
params["sslmode"] = opts.SSLMode
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 06:25:15 +03:00
|
|
|
// Rebuild query params
|
|
|
|
query := neturl.Values{}
|
|
|
|
for k, v := range params {
|
|
|
|
query.Add(k, v)
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|
2018-09-14 06:25:15 +03:00
|
|
|
uri.RawQuery = query.Encode()
|
2014-12-18 06:32:50 +03:00
|
|
|
|
2018-09-14 06:25:15 +03:00
|
|
|
return uri.String(), nil
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:03:45 +03:00
|
|
|
// IsBlank returns true if command options do not contain connection details
|
2015-04-30 19:47:07 +03:00
|
|
|
func IsBlank(opts command.Options) bool {
|
2019-11-02 21:00:23 +03:00
|
|
|
return opts.Host == "" && opts.User == "" && opts.DbName == "" && opts.URL == ""
|
2014-12-18 06:56:15 +03:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:03:45 +03:00
|
|
|
// BuildStringFromOptions returns a new connection string built from options
|
2018-09-14 06:44:11 +03:00
|
|
|
func BuildStringFromOptions(opts command.Options) (string, error) {
|
2020-02-06 07:01:25 +03:00
|
|
|
query := neturl.Values{}
|
|
|
|
|
2018-09-14 06:44:11 +03:00
|
|
|
// If connection string is provided we just use that
|
2019-11-02 21:00:23 +03:00
|
|
|
if opts.URL != "" {
|
2019-01-28 23:03:45 +03:00
|
|
|
return FormatURL(opts)
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to detect user from current OS user
|
|
|
|
if opts.User == "" {
|
2015-01-10 07:55:00 +03:00
|
|
|
u, err := currentUser()
|
2014-12-18 06:32:50 +03:00
|
|
|
if err == nil {
|
2015-01-10 07:55:00 +03:00
|
|
|
opts.User = u
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 01:21:30 +03:00
|
|
|
if opts.SSLMode != "" {
|
|
|
|
query.Add("sslmode", opts.SSLMode)
|
2020-02-06 07:01:25 +03:00
|
|
|
} else {
|
|
|
|
if opts.Host == "localhost" || opts.Host == "127.0.0.1" {
|
|
|
|
query.Add("sslmode", "disable")
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 01:21:30 +03:00
|
|
|
if opts.SSLCert != "" {
|
|
|
|
query.Add("sslcert", opts.SSLCert)
|
2020-02-06 07:09:13 +03:00
|
|
|
}
|
2022-11-24 01:21:30 +03:00
|
|
|
if opts.SSLKey != "" {
|
|
|
|
query.Add("sslkey", opts.SSLKey)
|
2020-02-06 07:09:13 +03:00
|
|
|
}
|
2022-11-24 01:21:30 +03:00
|
|
|
if opts.SSLRootCert != "" {
|
|
|
|
query.Add("sslrootcert", opts.SSLRootCert)
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 06:44:11 +03:00
|
|
|
url := neturl.URL{
|
|
|
|
Scheme: "postgres",
|
|
|
|
Host: fmt.Sprintf("%v:%v", opts.Host, opts.Port),
|
|
|
|
User: neturl.UserPassword(opts.User, opts.Pass),
|
|
|
|
Path: fmt.Sprintf("/%s", opts.DbName),
|
|
|
|
RawQuery: query.Encode(),
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|
|
|
|
|
2018-09-14 06:44:11 +03:00
|
|
|
return url.String(), nil
|
2014-12-18 06:32:50 +03:00
|
|
|
}
|