Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

physical/posgresql: add ability to prefer VAULT_PG_CONNECTION_URL envar over config file #7937

Merged
merged 12 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions physical/postgresql/configure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package postgresql
catsby marked this conversation as resolved.
Show resolved Hide resolved

import (
"os"
)

// connectionURL first check the environment variables for a connection URL. If
// no connection URL exists in the environment variable, the Vault config file is
// checked. If neither the environment variables or the config file set the connection
// URL for the Postgres backend, because it is a required field, an error is returned.
func connectionURL(conf map[string]string) string {
connURL := os.Getenv("PG_CONNECTION_URL")
if connURL == "" {
connURL = conf["connection_url"]
if connURL == "" {
return ""
}
}

return connURL
mccurdyc marked this conversation as resolved.
Show resolved Hide resolved
}
65 changes: 65 additions & 0 deletions physical/postgresql/configure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package postgresql

import (
"os"
"testing"
)

func TestConnectionURL(t *testing.T) {
type input struct {
envar string
conf map[string]string
}

var cases = []struct {
name string
want string
input input
catsby marked this conversation as resolved.
Show resolved Hide resolved
}{
{
name: "environment_variable_not_set_use_config_value",
want: "abc",
input: input{
envar: "",
conf: map[string]string{"connection_url": "abc"},
},
},
{
name: "no_value_connection_url_set_key_exists",
want: "",
input: input{
envar: "",
conf: map[string]string{"connection_url": ""},
},
},
{
name: "no_value_connection_url_set_key_doesnt_exist",
want: "",
input: input{
envar: "",
conf: map[string]string{},
},
},
{
name: "environment_variable_set",
want: "abc",
input: input{
envar: "abc",
conf: map[string]string{"connection_url": "def"},
},
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("PG_CONNECTION_URL", tt.input.envar)
defer os.Setenv("PG_CONNECTION_URL", "")

got := connectionURL(tt.input.conf)

if got != tt.want {
t.Errorf("connectionURL(%s): want '%s', got '%s'", tt.input, tt.want, got)
}
})
}
}
4 changes: 2 additions & 2 deletions physical/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ type PostgreSQLLock struct {
// API client, server address, credentials, and database.
func NewPostgreSQLBackend(conf map[string]string, logger log.Logger) (physical.Backend, error) {
// Get the PostgreSQL credentials to perform read/write operations.
connURL, ok := conf["connection_url"]
if !ok || connURL == "" {
connURL := connectionURL(conf)
if connURL == "" {
return nil, fmt.Errorf("missing connection_url")
}

Expand Down
3 changes: 2 additions & 1 deletion website/source/docs/configuration/storage/postgresql.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ LANGUAGE plpgsql;
## `postgresql` Parameters

- `connection_url` `(string: <required>)` – Specifies the connection string to
use to authenticate and connect to PostgreSQL. A full list of supported
use to authenticate and connect to PostgreSQL. The connection URL can also be
set using the `PG_CONNECTION_URL` environment variable. A full list of supported
parameters can be found in [the pq library documentation][pglib]. For example
connection string URLs, see the examples section below.

Expand Down