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"
|
|
|
|
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
|
|
|
role, err := getRole(req.Storage, roleName)
|
|
|
|
if err != nil {
|
2016-12-16 00:02:33 +00:00
|
|
|
return nil, fmt.Errorf("unable to load role: %s", err)
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
return framework.LeaseExtend(role.Lease, 0, b.System())(ctx, req, d)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
session, err := b.DB(req.Storage)
|
|
|
|
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 {
|
2016-12-16 00:02:33 +00:00
|
|
|
return nil, fmt.Errorf("error removing user %s", username)
|
2015-06-19 17:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|