encryptable_attributes 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5daff2b8fcca8a9a4e4b924fdfcdcc98489c6aac5babfb131fcc0974972824c2
4
- data.tar.gz: 9dc2f517d69e1444c433cd067a3e023250fe245c6f74bbfca83817a6f19dc6b5
3
+ metadata.gz: 97e30bc4d67879654d302bcfa054d8ddcedb39ec9c6e97d89c97a7cad65f1605
4
+ data.tar.gz: e8e1ee98d330083714721b1324dc72d7eedd0cd3f7d526f3cd4a2b39c6f18fea
5
5
  SHA512:
6
- metadata.gz: 509d8ed356ac43e3c181a44ce6e8e01421dc2d16945621b124eb5210ab28044c17e880d5b57f59b23528873148adf81e6c91694526db8ba56358f1b95373faf4
7
- data.tar.gz: 2b43868c8aa86cc44ae78c07c2a4e224232626a367db16ac1fa4831dec83af550bcefc1fcc4e051a835d37a6f55a414cc8222ec8bbdd5cee4b7033ec8c25d396
6
+ metadata.gz: bc858805e483b1018f54024032a31153aa2767061d3837b0647edb1ac47df61395531c40ea28dd482d80626a3d9c4aacc187ddd1e4fb570f156b7cb816ce79af
7
+ data.tar.gz: 986d1b553aeeb75b8e922dbbfeeb79762f4c1ec9545396bf407028e1644a5d3d1da4a609271e82177c68abc03de9eedb08381568b4fc7e61de907f1416625ce7
data/.gitignore CHANGED
@@ -6,4 +6,5 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
- /db/*.sqlite3
9
+ /db/*.sqlite3
10
+ Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ * Added `CHANGELOG.md`
2
+ * Speficy dependency versions
3
+ * Reduce loaded dependencies
4
+ * Support reading the encryption key at runtime via method call
5
+
6
+ ## EncryptableAttributes 0.1.0 (May 12, 2018)
7
+
8
+ * Initial release
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # EncryptableAttributes [![Build Status](https://travis-ci.org/nsommer/encryptable_attributes.svg?branch=master)](https://travis-ci.org/nsommer/encryptable_attributes)
1
+ # EncryptableAttributes [![Build Status](https://travis-ci.org/nsommer/encryptable_attributes.svg?branch=master)](https://travis-ci.org/nsommer/encryptable_attributes) [![Depfu](https://badges.depfu.com/badges/0aa2b808a183e56fa830b90ba072d137/overview.svg)](https://depfu.com/github/nsommer/encryptable_attributes)
2
2
 
3
- With the `encryptable_attributes` gem, you transparently encrypt and decrypt attributes of an ActiveRecord model. It uses `ActiveSupport::MessageEncryptor` to encrypt and decrypt values and provides a simple class-level DSL for configuration.
3
+ With the `encryptable_attributes` gem, you transparently encrypt and decrypt attributes of an ActiveRecord model. It uses [`ActiveSupport::MessageEncryptor`](http://api.rubyonrails.org/classes/ActiveSupport/MessageEncryptor.html) to encrypt and decrypt values and provides a simple class-level DSL for configuration.
4
4
 
5
5
  ## Installation
6
6
 
@@ -33,6 +33,25 @@ end
33
33
 
34
34
  ActiveRecord models use an `attributes` hash internally to keep attributes. EncryptablesAttributes overrides the accessor methods for the corresponding attributes and encrypts given values before storing them in the `attributes` hash and decrypts them when reading them from the `attributes` hash.
35
35
 
36
+ In addition to setting the encryption key statically as shown in the example above, you can also read the key dynamically per model instance via a method call. It looks like this.
37
+
38
+ ```ruby
39
+ class Message < ActiveRecord::Base
40
+ include EncryptableAttributes::Base
41
+
42
+ secure_key :individual_message_key
43
+ secure_attrs :title, :body
44
+
45
+ private
46
+
47
+ # Use this to build a message key from other model attributes
48
+ # or to set it from content fed from outside.
49
+ def individual_message_key
50
+ 'secret'
51
+ end
52
+ end
53
+ ```
54
+
36
55
  ## Development
37
56
 
38
57
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,8 @@
1
+ class AddMessageTestModel < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :messages do |t|
4
+ t.string :title
5
+ t.string :body
6
+ end
7
+ end
8
+ end
@@ -2,7 +2,7 @@ class AddUserTestModel < ActiveRecord::Migration[5.2]
2
2
  def change
3
3
  create_table :users do |t|
4
4
  t.string :name
5
- t.string :secret_info
5
+ t.string :salary
6
6
  end
7
7
  end
8
8
  end
data/db/schema.rb CHANGED
@@ -10,11 +10,16 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(version: 2018_05_12_123720) do
13
+ ActiveRecord::Schema.define(version: 2018_05_12_162331) do
14
+
15
+ create_table "messages", force: :cascade do |t|
16
+ t.string "title"
17
+ t.string "body"
18
+ end
14
19
 
15
20
  create_table "users", force: :cascade do |t|
16
21
  t.string "name"
17
- t.string "secret_info"
22
+ t.string "salary"
18
23
  end
19
24
 
20
25
  end
@@ -20,12 +20,12 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_dependency "activesupport"
23
+ spec.add_dependency "activesupport", "~> 5"
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.16"
26
- spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rake", "~> 12.3"
27
27
  spec.add_development_dependency "minitest", "~> 5.0"
28
- spec.add_development_dependency "activerecord"
29
- spec.add_development_dependency "standalone_migrations"
30
- spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "activerecord", "~> 5"
29
+ spec.add_development_dependency "standalone_migrations", "~> 5.2"
30
+ spec.add_development_dependency "sqlite3", "~> 1.3"
31
31
  end
@@ -1,7 +1,15 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/core_ext/class'
3
+ require 'active_support/message_encryptor'
4
+
1
5
  module EncryptableAttributes
2
6
  module Base
3
7
  extend ActiveSupport::Concern
4
8
 
9
+ included do
10
+ class_attribute :_secure_key
11
+ end
12
+
5
13
  # Override ActiveRecord accessor
6
14
  def [](key)
7
15
  send key
@@ -14,7 +22,7 @@ module EncryptableAttributes
14
22
 
15
23
  class_methods do
16
24
  def secure_key(key)
17
- @@secure_key = key
25
+ self._secure_key = key
18
26
  end
19
27
 
20
28
  def secure_attrs(*attr_names)
@@ -35,12 +43,22 @@ module EncryptableAttributes
35
43
  def new_crypt
36
44
  len = ActiveSupport::MessageEncryptor.key_len
37
45
  salt = SecureRandom.random_bytes(len)
38
- key = ActiveSupport::KeyGenerator.new(@@secure_key).generate_key(salt, len)
46
+ key = ActiveSupport::KeyGenerator.new(static_or_dynamic_secure_key).generate_key(salt, len)
39
47
  @crypt = ActiveSupport::MessageEncryptor.new(key)
40
48
  end
41
49
 
42
50
  def crypt
43
51
  @crypt ||= new_crypt
44
52
  end
53
+
54
+ def static_or_dynamic_secure_key
55
+ if self._secure_key.is_a?(String)
56
+ self._secure_key
57
+ elsif self._secure_key.is_a?(Symbol)
58
+ send self._secure_key
59
+ else
60
+ raise ArgumentError, "#{self._secure_key} bust be of type String or Symbol, but is of type #{self._secure_key.class}"
61
+ end
62
+ end
45
63
  end
46
64
  end
@@ -1,3 +1,3 @@
1
1
  module EncryptableAttributes
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -1,7 +1,2 @@
1
- require "active_support/all"
2
-
3
1
  require "encryptable_attributes/version"
4
2
  require "encryptable_attributes/base"
5
-
6
- module EncryptableAttributes
7
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encryptable_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nils Sommer
@@ -14,16 +14,16 @@ dependencies:
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '12.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '12.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -70,44 +70,44 @@ dependencies:
70
70
  name: activerecord
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '5'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '5'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: standalone_migrations
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '5.2'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: '5.2'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: sqlite3
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: '1.3'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '0'
110
+ version: '1.3'
111
111
  description:
112
112
  email:
113
113
  - mail@nilssommer.de
@@ -118,15 +118,16 @@ files:
118
118
  - ".editorconfig"
119
119
  - ".gitignore"
120
120
  - ".travis.yml"
121
+ - CHANGELOG.md
121
122
  - Gemfile
122
- - Gemfile.lock
123
123
  - LICENSE.txt
124
124
  - README.md
125
125
  - Rakefile
126
126
  - bin/console
127
127
  - bin/setup
128
128
  - db/config.yml
129
- - db/migrate/20180512123720_add_user_test_model.rb
129
+ - db/migrate/20180512123720_add_message_test_model.rb
130
+ - db/migrate/20180512162331_add_user_test_model.rb
130
131
  - db/schema.rb
131
132
  - encryptable_attributes.gemspec
132
133
  - lib/encryptable_attributes.rb
data/Gemfile.lock DELETED
@@ -1,87 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- encryptable_attributes (0.1.0)
5
- activesupport
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- actionpack (5.2.0)
11
- actionview (= 5.2.0)
12
- activesupport (= 5.2.0)
13
- rack (~> 2.0)
14
- rack-test (>= 0.6.3)
15
- rails-dom-testing (~> 2.0)
16
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
17
- actionview (5.2.0)
18
- activesupport (= 5.2.0)
19
- builder (~> 3.1)
20
- erubi (~> 1.4)
21
- rails-dom-testing (~> 2.0)
22
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
23
- activemodel (5.2.0)
24
- activesupport (= 5.2.0)
25
- activerecord (5.2.0)
26
- activemodel (= 5.2.0)
27
- activesupport (= 5.2.0)
28
- arel (>= 9.0)
29
- activesupport (5.2.0)
30
- concurrent-ruby (~> 1.0, >= 1.0.2)
31
- i18n (>= 0.7, < 2)
32
- minitest (~> 5.1)
33
- tzinfo (~> 1.1)
34
- arel (9.0.0)
35
- builder (3.2.3)
36
- concurrent-ruby (1.0.5)
37
- crass (1.0.4)
38
- erubi (1.7.1)
39
- i18n (1.0.1)
40
- concurrent-ruby (~> 1.0)
41
- loofah (2.2.2)
42
- crass (~> 1.0.2)
43
- nokogiri (>= 1.5.9)
44
- method_source (0.9.0)
45
- mini_portile2 (2.3.0)
46
- minitest (5.11.3)
47
- nokogiri (1.8.2)
48
- mini_portile2 (~> 2.3.0)
49
- rack (2.0.5)
50
- rack-test (1.0.0)
51
- rack (>= 1.0, < 3)
52
- rails-dom-testing (2.0.3)
53
- activesupport (>= 4.2.0)
54
- nokogiri (>= 1.6)
55
- rails-html-sanitizer (1.0.4)
56
- loofah (~> 2.2, >= 2.2.2)
57
- railties (5.2.0)
58
- actionpack (= 5.2.0)
59
- activesupport (= 5.2.0)
60
- method_source
61
- rake (>= 0.8.7)
62
- thor (>= 0.18.1, < 2.0)
63
- rake (10.5.0)
64
- sqlite3 (1.3.13)
65
- standalone_migrations (5.2.5)
66
- activerecord (>= 4.2.7, < 5.3.0)
67
- railties (>= 4.2.7, < 5.3.0)
68
- rake (>= 10.0)
69
- thor (0.20.0)
70
- thread_safe (0.3.6)
71
- tzinfo (1.2.5)
72
- thread_safe (~> 0.1)
73
-
74
- PLATFORMS
75
- ruby
76
-
77
- DEPENDENCIES
78
- activerecord
79
- bundler (~> 1.16)
80
- encryptable_attributes!
81
- minitest (~> 5.0)
82
- rake (~> 10.0)
83
- sqlite3
84
- standalone_migrations
85
-
86
- BUNDLED WITH
87
- 1.16.1