-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5369a0e
commit 9f0d6b6
Showing
10 changed files
with
1,537 additions
and
558 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package memdb | ||
|
||
import ( | ||
"github.com/authzed/spicedb/pkg/datastore" | ||
core "github.com/authzed/spicedb/pkg/proto/core/v1" | ||
|
||
"github.com/hashicorp/go-memdb" | ||
) | ||
|
||
const tableCaveats = "caveats" | ||
|
||
type caveat struct { | ||
digest string | ||
logic []byte | ||
caveatType core.Caveat_Type | ||
} | ||
|
||
func (c *caveat) CoreCaveat() *core.Caveat { | ||
return &core.Caveat{ | ||
Digest: c.digest, | ||
Logic: c.logic, | ||
Type: c.caveatType, | ||
} | ||
} | ||
|
||
func (r *memdbReader) ReadCaveat(digest string) (datastore.CaveatIterator, error) { | ||
r.lockOrPanic() | ||
defer r.Unlock() | ||
|
||
tx, err := r.txSource() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return r.readCaveat(tx, digest) | ||
} | ||
|
||
func (r *memdbReader) readCaveat(tx *memdb.Txn, digest string) (datastore.CaveatIterator, error) { | ||
it, err := tx.Get(tableCaveats, indexID, digest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &memdbCaveatIterator{it: it}, nil | ||
} | ||
|
||
func (rwt *memdbReadWriteTx) WriteCaveat(caveats []*core.Caveat) error { | ||
rwt.lockOrPanic() | ||
defer rwt.Unlock() | ||
tx, err := rwt.txSource() | ||
if err != nil { | ||
return err | ||
} | ||
return rwt.writeCaveat(tx, caveats) | ||
} | ||
|
||
func (rwt *memdbReadWriteTx) writeCaveat(tx *memdb.Txn, caveats []*core.Caveat) error { | ||
for _, coreCaveat := range caveats { | ||
c := caveat{ | ||
digest: coreCaveat.Digest, | ||
logic: coreCaveat.Logic, | ||
caveatType: coreCaveat.Type, | ||
} | ||
if err := tx.Insert(tableCaveats, &c); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (rwt *memdbReadWriteTx) DeleteCaveat(caveats []*core.Caveat) error { | ||
rwt.lockOrPanic() | ||
defer rwt.Unlock() | ||
tx, err := rwt.txSource() | ||
if err != nil { | ||
return err | ||
} | ||
return tx.Delete(tableCaveats, caveats) | ||
} | ||
|
||
type memdbCaveatIterator struct { | ||
it memdb.ResultIterator | ||
} | ||
|
||
func (mci *memdbCaveatIterator) Next() *core.Caveat { | ||
foundRaw := mci.it.Next() | ||
if foundRaw == nil { | ||
return nil | ||
} | ||
|
||
c := foundRaw.(*caveat) | ||
return c.CoreCaveat() | ||
} | ||
|
||
func (mci *memdbCaveatIterator) Err() error { | ||
return nil | ||
} | ||
|
||
func (mci *memdbCaveatIterator) Close() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package memdb | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/authzed/spicedb/internal/datastore/common" | ||
"github.com/authzed/spicedb/internal/testfixtures" | ||
"github.com/authzed/spicedb/pkg/caveats" | ||
"github.com/authzed/spicedb/pkg/datastore" | ||
core "github.com/authzed/spicedb/pkg/proto/core/v1" | ||
"github.com/authzed/spicedb/pkg/tuple" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWriteReadCaveat(t *testing.T) { | ||
req := require.New(t) | ||
|
||
ds, err := NewMemdbDatastore(0, 1*time.Hour, 1*time.Hour) | ||
req.NoError(err) | ||
c := createCompiledCaveat(t) | ||
coreCaveat := createCoreCaveat(t, c, false) | ||
ctx := context.Background() | ||
rev, err := writeCaveat(ctx, ds, coreCaveat) | ||
req.NoError(err) | ||
cr, ok := ds.SnapshotReader(rev).(datastore.CaveatReader) | ||
req.True(ok, "expected a CaveatStorer value") | ||
it, err := cr.ReadCaveat(c.Name()) | ||
req.NoError(err) | ||
cv := it.Next() | ||
req.Equal(coreCaveat, cv) | ||
req.Equal(core.Caveat_NAMED, cv.Type) | ||
req.NoError(err) | ||
|
||
it, err = cr.ReadCaveat("doesnotexist") | ||
req.NoError(err) | ||
cv = it.Next() | ||
req.Nil(cv) | ||
} | ||
|
||
func TestWriteTupleWithNamedCaveat(t *testing.T) { | ||
req := require.New(t) | ||
ctx := context.Background() | ||
|
||
ds, err := NewMemdbDatastore(0, 1*time.Hour, 1*time.Hour) | ||
req.NoError(err) | ||
sds, _ := testfixtures.StandardDatastoreWithSchema(ds, req) | ||
tpl := tuple.MustParse("document:companyplan#parent@folder:company#...") | ||
c := createCompiledCaveat(t) | ||
coreCaveat := createCoreCaveat(t, c, false) | ||
tpl.Caveat = &core.CaveatReference{ | ||
Caveat: coreCaveat, | ||
} | ||
_, err = common.WriteTuples(ctx, sds, core.RelationTupleUpdate_CREATE, tpl) | ||
// should fail because the name caveat is not present in the datastore | ||
req.Error(err) | ||
// let's write the named caveat and try again | ||
_, err = writeCaveat(ctx, ds, coreCaveat) | ||
req.NoError(err) | ||
rev, err := common.WriteTuples(ctx, sds, core.RelationTupleUpdate_CREATE, tpl) | ||
req.NoError(err) | ||
iter, err := ds.SnapshotReader(rev).QueryRelationships(ctx, datastore.RelationshipsFilter{ | ||
ResourceType: tpl.ResourceAndRelation.Namespace, | ||
}) | ||
req.NoError(err) | ||
defer iter.Close() | ||
readTpl := iter.Next() | ||
req.Equal(tpl, readTpl) | ||
} | ||
|
||
func TestWriteTupleWithAnonymousCaveat(t *testing.T) { | ||
req := require.New(t) | ||
ctx := context.Background() | ||
|
||
ds, err := NewMemdbDatastore(0, 1*time.Hour, 1*time.Hour) | ||
req.NoError(err) | ||
sds, _ := testfixtures.StandardDatastoreWithSchema(ds, req) | ||
tpl := tuple.MustParse("document:companyplan#parent@folder:company#...") | ||
c := createCompiledCaveat(t) | ||
coreCaveat := createCoreCaveat(t, c, true) | ||
tpl.Caveat = &core.CaveatReference{ | ||
Caveat: coreCaveat, | ||
} | ||
rev, err := common.WriteTuples(ctx, sds, core.RelationTupleUpdate_CREATE, tpl) | ||
// the caveat is anonymous and is created alongside the tuple | ||
req.NoError(err) | ||
iter, err := ds.SnapshotReader(rev).QueryRelationships(ctx, datastore.RelationshipsFilter{ | ||
ResourceType: tpl.ResourceAndRelation.Namespace, | ||
}) | ||
req.NoError(err) | ||
defer iter.Close() | ||
readTpl := iter.Next() | ||
req.Equal(tpl, readTpl) | ||
} | ||
|
||
func writeCaveat(ctx context.Context, ds datastore.Datastore, coreCaveat *core.Caveat) (datastore.Revision, error) { | ||
return ds.ReadWriteTx(ctx, func(ctx context.Context, tx datastore.ReadWriteTransaction) error { | ||
cs, ok := tx.(datastore.CaveatStorer) | ||
if !ok { | ||
panic("expected a CaveatStorer value") | ||
} | ||
return cs.WriteCaveat([]*core.Caveat{coreCaveat}) | ||
}) | ||
} | ||
|
||
func createCoreCaveat(t *testing.T, c *caveats.CompiledCaveat, anonymous bool) *core.Caveat { | ||
t.Helper() | ||
cBytes, err := c.Serialize() | ||
require.NoError(t, err) | ||
ty := core.Caveat_NAMED | ||
if anonymous { | ||
ty = core.Caveat_ANONYMOUS | ||
} | ||
coreCaveat := &core.Caveat{ | ||
Digest: c.Name(), | ||
Logic: cBytes, | ||
Type: ty, | ||
} | ||
require.NoError(t, err) | ||
return coreCaveat | ||
} | ||
|
||
func createCompiledCaveat(t *testing.T) *caveats.CompiledCaveat { | ||
t.Helper() | ||
env, err := caveats.EnvForVariables(map[string]caveats.VariableType{ | ||
"a": caveats.IntType, | ||
"b": caveats.IntType, | ||
}) | ||
require.NoError(t, err) | ||
c, err := caveats.CompileCaveatWithName(env, "a == b", "test") | ||
require.NoError(t, err) | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.