2015-04-19 00:34:36 +00:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
2015-04-19 01:09:33 +00:00
|
|
|
func pathConfigConnection(b *backend) *framework.Path {
|
2015-04-19 00:34:36 +00:00
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "config/connection",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2015-10-03 03:47:10 +00:00
|
|
|
"connection_url": &framework.FieldSchema{
|
2015-04-19 00:34:36 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "DB connection string",
|
|
|
|
},
|
2015-10-03 03:47:10 +00:00
|
|
|
"value": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: `
|
|
|
|
DB connection string. Use 'connection_url' instead.
|
|
|
|
This will be deprecated.`,
|
|
|
|
},
|
2015-10-02 00:11:24 +00:00
|
|
|
"max_open_connections": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeInt,
|
|
|
|
Description: "Maximum number of open connections to the database",
|
|
|
|
},
|
2015-04-19 00:34:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2015-04-19 01:09:33 +00:00
|
|
|
logical.WriteOperation: b.pathConnectionWrite,
|
2015-04-19 00:34:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathConfigConnectionHelpSyn,
|
|
|
|
HelpDescription: pathConfigConnectionHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 01:09:33 +00:00
|
|
|
func (b *backend) pathConnectionWrite(
|
2015-04-19 00:34:36 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
|
|
|
connString := data.Get("value").(string)
|
2015-10-03 03:47:10 +00:00
|
|
|
connURL := data.Get("connection_url").(string)
|
2015-04-19 00:34:36 +00:00
|
|
|
|
2015-10-02 00:11:24 +00:00
|
|
|
maxOpenConns := data.Get("max_open_connections").(int)
|
|
|
|
if maxOpenConns == 0 {
|
|
|
|
maxOpenConns = 2
|
|
|
|
}
|
|
|
|
|
2015-04-19 00:34:36 +00:00
|
|
|
// Verify the string
|
|
|
|
db, err := sql.Open("postgres", connString)
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
|
|
|
"Error validating connection info: %s", err)), nil
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
if err := db.Ping(); err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
|
|
|
"Error validating connection info: %s", err)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store it
|
2015-10-02 00:11:24 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("config/connection", connectionConfig{
|
|
|
|
ConnectionString: connString,
|
2015-10-03 03:47:10 +00:00
|
|
|
ConnectionURL: connURL,
|
2015-10-02 00:11:24 +00:00
|
|
|
MaxOpenConnections: maxOpenConns,
|
|
|
|
})
|
2015-04-19 00:34:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-19 01:09:33 +00:00
|
|
|
// Reset the DB connection
|
|
|
|
b.ResetDB()
|
|
|
|
|
2015-04-19 00:34:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-10-02 00:11:24 +00:00
|
|
|
type connectionConfig struct {
|
2015-10-03 03:47:10 +00:00
|
|
|
ConnectionURL string `json:"connection_url"`
|
2015-10-02 16:07:31 +00:00
|
|
|
// Deprecate "value" in coming releases
|
2015-10-03 03:47:10 +00:00
|
|
|
ConnectionString string `json:"value"`
|
2015-10-02 00:11:24 +00:00
|
|
|
MaxOpenConnections int `json:"max_open_connections"`
|
|
|
|
}
|
|
|
|
|
2015-04-19 00:34:36 +00:00
|
|
|
const pathConfigConnectionHelpSyn = `
|
|
|
|
Configure the connection string to talk to PostgreSQL.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathConfigConnectionHelpDesc = `
|
|
|
|
This path configures the connection string used to connect to PostgreSQL.
|
|
|
|
The value of the string can be a URL, or a PG style string in the
|
|
|
|
format of "user=foo host=bar" etc.
|
|
|
|
|
2015-04-27 19:00:07 +00:00
|
|
|
The URL looks like:
|
|
|
|
"postgresql://user:pass@host:port/dbname"
|
|
|
|
|
2015-04-19 00:34:36 +00:00
|
|
|
When configuring the connection string, the backend will verify its validity.
|
|
|
|
`
|