open-vault/builtin/logical/mongodb/path_config_connection.go

113 lines
3.2 KiB
Go
Raw Normal View History

2016-05-13 20:42:09 +00:00
package mongodb
import (
"context"
2016-05-13 20:42:09 +00:00
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
mgo "gopkg.in/mgo.v2"
2016-05-13 20:42:09 +00:00
)
func pathConfigConnection(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/connection",
Fields: map[string]*framework.FieldSchema{
"uri": {
2016-05-13 20:42:09 +00:00
Type: framework.TypeString,
Description: "MongoDB standard connection string (URI)",
},
"verify_connection": {
2016-05-13 20:42:09 +00:00
Type: framework.TypeBool,
Default: true,
Description: `If set, uri is verified by actually connecting to the database`,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathConnectionRead,
2016-05-13 20:42:09 +00:00
logical.UpdateOperation: b.pathConnectionWrite,
},
HelpSynopsis: pathConfigConnectionHelpSyn,
HelpDescription: pathConfigConnectionHelpDesc,
}
}
// pathConnectionRead reads out the connection configuration
func (b *backend) pathConnectionRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entry, err := req.Storage.Get(ctx, "config/connection")
if err != nil {
return nil, fmt.Errorf("failed to read connection configuration")
}
if entry == nil {
return nil, nil
}
return nil, nil
}
func (b *backend) pathConnectionWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
2016-05-13 20:42:09 +00:00
uri := data.Get("uri").(string)
if uri == "" {
return logical.ErrorResponse("uri parameter is required"), nil
2016-05-13 20:42:09 +00:00
}
dialInfo, err := parseMongoURI(uri)
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("invalid uri: %s", err)), nil
}
// Don't check the config if verification is disabled
verifyConnection := data.Get("verify_connection").(bool)
if verifyConnection {
// Verify the config
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
return logical.ErrorResponse(fmt.Sprintf(
"Error validating connection info: %s", err)), nil
}
defer session.Close()
if err := session.Ping(); err != nil {
return logical.ErrorResponse(fmt.Sprintf(
"Error validating connection info: %s", err)), nil
}
}
// Store it
entry, err := logical.StorageEntryJSON("config/connection", connectionConfig{
2016-08-19 20:48:32 +00:00
URI: uri,
2016-05-13 20:42:09 +00:00
})
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
2016-05-13 20:42:09 +00:00
return nil, err
}
// Reset the Session
b.ResetSession(ctx)
2016-05-13 20:42:09 +00:00
resp := &logical.Response{}
resp.AddWarning("Read access to this endpoint should be controlled via ACLs as it will return the connection URI as it is, including passwords, if any.")
return resp, nil
2016-05-13 20:42:09 +00:00
}
type connectionConfig struct {
2016-08-19 20:48:32 +00:00
URI string `json:"uri" structs:"uri" mapstructure:"uri"`
2016-05-13 20:42:09 +00:00
}
const pathConfigConnectionHelpSyn = `
Configure the connection string to talk to MongoDB.
`
const pathConfigConnectionHelpDesc = `
This path configures the standard connection string (URI) used to connect to MongoDB.
A MongoDB URI looks like:
"mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]"
See https://docs.mongodb.org/manual/reference/connection-string/ for detailed documentation of the URI format.
When configuring the connection string, the backend will verify its validity.
`