Skip to content

Commit 4ca5d3f

Browse files
committed
Further shift validation to first method access
1 parent a862b29 commit 4ca5d3f

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

lib/kms_rails/active_record.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, co
1414
include InstanceMethods
1515

1616
real_field = "#{field}_enc"
17-
raise RuntimeError, "Field '#{field}' must not be a real column, '#{real_field}' is the real column" if self.column_names.include?(field.to_s)
18-
19-
enc = Core.new(key_id: key_id, msgpack: msgpack, context_key: context_key, context_value: context_value)
17+
enc = Core.new(key_id: key_id, msgpack: msgpack, context_key: context_key, context_value: context_value)
2018

2119
define_method "#{field}=" do |data|
20+
raise RuntimeError, "Field '#{field}' must not be a real column, '#{real_field}' is the real column" if self.class.column_names.include?(field.to_s)
2221
raise RuntimeError, "Field '#{real_field}' must exist to store encrypted data" unless self.class.column_names.include?(real_field)
2322

2423
if data.blank? # Just set to nil if nil
@@ -35,11 +34,13 @@ def kms_attr(field, key_id:, retain: false, msgpack: false, context_key: nil, co
3534
end
3635

3736
define_method "#{real_field}" do
37+
raise RuntimeError, "Field '#{field}' must not be a real column, '#{real_field}' is the real column" if self.class.column_names.include?(field.to_s)
3838
raise RuntimeError, "Field '#{real_field}' must exist to retrieve encrypted data" unless self.class.column_names.include?(real_field)
3939
Core.to64( get_hash(field) )
4040
end
4141

4242
define_method "#{field}" do
43+
raise RuntimeError, "Field '#{field}' must not be a real column, '#{real_field}' is the real column" if self.class.column_names.include?(field.to_s)
4344
raise RuntimeError, "Field '#{real_field}' must exist to retrieve decrypted data" unless self.class.column_names.include?(real_field)
4445

4546
hash = get_hash(field)

lib/kms_rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module KmsRails
2-
VERSION = "0.0.9"
2+
VERSION = "0.0.10"
33
end

spec/kms_rails/active_record_spec.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@
7373
subject { model.new }
7474

7575
it 'throws an exception on retrieve' do
76-
expect { subject.the_secret }.to raise_error(RuntimeError)
76+
expect { subject.the_secret }.to raise_error(RuntimeError, /must exist to retrieve decrypted data/)
7777
end
7878

7979
it 'throws an exception on set' do
80-
expect { subject.the_secret = 'foo' }.to raise_error(RuntimeError)
80+
expect { subject.the_secret = 'foo' }.to raise_error(RuntimeError, /must exist to store encrypted data/)
8181
end
8282

8383
it 'throws an exception on real retrieve' do
84-
expect { subject.the_secret_enc }.to raise_error(RuntimeError)
84+
expect { subject.the_secret_enc }.to raise_error(RuntimeError, /must exist to retrieve encrypted data/)
8585
end
8686
end
8787

@@ -95,10 +95,23 @@
9595
end
9696

9797
let (:model) { RealFieldModel }
98-
subject { model.kms_attr :the_secret, key_id: 'a' }
9998

100-
it 'throws an exception' do
101-
expect { subject }.to raise_error(RuntimeError)
99+
before do
100+
model.kms_attr :the_secret, key_id: 'a'
101+
end
102+
103+
subject { model.new }
104+
105+
it 'throws an exception on retrieve' do
106+
expect { subject.the_secret }.to raise_error(RuntimeError, /must not be a real column/)
107+
end
108+
109+
it 'throws an exception on set' do
110+
expect { subject.the_secret = 'foo' }.to raise_error(RuntimeError, /must not be a real column/)
111+
end
112+
113+
it 'throws an exception on real retrieve' do
114+
expect { subject.the_secret_enc }.to raise_error(RuntimeError, /must not be a real column/)
102115
end
103116
end
104117
end

0 commit comments

Comments
 (0)