2016-05-13 20:42:09 +00:00
package mongodb
import (
2018-01-08 18:31:38 +00:00
"context"
2016-05-13 20:42:09 +00:00
"fmt"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
2019-01-09 00:48:57 +00:00
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 {
2016-07-19 16:35:14 +00:00
"uri" : {
2016-05-13 20:42:09 +00:00
Type : framework . TypeString ,
Description : "MongoDB standard connection string (URI)" ,
} ,
2016-07-19 16:35:14 +00:00
"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 {
2016-07-05 13:32:38 +00:00
logical . ReadOperation : b . pathConnectionRead ,
2016-05-13 20:42:09 +00:00
logical . UpdateOperation : b . pathConnectionWrite ,
} ,
HelpSynopsis : pathConfigConnectionHelpSyn ,
HelpDescription : pathConfigConnectionHelpDesc ,
}
}
2016-07-05 13:32:38 +00:00
// pathConnectionRead reads out the connection configuration
2018-01-08 18:31:38 +00:00
func ( b * backend ) pathConnectionRead ( ctx context . Context , req * logical . Request , data * framework . FieldData ) ( * logical . Response , error ) {
2018-01-19 06:44:44 +00:00
entry , err := req . Storage . Get ( ctx , "config/connection" )
2016-07-05 13:32:38 +00:00
if err != nil {
return nil , fmt . Errorf ( "failed to read connection configuration" )
}
if entry == nil {
return nil , nil
}
2018-03-30 14:17:39 +00:00
return nil , nil
2016-07-05 13:32:38 +00:00
}
2018-01-08 18:31:38 +00:00
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 == "" {
2016-07-08 03:09:45 +00:00
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
}
2018-01-19 06:44:44 +00:00
if err := req . Storage . Put ( ctx , entry ) ; err != nil {
2016-05-13 20:42:09 +00:00
return nil , err
}
// Reset the Session
2018-01-19 06:44:44 +00:00
b . ResetSession ( ctx )
2016-05-13 20:42:09 +00:00
2016-07-05 13:32:38 +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 .
`