-
Notifications
You must be signed in to change notification settings - Fork 166
Add a background job encryptor #6211
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
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
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
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
34 changes: 34 additions & 0 deletions
34
app/services/encryption/encryptors/background_proofing_arg_encryptor.rb
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,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 |
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
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
46 changes: 46 additions & 0 deletions
46
spec/services/encryption/encryptors/background_proofing_arg_encryptor_spec.rb
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,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 |
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
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.
maybe add a 3rd encryptor here? since this is for proofing but isn't backgrounded yet
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 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?
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.
oh right this is in a daily background job ... nvm let's leave it