Skip to content
Closed
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
13 changes: 10 additions & 3 deletions lib/kms_rails/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, co
include InstanceMethods

real_field = "#{field}_enc"
raise RuntimeError, "Field '#{real_field}' must exist to store encrypted data" unless self.column_names.include?(real_field)
# raise RuntimeError, "Field '#{real_field}' must exist to store encrypted data" unless self.column_names.include?(real_field)
raise RuntimeError, "Field '#{field}' must not be a real column, '#{real_field}' is the real column" if self.column_names.include?(field)

enc = Core.new(key_id: key_id, msgpack: msgpack, context_key: context_key, context_value: context_value)

define_method "#{field}=" do |data|
if data.nil? # Just set to nil if nil
if data.blank? # Just set to nil if nil
clear_retained(field)
self[real_field] = nil
return
end

if data.class == Hash
data = data.to_json
end
set_retained(field, data) if retain
encrypted_data = enc.encrypt(data)
data = nil
Expand All @@ -48,6 +50,11 @@ def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, co
set_retained(field, plaintext) if retain
plaintext
end
begin
plaintext = JSON.parse(plaintext)
rescue JSON::ParserError => e
end
return plaintext
end

define_method "#{field}_clear" do
Expand Down
13 changes: 8 additions & 5 deletions lib/kms_rails/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
module KmsRails
class << self
attr_accessor :configuration
attr_writer :configuration
end

def self.configuration
@configuration ||= Configuration.new
end

def self.configure
self.configuration ||= Configuration.new
yield(configuration)
yield(self.configuration)
end

def self.reset_config
self.configuration = Configuration.new
@configuration = Configuration.new
end

class Configuration
Expand All @@ -20,4 +23,4 @@ def initialize
@alias_prefix = ''
end
end
end
end
2 changes: 1 addition & 1 deletion lib/kms_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module KmsRails
VERSION = "0.0.8"
VERSION = "0.0.9"
end