Skip to content

Commit

Permalink
avoid log fatal (#466)
Browse files Browse the repository at this point in the history
* avoid log fatal

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* db: disallow new dbs with non-read thread keys

Signed-off-by: Sander Pick <sanderpick@gmail.com>

Co-authored-by: Sander Pick <sanderpick@gmail.com>
  • Loading branch information
jsign and sanderpick authored Dec 1, 2020
1 parent c8aace9 commit 1e36eea
Show file tree
Hide file tree
Showing 10 changed files with 2,100 additions and 3,511 deletions.
12 changes: 5 additions & 7 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,10 @@ func (c *Client) NewDB(ctx context.Context, dbID thread.ID, opts ...db.NewManage
ctx = thread.NewTokenContext(ctx, args.Token)
_, err := c.c.NewDB(ctx, &pb.NewDBRequest{
DbID: dbID.Bytes(),
Collections: pbcollections,
Name: args.Name,
Block: args.Block,
ThreadKey: args.ThreadKey.Bytes(),
Key: args.Key.Bytes(),
LogKey: logKey,
Name: args.Name,
Collections: pbcollections,
})
return err
}
Expand Down Expand Up @@ -213,11 +212,10 @@ func (c *Client) NewDBFromAddr(ctx context.Context, dbAddr ma.Multiaddr, dbKey t
_, err := c.c.NewDBFromAddr(ctx, &pb.NewDBFromAddrRequest{
Addr: dbAddr.Bytes(),
Key: dbKey.Bytes(),
Collections: pbcollections,
LogKey: logKey,
Name: args.Name,
Collections: pbcollections,
Block: args.Block,
ThreadKey: args.ThreadKey.Bytes(),
LogKey: logKey,
})
return err
}
Expand Down
21 changes: 21 additions & 0 deletions api/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net"
"os"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -52,6 +53,16 @@ func TestClient_NewDB(t *testing.T) {
client, done := setup(t)
defer done()

t.Run("test new db with missing read key", func(t *testing.T) {
if err := client.NewDB(
context.Background(),
thread.NewIDV1(thread.Raw, 32),
db.WithNewManagedKey(thread.NewRandomServiceKey()),
); err == nil || !strings.Contains(err.Error(), db.ErrThreadReadKeyRequired.Error()) {
t.Fatal("new db without read key should fail")
}
})

t.Run("test new db", func(t *testing.T) {
if err := client.NewDB(context.Background(), thread.NewIDV1(thread.Raw, 32)); err != nil {
t.Fatalf("failed to create new db: %v", err)
Expand All @@ -72,6 +83,16 @@ func TestClient_NewDBFromAddr(t *testing.T) {
info, err := client1.GetDBInfo(context.Background(), id)
checkErr(t, err)

t.Run("test new db with missing read key", func(t *testing.T) {
if err = client2.NewDBFromAddr(
context.Background(),
info.Addrs[0],
thread.NewServiceKey(info.Key.Service()),
); err == nil || !strings.Contains(err.Error(), db.ErrThreadReadKeyRequired.Error()) {
t.Fatal("new db from addr without read key should fail")
}
})

t.Run("test new db from address", func(t *testing.T) {
if err = client2.NewDBFromAddr(context.Background(), info.Addrs[0], info.Key); err != nil {
t.Fatalf("failed to create new db from address: %v", err)
Expand Down
Loading

0 comments on commit 1e36eea

Please sign in to comment.