vault-rails 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -69,6 +69,16 @@ describe Vault::Rails do
69
69
  person.reload
70
70
 
71
71
  expect(person.ssn).to eq("")
72
+ expect(person.ssn_encrypted).to eq("")
73
+ end
74
+
75
+ it "allows attributes to be null" do
76
+ person = Person.create!(ssn: "123-45-6789")
77
+ person.update_attributes!(ssn: nil)
78
+ person.reload
79
+
80
+ expect(person.ssn).to eq(nil)
81
+ expect(person.ssn_encrypted).to eq(nil)
72
82
  end
73
83
 
74
84
  it "reloads instance variables on reload" do
@@ -284,19 +294,73 @@ describe Vault::Rails do
284
294
  end
285
295
  end
286
296
 
297
+ context "with a default" do
298
+ %i[new create].each do |creation_method|
299
+ context "on #{creation_method}" do
300
+ context "without an initial attribute" do
301
+ it "sets the default" do
302
+ person = Person.public_send(creation_method)
303
+ expect(person.default).to eq("abc123")
304
+ person.save!
305
+ person.reload
306
+ expect(person.default).to eq("abc123")
307
+ end
308
+ end
309
+
310
+ context "with an initial attribute" do
311
+ it "does not set the default" do
312
+ person = Person.public_send(creation_method, default: "another")
313
+ expect(person.default).to eq("another")
314
+ person.save!
315
+ person.reload
316
+ expect(person.default).to eq("another")
317
+ end
318
+ end
319
+ end
320
+ end
321
+ end
322
+
323
+ context "with a default and serializer" do
324
+ %i[new create].each do |creation_method|
325
+ context "on #{creation_method}" do
326
+ context "without an initial attribute" do
327
+ it "sets the default" do
328
+ person = Person.public_send(creation_method)
329
+ expect(person.default_with_serializer).to eq({})
330
+ person.save!
331
+ person.reload
332
+ expect(person.default_with_serializer).to eq({})
333
+ end
334
+ end
335
+
336
+ context "with an initial attribute" do
337
+ it "does not set the default" do
338
+ person = Person.public_send(
339
+ creation_method,
340
+ default_with_serializer: { "foo" => "bar" }
341
+ )
342
+
343
+ expect(person.default_with_serializer).to eq({ "foo" => "bar" })
344
+ person.save!
345
+ person.reload
346
+ expect(person.default_with_serializer).to eq({ "foo" => "bar" })
347
+ end
348
+ end
349
+ end
350
+ end
351
+ end
352
+
287
353
  context "with the :json serializer" do
288
354
  before(:all) do
289
355
  Vault::Rails.logical.write("transit/keys/dummy_people_details")
290
356
  end
291
357
 
292
- it "has a default value for unpersisted records" do
358
+ it "does not default to a hash" do
293
359
  person = Person.new
294
- expect(person.details).to eq({})
295
- end
296
-
297
- it "has a default value for persisted records" do
298
- person = Person.create!
299
- expect(person.details).to eq({})
360
+ expect(person.details).to eq(nil)
361
+ person.save!
362
+ person.reload
363
+ expect(person.details).to eq(nil)
300
364
  end
301
365
 
302
366
  it "tracks dirty attributes" do
@@ -356,6 +420,89 @@ describe Vault::Rails do
356
420
  end
357
421
  end
358
422
 
423
+ context "with context" do
424
+ it "encodes and decodes with a string context" do
425
+ person = Person.create!(context_string: "foobar")
426
+ person.reload
427
+
428
+ raw = Vault::Rails.decrypt(
429
+ "transit", "dummy_people_context_string",
430
+ person.context_string_encrypted, context: "production")
431
+
432
+ expect(raw).to eq("foobar")
433
+
434
+ expect(person.context_string).to eq("foobar")
435
+
436
+ # Decrypting without the correct context fails
437
+ expect {
438
+ Vault::Rails.decrypt(
439
+ "transit", "dummy_people_context_string",
440
+ person.context_string_encrypted, context: "wrongcontext")
441
+ }.to raise_error(Vault::HTTPClientError, /invalid ciphertext/)
442
+
443
+ # Decrypting without a context fails
444
+ expect {
445
+ Vault::Rails.decrypt(
446
+ "transit", "dummy_people_context_string",
447
+ person.context_string_encrypted)
448
+ }.to raise_error(Vault::HTTPClientError, /context/)
449
+ end
450
+
451
+ it "encodes and decodes with a symbol context" do
452
+ person = Person.create!(context_symbol: "foobar")
453
+ person.reload
454
+
455
+ raw = Vault::Rails.decrypt(
456
+ "transit", "dummy_people_context_symbol",
457
+ person.context_symbol_encrypted, context: person.encryption_context)
458
+
459
+ expect(raw).to eq("foobar")
460
+
461
+ expect(person.context_symbol).to eq("foobar")
462
+
463
+ # Decrypting without the correct context fails
464
+ expect {
465
+ Vault::Rails.decrypt(
466
+ "transit", "dummy_people_context_symbol",
467
+ person.context_symbol_encrypted, context: "wrongcontext")
468
+ }.to raise_error(Vault::HTTPClientError, /invalid ciphertext/)
469
+
470
+ # Decrypting without a context fails
471
+ expect {
472
+ Vault::Rails.decrypt(
473
+ "transit", "dummy_people_context_symbol",
474
+ person.context_symbol_encrypted)
475
+ }.to raise_error(Vault::HTTPClientError, /context/)
476
+ end
477
+
478
+ it "encodes and decodes with a proc context" do
479
+ person = Person.create!(context_proc: "foobar")
480
+ person.reload
481
+
482
+ raw = Vault::Rails.decrypt(
483
+ "transit", "dummy_people_context_proc",
484
+ person.context_proc_encrypted, context: person.encryption_context)
485
+
486
+ expect(raw).to eq("foobar")
487
+
488
+ expect(person.context_proc).to eq("foobar")
489
+
490
+ # Decrypting without the correct context fails
491
+ expect {
492
+ Vault::Rails.decrypt(
493
+ "transit", "dummy_people_context_proc",
494
+ person.context_proc_encrypted, context: "wrongcontext")
495
+ }.to raise_error(Vault::HTTPClientError, /invalid ciphertext/)
496
+
497
+ # Decrypting without a context fails
498
+ expect {
499
+ Vault::Rails.decrypt(
500
+ "transit", "dummy_people_context_proc",
501
+ person.context_proc_encrypted)
502
+ }.to raise_error(Vault::HTTPClientError, /context/)
503
+ end
504
+ end
505
+
359
506
  context 'with errors' do
360
507
  it 'raises the appropriate exception' do
361
508
  expect {
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Vault::Rails::JSONSerializer do
4
+ [
5
+ nil,
6
+ false,
7
+ true,
8
+ "",
9
+ "foo",
10
+ {},
11
+ { "foo" => "bar" },
12
+ [],
13
+ ["foo", "bar"],
14
+ 0,
15
+ 123,
16
+ 0.0,
17
+ 0.123,
18
+ 0xff,
19
+ 123e123
20
+ ].each do |object|
21
+ it "encodes and decodes #{object.inspect}" do
22
+ encoded = described_class.encode(object)
23
+ expect(encoded).to be_a(String)
24
+ decoded = described_class.decode(encoded)
25
+ expect(decoded).to eq(object)
26
+ end
27
+ end
28
+
29
+ describe ".decode" do
30
+ subject(:decoded) { described_class.decode(raw) }
31
+
32
+ context "with nil" do
33
+ let(:raw) { nil }
34
+ it { is_expected.to eq(nil) }
35
+ end
36
+
37
+ context "with an empty string (only possible if column has a default)" do
38
+ let(:raw) { "" }
39
+ it { is_expected.to eq(nil) }
40
+ end
41
+ end
42
+ end
@@ -20,6 +20,12 @@ describe Vault::EncryptedModel do
20
20
  }.to raise_error(Vault::Rails::ValidationFailedError)
21
21
  end
22
22
 
23
+ it "raises an exception if a proc is passed to :context without an arity of 1" do
24
+ expect {
25
+ klass.vault_attribute(:foo, context: ->() { })
26
+ }.to raise_error(Vault::Rails::ValidationFailedError, /1 argument/i)
27
+ end
28
+
23
29
  it "defines a getter" do
24
30
  klass.vault_attribute(:foo)
25
31
  expect(klass.instance_methods).to include(:foo)
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Vault::Rails do
4
+ describe "#memory_key_for" do
5
+ input_examples = [
6
+ ["path", "key"],
7
+ ["path", "key", "context"],
8
+ ["a_really_long_path", "a_really_long_key"],
9
+ ["a_really_long_path", "a_really_long_key", "a_really_long_context"],
10
+ ]
11
+
12
+ input_examples.each do |path, key, encryption_context|
13
+ context "with path=#{path}, key=#{key}, context=#{encryption_context}" do
14
+ it "returns exactly 16 bytes as required by OpenSSL AES 128" do
15
+ memory_key = Vault::Rails.send(
16
+ :memory_key_for, path, key, context: encryption_context
17
+ )
18
+ expect(memory_key.bytesize).to eq(16)
19
+ end
20
+ end
21
+ end
22
+
23
+ it "returns unique keys for different paths, keys, and contexts" do
24
+ memory_keys = input_examples.map { |path, key, encryption_context|
25
+ Vault::Rails.send(
26
+ :memory_key_for, path, key, context: encryption_context
27
+ )
28
+ }
29
+
30
+ expect(memory_keys).to match_array(memory_keys.uniq)
31
+ end
32
+ end
33
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vault-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seth Vargo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-09 00:00:00.000000000 Z
11
+ date: 2019-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -17,9 +17,6 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.1'
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '5.1'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,9 +24,6 @@ dependencies:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '4.1'
30
- - - "<"
31
- - !ruby/object:Gem::Version
32
- version: '5.1'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: vault
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -118,16 +112,16 @@ dependencies:
118
112
  name: sqlite3
119
113
  requirement: !ruby/object:Gem::Requirement
120
114
  requirements:
121
- - - ">="
115
+ - - "~>"
122
116
  - !ruby/object:Gem::Version
123
- version: '0'
117
+ version: 1.3.0
124
118
  type: :development
125
119
  prerelease: false
126
120
  version_requirements: !ruby/object:Gem::Requirement
127
121
  requirements:
128
- - - ">="
122
+ - - "~>"
129
123
  - !ruby/object:Gem::Version
130
- version: '0'
124
+ version: 1.3.0
131
125
  description: Official Vault plugin for Rails
132
126
  email:
133
127
  - sethvargo@gmail.com
@@ -142,7 +136,7 @@ files:
142
136
  - lib/vault/rails.rb
143
137
  - lib/vault/rails/configurable.rb
144
138
  - lib/vault/rails/errors.rb
145
- - lib/vault/rails/serializer.rb
139
+ - lib/vault/rails/json_serializer.rb
146
140
  - lib/vault/rails/version.rb
147
141
  - spec/dummy/Rakefile
148
142
  - spec/dummy/app/models/lazy_person.rb
@@ -169,19 +163,25 @@ files:
169
163
  - spec/dummy/config/locales/en.yml
170
164
  - spec/dummy/config/routes.rb
171
165
  - spec/dummy/config/secrets.yml
166
+ - spec/dummy/db/development.sqlite3
172
167
  - spec/dummy/db/migrate/20150428220101_create_people.rb
173
168
  - spec/dummy/db/schema.rb
169
+ - spec/dummy/db/test.sqlite3
174
170
  - spec/dummy/lib/binary_serializer.rb
171
+ - spec/dummy/log/development.log
172
+ - spec/dummy/log/test.log
175
173
  - spec/dummy/public/404.html
176
174
  - spec/dummy/public/422.html
177
175
  - spec/dummy/public/500.html
178
176
  - spec/dummy/public/favicon.ico
179
177
  - spec/integration/rails_spec.rb
178
+ - spec/lib/vault/rails/json_serializer_spec.rb
180
179
  - spec/spec_helper.rb
181
180
  - spec/support/vault_server.rb
182
181
  - spec/unit/encrypted_model_spec.rb
183
182
  - spec/unit/rails/configurable_spec.rb
184
183
  - spec/unit/rails_spec.rb
184
+ - spec/unit/vault/rails_spec.rb
185
185
  homepage: https://github.com/hashicorp/vault-rails
186
186
  licenses:
187
187
  - MPL-2.0
@@ -202,46 +202,52 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
202
  version: '0'
203
203
  requirements: []
204
204
  rubyforge_project:
205
- rubygems_version: 2.6.10
205
+ rubygems_version: 2.6.14.3
206
206
  signing_key:
207
207
  specification_version: 4
208
208
  summary: Official Vault plugin for Rails
209
209
  test_files:
210
+ - spec/spec_helper.rb
211
+ - spec/unit/encrypted_model_spec.rb
212
+ - spec/unit/rails_spec.rb
213
+ - spec/unit/vault/rails_spec.rb
214
+ - spec/unit/rails/configurable_spec.rb
210
215
  - spec/dummy/app/models/lazy_person.rb
211
216
  - spec/dummy/app/models/person.rb
217
+ - spec/dummy/bin/rake
212
218
  - spec/dummy/bin/bundle
213
219
  - spec/dummy/bin/rails
214
- - spec/dummy/bin/rake
215
- - spec/dummy/config/application.rb
216
- - spec/dummy/config/boot.rb
217
- - spec/dummy/config/database.yml
218
- - spec/dummy/config/environment.rb
220
+ - spec/dummy/config/secrets.yml
221
+ - spec/dummy/config/routes.rb
222
+ - spec/dummy/config/locales/en.yml
219
223
  - spec/dummy/config/environments/development.rb
220
224
  - spec/dummy/config/environments/test.rb
221
- - spec/dummy/config/initializers/assets.rb
225
+ - spec/dummy/config/environment.rb
226
+ - spec/dummy/config/application.rb
227
+ - spec/dummy/config/database.yml
228
+ - spec/dummy/config/boot.rb
222
229
  - spec/dummy/config/initializers/backtrace_silencers.rb
223
- - spec/dummy/config/initializers/cookies_serializer.rb
224
- - spec/dummy/config/initializers/filter_parameter_logging.rb
225
- - spec/dummy/config/initializers/inflections.rb
226
230
  - spec/dummy/config/initializers/mime_types.rb
231
+ - spec/dummy/config/initializers/filter_parameter_logging.rb
227
232
  - spec/dummy/config/initializers/session_store.rb
228
- - spec/dummy/config/initializers/vault.rb
229
233
  - spec/dummy/config/initializers/wrap_parameters.rb
230
- - spec/dummy/config/locales/en.yml
231
- - spec/dummy/config/routes.rb
232
- - spec/dummy/config/secrets.yml
234
+ - spec/dummy/config/initializers/assets.rb
235
+ - spec/dummy/config/initializers/cookies_serializer.rb
236
+ - spec/dummy/config/initializers/vault.rb
237
+ - spec/dummy/config/initializers/inflections.rb
233
238
  - spec/dummy/config.ru
234
- - spec/dummy/db/migrate/20150428220101_create_people.rb
235
- - spec/dummy/db/schema.rb
236
- - spec/dummy/lib/binary_serializer.rb
237
- - spec/dummy/public/404.html
239
+ - spec/dummy/Rakefile
240
+ - spec/dummy/public/favicon.ico
238
241
  - spec/dummy/public/422.html
239
242
  - spec/dummy/public/500.html
240
- - spec/dummy/public/favicon.ico
241
- - spec/dummy/Rakefile
243
+ - spec/dummy/public/404.html
244
+ - spec/dummy/lib/binary_serializer.rb
245
+ - spec/dummy/db/schema.rb
246
+ - spec/dummy/db/test.sqlite3
247
+ - spec/dummy/db/migrate/20150428220101_create_people.rb
248
+ - spec/dummy/db/development.sqlite3
249
+ - spec/dummy/log/test.log
250
+ - spec/dummy/log/development.log
242
251
  - spec/integration/rails_spec.rb
243
- - spec/spec_helper.rb
244
252
  - spec/support/vault_server.rb
245
- - spec/unit/encrypted_model_spec.rb
246
- - spec/unit/rails/configurable_spec.rb
247
- - spec/unit/rails_spec.rb
253
+ - spec/lib/vault/rails/json_serializer_spec.rb