2015-11-18 16:25:42 +00:00
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-11-18 16:25:42 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-04-05 15:49:21 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
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"
|
2015-11-18 16:25:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SecretCredsType is the key for this backend's secrets.
|
|
|
|
const SecretCredsType = "creds"
|
|
|
|
|
|
|
|
func secretCreds(b *backend) *framework.Secret {
|
|
|
|
return &framework.Secret{
|
|
|
|
Type: SecretCredsType,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"username": {
|
2015-11-18 16:25:42 +00:00
|
|
|
Type: framework.TypeString,
|
2016-06-08 07:18:26 +00:00
|
|
|
Description: "RabbitMQ username",
|
2015-11-18 16:25:42 +00:00
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"password": {
|
2015-11-18 16:25:42 +00:00
|
|
|
Type: framework.TypeString,
|
2016-06-08 07:18:26 +00:00
|
|
|
Description: "Password for the RabbitMQ username",
|
2015-11-18 16:25:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Renew: b.secretCredsRenew,
|
|
|
|
Revoke: b.secretCredsRevoke,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Renew the previously issued secret
|
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-11-18 16:25:42 +00:00
|
|
|
// Get the lease information
|
2018-01-19 06:44:44 +00:00
|
|
|
lease, err := b.Lease(ctx, req.Storage)
|
2015-11-18 16:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if lease == nil {
|
2016-05-21 05:51:09 +00:00
|
|
|
lease = &configLease{}
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 16:20:20 +00:00
|
|
|
resp := &logical.Response{Secret: req.Secret}
|
|
|
|
resp.Secret.TTL = lease.TTL
|
|
|
|
resp.Secret.MaxTTL = lease.MaxTTL
|
|
|
|
return resp, nil
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Revoke the previously issued secret
|
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-11-18 16:25:42 +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")
|
|
|
|
}
|
2016-05-21 05:51:09 +00:00
|
|
|
username := usernameRaw.(string)
|
2015-11-18 16:25:42 +00:00
|
|
|
|
|
|
|
// Get our connection
|
2018-01-19 06:44:44 +00:00
|
|
|
client, err := b.Client(ctx, req.Storage)
|
2015-11-18 16:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
if _, err = client.DeleteUser(username); err != nil {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, errwrap.Wrapf("could not delete user: {{err}}", err)
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|