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
2 changes: 1 addition & 1 deletion app/jobs/address_proofing_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def perform(user_id:, issuer:, result_id:, encrypted_arguments:, trace_id:)
raise_stale_job! if stale_job?(enqueued_at)

decrypted_args = JSON.parse(
Encryption::Encryptors::SessionEncryptor.new.decrypt(encrypted_arguments),
Encryption::Encryptors::BackgroundProofingArgEncryptor.new.decrypt(encrypted_arguments),
symbolize_names: true,
)

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/document_proofing_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def perform(
user = dcs.user

decrypted_args = JSON.parse(
Encryption::Encryptors::SessionEncryptor.new.decrypt(encrypted_arguments),
Encryption::Encryptors::BackgroundProofingArgEncryptor.new.decrypt(encrypted_arguments),
symbolize_names: true,
)
document_args = decrypted_args[:document_arguments]
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/resolution_proofing_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def perform(result_id:, encrypted_arguments:, trace_id:, should_proof_state_id:,
raise_stale_job! if stale_job?(enqueued_at)

decrypted_args = JSON.parse(
Encryption::Encryptors::SessionEncryptor.new.decrypt(encrypted_arguments),
Encryption::Encryptors::BackgroundProofingArgEncryptor.new.decrypt(encrypted_arguments),
symbolize_names: true,
)

Expand Down
5 changes: 1 addition & 4 deletions app/models/gpo_confirmation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ def entry
private

def encryptor
# This currently uses the SessionEncryptor, which is meant to be used to
# encrypt the session. When this code is changed to integrate a new mail
# vendor we should create a purpose built encryptor for that vendor
Encryption::Encryptors::SessionEncryptor.new
Encryption::Encryptors::BackgroundProofingArgEncryptor.new
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.

maybe add a 3rd encryptor here? since this is for proofing but isn't backgrounded yet

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 thought about it and didn't feel great about the copy/paste part here. Especially since this does run in a background job when the GPO upload occurs. Maybe a subclass or something like that could work?

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.

oh right this is in a daily background job ... nvm let's leave it

end
end
4 changes: 2 additions & 2 deletions app/services/encrypted_redis_struct_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def load(id, type:)
ciphertext = REDIS_POOL.with { |client| client.get(key(id, type: type)) }
return nil if ciphertext.blank?

json = Encryption::Encryptors::SessionEncryptor.new.decrypt(ciphertext)
json = Encryption::Encryptors::BackgroundProofingArgEncryptor.new.decrypt(ciphertext)
data = JSON.parse(json, symbolize_names: true)
type.new.tap do |struct|
struct.id = id
Expand Down Expand Up @@ -52,7 +52,7 @@ def store(struct, expires_in: 60)
payload.transform_values!(&utf_8_encode_strs)

struct_key = key(struct.id, type: struct.class)
ciphertext = Encryption::Encryptors::SessionEncryptor.new.encrypt(payload.to_json)
ciphertext = Encryption::Encryptors::BackgroundProofingArgEncryptor.new.encrypt(payload.to_json)

REDIS_POOL.with do |client|
client.setex(struct_key, expires_in, ciphertext)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Encryption
module Encryptors
class BackgroundProofingArgEncryptor
include Encodable
include ::NewRelic::Agent::MethodTracer

def encrypt(plaintext)
aes_ciphertext = AesEncryptor.new.encrypt(plaintext, aes_encryption_key)
kms_ciphertext = KmsClient.new.encrypt(aes_ciphertext, 'context' => 'session-encryption')
encode(kms_ciphertext)
end

def decrypt(ciphertext)
aes_ciphertext = KmsClient.new.decrypt(
decode(ciphertext), 'context' => 'session-encryption'
)
aes_encryptor.decrypt(aes_ciphertext, aes_encryption_key)
end

private

def aes_encryptor
AesEncryptor.new
end

def aes_encryption_key
IdentityConfig.store.session_encryption_key[0...32]
end

add_method_tracer :encrypt, "Custom/#{name}/encrypt"
add_method_tracer :decrypt, "Custom/#{name}/decrypt"
end
end
end
6 changes: 3 additions & 3 deletions app/services/idv/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def proof_resolution(
)
document_capture_session.create_proofing_session

encrypted_arguments = Encryption::Encryptors::SessionEncryptor.new.encrypt(
encrypted_arguments = Encryption::Encryptors::BackgroundProofingArgEncryptor.new.encrypt(
{ applicant_pii: @applicant }.to_json,
)

Expand All @@ -30,7 +30,7 @@ def proof_resolution(

def proof_address(document_capture_session, user_id:, issuer:, trace_id:)
document_capture_session.create_proofing_session
encrypted_arguments = Encryption::Encryptors::SessionEncryptor.new.encrypt(
encrypted_arguments = Encryption::Encryptors::BackgroundProofingArgEncryptor.new.encrypt(
{ applicant_pii: @applicant }.to_json,
)

Expand All @@ -57,7 +57,7 @@ def proof_document(
analytics_data:,
flow_path: 'standard'
)
encrypted_arguments = Encryption::Encryptors::SessionEncryptor.new.encrypt(
encrypted_arguments = Encryption::Encryptors::BackgroundProofingArgEncryptor.new.encrypt(
@applicant.to_json,
)

Expand Down
2 changes: 1 addition & 1 deletion spec/features/idv/doc_auth/document_capture_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@
expect(page).to have_current_path(next_step, wait: 20)
expect(DocumentProofingJob).to have_received(:perform_later) do |encrypted_arguments:, **|
args = JSON.parse(
Encryption::Encryptors::SessionEncryptor.new.decrypt(encrypted_arguments),
Encryption::Encryptors::BackgroundProofingArgEncryptor.new.decrypt(encrypted_arguments),
symbolize_names: true,
)[:document_arguments]

Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/address_proofing_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe AddressProofingJob, type: :job do
let(:document_capture_session) { DocumentCaptureSession.new(result_id: SecureRandom.hex) }
let(:encrypted_arguments) do
Encryption::Encryptors::SessionEncryptor.new.encrypt(
Encryption::Encryptors::BackgroundProofingArgEncryptor.new.encrypt(
{ applicant_pii: applicant_pii }.to_json,
)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/document_proofing_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
end

let(:encrypted_arguments) do
Encryption::Encryptors::SessionEncryptor.new.encrypt(
Encryption::Encryptors::BackgroundProofingArgEncryptor.new.encrypt(
{
document_arguments: {
encryption_key: Base64.encode64(encryption_key),
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/resolution_proofing_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
end
let(:encrypted_arguments) do
Encryption::Encryptors::SessionEncryptor.new.encrypt(
Encryption::Encryptors::BackgroundProofingArgEncryptor.new.encrypt(
{ applicant_pii: pii }.to_json,
)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/identity_job_log_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
end

document_capture_session = DocumentCaptureSession.new(result_id: SecureRandom.hex)
encrypted_arguments = Encryption::Encryptors::SessionEncryptor.new.encrypt(
encrypted_arguments = Encryption::Encryptors::BackgroundProofingArgEncryptor.new.encrypt(
{ applicant_pii: { phone: Faker::PhoneNumber.cell_phone } }.to_json,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rails_helper'

describe Encryption::Encryptors::BackgroundProofingArgEncryptor do
let(:plaintext) { '{ "foo": "bar" }' }

describe '#encrypt' do
it 'returns a KMS wrapped AES encrypted ciphertext' do
aes_encryptor = instance_double(Encryption::Encryptors::AesEncryptor)
kms_client = instance_double(Encryption::KmsClient)
allow(aes_encryptor).to receive(:encrypt).
with(plaintext, IdentityConfig.store.session_encryption_key[0...32]).
and_return('aes output')
allow(kms_client).to receive(:encrypt).
with('aes output', 'context' => 'session-encryption').
and_return('kms output')
allow(Encryption::Encryptors::AesEncryptor).to receive(:new).and_return(aes_encryptor)
allow(Encryption::KmsClient).to receive(:new).and_return(kms_client)

expected_ciphertext = Base64.strict_encode64('kms output')

ciphertext = subject.encrypt(plaintext)

expect(ciphertext).to eq(expected_ciphertext)
end

it 'sets an encryption context' do
client = instance_double(Encryption::KmsClient)
expect(client).to receive(:encrypt).with(
instance_of(String), 'context' => 'session-encryption'
).and_return('kms_ciphertext')
allow(Encryption::KmsClient).to receive(:new).and_return(client)

subject.encrypt(plaintext)
end
end

describe '#decrypt' do
let(:ciphertext) do
Encryption::Encryptors::BackgroundProofingArgEncryptor.new.encrypt(plaintext)
end

it 'decrypts the ciphertext' do
expect(subject.decrypt(ciphertext)).to eq(plaintext)
end
end
end
2 changes: 1 addition & 1 deletion spec/services/gpo_confirmation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
ssn: '123-456-7890',
}
end
let(:encryptor) { Encryption::Encryptors::SessionEncryptor.new }
let(:encryptor) { Encryption::Encryptors::BackgroundProofingArgEncryptor.new }

subject { GpoConfirmation.create!(entry: attributes) }

Expand Down