2015-03-15 21:26:48 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-03-16 00:07:54 +00:00
|
|
|
"strings"
|
2015-03-15 21:26:48 +00:00
|
|
|
|
2016-07-11 18:19:35 +00:00
|
|
|
"github.com/hashicorp/vault/helper/duration"
|
2016-07-06 16:25:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
2015-03-15 21:26:48 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-03-16 00:07:54 +00:00
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
2015-03-15 21:26:48 +00:00
|
|
|
)
|
|
|
|
|
2015-09-19 22:24:53 +00:00
|
|
|
// PassthroughBackendFactory returns a PassthroughBackend
|
|
|
|
// with leases switched off
|
2015-09-04 20:58:12 +00:00
|
|
|
func PassthroughBackendFactory(conf *logical.BackendConfig) (logical.Backend, error) {
|
2015-09-19 22:24:53 +00:00
|
|
|
return LeaseSwitchedPassthroughBackend(conf, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PassthroughBackendWithLeasesFactory returns a PassthroughBackend
|
|
|
|
// with leases switched on
|
|
|
|
func LeasedPassthroughBackendFactory(conf *logical.BackendConfig) (logical.Backend, error) {
|
|
|
|
return LeaseSwitchedPassthroughBackend(conf, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LeaseSwitchedPassthroughBackendFactory returns a PassthroughBackend
|
|
|
|
// with leases switched on or off
|
|
|
|
func LeaseSwitchedPassthroughBackend(conf *logical.BackendConfig, leases bool) (logical.Backend, error) {
|
2015-03-16 00:07:54 +00:00
|
|
|
var b PassthroughBackend
|
2015-09-19 22:24:53 +00:00
|
|
|
b.generateLeases = leases
|
2015-03-19 22:11:42 +00:00
|
|
|
b.Backend = &framework.Backend{
|
2015-04-04 03:45:00 +00:00
|
|
|
Help: strings.TrimSpace(passthroughHelp),
|
|
|
|
|
2015-03-16 00:07:54 +00:00
|
|
|
Paths: []*framework.Path{
|
|
|
|
&framework.Path{
|
|
|
|
Pattern: ".*",
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
|
|
|
logical.ReadOperation: b.handleRead,
|
2016-01-07 20:10:05 +00:00
|
|
|
logical.CreateOperation: b.handleWrite,
|
|
|
|
logical.UpdateOperation: b.handleWrite,
|
2015-03-16 00:07:54 +00:00
|
|
|
logical.DeleteOperation: b.handleDelete,
|
|
|
|
logical.ListOperation: b.handleList,
|
|
|
|
},
|
|
|
|
|
2016-01-07 20:10:05 +00:00
|
|
|
ExistenceCheck: b.handleExistenceCheck,
|
|
|
|
|
2015-03-16 00:07:54 +00:00
|
|
|
HelpSynopsis: strings.TrimSpace(passthroughHelpSynopsis),
|
|
|
|
HelpDescription: strings.TrimSpace(passthroughHelpDescription),
|
|
|
|
},
|
|
|
|
},
|
2015-09-19 22:24:53 +00:00
|
|
|
}
|
2015-03-19 19:20:25 +00:00
|
|
|
|
2015-10-07 15:42:23 +00:00
|
|
|
b.Backend.Secrets = []*framework.Secret{
|
|
|
|
&framework.Secret{
|
|
|
|
Type: "generic",
|
2015-03-19 19:20:25 +00:00
|
|
|
|
2015-10-07 15:42:23 +00:00
|
|
|
Renew: b.handleRead,
|
|
|
|
Revoke: b.handleRevoke,
|
|
|
|
},
|
2015-03-19 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 20:58:12 +00:00
|
|
|
if conf == nil {
|
|
|
|
return nil, fmt.Errorf("Configuation passed into backend is nil")
|
|
|
|
}
|
|
|
|
b.Backend.Setup(conf)
|
|
|
|
|
2015-09-19 22:24:53 +00:00
|
|
|
return &b, nil
|
2015-03-15 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 21:26:48 +00:00
|
|
|
// PassthroughBackend is used storing secrets directly into the physical
|
2015-09-09 19:42:29 +00:00
|
|
|
// backend. The secrets are encrypted in the durable storage and custom TTL
|
2015-03-15 21:26:48 +00:00
|
|
|
// information can be specified, but otherwise this backend doesn't do anything
|
|
|
|
// fancy.
|
2015-03-19 22:11:42 +00:00
|
|
|
type PassthroughBackend struct {
|
|
|
|
*framework.Backend
|
2015-09-19 22:24:53 +00:00
|
|
|
generateLeases bool
|
2015-03-19 22:11:42 +00:00
|
|
|
}
|
2015-03-15 21:26:48 +00:00
|
|
|
|
2015-03-16 23:26:34 +00:00
|
|
|
func (b *PassthroughBackend) handleRevoke(
|
2015-03-19 22:11:42 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-03-16 23:26:34 +00:00
|
|
|
// This is a no-op
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:10:05 +00:00
|
|
|
func (b *PassthroughBackend) handleExistenceCheck(
|
|
|
|
req *logical.Request, data *framework.FieldData) (bool, error) {
|
|
|
|
out, err := req.Storage.Get(req.Path)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("existence check failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out != nil, nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 00:07:54 +00:00
|
|
|
func (b *PassthroughBackend) handleRead(
|
2015-03-19 22:11:42 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-03-15 21:26:48 +00:00
|
|
|
// Read the path
|
|
|
|
out, err := req.Storage.Get(req.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast-path the no data case
|
|
|
|
if out == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the data
|
2015-03-19 14:05:22 +00:00
|
|
|
var rawData map[string]interface{}
|
2016-07-06 16:25:40 +00:00
|
|
|
|
|
|
|
if err := jsonutil.DecodeJSON(out.Value, &rawData); err != nil {
|
2015-03-15 21:26:48 +00:00
|
|
|
return nil, fmt.Errorf("json decoding failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-19 22:24:53 +00:00
|
|
|
var resp *logical.Response
|
|
|
|
if b.generateLeases {
|
|
|
|
// Generate the response
|
|
|
|
resp = b.Secret("generic").Response(rawData, nil)
|
|
|
|
resp.Secret.Renewable = false
|
|
|
|
} else {
|
|
|
|
resp = &logical.Response{
|
|
|
|
Secret: &logical.Secret{},
|
|
|
|
Data: rawData,
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 19:20:25 +00:00
|
|
|
|
2015-08-21 05:27:01 +00:00
|
|
|
// Check if there is a ttl key
|
|
|
|
var ttl string
|
2015-10-29 15:05:21 +00:00
|
|
|
ttl, _ = rawData["ttl"].(string)
|
2015-08-21 05:27:01 +00:00
|
|
|
if len(ttl) == 0 {
|
2015-10-29 15:05:21 +00:00
|
|
|
ttl, _ = rawData["lease"].(string)
|
2015-08-20 23:41:25 +00:00
|
|
|
}
|
2015-09-21 20:57:41 +00:00
|
|
|
ttlDuration := b.System().DefaultLeaseTTL()
|
2015-08-21 05:27:01 +00:00
|
|
|
if len(ttl) != 0 {
|
2016-07-11 18:19:35 +00:00
|
|
|
dur, err := duration.ParseDurationSecond(ttl)
|
|
|
|
if err == nil {
|
|
|
|
ttlDuration = dur
|
2015-09-19 22:24:53 +00:00
|
|
|
}
|
2016-06-09 00:14:36 +00:00
|
|
|
|
2015-09-19 22:24:53 +00:00
|
|
|
if b.generateLeases {
|
2015-08-20 23:41:25 +00:00
|
|
|
resp.Secret.Renewable = true
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-19 22:24:53 +00:00
|
|
|
resp.Secret.TTL = ttlDuration
|
|
|
|
|
2015-03-15 21:26:48 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 00:07:54 +00:00
|
|
|
func (b *PassthroughBackend) handleWrite(
|
2015-03-19 22:11:42 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-03-15 21:26:48 +00:00
|
|
|
// Check that some fields are given
|
|
|
|
if len(req.Data) == 0 {
|
2015-12-09 15:39:22 +00:00
|
|
|
return logical.ErrorResponse("missing data fields"), nil
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JSON encode the data
|
|
|
|
buf, err := json.Marshal(req.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("json encoding failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out a new key
|
|
|
|
entry := &logical.StorageEntry{
|
2016-01-19 23:19:38 +00:00
|
|
|
Key: req.Path,
|
2015-03-15 21:26:48 +00:00
|
|
|
Value: buf,
|
|
|
|
}
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 00:07:54 +00:00
|
|
|
func (b *PassthroughBackend) handleDelete(
|
2015-03-19 22:11:42 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2015-03-15 21:26:48 +00:00
|
|
|
// Delete the key at the request path
|
|
|
|
if err := req.Storage.Delete(req.Path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 00:07:54 +00:00
|
|
|
func (b *PassthroughBackend) handleList(
|
2015-03-19 22:11:42 +00:00
|
|
|
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
2016-01-19 22:05:01 +00:00
|
|
|
// Right now we only handle directories, so ensure it ends with /; however,
|
|
|
|
// some physical backends may not handle the "/" case properly, so only add
|
|
|
|
// it if we're not listing the root
|
|
|
|
path := req.Path
|
|
|
|
if path != "" && !strings.HasSuffix(path, "/") {
|
|
|
|
path = path + "/"
|
|
|
|
}
|
|
|
|
|
2015-03-15 21:26:48 +00:00
|
|
|
// List the keys at the prefix given by the request
|
2016-01-19 22:05:01 +00:00
|
|
|
keys, err := req.Storage.List(path)
|
2015-03-15 21:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the response
|
2016-01-19 22:05:01 +00:00
|
|
|
return logical.ListResponse(keys), nil
|
2015-03-15 21:26:48 +00:00
|
|
|
}
|
2015-03-16 00:07:54 +00:00
|
|
|
|
2015-09-19 22:24:53 +00:00
|
|
|
func (b *PassthroughBackend) GeneratesLeases() bool {
|
|
|
|
return b.generateLeases
|
|
|
|
}
|
|
|
|
|
2015-04-04 03:45:00 +00:00
|
|
|
const passthroughHelp = `
|
|
|
|
The generic backend reads and writes arbitrary secrets to the backend.
|
|
|
|
The secrets are encrypted/decrypted by Vault: they are never stored
|
|
|
|
unencrypted in the backend and the backend never has an opportunity to
|
|
|
|
see the unencrypted value.
|
|
|
|
|
2015-08-20 23:41:25 +00:00
|
|
|
TTLs can be set on a per-secret basis. These TTLs will be sent down
|
2015-04-04 03:45:00 +00:00
|
|
|
when that secret is read, and it is assumed that some outside process will
|
|
|
|
revoke and/or replace the secret at that path.
|
|
|
|
`
|
|
|
|
|
2015-03-16 00:07:54 +00:00
|
|
|
const passthroughHelpSynopsis = `
|
2015-05-27 21:33:58 +00:00
|
|
|
Pass-through secret storage to the storage backend, allowing you to
|
2015-03-16 00:07:54 +00:00
|
|
|
read/write arbitrary data into secret storage.
|
|
|
|
`
|
|
|
|
|
|
|
|
const passthroughHelpDescription = `
|
|
|
|
The pass-through backend reads and writes arbitrary data into secret storage,
|
|
|
|
encrypting it along the way.
|
|
|
|
|
2015-08-21 00:59:30 +00:00
|
|
|
A TTL can be specified when writing with the "ttl" field. If given, the
|
|
|
|
duration of leases returned by this backend will be set to this value. This
|
|
|
|
can be used as a hint from the writer of a secret to the consumer of a secret
|
|
|
|
that the consumer should re-read the value before the TTL has expired.
|
|
|
|
However, any revocation must be handled by the user of this backend; the lease
|
|
|
|
duration does not affect the provided data in any way.
|
2015-03-16 00:07:54 +00:00
|
|
|
`
|