-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-45366 Encryption #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jordan-smith721
merged 6 commits into
mongodb:standardized
from
jordan-smith721:DOCSP-45366-encryption
Jan 21, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
43bcf10
Add encryption page
jordan-smith721 61f4332
edits
jordan-smith721 e4aec01
Merge branch 'standardized' into DOCSP-45366-encryption
jordan-smith721 c863f4c
feedback
jordan-smith721 d67a292
missed change
jordan-smith721 213cfac
feedback
jordan-smith721 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,102 @@ | ||
# start-encryption-schema | ||
class Patient | ||
include Mongoid::Document | ||
include Mongoid::Timestamps | ||
|
||
encrypt_with key_id: '<data encryption key>' | ||
|
||
# This field is not encrypted | ||
field :category, type: String | ||
|
||
# This field is encrypted by using AEAD_AES_256_CBC_HMAC_SHA_512-Random | ||
# algorithm | ||
field :passport_id, type: String, encrypt: { | ||
deterministic: false | ||
} | ||
|
||
# This field is encrypted by using AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic | ||
# algorithm | ||
field :blood_type, type: String, encrypt: { | ||
deterministic: true | ||
} | ||
|
||
# This field is encrypted by using AEAD_AES_256_CBC_HMAC_SHA_512-Random | ||
# algorithm and a different data key | ||
field :ssn, type: Integer, encrypt: { | ||
deterministic: false, key_id: '<New key ID' | ||
} | ||
|
||
embeds_one :insurance | ||
end | ||
|
||
class Insurance | ||
include Mongoid::Document | ||
include Mongoid::Timestamps | ||
|
||
field :insurer, type: String | ||
|
||
# This field is encrypted using AEAD_AES_256_CBC_HMAC_SHA_512-Random | ||
# algorithm using a key with an alternate name stored in the policy_number_key field | ||
field :policy_number, type: Integer, encrypt: { | ||
deterministic: false, | ||
key_name_field: :policy_number_key | ||
} | ||
|
||
embedded_in :patient | ||
end | ||
# end-encryption-schema | ||
|
||
# start-query-encrypted | ||
Patient.create!( | ||
category: 'ER', | ||
passport_id: '123456', | ||
blood_type: 'AB+', | ||
ssn: 98765, | ||
insurance: Insurance.new(insurer: 'TK', policy_number: 123456, policy_number_key: 'my_data_key') | ||
) | ||
|
||
# Fields are encrypted in the database | ||
unencrypted_client['patients'].find.first | ||
# end-query-encrypted | ||
|
||
# start-rewrap-keys | ||
# Create a key vault client | ||
key_vault_client = Mongo::Client.new('<connection string>') | ||
|
||
# Create the encryption object | ||
encryption = Mongo::ClientEncryption.new( | ||
key_vault_client, | ||
key_vault_namespace: 'encryption.__keyVault', | ||
kms_providers: { | ||
aws: { | ||
"accessKeyId": "<IAM User Access Key ID>", | ||
"secretAccessKey": "<IAM User Secret Access Key>" | ||
} | ||
} | ||
) | ||
|
||
encryption.rewrap_many_data_key( | ||
{}, # Empty filter to rewrap all keys | ||
{ | ||
provider: 'aws', | ||
master_key: { | ||
region: 'us-east-2', | ||
key: 'arn:aws:kms:us-east-2:...' | ||
} | ||
} | ||
) | ||
# end-rewrap-keys | ||
|
||
# start-in-place | ||
|
||
# Print all documents in the collection. The first document is unencrypted, and | ||
# the second is encrypted. | ||
Patient.all.to_a | ||
# => | ||
# [#<Patient _id: 644937ac46ebfd02468e58c8, category: "ER", passport_id: "DE-1257", blood_type: "AB+", ssn: 123456>, | ||
# #<Patient _id: 644937c946ebfd029309b912, category: "ER", passport_id: "AT-1545", blood_type: "AB+", ssn: 987654>] | ||
|
||
# Querying for documents with a CSFLE-enabled client returns only the encrypted document | ||
Patient.where(blood_type: 'AB+').to_a | ||
# => [#<Patient _id: 644937c946ebfd029309b912, category: "ER", passport_id: "AT-1545", blood_type: "AB+", ssn: 987654>] | ||
# end-in-place |
This file contains hidden or 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 hidden or 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,21 @@ | ||
.. _mongoid-security: | ||
|
||
================ | ||
Secure Your Data | ||
================ | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: ruby framework, odm, security | ||
|
||
.. toctree:: | ||
:caption: Secure Your Data | ||
|
||
In-Use Encryption </security/encryption> | ||
|
||
In this section, you can learn how to secure your data when using {+odm+}. | ||
|
||
- :ref:`Client-Side Field Level Encryption <mongoid-encryption>` Learn how to encrypt your data with {+odm+}. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: does this code example mean that
all
returns all documents but querying only returns encrypted documents?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. So apparently
all
isn't considered a query operation, the way I understood it