-
Notifications
You must be signed in to change notification settings - Fork 166
Add Document Encryption Service for Doc Escrow #11714
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3d09079
changelog: Upcoming Features, Doc Escrow, Add encryption and storage …
Sgtpluck 9fc4634
Update writer to only take one image
Sgtpluck 767b5f6
Update spec/services/encrypted_doc_storage/doc_writer_spec.rb
Sgtpluck 98539bc
Update app/services/encrypted_doc_storage/doc_writer.rb
Sgtpluck d36e791
Add encoding so LocalStorage can handle encoding
Sgtpluck b428e11
Fix linting errors
Sgtpluck 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module EncryptedDocStorage | ||
| class DocWriter | ||
| Result = Struct.new( | ||
| :name, | ||
| :encryption_key, | ||
| ) | ||
|
|
||
| def write(image:, data_store: LocalStorage) | ||
| name = SecureRandom.uuid | ||
| storage = data_store.new | ||
|
|
||
| storage.write_image( | ||
| encrypted_image: aes_cipher.encrypt(image, key), | ||
| name:, | ||
| ) | ||
|
|
||
| Result.new( | ||
| name:, | ||
| encryption_key: Base64.strict_encode64(key), | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def aes_cipher | ||
| @aes_cipher ||= Encryption::AesCipherV2.new | ||
| end | ||
|
|
||
| def key | ||
| @key ||= SecureRandom.bytes(32) | ||
| end | ||
| end | ||
| end |
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,19 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module EncryptedDocStorage | ||
| class LocalStorage | ||
| def write_image(encrypted_image:, name:) | ||
| FileUtils.mkdir_p(tmp_document_storage_dir) | ||
|
|
||
| File.open(tmp_document_storage_dir.join(name), 'wb') do |f| | ||
| f.write(encrypted_image) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def tmp_document_storage_dir | ||
| Rails.root.join('tmp', 'encrypted_doc_storage') | ||
| end | ||
| end | ||
| end |
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,27 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module EncryptedDocStorage | ||
| class S3Storage | ||
| def write_image(encrypted_image:, name:) | ||
| s3_client.put_object( | ||
| bucket:, | ||
| body: encrypted_image, | ||
| key: name, | ||
| ) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def s3_client | ||
| Aws::S3::Client.new( | ||
| http_open_timeout: 5, | ||
| http_read_timeout: 5, | ||
| compute_checksums: false, | ||
| ) | ||
| end | ||
|
|
||
| def bucket | ||
| IdentityConfig.store.encrypted_document_storage_s3_bucket | ||
| end | ||
| end | ||
| end |
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
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,52 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe EncryptedDocStorage::DocWriter do | ||
| describe '#write' do | ||
| let(:img_path) { Rails.root.join('app', 'assets', 'images', 'logo.svg') } | ||
| let(:image) { File.read(img_path) } | ||
|
|
||
| subject do | ||
| EncryptedDocStorage::DocWriter.new | ||
| end | ||
|
|
||
| it 'encrypts the document and writes it to storage' do | ||
| result = subject.write(image:) | ||
|
|
||
| key = Base64.strict_decode64(result.encryption_key) | ||
| aes_cipher = Encryption::AesCipherV2.new | ||
|
|
||
| written_image = aes_cipher.decrypt( | ||
| File.read(file_path(result.name)), | ||
| key, | ||
| ) | ||
|
|
||
| # cleanup | ||
| File.delete(file_path(result.name)) | ||
|
|
||
| expect(written_image).to eq(image) | ||
| end | ||
|
|
||
| it 'uses LocalStorage by default' do | ||
| expect_any_instance_of(EncryptedDocStorage::LocalStorage).to receive(:write_image).once | ||
| expect_any_instance_of(EncryptedDocStorage::S3Storage).to_not receive(:write_image) | ||
|
|
||
| subject.write(image:) | ||
| end | ||
|
|
||
| context 'when S3Storage is passed in' do | ||
| it 'uses S3' do | ||
| expect_any_instance_of(EncryptedDocStorage::S3Storage).to receive(:write_image).once | ||
| expect_any_instance_of(EncryptedDocStorage::LocalStorage).not_to receive(:write_image) | ||
|
|
||
| subject.write( | ||
| image:, | ||
| data_store: EncryptedDocStorage::S3Storage, | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| def file_path(uuid) | ||
| Rails.root.join('tmp', 'encrypted_doc_storage', uuid) | ||
| end | ||
| end | ||
| end | ||
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,30 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe EncryptedDocStorage::LocalStorage do | ||
| let(:img_path) { Rails.root.join('app', 'assets', 'images', 'logo.svg') } | ||
| let(:image) { File.read(img_path) } | ||
| let(:encrypted_image) do | ||
| Encryption::AesCipherV2.new.encrypt(image, SecureRandom.bytes(32)) | ||
| end | ||
|
|
||
| describe '#write_image' do | ||
| it 'writes the document to the disk' do | ||
| name = SecureRandom.uuid | ||
|
|
||
| EncryptedDocStorage::LocalStorage.new.write_image( | ||
| encrypted_image:, | ||
| name:, | ||
| ) | ||
| path = Rails.root.join('tmp', 'encrypted_doc_storage', name) | ||
|
|
||
| f = File.new(path, 'rb') | ||
| result = f.read | ||
| f.close | ||
|
|
||
| # cleanup | ||
| File.delete(path) | ||
|
|
||
| expect(result).to eq(encrypted_image) | ||
| end | ||
| end | ||
| end |
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,31 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe EncryptedDocStorage::S3Storage do | ||
| subject { EncryptedDocStorage::S3Storage.new } | ||
| let(:img_path) { Rails.root.join('app', 'assets', 'images', 'logo.svg') } | ||
| let(:image) { File.read(img_path) } | ||
| let(:encrypted_image) do | ||
| Encryption::AesCipherV2.new.encrypt(image, SecureRandom.bytes(32)) | ||
| end | ||
|
|
||
| describe '#write_image' do | ||
| let(:stubbed_s3_client) { Aws::S3::Client.new(stub_responses: true) } | ||
|
|
||
| before do | ||
| allow(subject).to receive(:s3_client).and_return(stubbed_s3_client) | ||
| allow(stubbed_s3_client).to receive(:put_object) | ||
| end | ||
|
|
||
| it 'writes the document to S3' do | ||
| name = '123abc' | ||
|
|
||
| subject.write_image(encrypted_image:, name:) | ||
|
|
||
| expect(stubbed_s3_client).to have_received(:put_object).with( | ||
| bucket: IdentityConfig.store.encrypted_document_storage_s3_bucket, | ||
| key: name, | ||
| body: encrypted_image, | ||
| ) | ||
| end | ||
| end | ||
| end |
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.
another approach to cleanup and writing files to disk would be to make the whole example use a tmpdir, like we do here. We could update the class to take an optional
dirargument and pass the path in here, the default value would be theRails.root.join('tmp')like have it nowidentity-idp/spec/lib/i18n_flat_yml_backend_spec.rb
Lines 36 to 41 in 0b303ab
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.
i considered a couple different approaches to cleanup, but since it was just two specs in different spec files, it seemed fine to just inline it for now. will definitely consider other approaches as we extend it. (unless you feel very strongly about it, then i can update it now!)
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.
Not that strongly!