open-vault/builtin/logical/rabbitmq/secret_creds.go

70 lines
1.7 KiB
Go
Raw Normal View History

2015-11-18 16:25:42 +00:00
package rabbitmq
import (
"context"
2015-11-18 16:25:42 +00:00
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"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{
"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
},
"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
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
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
}
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
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
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 {
return nil, fmt.Errorf("could not delete user: %w", err)
2015-11-18 16:25:42 +00:00
}
return nil, nil
}