mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-10 16:47:01 +02:00
* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License. Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUS-1.1 * Fix test that expected exact offset on hcl file --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Sarah Thompson <sthompson@hashicorp.com> Co-authored-by: Brian Kassouf <bkassouf@hashicorp.com>
142 lines
2.9 KiB
Go
142 lines
2.9 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package rabbitmq
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
|
|
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
|
"github.com/hashicorp/vault/sdk/framework"
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
rabbithole "github.com/michaelklishin/rabbit-hole/v2"
|
|
)
|
|
|
|
const operationPrefixRabbitMQ = "rabbit-mq"
|
|
|
|
// Factory creates and configures the backend
|
|
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
|
|
b := Backend()
|
|
if err := b.Setup(ctx, conf); err != nil {
|
|
return nil, err
|
|
}
|
|
return b, nil
|
|
}
|
|
|
|
// Creates a new backend with all the paths and secrets belonging to it
|
|
func Backend() *backend {
|
|
var b backend
|
|
b.Backend = &framework.Backend{
|
|
Help: strings.TrimSpace(backendHelp),
|
|
|
|
PathsSpecial: &logical.Paths{
|
|
SealWrapStorage: []string{
|
|
"config/connection",
|
|
},
|
|
},
|
|
|
|
Paths: []*framework.Path{
|
|
pathConfigConnection(&b),
|
|
pathConfigLease(&b),
|
|
pathListRoles(&b),
|
|
pathCreds(&b),
|
|
pathRoles(&b),
|
|
},
|
|
|
|
Secrets: []*framework.Secret{
|
|
secretCreds(&b),
|
|
},
|
|
|
|
Clean: b.resetClient,
|
|
Invalidate: b.invalidate,
|
|
BackendType: logical.TypeLogical,
|
|
}
|
|
|
|
return &b
|
|
}
|
|
|
|
type backend struct {
|
|
*framework.Backend
|
|
|
|
client *rabbithole.Client
|
|
lock sync.RWMutex
|
|
}
|
|
|
|
// DB returns the database connection.
|
|
func (b *backend) Client(ctx context.Context, s logical.Storage) (*rabbithole.Client, error) {
|
|
b.lock.RLock()
|
|
|
|
// If we already have a client, return it
|
|
if b.client != nil {
|
|
b.lock.RUnlock()
|
|
return b.client, nil
|
|
}
|
|
|
|
b.lock.RUnlock()
|
|
|
|
// Otherwise, attempt to make connection
|
|
connConfig, err := readConfig(ctx, s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
// If the client was created during the lock switch, return it
|
|
if b.client != nil {
|
|
return b.client, nil
|
|
}
|
|
|
|
b.client, err = rabbithole.NewClient(connConfig.URI, connConfig.Username, connConfig.Password)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Use a default pooled transport so there would be no leaked file descriptors
|
|
b.client.SetTransport(cleanhttp.DefaultPooledTransport())
|
|
|
|
return b.client, nil
|
|
}
|
|
|
|
// resetClient forces a connection next time Client() is called.
|
|
func (b *backend) resetClient(_ context.Context) {
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
b.client = nil
|
|
}
|
|
|
|
func (b *backend) invalidate(ctx context.Context, key string) {
|
|
switch key {
|
|
case "config/connection":
|
|
b.resetClient(ctx)
|
|
}
|
|
}
|
|
|
|
// Lease returns the lease information
|
|
func (b *backend) Lease(ctx context.Context, s logical.Storage) (*configLease, error) {
|
|
entry, err := s.Get(ctx, "config/lease")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if entry == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
var result configLease
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &result, nil
|
|
}
|
|
|
|
const backendHelp = `
|
|
The RabbitMQ backend dynamically generates RabbitMQ users.
|
|
|
|
After mounting this backend, configure it using the endpoints within
|
|
the "config/" path.
|
|
`
|