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"
|
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/framework"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2019-01-09 00:48:57 +00:00
|
|
|
mgo "gopkg.in/mgo.v2"
|
2016-05-13 20:42:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const SecretCredsType = "creds"
|
|
|
|
|
|
|
|
func secretCreds(b *backend) *framework.Secret {
|
|
|
|
return &framework.Secret{
|
|
|
|
Type: SecretCredsType,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2016-07-19 16:35:14 +00:00
|
|
|
"username": {
|
2016-05-13 20:42:09 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Username",
|
|
|
|
},
|
|
|
|
|
2016-07-19 16:35:14 +00:00
|
|
|
"password": {
|
2016-05-13 20:42:09 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Password",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Renew: b.secretCredsRenew,
|
|
|
|
Revoke: b.secretCredsRevoke,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) secretCredsRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-05-13 20:42:09 +00:00
|
|
|
// Get the lease information
|
2018-01-19 06:44:44 +00:00
|
|
|
leaseConfig, err := b.LeaseConfig(ctx, req.Storage)
|
2016-05-13 20:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if leaseConfig == nil {
|
|
|
|
leaseConfig = &configLease{}
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:20:20 +00:00
|
|
|
resp := &logical.Response{Secret: req.Secret}
|
|
|
|
resp.Secret.TTL = leaseConfig.TTL
|
|
|
|
resp.Secret.MaxTTL = leaseConfig.MaxTTL
|
|
|
|
return resp, nil
|
2016-05-13 20:42:09 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) secretCredsRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-05-13 20:42:09 +00:00
|
|
|
// Get the username from the internal data
|
|
|
|
usernameRaw, ok := req.Secret.InternalData["username"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing username internal data")
|
|
|
|
}
|
|
|
|
username, ok := usernameRaw.(string)
|
2016-07-19 15:18:00 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("username internal data is not a string")
|
|
|
|
}
|
2016-05-13 20:42:09 +00:00
|
|
|
|
|
|
|
// Get the db from the internal data
|
|
|
|
dbRaw, ok := req.Secret.InternalData["db"]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("secret is missing db internal data")
|
|
|
|
}
|
|
|
|
db, ok := dbRaw.(string)
|
2016-07-19 15:18:00 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("db internal data is not a string")
|
|
|
|
}
|
2016-05-13 20:42:09 +00:00
|
|
|
|
|
|
|
// Get our connection
|
2018-01-19 06:44:44 +00:00
|
|
|
session, err := b.Session(ctx, req.Storage)
|
2016-05-13 20:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the user
|
|
|
|
err = session.DB(db).RemoveUser(username)
|
2016-07-08 02:27:47 +00:00
|
|
|
if err != nil && err != mgo.ErrNotFound {
|
2016-05-13 20:42:09 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|