action_encrypt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +46 -0
- data/Rakefile +32 -0
- data/app/models/action_encrypt/application_record.rb +5 -0
- data/app/models/action_encrypt/data_encryption_key.rb +23 -0
- data/app/models/action_encrypt/encrypted_field.rb +17 -0
- data/config/routes.rb +2 -0
- data/db/migrate/20190404115831_create_action_encrypt_encrypted_fields.rb +17 -0
- data/db/migrate/20190404121918_create_action_encrypt_data_encryption_keys.rb +11 -0
- data/db/migrate/20190404122056_add_data_encryption_key_to_action_encrypt_encrypted_fields.rb +5 -0
- data/lib/action_encrypt/attribute.rb +44 -0
- data/lib/action_encrypt/engine.rb +14 -0
- data/lib/action_encrypt/version.rb +3 -0
- data/lib/action_encrypt.rb +9 -0
- data/lib/tasks/action_encrypt_tasks.rake +4 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 99186586bdc0a439f72fded1d9a8794934e4fa0097199a87246b4f772614d3f3
|
4
|
+
data.tar.gz: 6c8a351353df7358a06b3c526fdceb1f32f1861a3f6f164a3d3affd8091f1c1f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 565df08df3127620e009755c16163d7628142d93e607f2917ccb6ade143c4b646a56768d705efc24bbf2ddbd962c172d145ae2b77c6be69514b28dd464e67b90
|
7
|
+
data.tar.gz: 4b8e048b55ab3a59cd87e6499ee9e6f0db1ff56d1b22c5445670a52559b3ddd0340042812a5299060aa9705e3c1639397c34baa5f5f0cdceaebbc0f1177c3959
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 David Van Der Beek
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# ActionEncrypt
|
2
|
+
Add encrypted fields to ActiveRecord models easily
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'action_encrypt'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
```bash
|
13
|
+
$ bundle
|
14
|
+
```
|
15
|
+
|
16
|
+
Install migrations:
|
17
|
+
```bash
|
18
|
+
$ rails action_encrypt:install:migrations
|
19
|
+
```
|
20
|
+
|
21
|
+
Add a key_encryption_key to your Rails encrypted credentials file. Generate
|
22
|
+
one using `SecureRandom.hex(32)`
|
23
|
+
```yml
|
24
|
+
key_encryption_key: <<key>>
|
25
|
+
```
|
26
|
+
|
27
|
+
Create a Data Encryption Key
|
28
|
+
```bash
|
29
|
+
$ ActionEncrypt::DataEncryptionKey.generate!(primary: true)
|
30
|
+
```
|
31
|
+
|
32
|
+
Add an encrypted field to your models
|
33
|
+
```ruby
|
34
|
+
class User < ApplicationRecord
|
35
|
+
has_encrypted :ssn
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Now you can use user.ssn as you would any other attribute, but it will be stored
|
40
|
+
securely in the action_encrypt_encrypted_fields table.
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
Contribution directions go here.
|
44
|
+
|
45
|
+
## License
|
46
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ActionEncrypt'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
load 'rails/tasks/statistics.rake'
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.pattern = 'test/**/*_test.rb'
|
29
|
+
t.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ActionEncrypt
|
2
|
+
class DataEncryptionKey < ApplicationRecord
|
3
|
+
attr_encrypted :key, key: :encryption_key
|
4
|
+
|
5
|
+
scope :primary, -> { find_by(primary: true) }
|
6
|
+
scope :unused, -> { where.not(primary: true, id: EncryptedField.pluck(:data_encryption_key_id).uniq) }
|
7
|
+
|
8
|
+
def self.generate!(attrs={})
|
9
|
+
create!(attrs.merge(key: SecureRandom.hex(32)))
|
10
|
+
end
|
11
|
+
|
12
|
+
def encryption_key
|
13
|
+
[Rails.application.credentials.key_encryption_key].pack("H*")
|
14
|
+
end
|
15
|
+
|
16
|
+
def promote!
|
17
|
+
transaction do
|
18
|
+
DataEncryptionKey.primary.update_attributes!(primary: false)
|
19
|
+
update_attributes!(primary: true)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActionEncrypt
|
2
|
+
class EncryptedField < ApplicationRecord
|
3
|
+
belongs_to :record, polymorphic: true
|
4
|
+
belongs_to :data_encryption_key
|
5
|
+
|
6
|
+
attr_encrypted :blob, key: :encryption_key
|
7
|
+
|
8
|
+
def encryption_key
|
9
|
+
self.data_encryption_key ||= DataEncryptionKey.primary
|
10
|
+
[data_encryption_key.key].pack("H*")
|
11
|
+
end
|
12
|
+
|
13
|
+
def reencrypt!(new_key)
|
14
|
+
update_attributes!(data_encryption_key: new_key, blob: blob)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateActionEncryptEncryptedFields < ActiveRecord::Migration[5.2]
|
2
|
+
def change
|
3
|
+
enable_extension 'pgcrypto'
|
4
|
+
|
5
|
+
create_table :action_encrypt_encrypted_fields, id: :uuid do |t|
|
6
|
+
t.string :encrypted_blob
|
7
|
+
t.string :encrypted_blob_iv
|
8
|
+
t.string :name, null: false
|
9
|
+
t.uuid :record_id
|
10
|
+
t.string :record_type
|
11
|
+
|
12
|
+
t.timestamps
|
13
|
+
|
14
|
+
t.index [:record_type, :record_id, :name], name: "index_action_encrypt_fields_uniqueness", unique: true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class CreateActionEncryptDataEncryptionKeys < ActiveRecord::Migration[5.2]
|
2
|
+
def change
|
3
|
+
create_table :action_encrypt_data_encryption_keys, id: :uuid do |t|
|
4
|
+
t.string :encrypted_key
|
5
|
+
t.string :encrypted_key_iv
|
6
|
+
t.boolean :primary
|
7
|
+
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActionEncrypt
|
4
|
+
module Attribute
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
class_methods do
|
8
|
+
def search_encrypted(name, opts = {})
|
9
|
+
blind_index :"#{name}", key: [Rails.application.credentials.blind_index_key].pack("H*")
|
10
|
+
|
11
|
+
before_validation :"compute_#{name}_bidx"
|
12
|
+
|
13
|
+
if opts[:unique] == true
|
14
|
+
validates :"#{name}", uniqueness: true, allow_nil: true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def has_encrypted(name)
|
19
|
+
class_eval <<-CODE, __FILE__, __LINE__ + 1
|
20
|
+
def #{name}
|
21
|
+
#{name}_encrypted_field.blob
|
22
|
+
end
|
23
|
+
|
24
|
+
def #{name}=(value)
|
25
|
+
#{name}_encrypted_field.blob = value
|
26
|
+
end
|
27
|
+
|
28
|
+
def #{name}_encrypted_field
|
29
|
+
super || build_#{name}_encrypted_field
|
30
|
+
end
|
31
|
+
CODE
|
32
|
+
|
33
|
+
has_one :"#{name}_encrypted_field", -> { where(name: name) },
|
34
|
+
class_name: "ActionEncrypt::EncryptedField",
|
35
|
+
as: :record,
|
36
|
+
inverse_of: :record,
|
37
|
+
autosave: true,
|
38
|
+
dependent: :destroy
|
39
|
+
|
40
|
+
scope :"with_#{name}_encrypted_field", -> { includes("#{name}_encrypted_field": :data_encryption_key) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "attr_encrypted"
|
2
|
+
require "active_record/railtie"
|
3
|
+
|
4
|
+
module ActionEncrypt
|
5
|
+
class Engine < ::Rails::Engine
|
6
|
+
isolate_namespace ActionEncrypt
|
7
|
+
|
8
|
+
initializer "action_text.attribute" do
|
9
|
+
ActiveSupport.on_load(:active_record) do
|
10
|
+
include ActionEncrypt::Attribute
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: action_encrypt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Van Der Beek
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: attr_encrypted
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: blind_index
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pg
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- earlynovrock@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- MIT-LICENSE
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- app/models/action_encrypt/application_record.rb
|
80
|
+
- app/models/action_encrypt/data_encryption_key.rb
|
81
|
+
- app/models/action_encrypt/encrypted_field.rb
|
82
|
+
- config/routes.rb
|
83
|
+
- db/migrate/20190404115831_create_action_encrypt_encrypted_fields.rb
|
84
|
+
- db/migrate/20190404121918_create_action_encrypt_data_encryption_keys.rb
|
85
|
+
- db/migrate/20190404122056_add_data_encryption_key_to_action_encrypt_encrypted_fields.rb
|
86
|
+
- lib/action_encrypt.rb
|
87
|
+
- lib/action_encrypt/attribute.rb
|
88
|
+
- lib/action_encrypt/engine.rb
|
89
|
+
- lib/action_encrypt/version.rb
|
90
|
+
- lib/tasks/action_encrypt_tasks.rake
|
91
|
+
homepage:
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubygems_version: 3.0.1
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Easily add encrypted fields to your models.
|
114
|
+
test_files: []
|