Skip to content

Commit

Permalink
Merge pull request #17 from tozny/feature/e3db-648
Browse files Browse the repository at this point in the history
Delete AKs when revoking sharing
  • Loading branch information
ericmann committed Aug 25, 2017
2 parents 385794c + 9c46987 commit bc13297
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
7 changes: 5 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,6 @@ func (c *Client) Share(ctx context.Context, recordType string, readerID string)
// Unshare revokes another e3db client's permission to read records of the
// given record type.
func (c *Client) Unshare(ctx context.Context, recordType string, readerID string) error {
// TODO: Need to delete their access key!

id := c.Options.ClientID
u := fmt.Sprintf("%s/v1/storage/policy/%s/%s/%s/%s", c.apiURL(), id, id, readerID, recordType)
req, err := http.NewRequest("PUT", u, strings.NewReader(denyReadPolicy))
Expand All @@ -429,6 +427,11 @@ func (c *Client) Unshare(ctx context.Context, recordType string, readerID string
return err
}

err = c.deleteAccessKey(ctx, id, id, readerID, recordType)
if err != nil {
return err
}

defer closeResp(resp)
return nil
}
Expand Down
17 changes: 10 additions & 7 deletions client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ func TestWriteThenDelete(t *testing.T) {

func TestShare(t *testing.T) {
data := make(map[string]string)
ctype := "test-data-" + base64Encode(randomSecretKey()[:8])

data["message"] = "Hello, world!"
_, err := client.Write(context.Background(), "test-data", data, nil)
_, err := client.Write(context.Background(), ctype, data, nil)
if err != nil {
t.Fatal(err)
}

err = client.Share(context.Background(), "test-data", clientSharedWithID)
err = client.Share(context.Background(), ctype, clientSharedWithID)
if err != nil {
t.Error(err)
}
Expand All @@ -169,30 +171,31 @@ func haveSharedWith(id, recordType string) (bool, error) {
// TestShareThenUnshare should share then revoke sharing
func TestShareThenUnshare(t *testing.T) {
data := make(map[string]string)
ctype := "test-share-data-" + base64Encode(randomSecretKey()[:8])
data["message"] = "Hello, world!"
_, err := client.Write(context.Background(), "test-share-data", data, nil)
_, err := client.Write(context.Background(), ctype, data, nil)
if err != nil {
t.Fatal(err)
}

err = client.Share(context.Background(), "test-share-data", clientSharedWithID)
err = client.Share(context.Background(), ctype, clientSharedWithID)
if err != nil {
t.Error(err)
}

isShared, err := haveSharedWith(clientSharedWithID, "test-share-data")
isShared, err := haveSharedWith(clientSharedWithID, ctype)
if err != nil {
t.Errorf("share failed: %s", err)
} else if !isShared {
t.Error("share: have not shared with client")
}

err = client.Unshare(context.Background(), "test-share-data", clientSharedWithID)
err = client.Unshare(context.Background(), ctype, clientSharedWithID)
if err != nil {
t.Errorf("Unshare failed: %s", err)
}

isShared, err = haveSharedWith(clientSharedWithID, "test-share-data")
isShared, err = haveSharedWith(clientSharedWithID, ctype)
if err != nil {
t.Errorf("unshare failed: %s", err)
} else if isShared {
Expand Down
22 changes: 22 additions & 0 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,28 @@ func (c *Client) putAccessKey(ctx context.Context, writerID, userID, readerID, r
return nil
}

func (c *Client) deleteAccessKey(ctx context.Context, writerID, userID, readerID, recordType string) error {
u := fmt.Sprintf("%s/v1/storage/access_keys/%s/%s/%s/%s", c.apiURL(), writerID, userID, readerID, recordType)
req, err := http.NewRequest("DELETE", u, nil)
if err != nil {
return err
}

resp, err := c.rawCall(ctx, req, nil)
if err != nil {
return err
}

defer closeResp(resp)

if c.akCache != nil {
cacheKey := akCacheKey{writerID, userID, recordType}
c.akCache[cacheKey] = nil
}

return nil
}

// decryptRecord modifies a record in-place, decrypting all data fields
// using an access key granted by an authorizer.
func (c *Client) decryptRecord(ctx context.Context, record *Record) error {
Expand Down

0 comments on commit bc13297

Please sign in to comment.