Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ server-manual = "Server manual"
api = "https://www.mongodb.com/docs/mongoid/current/api"
ruby-api = "https://www.mongodb.com/docs/ruby-driver/current/api"
active-record-docs = "https://guides.rubyonrails.org"
shared-library = "Automatic Encryption Shared Library"
102 changes: 102 additions & 0 deletions source/includes/security/encryption.rb
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
Comment on lines +92 to +94
Copy link
Contributor

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?

Copy link
Contributor Author

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

# =>
# [#<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
3 changes: 2 additions & 1 deletion source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
Interact with Data </interact-data>
Model Your Data </data-modeling>
Configuration </configuration>
Secure Your Data </security>
/working-with-data
API Documentation </api>
/whats-new
Issues & Help </issues-and-help>
/additional-resources
/ecosystem
/ecosystem

Check failure on line 27 in source/index.txt

View workflow job for this annotation

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.ConciseTerms] 'more' is preferred over 'additional'. Raw Output: {"message": "[MongoDB.ConciseTerms] 'more' is preferred over 'additional'.", "location": {"path": "source/index.txt", "range": {"start": {"line": 27, "column": 5}}}, "severity": "ERROR"}
21 changes: 21 additions & 0 deletions source/security.txt
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+}.
Loading
Loading