mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-10 00:36:20 +00:00
logical/framework: WAL entry supports "kind"
This commit is contained in:
@@ -34,7 +34,7 @@ type Backend struct {
|
|||||||
// Rollback is called when a WAL entry (see wal.go) has to be rolled
|
// Rollback is called when a WAL entry (see wal.go) has to be rolled
|
||||||
// back. It is called with the data from the entry. Boolean true should
|
// back. It is called with the data from the entry. Boolean true should
|
||||||
// be returned on success. Errors should just be logged.
|
// be returned on success. Errors should just be logged.
|
||||||
Rollback func(data interface{}) bool
|
Rollback func(kind string, data interface{}) bool
|
||||||
|
|
||||||
once sync.Once
|
once sync.Once
|
||||||
pathsRe []*regexp.Regexp
|
pathsRe []*regexp.Regexp
|
||||||
@@ -193,13 +193,13 @@ func (b *Backend) handleRollback(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
data, err := GetWAL(req.Storage, k)
|
kind, data, err := GetWAL(req.Storage, k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
merr = multierror.Append(merr, err)
|
merr = multierror.Append(merr, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.Rollback(data) {
|
if b.Rollback(kind, data) {
|
||||||
if err := DeleteWAL(req.Storage, k); err != nil {
|
if err := DeleteWAL(req.Storage, k); err != nil {
|
||||||
merr = multierror.Append(merr, err)
|
merr = multierror.Append(merr, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ func TestBackendHandleRequest_help(t *testing.T) {
|
|||||||
|
|
||||||
func TestBackendHandleRequest_rollback(t *testing.T) {
|
func TestBackendHandleRequest_rollback(t *testing.T) {
|
||||||
var called uint32
|
var called uint32
|
||||||
callback := func(data interface{}) bool {
|
callback := func(kind string, data interface{}) bool {
|
||||||
if data == "foo" {
|
if data == "foo" {
|
||||||
atomic.AddUint32(&called, 1)
|
atomic.AddUint32(&called, 1)
|
||||||
}
|
}
|
||||||
@@ -151,7 +151,7 @@ func TestBackendHandleRequest_rollback(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
storage := new(logical.InmemStorage)
|
storage := new(logical.InmemStorage)
|
||||||
if _, err := PutWAL(storage, "foo"); err != nil {
|
if _, err := PutWAL(storage, "kind", "foo"); err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ const WALPrefix = "wal/"
|
|||||||
|
|
||||||
// PutWAL writes some data to the WAL.
|
// PutWAL writes some data to the WAL.
|
||||||
//
|
//
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
// Data within the WAL that is uncommitted (CommitWAL hasn't be called)
|
// Data within the WAL that is uncommitted (CommitWAL hasn't be called)
|
||||||
// will be given to the rollback callback when an rollback operation is
|
// will be given to the rollback callback when an rollback operation is
|
||||||
// received, allowing the backend to clean up some partial states.
|
// received, allowing the backend to clean up some partial states.
|
||||||
@@ -21,8 +25,11 @@ const WALPrefix = "wal/"
|
|||||||
// This returns a unique ID that can be used to reference this WAL data.
|
// 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 data cannot be modified. You can only add to the WAL and commit existing
|
||||||
// WAL entries.
|
// WAL entries.
|
||||||
func PutWAL(s logical.Storage, data interface{}) (string, error) {
|
func PutWAL(s logical.Storage, kind string, data interface{}) (string, error) {
|
||||||
value, err := json.Marshal(data)
|
value, err := json.Marshal(map[string]interface{}{
|
||||||
|
"kind": kind,
|
||||||
|
"data": data,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -40,21 +47,23 @@ func PutWAL(s logical.Storage, data interface{}) (string, error) {
|
|||||||
|
|
||||||
// GetWAL reads a specific entry from the WAL. If the entry doesn't exist,
|
// GetWAL reads a specific entry from the WAL. If the entry doesn't exist,
|
||||||
// then nil value is returned.
|
// then nil value is returned.
|
||||||
func GetWAL(s logical.Storage, id string) (interface{}, error) {
|
//
|
||||||
|
// The kind, value, and error are returned.
|
||||||
|
func GetWAL(s logical.Storage, id string) (string, interface{}, error) {
|
||||||
entry, err := s.Get(WALPrefix + id)
|
entry, err := s.Get(WALPrefix + id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if entry == nil {
|
if entry == nil {
|
||||||
return nil, nil
|
return "", nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var result interface{}
|
var result map[string]interface{}
|
||||||
if err := json.Unmarshal(entry.Value, &result); err != nil {
|
if err := json.Unmarshal(entry.Value, &result); err != nil {
|
||||||
return nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result["kind"].(string), result["data"], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteWAL commits the WAL entry with the given ID. Once comitted,
|
// DeleteWAL commits the WAL entry with the given ID. Once comitted,
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func TestWAL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write an entry to the WAL
|
// Write an entry to the WAL
|
||||||
id, err := PutWAL(s, "bar")
|
id, err := PutWAL(s, "foo", "bar")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
@@ -35,10 +35,13 @@ func TestWAL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Should be able to get the value
|
// Should be able to get the value
|
||||||
v, err := GetWAL(s, id)
|
kind, v, err := GetWAL(s, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
if kind != "foo" {
|
||||||
|
t.Fatalf("bad: %#v", kind)
|
||||||
|
}
|
||||||
if v != "bar" {
|
if v != "bar" {
|
||||||
t.Fatalf("bad: %#v", v)
|
t.Fatalf("bad: %#v", v)
|
||||||
}
|
}
|
||||||
@@ -47,7 +50,7 @@ func TestWAL(t *testing.T) {
|
|||||||
if err := DeleteWAL(s, id); err != nil {
|
if err := DeleteWAL(s, id); err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
v, err = GetWAL(s, id)
|
_, v, err = GetWAL(s, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user