2015-11-18 16:25:42 +00:00
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2021-04-22 15:20:59 +00:00
|
|
|
"fmt"
|
2015-11-18 16:25:42 +00:00
|
|
|
|
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"
|
2019-01-09 00:48:57 +00:00
|
|
|
rabbithole "github.com/michaelklishin/rabbit-hole"
|
2015-11-18 16:25:42 +00:00
|
|
|
)
|
|
|
|
|
2020-06-11 22:08:20 +00:00
|
|
|
const (
|
|
|
|
storageKey = "config/connection"
|
|
|
|
)
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
func pathConfigConnection(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "config/connection",
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
2021-04-08 16:43:39 +00:00
|
|
|
"connection_uri": {
|
2015-11-18 16:25:42 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "RabbitMQ Management URI",
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"username": {
|
2015-11-18 16:25:42 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Username of a RabbitMQ management administrator",
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"password": {
|
2015-11-18 16:25:42 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Password of the provided RabbitMQ management user",
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"verify_connection": {
|
2016-04-08 16:44:15 +00:00
|
|
|
Type: framework.TypeBool,
|
|
|
|
Default: true,
|
|
|
|
Description: `If set, connection_uri is verified by actually connecting to the RabbitMQ management API`,
|
|
|
|
},
|
2021-04-08 16:43:39 +00:00
|
|
|
"password_policy": {
|
2020-06-11 22:08:20 +00:00
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the password policy to use to generate passwords for dynamic credentials.",
|
|
|
|
},
|
2015-11-18 16:25:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-05-21 05:51:09 +00:00
|
|
|
logical.UpdateOperation: b.pathConnectionUpdate,
|
2015-11-18 16:25:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathConfigConnectionHelpSyn,
|
|
|
|
HelpDescription: pathConfigConnectionHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathConnectionUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2016-04-08 16:04:42 +00:00
|
|
|
uri := data.Get("connection_uri").(string)
|
2015-11-18 16:25:42 +00:00
|
|
|
if uri == "" {
|
2016-06-08 07:18:26 +00:00
|
|
|
return logical.ErrorResponse("missing connection_uri"), nil
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
username := data.Get("username").(string)
|
2015-11-18 16:25:42 +00:00
|
|
|
if username == "" {
|
2016-06-08 07:18:26 +00:00
|
|
|
return logical.ErrorResponse("missing username"), nil
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
password := data.Get("password").(string)
|
2015-11-18 16:25:42 +00:00
|
|
|
if password == "" {
|
2016-06-08 07:18:26 +00:00
|
|
|
return logical.ErrorResponse("missing password"), nil
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 22:08:20 +00:00
|
|
|
passwordPolicy := data.Get("password_policy").(string)
|
|
|
|
|
2016-04-08 16:44:15 +00:00
|
|
|
// Don't check the connection_url if verification is disabled
|
|
|
|
verifyConnection := data.Get("verify_connection").(bool)
|
|
|
|
if verifyConnection {
|
|
|
|
// Create RabbitMQ management client
|
|
|
|
client, err := rabbithole.NewClient(uri, username, password)
|
|
|
|
if err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to create client: %w", err)
|
2016-04-08 16:44:15 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Verify that configured credentials is capable of listing
|
|
|
|
if _, err = client.ListUsers(); err != nil {
|
2021-04-22 15:20:59 +00:00
|
|
|
return nil, fmt.Errorf("failed to validate the connection: %w", err)
|
2016-04-08 16:44:15 +00:00
|
|
|
}
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store it
|
2020-06-11 22:08:20 +00:00
|
|
|
config := connectionConfig{
|
|
|
|
URI: uri,
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
PasswordPolicy: passwordPolicy,
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
2020-06-11 22:08:20 +00:00
|
|
|
err := writeConfig(ctx, req.Storage, config)
|
|
|
|
if err != nil {
|
2015-11-18 16:25:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the client connection
|
2018-01-19 06:44:44 +00:00
|
|
|
b.resetClient(ctx)
|
2015-11-18 16:25:42 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:08:20 +00:00
|
|
|
func readConfig(ctx context.Context, storage logical.Storage) (connectionConfig, error) {
|
|
|
|
entry, err := storage.Get(ctx, storageKey)
|
|
|
|
if err != nil {
|
|
|
|
return connectionConfig{}, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return connectionConfig{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var connConfig connectionConfig
|
|
|
|
if err := entry.DecodeJSON(&connConfig); err != nil {
|
|
|
|
return connectionConfig{}, err
|
|
|
|
}
|
|
|
|
return connConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeConfig(ctx context.Context, storage logical.Storage, config connectionConfig) error {
|
|
|
|
entry, err := logical.StorageEntryJSON(storageKey, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := storage.Put(ctx, entry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// connectionConfig contains the information required to make a connection to a RabbitMQ node
|
2015-11-18 16:25:42 +00:00
|
|
|
type connectionConfig struct {
|
2016-06-08 07:18:26 +00:00
|
|
|
// URI of the RabbitMQ server
|
|
|
|
URI string `json:"connection_uri"`
|
|
|
|
|
|
|
|
// Username which has 'administrator' tag attached to it
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
|
|
|
// Password for the Username
|
|
|
|
Password string `json:"password"`
|
2020-06-11 22:08:20 +00:00
|
|
|
|
|
|
|
// PasswordPolicy for generating passwords for dynamic credentials
|
|
|
|
PasswordPolicy string `json:"password_policy"`
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const pathConfigConnectionHelpSyn = `
|
2016-04-08 16:04:42 +00:00
|
|
|
Configure the connection URI, username, and password to talk to RabbitMQ management HTTP API.
|
2015-11-18 16:25:42 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const pathConfigConnectionHelpDesc = `
|
|
|
|
This path configures the connection properties used to connect to RabbitMQ management HTTP API.
|
2016-04-08 16:05:06 +00:00
|
|
|
The "connection_uri" parameter is a string that is used to connect to the API. The "username"
|
2016-04-08 16:44:15 +00:00
|
|
|
and "password" parameters are strings that are used as credentials to the API. The "verify_connection"
|
|
|
|
parameter is a boolean that is used to verify whether the provided connection URI, username, and password
|
|
|
|
are valid.
|
2015-11-18 16:25:42 +00:00
|
|
|
|
|
|
|
The URI looks like:
|
|
|
|
"http://localhost:15672"
|
|
|
|
`
|