2015-03-18 00:05:18 +00:00
|
|
|
package framework
|
2015-03-18 00:03:00 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-03-18 00:03:00 +00:00
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
2015-03-18 00:58:05 +00:00
|
|
|
"time"
|
2015-03-18 00:05:18 +00:00
|
|
|
|
2016-07-07 21:57:36 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2016-07-06 16:25:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
2015-03-18 00:05:18 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2015-03-18 00:03:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WALPrefix is the prefix within Storage where WAL entries will be written.
|
|
|
|
const WALPrefix = "wal/"
|
|
|
|
|
2015-03-18 00:58:05 +00:00
|
|
|
type WALEntry struct {
|
|
|
|
ID string `json:"-"`
|
|
|
|
Kind string `json:"type"`
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
CreatedAt int64 `json:"created_at"`
|
|
|
|
}
|
|
|
|
|
2015-03-18 00:03:00 +00:00
|
|
|
// PutWAL writes some data to the WAL.
|
|
|
|
//
|
2015-03-18 00:23:18 +00:00
|
|
|
// The kind parameter is used by the framework to allow users to store
|
|
|
|
// multiple kinds of WAL data and to easily disambiguate what data they're
|
|
|
|
// expecting.
|
|
|
|
//
|
2015-03-18 00:03:00 +00:00
|
|
|
// Data within the WAL that is uncommitted (CommitWAL hasn't be called)
|
|
|
|
// will be given to the rollback callback when an rollback operation is
|
|
|
|
// received, allowing the backend to clean up some partial states.
|
|
|
|
//
|
|
|
|
// The data must be JSON encodable.
|
|
|
|
//
|
|
|
|
// This returns a unique ID that can be used to reference this WAL data.
|
|
|
|
// WAL data cannot be modified. You can only add to the WAL and commit existing
|
|
|
|
// WAL entries.
|
2018-01-19 06:44:44 +00:00
|
|
|
func PutWAL(ctx context.Context, s logical.Storage, kind string, data interface{}) (string, error) {
|
2015-03-18 00:58:05 +00:00
|
|
|
value, err := json.Marshal(&WALEntry{
|
|
|
|
Kind: kind,
|
|
|
|
Data: data,
|
|
|
|
CreatedAt: time.Now().UTC().Unix(),
|
2015-03-18 00:23:18 +00:00
|
|
|
})
|
2015-03-18 00:03:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-07-07 21:57:36 +00:00
|
|
|
id, err := uuid.GenerateUUID()
|
2015-03-18 00:03:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
return id, s.Put(ctx, &logical.StorageEntry{
|
2015-03-18 00:03:00 +00:00
|
|
|
Key: WALPrefix + id,
|
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWAL reads a specific entry from the WAL. If the entry doesn't exist,
|
|
|
|
// then nil value is returned.
|
2015-03-18 00:23:18 +00:00
|
|
|
//
|
|
|
|
// The kind, value, and error are returned.
|
2018-01-19 06:44:44 +00:00
|
|
|
func GetWAL(ctx context.Context, s logical.Storage, id string) (*WALEntry, error) {
|
|
|
|
entry, err := s.Get(ctx, WALPrefix+id)
|
2015-03-18 00:03:00 +00:00
|
|
|
if err != nil {
|
2015-03-18 00:58:05 +00:00
|
|
|
return nil, err
|
2015-03-18 00:03:00 +00:00
|
|
|
}
|
|
|
|
if entry == nil {
|
2015-03-18 00:58:05 +00:00
|
|
|
return nil, nil
|
2015-03-18 00:03:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 00:58:05 +00:00
|
|
|
var raw WALEntry
|
2016-07-06 16:25:40 +00:00
|
|
|
if err := jsonutil.DecodeJSON(entry.Value, &raw); err != nil {
|
2015-03-18 00:58:05 +00:00
|
|
|
return nil, err
|
2015-03-18 00:03:00 +00:00
|
|
|
}
|
2015-03-18 00:58:05 +00:00
|
|
|
raw.ID = id
|
2015-03-18 00:03:00 +00:00
|
|
|
|
2015-03-18 00:58:05 +00:00
|
|
|
return &raw, nil
|
2015-03-18 00:03:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 16:58:36 +00:00
|
|
|
// DeleteWAL commits the WAL entry with the given ID. Once committed,
|
2015-03-18 00:03:00 +00:00
|
|
|
// it is assumed that the operation was a success and doesn't need to
|
|
|
|
// be rolled back.
|
2018-01-19 06:44:44 +00:00
|
|
|
func DeleteWAL(ctx context.Context, s logical.Storage, id string) error {
|
|
|
|
return s.Delete(ctx, WALPrefix+id)
|
2015-03-18 00:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListWAL lists all the entries in the WAL.
|
2018-01-19 06:44:44 +00:00
|
|
|
func ListWAL(ctx context.Context, s logical.Storage) ([]string, error) {
|
|
|
|
keys, err := s.List(ctx, WALPrefix)
|
2015-03-18 00:03:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, k := range keys {
|
|
|
|
keys[i] = strings.TrimPrefix(k, WALPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|