-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(DOCSP-33626) Document metadata encryption (#3068)
## Pull Request Info ### Jira - https://jira.mongodb.org/browse/DOCSP-33626 ### Staged Changes - Node: [Connect to an Atlas App Services Backend](https://preview-mongodbkrollinsmdb.gatsbyjs.io/realm/DOCSP-33626/sdk/node/app-services/connect-to-app-services-backend/#encrypt-app-metadata) - Node: [Encrypt a Realm](https://preview-mongodbkrollinsmdb.gatsbyjs.io/realm/DOCSP-33626/sdk/node/realm-files/encrypt/#encrypt-app-services-app-metadata) - React Native: [Connect to an Atlas App Services App](https://preview-mongodbkrollinsmdb.gatsbyjs.io/realm/DOCSP-33626/sdk/react-native/app-services/connect-to-app-services-app/#encrypt-app-metadata) - React Native: [Encrypt a Realm](https://preview-mongodbkrollinsmdb.gatsbyjs.io/realm/DOCSP-33626/sdk/react-native/realm-files/encrypt/#encrypt-app-services-app-metadata) ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md) ### Animal Wearing a Hat <img src="https://i.pinimg.com/432x292/1c/c5/a7/1cc5a7f86c07156be178fff0b659306f.jpg" width=400> --------- Co-authored-by: cbullinger <115956901+cbullinger@users.noreply.github.com>
- Loading branch information
1 parent
12f0090
commit 60532d0
Showing
25 changed files
with
489 additions
and
74 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
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,64 @@ | ||
// :snippet-start: imports | ||
import Realm, { BSON, MetadataMode } from "realm"; | ||
// :snippet-end: | ||
import { Task } from "./models/models.ts"; | ||
import { APP_ID } from "../config.ts"; | ||
|
||
describe("Handle Realm Metadata", () => { | ||
test("encrypting realm metadata", () => { | ||
// :snippet-start: encrypt-metadata | ||
// Retrieve encryption key from secure location or create one | ||
const encryptionKey = new ArrayBuffer(64); | ||
|
||
// Use encryption key in app configuration | ||
const config = { | ||
id: APP_ID, | ||
// :emphasize-start: | ||
metadata: { mode: MetadataMode.Encryption, encryptionKey: encryptionKey }, | ||
// :emphasize-end: | ||
}; | ||
const app = new Realm.App(config); | ||
// :snippet-end: | ||
|
||
expect(app).toBeInstanceOf(Realm.App); | ||
}); | ||
|
||
test("encrypting a realm", async () => { | ||
const taskId = new BSON.ObjectId(); | ||
// :snippet-start: encrypt-realm | ||
// Retrieve encryption key from secure location or create one | ||
const encryptionKey = new ArrayBuffer(64); | ||
|
||
// Use encryption key in realm configuration | ||
const config = { | ||
schema: [Task], | ||
encryptionKey: encryptionKey, // :emphasize: | ||
}; | ||
|
||
const realm = await Realm.open(config); | ||
// :snippet-end: | ||
expect(realm.isClosed).toBeFalsy; | ||
|
||
realm.write(() => { | ||
realm.create(Task, { | ||
_id: taskId, | ||
name: "Sweep the floor", | ||
}); | ||
}); | ||
|
||
const Tasks = realm.objects(Task); | ||
expect(Tasks.length).toBeGreaterThan(0); | ||
|
||
realm.close(); | ||
|
||
// Reopen realm with key | ||
const openRealmAgain = await Realm.open({ | ||
schema: [Task], | ||
encryptionKey: encryptionKey, | ||
}); | ||
|
||
const existingTask = openRealmAgain.objectForPrimaryKey(Task, taskId); | ||
expect(existingTask).toBeTruthy; | ||
expect(existingTask?._id).toEqual(taskId); | ||
}); | ||
}); |
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,69 @@ | ||
// :snippet-start: imports | ||
import Realm, { | ||
AppConfiguration, | ||
BSON, | ||
MetadataMode, | ||
Configuration, | ||
} from "realm"; | ||
// :snippet-end: | ||
import { Task } from "./models/models"; | ||
import { APP_ID } from "../config"; | ||
|
||
describe("Handle Realm Metadata", () => { | ||
test("encrypting realm metadata", () => { | ||
// :snippet-start: encrypt-metadata | ||
// Retrieve encryption key from secure location or create one | ||
const encryptionKey = new ArrayBuffer(64); | ||
|
||
// Use encryption key in app configuration | ||
const config: AppConfiguration = { | ||
id: APP_ID, | ||
// :emphasize-start: | ||
metadata: { mode: MetadataMode.Encryption, encryptionKey: encryptionKey }, | ||
// :emphasize-end: | ||
}; | ||
const app = new Realm.App(config); | ||
// :snippet-end: | ||
|
||
expect(app).toBeInstanceOf(Realm.App); | ||
}); | ||
|
||
test("encrypting a realm", async () => { | ||
const taskId = new BSON.ObjectId(); | ||
// :snippet-start: encrypt-realm | ||
// Retrieve encryption key from secure location or create one | ||
const encryptionKey = new ArrayBuffer(64); | ||
|
||
// Use encryption key in realm configuration | ||
const config: Configuration = { | ||
schema: [Task], | ||
encryptionKey: encryptionKey, // :emphasize: | ||
}; | ||
|
||
const realm = await Realm.open(config); | ||
// :snippet-end: | ||
expect(realm.isClosed).toBeFalsy; | ||
|
||
realm.write(() => { | ||
realm.create(Task, { | ||
_id: taskId, | ||
name: "Sweep the floor", | ||
}); | ||
}); | ||
|
||
const Tasks = realm.objects(Task); | ||
expect(Tasks.length).toBeGreaterThan(0); | ||
|
||
realm.close(); | ||
|
||
// Reopen realm with key | ||
const openRealmAgain = await Realm.open({ | ||
schema: [Task], | ||
encryptionKey: encryptionKey, | ||
}); | ||
|
||
const existingTask = openRealmAgain.objectForPrimaryKey(Task, taskId); | ||
expect(existingTask).toBeTruthy; | ||
expect(existingTask?._id).toEqual(taskId); | ||
}); | ||
}); |
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,23 @@ | ||
import Realm, { BSON, ObjectSchema } from "realm"; | ||
|
||
export class Task extends Realm.Object<Task> { | ||
_id!: BSON.ObjectId; | ||
name!: String; | ||
status?: String; | ||
progressMinutes?: Number; | ||
owner?: String; | ||
dueDate?: Date; | ||
|
||
static schema: ObjectSchema = { | ||
name: "Task", | ||
properties: { | ||
_id: "objectId", | ||
name: "string", | ||
status: "string?", | ||
progressMinutes: "int?", | ||
owner: "string?", | ||
dueDate: "date?", | ||
}, | ||
primaryKey: "_id", | ||
}; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
examples/react-native/v12/TestApp/src/components/encryption/EncryptMetadata.test.tsx
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,16 @@ | ||
import 'react-native'; | ||
import React from 'react'; | ||
import {render, screen} from '@testing-library/react-native'; | ||
|
||
import {EncryptMetadata} from './EncryptMetadata'; | ||
|
||
// Create encryption key for encryption examples. | ||
const encryptionKey = new ArrayBuffer(64); | ||
|
||
test('linking an anonymous user with an email/password account', async () => { | ||
render(<EncryptMetadata encryptionKey={encryptionKey} />); | ||
|
||
const encryptionResultTextNode = await screen.findByTestId('is-realm-app'); | ||
expect(encryptionResultTextNode).toBeInTheDocument; | ||
expect(encryptionResultTextNode.children[1]).toBe('true'); | ||
}); |
59 changes: 59 additions & 0 deletions
59
examples/react-native/v12/TestApp/src/components/encryption/EncryptMetadata.tsx
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,59 @@ | ||
// :snippet-start: imports | ||
import React from 'react'; | ||
import {Text, View} from 'react-native'; | ||
import {MetadataMode} from 'realm'; | ||
import {AppProvider} from '@realm/react'; | ||
// :snippet-end: | ||
import {StyleSheet} from 'react-native'; | ||
import {useApp} from '@realm/react'; | ||
import {APP_ID} from '../../../appServicesConfig'; | ||
|
||
// :snippet-start: encrypt-metadata | ||
// :replace-start: { | ||
// "terms": { | ||
// "export ": "" | ||
// } | ||
// } | ||
export const EncryptMetadata = ({ | ||
encryptionKey, | ||
}: { | ||
encryptionKey: ArrayBuffer; | ||
}) => { | ||
const metadataConfig = { | ||
// :emphasize-start: | ||
mode: MetadataMode.Encryption, | ||
encryptionKey: encryptionKey, | ||
// :emphasize-end: | ||
}; | ||
|
||
return ( | ||
<AppProvider | ||
id={APP_ID} | ||
metadata={metadataConfig}> | ||
<RestOfApp /> | ||
</AppProvider> | ||
); | ||
}; | ||
// :replace-end: | ||
// :snippet-end: | ||
|
||
const RestOfApp = () => { | ||
const app = useApp(); | ||
|
||
return ( | ||
<View style={styles.section}> | ||
<Text testID="is-realm-app"> | ||
Is an instance of `Realm.App`?: {app ? 'true' : 'false'} | ||
</Text> | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
section: { | ||
flex: 1, | ||
marginTop: 8, | ||
paddingVertical: 12, | ||
alignItems: 'center', | ||
}, | ||
}); |
Oops, something went wrong.