Skip to content
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

Skip missing attributes when encrypting/decrypting event's payload #743

Merged
merged 1 commit into from
Jul 15, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ def encryption_metadata(data, schema)

def encrypt_data(data, meta)
meta.reduce(data) do |acc, (key, value)|
acc[key] = encrypt_attribute(acc, key, value)
acc[key] = encrypt_attribute(acc, key, value) if data.has_key?(key)
acc
end
end

def decrypt_data(data, meta)
meta.reduce(data) do |acc, (key, value)|
acc[key] = decrypt_attribute(data, key, value)
acc[key] = decrypt_attribute(data, key, value) if data.has_key?(key)
acc
end
end
Expand Down
27 changes: 27 additions & 0 deletions ruby_event_store/spec/mappers/transformation/encryption_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ def decrypt(r)
expect(event.metadata).to eq(metadata)
end

context "when encryptable event keys are missing" do
let(:sender) do
{
user_id: sender_id,
email: sender_email,
twitter: '@alice'
}
end

specify 'skip missing data keys' do
key_repository.create(sender_id)
key_repository.create(recipient_id)

event = decrypt(encrypt(ticket_transferred))

expect(event.event_id).to eq(event_id)

expect(event.data).to eq({
ticket_id: ticket_id,
sender: sender,
recipient: recipient
})

expect(event.metadata).to eq(metadata)
end
end

specify 'obfuscates data for missing keys on decryption' do
key_repository.create(sender_id)
key_repository.create(recipient_id)
Expand Down