2015-04-25 19:05:26 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathConfigConnection(b *backend) *framework.Path {
|
|
|
|
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-25 19:05:26 +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 01:15:56 +00:00
|
|
|
"max_open_connections": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeInt,
|
|
|
|
Description: "Maximum number of open connections to database",
|
|
|
|
},
|
2016-02-18 22:02:59 +00:00
|
|
|
"allow_verification": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: true,
|
|
|
|
Description: "If set, connection_url is verified by actually connecting to the database",
|
|
|
|
},
|
2015-04-25 19:05:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-01-07 15:30:47 +00:00
|
|
|
logical.UpdateOperation: b.pathConnectionWrite,
|
2015-04-25 19:05:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathConfigConnectionHelpSyn,
|
|
|
|
HelpDescription: pathConfigConnectionHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) pathConnectionWrite(
|
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2016-02-18 22:02:59 +00:00
|
|
|
connValue := data.Get("value").(string)
|
2015-10-03 03:47:10 +00:00
|
|
|
connURL := data.Get("connection_url").(string)
|
2016-02-18 22:02:59 +00:00
|
|
|
if connURL == "" {
|
|
|
|
if connValue == "" {
|
2016-02-19 21:07:00 +00:00
|
|
|
return logical.ErrorResponse("the connection_url parameter must be supplied"), nil
|
2016-02-18 22:02:59 +00:00
|
|
|
} else {
|
|
|
|
connURL = connValue
|
|
|
|
}
|
|
|
|
}
|
2015-04-25 19:05:26 +00:00
|
|
|
|
2015-10-02 01:15:56 +00:00
|
|
|
maxOpenConns := data.Get("max_open_connections").(int)
|
|
|
|
if maxOpenConns == 0 {
|
|
|
|
maxOpenConns = 2
|
|
|
|
}
|
|
|
|
|
2016-02-18 22:02:59 +00:00
|
|
|
// Don't check the connection_url if verification is disabled
|
|
|
|
allowVerification := data.Get("allow_verification").(bool)
|
|
|
|
if allowVerification {
|
|
|
|
// Verify the string
|
|
|
|
db, err := sql.Open("mysql", connURL)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-04-25 19:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store it
|
2015-10-02 01:15:56 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("config/connection", connectionConfig{
|
2015-10-03 03:47:10 +00:00
|
|
|
ConnectionURL: connURL,
|
2015-10-02 01:15:56 +00:00
|
|
|
MaxOpenConnections: maxOpenConns,
|
|
|
|
})
|
2015-04-25 19:05:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the DB connection
|
|
|
|
b.ResetDB()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-10-02 01:15:56 +00:00
|
|
|
type connectionConfig struct {
|
2016-02-19 19:59:15 +00:00
|
|
|
ConnectionURL string `json:"connection_url"`
|
|
|
|
// Deprecate "value" in coming releases
|
|
|
|
ConnectionString string `json:"value"`
|
2015-10-02 01:15:56 +00:00
|
|
|
MaxOpenConnections int `json:"max_open_connections"`
|
|
|
|
}
|
|
|
|
|
2015-04-25 19:05:26 +00:00
|
|
|
const pathConfigConnectionHelpSyn = `
|
|
|
|
Configure the connection string to talk to MySQL.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathConfigConnectionHelpDesc = `
|
|
|
|
This path configures the connection string used to connect to MySQL.
|
|
|
|
The value of the string is a Data Source Name (DSN). An example is
|
|
|
|
using "username:password@protocol(address)/dbname?param=value"
|
|
|
|
|
|
|
|
For example, RDS may look like: "id:password@tcp(your-amazonaws-uri.com:3306)/dbname"
|
|
|
|
|
|
|
|
When configuring the connection string, the backend will verify its validity.
|
2016-02-18 22:02:59 +00:00
|
|
|
In case the database needs to be provisioned beforehand, disable the verification
|
|
|
|
of connection URL using "allow_verification" option, which defaults to "true".
|
2015-04-25 19:05:26 +00:00
|
|
|
`
|