-
Notifications
You must be signed in to change notification settings - Fork 166
Create EncryptedRedisStructStorage #4217
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
Changes from all commits
24fbf44
2a8e33e
e25a09d
2d5917f
61bd95c
152dd1c
9531d72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,62 +1,10 @@ | ||
| class DocumentCaptureSessionResult | ||
| REDIS_KEY_PREFIX = 'dcs:result'.freeze | ||
| # frozen_string_literal: true | ||
|
|
||
| attr_reader :id, :success, :pii | ||
|
|
||
| alias success? success | ||
| alias pii_from_doc pii | ||
|
|
||
| class << self | ||
| def load(id) | ||
| ciphertext = REDIS_POOL.with { |client| client.read(key(id)) } | ||
| return nil if ciphertext.blank? | ||
| decrypt_and_deserialize(id, ciphertext) | ||
| end | ||
|
|
||
| def store(id:, success:, pii:) | ||
| result = new(id: id, success: success, pii: pii) | ||
| REDIS_POOL.with do |client| | ||
| client.write(key(id), result.serialize_and_encrypt, expires_in: 60) | ||
| end | ||
| end | ||
|
|
||
| def key(id) | ||
| [REDIS_KEY_PREFIX, id].join(':') | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def decrypt_and_deserialize(id, ciphertext) | ||
| deserialize( | ||
| id, | ||
| Encryption::Encryptors::SessionEncryptor.new.decrypt(ciphertext), | ||
| ) | ||
| end | ||
|
|
||
| def deserialize(id, json) | ||
| data = JSON.parse(json) | ||
| new( | ||
| id: id, | ||
| success: data['success'], | ||
| pii: data['pii'], | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| def initialize(id:, success:, pii:) | ||
| @id = id | ||
| @success = success | ||
| @pii = pii | ||
| DocumentCaptureSessionResult = Struct.new(:id, :success, :pii, keyword_init: true) do | ||
| def self.redis_key_prefix | ||
| 'dcs:result' | ||
| end | ||
|
|
||
| def serialize | ||
| { | ||
| success: success, | ||
| pii: pii, | ||
| }.to_json | ||
| end | ||
|
|
||
| def serialize_and_encrypt | ||
| Encryption::Encryptors::SessionEncryptor.new.encrypt(serialize) | ||
| end | ||
| alias_method :success?, :success | ||
| alias_method :pii_from_doc, :pii | ||
| end |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||||||
| # Use this class to store a plain Struct in redis. It will be stored | ||||||||||
| # encrypted and by default will expire, the struct must have a +redis_key_prefix+ | ||||||||||
| # class method | ||||||||||
| # | ||||||||||
| # @example | ||||||||||
| # MyStruct = Struct.new(:id, :a, :b) do | ||||||||||
| # def self.redis_key_prefix | ||||||||||
| # 'mystruct' | ||||||||||
| # end | ||||||||||
| # end | ||||||||||
| # | ||||||||||
| # struct = MyStruct.new('id123', 'a', 'b') | ||||||||||
| # | ||||||||||
| # EncryptedRedisStructStorage.store(struct) | ||||||||||
| # s = EncryptedRedisStructStorage.load('id123', type: MyStruct) | ||||||||||
| module EncryptedRedisStructStorage | ||||||||||
| module_function | ||||||||||
|
|
||||||||||
| def load(id, type:) | ||||||||||
| check_for_id_property!(type) | ||||||||||
|
|
||||||||||
| ciphertext = REDIS_POOL.with { |client| client.read(key(id, type: type)) } | ||||||||||
| return nil if ciphertext.blank? | ||||||||||
|
|
||||||||||
| json = Encryption::Encryptors::SessionEncryptor.new.decrypt(ciphertext) | ||||||||||
| data = JSON.parse(json) | ||||||||||
| type.new.tap do |struct| | ||||||||||
| struct.id = id | ||||||||||
| init_fields(struct: struct, data: data) | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def store(struct, expires_in: 60) | ||||||||||
| check_for_id_property!(struct.class) | ||||||||||
| check_for_empty_id!(struct.id) | ||||||||||
|
|
||||||||||
| payload = struct.as_json | ||||||||||
| payload.delete('id') | ||||||||||
|
|
||||||||||
| REDIS_POOL.with do |client| | ||||||||||
| client.write( | ||||||||||
| key(struct.id, type: struct.class), | ||||||||||
| Encryption::Encryptors::SessionEncryptor.new.encrypt(payload.to_json), | ||||||||||
| expires_in: expires_in, | ||||||||||
| ) | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def key(id, type:) | ||||||||||
| if type.respond_to?(:redis_key_prefix) | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to don't ask if the type respond to NoMethodError (undefined method `redis_key_prefix' for DocumentCaptureSessionResult:Class)Is not as nice as the descriptive raise on line 53, but close enough to see where it's failing. [1] Compared to user error. |
||||||||||
| [type.redis_key_prefix, id].join(':') | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method would create an array + string on every single call.
Suggested change
|
||||||||||
| else | ||||||||||
| raise "#{self} expected #{type.name} to have defined class method redis_key_prefix" | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| # Assigns member fields from a hash. That way, it doesn't matter | ||||||||||
| # if a Struct was created with keyword_init or not (and we can't currently | ||||||||||
| # reflect on that field) | ||||||||||
| # @param [Hash] data | ||||||||||
| def init_fields(struct:, data:) | ||||||||||
| data.each do |key, value| | ||||||||||
| struct[key] = value | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def check_for_id_property!(type) | ||||||||||
| return if type.members.include?(:id) | ||||||||||
| raise "#{self} expected #{type.name} to have an id property" | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def check_for_empty_id!(id) | ||||||||||
| raise ArgumentError, 'id cannot be empty' if id.blank? | ||||||||||
| end | ||||||||||
| end | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,59 +1,7 @@ | ||
| class ProofingDocumentCaptureSessionResult | ||
| REDIS_KEY_PREFIX = 'dcs-proofing:result'.freeze | ||
| # frozen_string_literal: true | ||
|
|
||
| attr_reader :id, :pii, :result | ||
|
|
||
| class << self | ||
| def load(id) | ||
| ciphertext = REDIS_POOL.with { |client| client.read(key(id)) } | ||
| return nil if ciphertext.blank? | ||
| decrypt_and_deserialize(id, ciphertext) | ||
| end | ||
|
|
||
| def store(id:, pii:, result:) | ||
| result = new(id: id, pii: pii, result: result) | ||
| REDIS_POOL.with do |client| | ||
| client.write(key(id), result.serialize_and_encrypt, expires_in: 60) | ||
| end | ||
| end | ||
|
|
||
| def key(id) | ||
| [REDIS_KEY_PREFIX, id].join(':') | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def decrypt_and_deserialize(id, ciphertext) | ||
| deserialize( | ||
| id, | ||
| Encryption::Encryptors::SessionEncryptor.new.decrypt(ciphertext), | ||
| ) | ||
| end | ||
|
|
||
| def deserialize(id, json) | ||
| data = JSON.parse(json) | ||
| new( | ||
| id: id, | ||
| pii: data['pii'], | ||
| result: data['result'], | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| def initialize(id:, pii:, result:) | ||
| @id = id | ||
| @pii = pii | ||
| @result = result | ||
| end | ||
|
|
||
| def serialize | ||
| { | ||
| pii: pii, | ||
| result: result, | ||
| }.to_json | ||
| end | ||
|
|
||
| def serialize_and_encrypt | ||
| Encryption::Encryptors::SessionEncryptor.new.encrypt(serialize) | ||
| ProofingDocumentCaptureSessionResult = Struct.new(:id, :pii, :result, keyword_init: true) do | ||
| def self.redis_key_prefix | ||
| 'dcs-proofing:result' | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.