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"
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
"github.com/fatih/structs"
|
2016-07-06 16:25:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
2015-11-18 16:25:42 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2016-04-08 16:46:25 +00:00
|
|
|
func pathListRoles(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "roles/?$",
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ListOperation: b.pathRoleList,
|
|
|
|
},
|
|
|
|
HelpSynopsis: pathRoleHelpSyn,
|
|
|
|
HelpDescription: pathRoleHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
func pathRoles(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: "roles/" + framework.GenericNameRegex("name"),
|
2016-06-08 07:18:26 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the role.",
|
|
|
|
},
|
|
|
|
"tags": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Comma-separated list of tags for this role.",
|
|
|
|
},
|
|
|
|
"vhosts": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "A map of virtual hosts to permissions.",
|
|
|
|
},
|
|
|
|
},
|
2015-11-18 16:25:42 +00:00
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.pathRoleRead,
|
2016-05-21 06:27:22 +00:00
|
|
|
logical.UpdateOperation: b.pathRoleUpdate,
|
2015-11-18 16:25:42 +00:00
|
|
|
logical.DeleteOperation: b.pathRoleDelete,
|
|
|
|
},
|
|
|
|
HelpSynopsis: pathRoleHelpSyn,
|
|
|
|
HelpDescription: pathRoleHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Reads the role configuration from the storage
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) Role(ctx context.Context, s logical.Storage, n string) (*roleEntry, error) {
|
|
|
|
entry, err := s.Get(ctx, "role/"+n)
|
2015-11-18 16:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result roleEntry
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Deletes an existing role
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathRoleDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-06-08 07:18:26 +00:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
if name == "" {
|
|
|
|
return logical.ErrorResponse("missing name"), nil
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return nil, req.Storage.Delete(ctx, "role/"+name)
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Reads an existing role
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathRoleRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-06-08 07:18:26 +00:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
if name == "" {
|
|
|
|
return logical.ErrorResponse("missing name"), nil
|
2016-05-21 05:51:09 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
role, err := b.Role(ctx, req.Storage, name)
|
2015-11-18 16:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if role == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
2016-06-08 07:18:26 +00:00
|
|
|
Data: structs.New(role).Map(),
|
2015-11-18 16:25:42 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Lists all the roles registered with the backend
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathRoleList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
roles, err := req.Storage.List(ctx, "role/")
|
2016-04-08 16:46:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
return logical.ListResponse(roles), nil
|
2016-04-08 16:46:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Registers a new role with the backend
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathRoleUpdate(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2016-06-08 07:18:26 +00:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
if name == "" {
|
|
|
|
return logical.ErrorResponse("missing name"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := d.Get("tags").(string)
|
|
|
|
rawVHosts := d.Get("vhosts").(string)
|
|
|
|
|
|
|
|
if tags == "" && rawVHosts == "" {
|
|
|
|
return logical.ErrorResponse("both tags and vhosts not specified"), nil
|
2016-05-21 05:51:09 +00:00
|
|
|
}
|
2015-11-18 16:25:42 +00:00
|
|
|
|
|
|
|
var vhosts map[string]vhostPermission
|
|
|
|
if len(rawVHosts) > 0 {
|
2016-07-06 16:25:40 +00:00
|
|
|
if err := jsonutil.DecodeJSON([]byte(rawVHosts), &vhosts); err != nil {
|
2015-11-18 16:25:42 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf("failed to unmarshal vhosts: %s", err)), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store it
|
|
|
|
entry, err := logical.StorageEntryJSON("role/"+name, &roleEntry{
|
|
|
|
Tags: tags,
|
|
|
|
VHosts: vhosts,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Put(ctx, entry); err != nil {
|
2015-11-18 16:25:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Role that defines the capabilities of the credentials issued against it
|
2015-11-18 16:25:42 +00:00
|
|
|
type roleEntry struct {
|
2016-06-08 07:18:26 +00:00
|
|
|
Tags string `json:"tags" structs:"tags" mapstructure:"tags"`
|
|
|
|
VHosts map[string]vhostPermission `json:"vhosts" structs:"vhosts" mapstructure:"vhosts"`
|
2015-11-18 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 07:18:26 +00:00
|
|
|
// Structure representing the permissions of a vhost
|
2015-11-18 16:25:42 +00:00
|
|
|
type vhostPermission struct {
|
2016-06-08 07:18:26 +00:00
|
|
|
Configure string `json:"configure" structs:"configure" mapstructure:"configure"`
|
|
|
|
Write string `json:"write" structs:"write" mapstructure:"write"`
|
2016-06-08 15:31:57 +00:00
|
|
|
Read string `json:"read" structs:"read" mapstructure:"read"`
|
2016-05-21 05:51:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 16:25:42 +00:00
|
|
|
const pathRoleHelpSyn = `
|
|
|
|
Manage the roles that can be created with this backend.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathRoleHelpDesc = `
|
|
|
|
This path lets you manage the roles that can be created with this backend.
|
|
|
|
|
|
|
|
The "tags" parameter customizes the tags used to create the role.
|
|
|
|
This is a comma separated list of strings. The "vhosts" parameter customizes
|
|
|
|
the virtual hosts that this user will be associated with. This is a JSON object
|
|
|
|
passed as a string in the form:
|
|
|
|
{
|
|
|
|
"vhostOne": {
|
|
|
|
"configure": ".*",
|
|
|
|
"write": ".*",
|
|
|
|
"read": ".*"
|
|
|
|
},
|
|
|
|
"vhostTwo": {
|
|
|
|
"configure": ".*",
|
|
|
|
"write": ".*",
|
|
|
|
"read": ".*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|