-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read 2L-KMS encrypted sessions (#2367)
**Why**: To start the migration to 2L-KMS. This commit wraps an AES encrypted ciphertext with KMS. This commit allows new and old instances to exist in parallel when we start writing 2L-KMS encrypted cihpertexts to the session.
- Loading branch information
Showing
4 changed files
with
156 additions
and
77 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/services/encryption/encryptors/deprecated_session_encryptor.rb
This file contains 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,28 @@ | ||
module Encryption | ||
module Encryptors | ||
class DeprecatedSessionEncryptor | ||
def encrypt(plaintext) | ||
user_access_key = self.class.load_or_init_user_access_key | ||
UserAccessKeyEncryptor.new(user_access_key).encrypt(plaintext) | ||
end | ||
|
||
def decrypt(ciphertext) | ||
user_access_key = self.class.load_or_init_user_access_key | ||
UserAccessKeyEncryptor.new(user_access_key).decrypt(ciphertext) | ||
end | ||
|
||
def self.load_or_init_user_access_key | ||
if @user_access_key_scrypt_hash.present? | ||
return UserAccessKey.new(scrypt_hash: @user_access_key_scrypt_hash) | ||
end | ||
|
||
key = Figaro.env.session_encryption_key | ||
user_access_key = UserAccessKey.new( | ||
password: key, salt: OpenSSL::Digest::SHA256.hexdigest(key) | ||
) | ||
@user_access_key_scrypt_hash = user_access_key.as_scrypt_hash | ||
user_access_key | ||
end | ||
end | ||
end | ||
end |
This file contains 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
79 changes: 79 additions & 0 deletions
79
spec/services/encryption/encryptors/deprecated_session_encryptor.rb
This file contains 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,79 @@ | ||
require 'rails_helper' | ||
|
||
describe Encryption::Encryptors::DeprecatedSessionEncryptor do | ||
let(:plaintext) { '{ "foo": "bar" }' } | ||
|
||
before do | ||
described_class.instance_variable_set(:@user_access_key_scrypt_hash, nil) | ||
end | ||
|
||
describe '#encrypt' do | ||
it 'returns encrypted text' do | ||
ciphertext = subject.encrypt(plaintext) | ||
|
||
expect(ciphertext).to_not eq(plaintext) | ||
end | ||
|
||
it 'only computes an scrypt hash on the first encryption' do | ||
expect(SCrypt::Engine).to receive(:hash_secret).once.and_call_original | ||
|
||
subject.encrypt(plaintext) | ||
|
||
expect(SCrypt::Engine).to_not receive(:hash_secret) | ||
|
||
subject.encrypt(plaintext) | ||
end | ||
end | ||
|
||
describe '#decrypt' do | ||
let(:ciphertext) do | ||
result = subject.encrypt(plaintext) | ||
described_class.instance_variable_set(:@user_access_key_scrypt_hash, nil) | ||
result | ||
end | ||
|
||
before do | ||
# Memoize the ciphertext and purge memoized key so that encryption does not | ||
# affect expected call counts | ||
ciphertext | ||
end | ||
|
||
it 'returns a decrypted ciphertext' do | ||
expect(subject.decrypt(ciphertext)).to eq(plaintext) | ||
end | ||
|
||
it 'only computes and scrypt hash on the first decryption' do | ||
expect(SCrypt::Engine).to receive(:hash_secret).once.and_call_original | ||
|
||
subject.decrypt(ciphertext) | ||
|
||
expect(SCrypt::Engine).to_not receive(:hash_secret) | ||
|
||
subject.decrypt(ciphertext) | ||
end | ||
end | ||
|
||
describe '.load_or_init_user_access_key' do | ||
it 'does not return the same key object for the same salt and cost' do | ||
expect(SCrypt::Engine).to receive(:hash_secret).once.and_call_original | ||
|
||
key1 = described_class.load_or_init_user_access_key | ||
key2 = described_class.load_or_init_user_access_key | ||
|
||
expect(key1.as_scrypt_hash).to eq(key2.as_scrypt_hash) | ||
expect(key1).to_not eq(key2) | ||
end | ||
end | ||
|
||
it 'makes a roundtrip across multiple encryptors' do | ||
encryptor1 = described_class.new | ||
encryptor2 = described_class.new | ||
|
||
# Memoize user access key scrypt hash | ||
encryptor1.decrypt(encryptor1.encrypt('asdf')) | ||
encryptor2.decrypt(encryptor2.encrypt('1234')) | ||
|
||
encrypted_text = encryptor1.encrypt(plaintext) | ||
expect(encryptor2.decrypt(encrypted_text)).to eq(plaintext) | ||
end | ||
end |
This file contains 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