2015-03-20 16:59:48 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathPolicy() *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: `policy/(?P<name>\w+)`,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"name": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Name of the policy",
|
|
|
|
},
|
|
|
|
|
|
|
|
"policy": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
2015-04-01 00:26:41 +00:00
|
|
|
Description: "Policy document",
|
2015-03-20 16:59:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.WriteOperation: pathPolicyWrite,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pathPolicyWrite(
|
|
|
|
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-04-01 00:26:41 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.Compact(&buf, []byte(d.Get("policy").(string))); err != nil {
|
2015-03-20 16:59:48 +00:00
|
|
|
return logical.ErrorResponse(fmt.Sprintf(
|
|
|
|
"Error compacting policy: %s", err)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the policy into storage
|
2015-04-01 00:26:41 +00:00
|
|
|
err := req.Storage.Put(&logical.StorageEntry{
|
2015-03-20 16:59:48 +00:00
|
|
|
Key: "policy/" + d.Get("name").(string),
|
2015-04-01 00:26:41 +00:00
|
|
|
Value: buf.Bytes(),
|
2015-03-20 16:59:48 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|