Skip to content

Commit

Permalink
Encrypt counter CRDT fields
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Jun 25, 2024
1 parent f99525f commit f63fa88
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 17 deletions.
8 changes: 4 additions & 4 deletions internal/core/crdt/ipld_union.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,21 @@ func (c CRDT) GetStatus() uint8 {
}

// GetData returns the data of the delta.
//
// Currently only implemented for LWWRegDelta.
func (c CRDT) GetData() []byte {
if c.LWWRegDelta != nil {
return c.LWWRegDelta.Data
} else if c.CounterDelta != nil {
return c.CounterDelta.Data
}
return nil
}

// SetData sets the data of the delta.
//
// Currently only implemented for LWWRegDelta.
func (c CRDT) SetData(data []byte) {
if c.LWWRegDelta != nil {
c.LWWRegDelta.Data = data
} else if c.CounterDelta != nil {
c.CounterDelta.Data = data
}
}

Expand Down
112 changes: 100 additions & 12 deletions tests/integration/encryption/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func encrypt(plaintext []byte) []byte {
return val
}

func TestDocEncryption_ShouldStoreCommitsDeltaEncrypted(t *testing.T) {
func TestDocEncryption_WithEncryptionOnLWWCRDT_ShouldStoreCommitsDeltaEncrypted(t *testing.T) {
const docID = "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3"

test := testUtils.TestCase{
Actions: []any{
updateUserCollectionSchema(),
Expand Down Expand Up @@ -56,7 +58,7 @@ func TestDocEncryption_ShouldStoreCommitsDeltaEncrypted(t *testing.T) {
"cid": "bafyreih7ry7ef26xn3lm2rhxusf2rbgyvl535tltrt6ehpwtvdnhlmptiu",
"collectionID": int64(1),
"delta": encrypt(testUtils.CBORValue(21)),
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
"docID": docID,
"fieldId": "1",
"fieldName": "age",
"height": int64(1),
Expand All @@ -66,7 +68,7 @@ func TestDocEncryption_ShouldStoreCommitsDeltaEncrypted(t *testing.T) {
"cid": "bafyreifusejlwidaqswasct37eorazlfix6vyyn5af42pmjvktilzj5cty",
"collectionID": int64(1),
"delta": encrypt(testUtils.CBORValue("John")),
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
"docID": docID,
"fieldId": "2",
"fieldName": "name",
"height": int64(1),
Expand All @@ -76,7 +78,7 @@ func TestDocEncryption_ShouldStoreCommitsDeltaEncrypted(t *testing.T) {
"cid": "bafyreicvxlfxeqghmc3gy56rp5rzfejnbng4nu77x5e3wjinfydl6wvycq",
"collectionID": int64(1),
"delta": nil,
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
"docID": docID,
"fieldId": "C",
"fieldName": nil,
"height": int64(1),
Expand All @@ -99,7 +101,7 @@ func TestDocEncryption_ShouldStoreCommitsDeltaEncrypted(t *testing.T) {
testUtils.ExecuteTestCase(t, test)
}

func TestDocEncryption_UponUpdate_ShouldEncryptedCommitDelta(t *testing.T) {
func TestDocEncryption_UponUpdateOnLWWCRDT_ShouldEncryptCommitDelta(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
updateUserCollectionSchema(),
Expand Down Expand Up @@ -138,20 +140,21 @@ func TestDocEncryption_UponUpdate_ShouldEncryptedCommitDelta(t *testing.T) {
testUtils.ExecuteTestCase(t, test)
}

func TestDocEncryption_WithMultipleDocsUponUpdate_ShouldEncryptedOnlyRelevantDocs(t *testing.T) {
func TestDocEncryption_WithMultipleDocsUponUpdate_ShouldEncryptOnlyRelevantDocs(t *testing.T) {
const johnDocID = "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3"
const islamDocID = "bae-d55bd956-1cc4-5d26-aa71-b98807ad49d6"

test := testUtils.TestCase{
Actions: []any{
updateUserCollectionSchema(),
testUtils.CreateDoc{
// bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3
Doc: `{
"name": "John",
"age": 21
}`,
IsEncrypted: true,
},
testUtils.CreateDoc{
// bae-d55bd956-1cc4-5d26-aa71-b98807ad49d6
Doc: `{
"name": "Islam",
"age": 33
Expand Down Expand Up @@ -181,19 +184,104 @@ func TestDocEncryption_WithMultipleDocsUponUpdate_ShouldEncryptedOnlyRelevantDoc
Results: []map[string]any{
{
"delta": encrypt(testUtils.CBORValue(22)),
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
"docID": johnDocID,
},
{
"delta": encrypt(testUtils.CBORValue(21)),
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
"docID": johnDocID,
},
{
"delta": testUtils.CBORValue(34),
"docID": "bae-d55bd956-1cc4-5d26-aa71-b98807ad49d6",
"docID": islamDocID,
},
{
"delta": testUtils.CBORValue(33),
"docID": "bae-d55bd956-1cc4-5d26-aa71-b98807ad49d6",
"docID": islamDocID,
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}

func TestDocEncryption_WithEncryptionOnCounterCRDT_ShouldStoreCommitsDeltaEncrypted(t *testing.T) {
const docID = "bae-d3cc98b4-38d5-5c50-85a3-d3045d44094e"

test := testUtils.TestCase{
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
points: Int @crdt(type: "pcounter")
}
`},
testUtils.CreateDoc{
Doc: `{ "points": 5 }`,
IsEncrypted: true,
},
testUtils.Request{
Request: `
query {
commits {
cid
delta
docID
}
}
`,
Results: []map[string]any{
{
"cid": "bafyreieb6owsoljj4vondkx35ngxmhliauwvphicz4edufcy7biexij7mu",
"delta": encrypt(testUtils.CBORValue(5)),
"docID": docID,
},
{
"cid": "bafyreif2lejhvdja2rmo237lrwpj45usrm55h6gzr4ewl6gajq3cl4ppsi",
"delta": nil,
"docID": docID,
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}

func TestDocEncryption_UponUpdateOnCounterCRDT_ShouldEncryptedCommitDelta(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
points: Int @crdt(type: "pcounter")
}
`},
testUtils.CreateDoc{
Doc: `{ "points": 5 }`,
IsEncrypted: true,
},
testUtils.UpdateDoc{
Doc: `{
"points": 3
}`,
},
testUtils.Request{
Request: `
query {
commits(fieldId: "1") {
delta
}
}
`,
Results: []map[string]any{
{
"delta": encrypt(testUtils.CBORValue(3)),
},
{
"delta": encrypt(testUtils.CBORValue(5)),
},
},
},
Expand Down
58 changes: 58 additions & 0 deletions tests/integration/encryption/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,61 @@ func TestDocEncryption_WithEncryption_ShouldFetchDecrypted(t *testing.T) {

testUtils.ExecuteTestCase(t, test)
}

func TestDocEncryption_WithEncryptionOnCounterCRDT_ShouldFetchDecrypted(t *testing.T) {
const docID = "bae-ab8ae7d9-6473-5101-ba02-66b217948d7a"

const query = `
query {
Users {
_docID
name
points
}
}`

test := testUtils.TestCase{
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
points: Int @crdt(type: "pcounter")
}
`},
testUtils.CreateDoc{
Doc: `{
"name": "John",
"points": 5
}`,
IsEncrypted: true,
},
testUtils.Request{
Request: query,
Results: []map[string]any{
{
"_docID": docID,
"name": "John",
"points": 5,
},
},
},
testUtils.UpdateDoc{
DocID: 0,
Doc: `{ "points": 3 }`,
},
testUtils.Request{
Request: query,
Results: []map[string]any{
{
"_docID": docID,
"name": "John",
"points": 8,
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}
2 changes: 1 addition & 1 deletion tests/integration/encryption/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
const userCollectionGQLSchema = (`
type Users {
name: String
age: Int
age: Int @crdt(type: "lww")
verified: Boolean
}
`)
Expand Down

0 comments on commit f63fa88

Please sign in to comment.