Skip to content

Commit

Permalink
Fix encryption in stream writer (dgraph-io#1146)
Browse files Browse the repository at this point in the history
We store information about tables in the table manifest. When stream
writer would create a new table, it wouldn't store the key ID for the
respective table in the manifest file. This commit fixes it.

Fixes dgraph-io#1144
  • Loading branch information
Ibrahim Jarif authored Dec 6, 2019
1 parent 1ee50f7 commit 8b99eb4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions stream_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ func (w *sortedWriter) createTable(builder *table.Builder) error {
// Now that table can be opened successfully, let's add this to the MANIFEST.
change := &pb.ManifestChange{
Id: tbl.ID(),
KeyId: tbl.KeyID(),
Op: pb.ManifestChange_CREATE,
Level: uint32(lhandler.level),
Compression: uint32(tbl.CompressionType()),
Expand Down
43 changes: 43 additions & 0 deletions stream_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,46 @@ func TestSendOnClosedStream2(t *testing.T) {

require.NoError(t, sw.Write(list), "sw.Write() failed")
}

func TestStreamWriterEncrypted(t *testing.T) {
dir, err := ioutil.TempDir("", "badger-test")
require.NoError(t, err)

opts := DefaultOptions(dir)
defer removeDir(dir)

opts = opts.WithEncryptionKey([]byte("badgerkey16bytes"))
db, err := Open(opts)
require.NoError(t, err)
key := []byte("mykey")
value := []byte("myvalue")

list := &pb.KVList{}
list.Kv = append(list.Kv, &pb.KV{
Key: key,
Value: value,
Version: 20,
})

sw := db.NewStreamWriter()
require.NoError(t, sw.Prepare(), "Prepare failed")
require.NoError(t, sw.Write(list), "Write failed")
require.NoError(t, sw.Flush(), "Flush failed")

err = db.View(func(txn *Txn) error {
item, err := txn.Get(key)
require.NoError(t, err)
val, err := item.ValueCopy(nil)
require.Equal(t, value, val)
require.NoError(t, err)
return nil
})
require.NoError(t, err, "Error while retrieving key")
require.NoError(t, db.Close())

opts = opts.WithEncryptionKey([]byte("badgerkey16bytes"))
db, err = Open(opts)
require.NoError(t, err)
require.NoError(t, db.Close())

}

0 comments on commit 8b99eb4

Please sign in to comment.