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
35 changes: 35 additions & 0 deletions app/services/encrypted_doc_storage/doc_writer.rb
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
19 changes: 19 additions & 0 deletions app/services/encrypted_doc_storage/local_storage.rb
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
27 changes: 27 additions & 0 deletions app/services/encrypted_doc_storage/s3_storage.rb
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
2 changes: 2 additions & 0 deletions config/application.yml.default
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ enable_load_testing_mode: false
enable_rate_limiting: true
enable_test_routes: true
enable_usps_verification: true
encrypted_document_storage_s3_bucket: 'test-bucket'
event_disavowal_expiration_hours: 240
facial_match_general_availability_enabled: true
feature_idv_force_gpo_verification_enabled: false
Expand Down Expand Up @@ -522,6 +523,7 @@ production:
email_registrations_per_ip_track_only_mode: true
enable_test_routes: false
enable_usps_verification: false
encrypted_document_storage_s3_bucket: ''
facial_match_general_availability_enabled: false
feature_select_email_to_share_enabled: false
idv_sp_required: true
Expand Down
1 change: 1 addition & 0 deletions lib/identity_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def self.store
config.add(:enable_rate_limiting, type: :boolean)
config.add(:enable_test_routes, type: :boolean)
config.add(:enable_usps_verification, type: :boolean)
config.add(:encrypted_document_storage_s3_bucket, type: :string)
config.add(:event_disavowal_expiration_hours, type: :integer)
config.add(:facial_match_general_availability_enabled, type: :boolean)
config.add(:feature_idv_force_gpo_verification_enabled, type: :boolean)
Expand Down
52 changes: 52 additions & 0 deletions spec/services/encrypted_doc_storage/doc_writer_spec.rb
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))
Comment on lines +23 to +24
Copy link
Copy Markdown
Contributor

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 dir argument and pass the path in here, the default value would be the Rails.root.join('tmp') like have it now

around do |ex|
Dir.mktmpdir('/locales') do |dir|
@tmpdir = dir
ex.run
end
end

Copy link
Copy Markdown
Contributor Author

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!)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that strongly!


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
30 changes: 30 additions & 0 deletions spec/services/encrypted_doc_storage/local_storage_spec.rb
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
31 changes: 31 additions & 0 deletions spec/services/encrypted_doc_storage/s3_storage_spec.rb
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