2015-06-19 17:10:19 +00:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-06-19 17:10:19 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2015-06-19 17:10:19 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SecretCredsType is the type of creds issued from this backend
|
|
|
|
const SecretCredsType = "cassandra"
|
|
|
|
|
|
|
|
func secretCreds(b *backend) *framework.Secret {
|
|
|
|
return &framework.Secret{
|
|
|
|
Type: SecretCredsType,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"username": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Username",
|
|
|
|
},
|
|
|
|
|
|
|
|
"password": &framework.FieldSchema{
|
|
|
|
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) {
|
2015-06-19 17:10:19 +00:00
|
|
|
// Get the lease information
|
|
|
|
roleRaw, ok := req.Secret.InternalData["role"]
|
|
|
|
if !ok {
|
2016-12-16 00:02:33 +00:00
|
|
|
return nil, fmt.Errorf("secret is missing role internal data")
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
roleName, ok := roleRaw.(string)
|
|
|
|
if !ok {
|
2016-12-16 00:02:33 +00:00
|
|
|
return nil, fmt.Errorf("error converting role internal data to string")
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err := getRole(ctx, req.Storage, roleName)
|
2015-06-19 17:10:19 +00:00
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("unable to load role: {{err}}", err)
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 16:20:20 +00:00
|
|
|
resp := &logical.Response{Secret: req.Secret}
|
|
|
|
resp.Secret.TTL = role.Lease
|
|
|
|
return resp, nil
|
2015-06-19 17:10:19 +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) {
|
2015-06-19 17:10:19 +00:00
|
|
|
// Get the username from the internal data
|
|
|
|
usernameRaw, ok := req.Secret.InternalData["username"]
|
|
|
|
if !ok {
|
2016-12-16 00:02:33 +00:00
|
|
|
return nil, fmt.Errorf("secret is missing username internal data")
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
username, ok := usernameRaw.(string)
|
|
|
|
if !ok {
|
2016-12-16 00:02:33 +00:00
|
|
|
return nil, fmt.Errorf("error converting username internal data to string")
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
session, err := b.DB(ctx, req.Storage)
|
2015-06-19 17:10:19 +00:00
|
|
|
if err != nil {
|
2016-12-16 00:02:33 +00:00
|
|
|
return nil, fmt.Errorf("error getting session")
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = session.Query(fmt.Sprintf("DROP USER '%s'", username)).Exec()
|
|
|
|
if err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("error removing user %q", username)
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|