Add bookmark options to load username/password from env vars (#638)
This commit is contained in:
parent
79bd9c2f4e
commit
06212b4c34
@ -1,21 +1,25 @@
|
|||||||
package bookmarks
|
package bookmarks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/sosedoff/pgweb/pkg/command"
|
"github.com/sosedoff/pgweb/pkg/command"
|
||||||
"github.com/sosedoff/pgweb/pkg/shared"
|
"github.com/sosedoff/pgweb/pkg/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Bookmark contains information about bookmarked database connection
|
// Bookmark contains information about bookmarked database connection
|
||||||
type Bookmark struct {
|
type Bookmark struct {
|
||||||
ID string // ID generated from the filename
|
ID string // ID generated from the filename
|
||||||
URL string // Postgres connection URL
|
URL string // Postgres connection URL
|
||||||
Host string // Server hostname
|
Host string // Server hostname
|
||||||
Port int // Server port
|
Port int // Server port
|
||||||
User string // Database user
|
User string // Database user
|
||||||
Password string // User password
|
UserVar string // Database user environment variable
|
||||||
Database string // Database name
|
Password string // User password
|
||||||
SSLMode string // Connection SSL mode
|
PasswordVar string // User password environment variable
|
||||||
SSH *shared.SSHInfo // SSH tunnel config
|
Database string // Database name
|
||||||
|
SSLMode string // Connection SSL mode
|
||||||
|
SSH *shared.SSHInfo // SSH tunnel config
|
||||||
}
|
}
|
||||||
|
|
||||||
// SSHInfoIsEmpty returns true if ssh configuration is not provided
|
// SSHInfoIsEmpty returns true if ssh configuration is not provided
|
||||||
@ -25,12 +29,22 @@ func (b Bookmark) SSHInfoIsEmpty() bool {
|
|||||||
|
|
||||||
// ConvertToOptions returns an options struct from connection details
|
// ConvertToOptions returns an options struct from connection details
|
||||||
func (b Bookmark) ConvertToOptions() command.Options {
|
func (b Bookmark) ConvertToOptions() command.Options {
|
||||||
|
user := b.User
|
||||||
|
if b.User == "" {
|
||||||
|
user = os.Getenv(b.UserVar)
|
||||||
|
}
|
||||||
|
|
||||||
|
pass := b.Password
|
||||||
|
if b.Password == "" {
|
||||||
|
pass = os.Getenv(b.PasswordVar)
|
||||||
|
}
|
||||||
|
|
||||||
return command.Options{
|
return command.Options{
|
||||||
URL: b.URL,
|
URL: b.URL,
|
||||||
Host: b.Host,
|
Host: b.Host,
|
||||||
Port: b.Port,
|
Port: b.Port,
|
||||||
User: b.User,
|
User: user,
|
||||||
Pass: b.Password,
|
Pass: pass,
|
||||||
DbName: b.Database,
|
DbName: b.Database,
|
||||||
SSLMode: b.SSLMode,
|
SSLMode: b.SSLMode,
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,63 @@ func TestBookmarkSSHInfoIsEmpty(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBookmarkWithVarsConvertToOptions(t *testing.T) {
|
||||||
|
t.Run("literals set", func(t *testing.T) {
|
||||||
|
b := Bookmark{
|
||||||
|
User: "user",
|
||||||
|
UserVar: "",
|
||||||
|
Password: "password",
|
||||||
|
PasswordVar: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
expOpt := command.Options{
|
||||||
|
User: "user",
|
||||||
|
Pass: "password",
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := b.ConvertToOptions()
|
||||||
|
assert.Equal(t, expOpt, opt)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("all set", func(t *testing.T) {
|
||||||
|
b := Bookmark{
|
||||||
|
User: "user",
|
||||||
|
UserVar: "DB_USER",
|
||||||
|
Password: "password",
|
||||||
|
PasswordVar: "DB_PASSWORD",
|
||||||
|
}
|
||||||
|
|
||||||
|
expOpt := command.Options{
|
||||||
|
User: "user",
|
||||||
|
Pass: "password",
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Setenv("DB_USER", "user123")
|
||||||
|
t.Setenv("DB_PASSWORD", "password123")
|
||||||
|
opt := b.ConvertToOptions()
|
||||||
|
assert.Equal(t, expOpt, opt)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("env vars set", func(t *testing.T) {
|
||||||
|
b := Bookmark{
|
||||||
|
User: "",
|
||||||
|
UserVar: "DB_USER",
|
||||||
|
Password: "",
|
||||||
|
PasswordVar: "DB_PASSWORD",
|
||||||
|
}
|
||||||
|
|
||||||
|
expOpt := command.Options{
|
||||||
|
User: "user123",
|
||||||
|
Pass: "password123",
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Setenv("DB_USER", "user123")
|
||||||
|
t.Setenv("DB_PASSWORD", "password123")
|
||||||
|
opt := b.ConvertToOptions()
|
||||||
|
assert.Equal(t, expOpt, opt)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestBookmarkConvertToOptions(t *testing.T) {
|
func TestBookmarkConvertToOptions(t *testing.T) {
|
||||||
b := Bookmark{
|
b := Bookmark{
|
||||||
URL: "postgres://username:password@host:port/database?sslmode=disable",
|
URL: "postgres://username:password@host:port/database?sslmode=disable",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user