2015-03-21 16:19:37 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2015-05-26 00:33:31 +00:00
|
|
|
"time"
|
2015-03-21 16:19:37 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-04-27 20:59:56 +00:00
|
|
|
func pathRoles() *framework.Path {
|
2015-03-21 16:19:37 +00:00
|
|
|
return &framework.Path{
|
2015-04-27 20:59:56 +00:00
|
|
|
Pattern: `roles/(?P<name>\w+)`,
|
2015-03-21 16:19:37 +00:00
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2015-04-27 20:59:56 +00:00
|
|
|
Description: "Name of the role",
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"policy": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Policy document, base64 encoded.",
|
|
|
|
},
|
2015-05-26 00:33:31 +00:00
|
|
|
|
|
|
|
"lease": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Lease time of the role.",
|
|
|
|
},
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2015-04-27 20:59:56 +00:00
|
|
|
logical.ReadOperation: pathRolesRead,
|
|
|
|
logical.WriteOperation: pathRolesWrite,
|
|
|
|
logical.DeleteOperation: pathRolesDelete,
|
2015-03-21 16:19:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 20:59:56 +00:00
|
|
|
func pathRolesRead(
|
2015-04-27 18:08:37 +00:00
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
2015-05-27 23:36:23 +00:00
|
|
|
entry, err := req.Storage.Get("policy/" + name)
|
2015-04-27 18:08:37 +00:00
|
|
|
if err != nil {
|
2015-05-27 23:36:23 +00:00
|
|
|
return nil, err
|
2015-04-27 18:08:37 +00:00
|
|
|
}
|
2015-05-27 23:36:23 +00:00
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
2015-04-27 18:08:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 23:36:23 +00:00
|
|
|
var result roleConfig
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
2015-05-26 00:33:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 18:08:37 +00:00
|
|
|
// Generate the response
|
|
|
|
resp := &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
2015-05-27 23:36:23 +00:00
|
|
|
"policy": base64.StdEncoding.EncodeToString([]byte(result.Policy)),
|
|
|
|
"lease": result.Lease.String(),
|
2015-04-27 18:08:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2015-04-27 20:59:56 +00:00
|
|
|
func pathRolesWrite(
|
2015-03-21 16:19:37 +00:00
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-05-26 00:33:31 +00:00
|
|
|
name := d.Get("name").(string)
|
2015-03-21 16:19:37 +00:00
|
|
|
policyRaw, err := base64.StdEncoding.DecodeString(d.Get("policy").(string))
|
|
|
|
if err != nil {
|
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
|
|
|
"Error decoding policy base64: %s", err)), nil
|
|
|
|
}
|
2015-05-26 00:33:31 +00:00
|
|
|
lease, err := time.ParseDuration(d.Get("lease").(string))
|
|
|
|
if err != nil || lease == time.Duration(0) {
|
|
|
|
lease = DefaultLeaseDuration
|
|
|
|
}
|
2015-05-27 23:36:23 +00:00
|
|
|
|
|
|
|
entry, err := logical.StorageEntryJSON("policy/"+name, roleConfig{
|
|
|
|
Policy: string(policyRaw),
|
|
|
|
Lease: lease,
|
2015-05-26 00:33:31 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-27 23:36:23 +00:00
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:19:37 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-04-27 18:08:37 +00:00
|
|
|
|
2015-04-27 20:59:56 +00:00
|
|
|
func pathRolesDelete(
|
2015-04-27 18:08:37 +00:00
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
if err := req.Storage.Delete("policy/" + name); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-05-27 23:36:23 +00:00
|
|
|
|
|
|
|
type roleConfig struct {
|
|
|
|
Policy string `json:"policy"`
|
|
|
|
Lease time.Duration `json:"lease"`
|
|
|
|
}
|