attr_keyring 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/.gitignore +8 -0
- data/.rubocop.yml +70 -0
- data/.travis.yml +16 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +83 -0
- data/LICENSE.txt +21 -0
- data/README.md +127 -0
- data/Rakefile +16 -0
- data/attr_keyring.gemspec +30 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/attr_keyring.rb +25 -0
- data/lib/attr_keyring/active_record.rb +58 -0
- data/lib/attr_keyring/key.rb +29 -0
- data/lib/attr_keyring/keyring.rb +51 -0
- data/lib/attr_keyring/version.rb +3 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3a7c2700c18142c251312d06c17ee3313fc570aeb1b163d25614db6d9cd75455
|
4
|
+
data.tar.gz: 68a9b8eda34be0e99bc8001fe13af68d1ab5dcd70b3aeb6a97d8422bcab75b5d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42775a257403349c7c64b8ec5720cc0985400478252db31c9a15d7cedf572f67ea14802f95552368bcad15ec2fce568b26c1cd3988661e3d40311c811e630288
|
7
|
+
data.tar.gz: 6b672080b7eef29da9f5b99b1eb51a6878781b25fa046c762970abfc54ab7e2a8a7c6cc75fdee59f9ecb3b02a095b7847cb7c55dd1c7d528709c7246a48bed53
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
Include:
|
4
|
+
- '**/*.gemspec'
|
5
|
+
- '**/*.podspec'
|
6
|
+
- '**/*.jbuilder'
|
7
|
+
- '**/*.rake'
|
8
|
+
- '**/*.opal'
|
9
|
+
- '**/config.ru'
|
10
|
+
- '**/Gemfile'
|
11
|
+
- '**/Rakefile'
|
12
|
+
- '**/Capfile'
|
13
|
+
- '**/Guardfile'
|
14
|
+
- '**/Podfile'
|
15
|
+
- '**/Thorfile'
|
16
|
+
- '**/Vagrantfile'
|
17
|
+
- '**/Berksfile'
|
18
|
+
- '**/Cheffile'
|
19
|
+
- '**/Vagabondfile'
|
20
|
+
|
21
|
+
Style/Documentation:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Style/StringLiterals:
|
25
|
+
EnforcedStyle: double_quotes
|
26
|
+
|
27
|
+
Layout/SpaceInsideBlockBraces:
|
28
|
+
EnforcedStyle: space
|
29
|
+
EnforcedStyleForEmptyBraces: space
|
30
|
+
SpaceBeforeBlockParameters: false
|
31
|
+
|
32
|
+
Layout/SpaceInsideHashLiteralBraces:
|
33
|
+
EnforcedStyle: no_space
|
34
|
+
EnforcedStyleForEmptyBraces: no_space
|
35
|
+
|
36
|
+
Layout/FirstArrayElementLineBreak:
|
37
|
+
Enabled: true
|
38
|
+
|
39
|
+
Layout/FirstHashElementLineBreak:
|
40
|
+
Enabled: true
|
41
|
+
|
42
|
+
Style/SymbolArray:
|
43
|
+
Enabled: true
|
44
|
+
|
45
|
+
Style/PercentLiteralDelimiters:
|
46
|
+
PreferredDelimiters:
|
47
|
+
'%': '[]'
|
48
|
+
'%i': '[]'
|
49
|
+
'%q': '[]'
|
50
|
+
'%Q': '[]'
|
51
|
+
'%r': '{}'
|
52
|
+
'%s': '[]'
|
53
|
+
'%w': '[]'
|
54
|
+
'%W': '[]'
|
55
|
+
'%x': '[]'
|
56
|
+
|
57
|
+
Metrics/LineLength:
|
58
|
+
Enabled: false
|
59
|
+
|
60
|
+
Style/EmptyMethod:
|
61
|
+
EnforcedStyle: expanded
|
62
|
+
|
63
|
+
Style/FrozenStringLiteralComment:
|
64
|
+
Enabled: false
|
65
|
+
|
66
|
+
Style/AccessModifierDeclarations:
|
67
|
+
EnforcedStyle: inline
|
68
|
+
|
69
|
+
Style/Alias:
|
70
|
+
EnforcedStyle: prefer_alias_method
|
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
sudo: false
|
4
|
+
notifications:
|
5
|
+
email: false
|
6
|
+
rvm:
|
7
|
+
- 2.5.3
|
8
|
+
before_script:
|
9
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
10
|
+
- chmod +x ./cc-test-reporter
|
11
|
+
- "./cc-test-reporter before-build"
|
12
|
+
after_script:
|
13
|
+
- "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
|
14
|
+
env:
|
15
|
+
global:
|
16
|
+
secure: c0y7opFgX78UQL0dVq2gciMr3Ca4y4Aw4cSbQMnUwGecwuzOPUhjV98yy4b6EpQ0bLVbVcSPtx/PCVV750nxJPQsz9tWS0yGxQPBXuh2w0AX+ErYJVYaF6+hTjovEiHB86Q9g8YCD29CIMLZs2yeUrB+ORJWQcuAn8fw475Zskk8d8BWqR8CDdonFKlwS0Bx6rOqkyVy0JiNbOM4+trV/RzrNC+dc1geqOo45ceTYiGzkkMU1XANjNhzl/v0DYtCWLF/Dj1s8da96btqU6msZDfsBM73zKWtu0KJMnzqa8Ba4Tjc39kd2ro6Zb22cELBdXOFBvNCAEjbmZIaJ2OC45fES1OGZnB66SjAScdVdxKy2jOWjlFvrRiHu3Zrbl5tFTEaJ/PMHueQn4AzneK1wU2kzjq5iCwBZtMp/iJtCvz0V6qBt77qJe65YuENhcj26cDMqQkhKd0QBTWNs8r02KY3HFKcprgM+2TXxVSvfDu2cbiMInvc3K+uFNnEbu/1piTyStKWGd64WHixV6CEFpHxLU04IUNB62mSvUZtZ6V782X9kawoRyUg6lWvXmnGUUvczdJdpSR5/3gVXOWHireYy/qA6Zqoup27PPoaNgnKCa/fWvN/aJDvrGJb9OWpiK8DGi6T35V5gtDF+vd8mVzyPnYJznlWLgA5m7FSzLg=
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at me@fnando.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
attr_keyring (0.1.0)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (5.2.1.1)
|
11
|
+
activesupport (= 5.2.1.1)
|
12
|
+
activerecord (5.2.1.1)
|
13
|
+
activemodel (= 5.2.1.1)
|
14
|
+
activesupport (= 5.2.1.1)
|
15
|
+
arel (>= 9.0)
|
16
|
+
activesupport (5.2.1.1)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 0.7, < 2)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
arel (9.0.0)
|
22
|
+
ast (2.4.0)
|
23
|
+
awesome_print (1.8.0)
|
24
|
+
byebug (10.0.2)
|
25
|
+
coderay (1.1.2)
|
26
|
+
concurrent-ruby (1.1.3)
|
27
|
+
i18n (1.1.1)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
jaro_winkler (1.5.1)
|
30
|
+
method_source (0.9.2)
|
31
|
+
minitest (5.11.3)
|
32
|
+
minitest-utils (0.4.4)
|
33
|
+
minitest
|
34
|
+
parallel (1.12.1)
|
35
|
+
parser (2.5.3.0)
|
36
|
+
ast (~> 2.4.0)
|
37
|
+
powerpack (0.1.2)
|
38
|
+
pry (0.12.2)
|
39
|
+
coderay (~> 1.1.0)
|
40
|
+
method_source (~> 0.9.0)
|
41
|
+
pry-byebug (3.6.0)
|
42
|
+
byebug (~> 10.0)
|
43
|
+
pry (~> 0.10)
|
44
|
+
pry-meta (0.0.10)
|
45
|
+
awesome_print
|
46
|
+
pry
|
47
|
+
pry-byebug
|
48
|
+
pry-remote
|
49
|
+
pry-remote (0.1.8)
|
50
|
+
pry (~> 0.9)
|
51
|
+
slop (~> 3.0)
|
52
|
+
rainbow (3.0.0)
|
53
|
+
rake (12.3.1)
|
54
|
+
rubocop (0.60.0)
|
55
|
+
jaro_winkler (~> 1.5.1)
|
56
|
+
parallel (~> 1.10)
|
57
|
+
parser (>= 2.5, != 2.5.1.1)
|
58
|
+
powerpack (~> 0.1)
|
59
|
+
rainbow (>= 2.2.2, < 4.0)
|
60
|
+
ruby-progressbar (~> 1.7)
|
61
|
+
unicode-display_width (~> 1.4.0)
|
62
|
+
ruby-progressbar (1.10.0)
|
63
|
+
slop (3.6.0)
|
64
|
+
sqlite3 (1.3.13)
|
65
|
+
thread_safe (0.3.6)
|
66
|
+
tzinfo (1.2.5)
|
67
|
+
thread_safe (~> 0.1)
|
68
|
+
unicode-display_width (1.4.0)
|
69
|
+
|
70
|
+
PLATFORMS
|
71
|
+
ruby
|
72
|
+
|
73
|
+
DEPENDENCIES
|
74
|
+
attr_keyring!
|
75
|
+
bundler
|
76
|
+
minitest-utils
|
77
|
+
pry-meta
|
78
|
+
rake
|
79
|
+
rubocop
|
80
|
+
sqlite3
|
81
|
+
|
82
|
+
BUNDLED WITH
|
83
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Nando Vieira
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
# attr_keyring
|
2
|
+
|
3
|
+
[![Travis-CI](https://travis-ci.org/fnando/attr_keyring.svg)](https://travis-ci.org/fnando/attr_keyring)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/fnando/attr_keyring/badges/gpa.svg)](https://codeclimate.com/github/fnando/attr_keyring)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/fnando/attr_keyring/badges/coverage.svg)](https://codeclimate.com/github/fnando/attr_keyring/coverage)
|
6
|
+
[![Gem](https://img.shields.io/gem/v/attr_keyring.svg)](https://rubygems.org/gems/attr_keyring)
|
7
|
+
[![Gem](https://img.shields.io/gem/dt/attr_keyring.svg)](https://rubygems.org/gems/attr_keyring)
|
8
|
+
|
9
|
+
Simple encryption-at-rest with key rotation support for ActiveRecord.
|
10
|
+
|
11
|
+
N.B.: attr_keyring is *not* for encrypting passwords--for that, you should use something like [bcrypt](https://github.com/codahale/bcrypt-ruby). It's meant for encrypting sensitive data you will need to access in plain text (e.g. storing OAuth token from users). Passwords do not fall in that category.
|
12
|
+
|
13
|
+
This library is heavily inspired by [attr_vault](https://github.com/uhoh-itsmaciek/attr_vault) but it's not a direct port and same keys won't work here without some manual intervention.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem "attr_keyring"
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install attr_keyring
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### Encryption
|
34
|
+
|
35
|
+
The key should be 16 bytes of random data, base64-encoded. A simple way to generate that is:
|
36
|
+
|
37
|
+
```console
|
38
|
+
$ dd if=/dev/urandom bs=16 count=1 2>/dev/null | openssl base64
|
39
|
+
```
|
40
|
+
|
41
|
+
Include the result of this in the `value` section of the key description in the keyring.
|
42
|
+
|
43
|
+
### Keyring
|
44
|
+
|
45
|
+
Keys are managed through a keyring--a short JSON document describing your encryption keys. The keyring must be a JSON object mapping numeric ids of the keys to the key values. A keyring must have at least one key. For example:
|
46
|
+
|
47
|
+
```json
|
48
|
+
{
|
49
|
+
"1": "PV8+EHgJlHfsVVVstJHgEo+3OCSn4iJDzqJs55U650Q=",
|
50
|
+
"2": "0HyJ15am4haRsCyiFCxDdlKwl3G5yPNKTUbadpaIfPI="
|
51
|
+
}
|
52
|
+
```
|
53
|
+
|
54
|
+
The `id` is used to track which key encrypted which piece of data; a key with a larger id is assumed to be newer. The value is the actual bytes of the encryption key.
|
55
|
+
|
56
|
+
You can dynamically load
|
57
|
+
|
58
|
+
### Model Setup
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
class ApplicationRecord < ActiveRecord::Base
|
62
|
+
self.abstract_class = true
|
63
|
+
|
64
|
+
include AttrKeyring
|
65
|
+
end
|
66
|
+
|
67
|
+
class User < ApplicationRecord
|
68
|
+
attr_keyring ENV["APP_KEYRING"]
|
69
|
+
attr_encrypt :twitter_oauth_token
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
The code above will encrypt your columns with the current key. If you're updating a record, then the column will be migrated to the latest key available.
|
74
|
+
|
75
|
+
### Lookup
|
76
|
+
|
77
|
+
One tricky aspect of encryption is looking up records by known secret. E.g.,
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
User.where(twitter_oauth_token: "241F596D-79FF-4C08-921A-A19E533B4F52")
|
81
|
+
```
|
82
|
+
|
83
|
+
is trivial with plain text fields, but impossible with the model defined as above.
|
84
|
+
|
85
|
+
If add a column `<attribute>_digest`, a SHA256 value will be saved, allowing you to lookup by that value instead.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
User.where(twitter_oauth_token_digest: Digest::SHA256.hexdigest("241F596D-79FF-4C08-921A-A19E533B4F52"))
|
89
|
+
```
|
90
|
+
|
91
|
+
### Key Rotation
|
92
|
+
|
93
|
+
Because attr_keyring uses a keyring, with access to multiple keys at once, key rotation is fairly straightforward: if you add a key to the keyring with a higher id than any other key, that key will automatically be used for encryption when records are either created or updated. Any keys that are no longer in use can be safely removed from the keyring.
|
94
|
+
|
95
|
+
To check if an existing key with id `123` is still in use, run:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
User.where(keyring_id: 123).empty?
|
99
|
+
```
|
100
|
+
|
101
|
+
For a large dataset, you may want to index the `keyring_id` column.
|
102
|
+
|
103
|
+
You may not want wait for records to be updated (e.g. key leaking). In that case, you can manually run something like the following:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
User.where(keyring_id: 1234).find_each do |user|
|
107
|
+
user.keyring_rotate!
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
## Development
|
112
|
+
|
113
|
+
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.
|
114
|
+
|
115
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
116
|
+
|
117
|
+
## Contributing
|
118
|
+
|
119
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/attr_keyring. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
120
|
+
|
121
|
+
## License
|
122
|
+
|
123
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
124
|
+
|
125
|
+
## Code of Conduct
|
126
|
+
|
127
|
+
Everyone interacting in the AttrKeyring project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fnando/attr_keyring/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "Run rubocop"
|
11
|
+
task(:rubocop) do
|
12
|
+
require "rubocop"
|
13
|
+
RuboCop::CLI.new.run(["--config", File.join(__dir__, ".rubocop.yml")])
|
14
|
+
end
|
15
|
+
|
16
|
+
task :default => [:test, :rubocop] # rubocop:disable Style/HashSyntax, Style/SymbolArray
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "./lib/attr_keyring/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "attr_keyring"
|
5
|
+
spec.version = AttrKeyring::VERSION
|
6
|
+
spec.authors = ["Nando Vieira"]
|
7
|
+
spec.email = ["me@fnando.com"]
|
8
|
+
|
9
|
+
spec.summary = "Simple encryption-at-rest plugin for ActiveRecord."
|
10
|
+
spec.description = spec.summary
|
11
|
+
spec.homepage = "https://github.com/fnando/attr_keyring"
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
# Specify which files should be added to the gem when it is released.
|
15
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
16
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "activerecord"
|
24
|
+
spec.add_development_dependency "bundler"
|
25
|
+
spec.add_development_dependency "minitest-utils"
|
26
|
+
spec.add_development_dependency "pry-meta"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
spec.add_development_dependency "rubocop"
|
29
|
+
spec.add_development_dependency "sqlite3"
|
30
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "attr_keyring"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/attr_keyring.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module AttrKeyring
|
2
|
+
require "active_record"
|
3
|
+
require "openssl"
|
4
|
+
|
5
|
+
require "attr_keyring/version"
|
6
|
+
require "attr_keyring/active_record"
|
7
|
+
require "attr_keyring/keyring"
|
8
|
+
require "attr_keyring/key"
|
9
|
+
|
10
|
+
UnknownKey = Class.new(StandardError)
|
11
|
+
InvalidSecret = Class.new(StandardError)
|
12
|
+
|
13
|
+
def self.included(target)
|
14
|
+
target.class_eval do
|
15
|
+
extend AttrKeyring::ActiveRecord::ClassMethods
|
16
|
+
include AttrKeyring::ActiveRecord::InstanceMethods
|
17
|
+
|
18
|
+
cattr_accessor :keyring, default: Keyring.new({})
|
19
|
+
cattr_accessor :keyring_column_name, default: "keyring_id"
|
20
|
+
cattr_accessor :keyring_attrs, default: []
|
21
|
+
|
22
|
+
before_save :migrate_to_latest_encryption_key
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module AttrKeyring
|
2
|
+
module ActiveRecord
|
3
|
+
module ClassMethods
|
4
|
+
def attr_keyring(keyring)
|
5
|
+
self.keyring = Keyring.new(keyring)
|
6
|
+
end
|
7
|
+
|
8
|
+
def attr_encrypt(*attributes)
|
9
|
+
keyring_attrs.push(*attributes)
|
10
|
+
|
11
|
+
attributes.each do |attribute|
|
12
|
+
define_attr_encrypt_writer(attribute)
|
13
|
+
define_attr_encrypt_reader(attribute)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def define_attr_encrypt_writer(attribute)
|
18
|
+
define_method("#{attribute}=") do |value|
|
19
|
+
keyring_id = public_send(keyring_column_name)
|
20
|
+
encrypted_value = keyring.encrypt(value, keyring_id)
|
21
|
+
|
22
|
+
public_send("encrypted_#{attribute}=", encrypted_value)
|
23
|
+
public_send("#{keyring_column_name}=", keyring_id || keyring.current_key.id) unless keyring_id
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def define_attr_encrypt_reader(attribute)
|
28
|
+
define_method(attribute) do
|
29
|
+
keyring_id = public_send(keyring_column_name)
|
30
|
+
keyring.decrypt(public_send("encrypted_#{attribute}"), keyring_id)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
module InstanceMethods
|
36
|
+
private def migrate_to_latest_encryption_key
|
37
|
+
keyring_id = keyring.current_key.id
|
38
|
+
|
39
|
+
keyring_attrs.each do |attribute|
|
40
|
+
value = public_send(attribute)
|
41
|
+
encrypted_value = keyring.encrypt(value, keyring_id)
|
42
|
+
|
43
|
+
public_send("encrypted_#{attribute}=", encrypted_value)
|
44
|
+
|
45
|
+
digest_column = "#{attribute}_digest"
|
46
|
+
public_send("#{digest_column}=", Digest::SHA256.hexdigest(value)) if respond_to?(digest_column)
|
47
|
+
end
|
48
|
+
|
49
|
+
public_send("#{keyring_column_name}=", keyring_id)
|
50
|
+
end
|
51
|
+
|
52
|
+
def keyring_rotate!
|
53
|
+
migrate_to_latest_encryption_key
|
54
|
+
save! if changed?
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module AttrKeyring
|
2
|
+
class Key
|
3
|
+
attr_reader :id, :value
|
4
|
+
|
5
|
+
def initialize(id, value)
|
6
|
+
@id = Integer(id)
|
7
|
+
@value = decode(value)
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
"#<AttrKeyring::Key id=#{id.inspect}>"
|
12
|
+
end
|
13
|
+
alias_method :inspect, :to_s
|
14
|
+
|
15
|
+
private def decode(secret)
|
16
|
+
return secret if secret.bytesize == 16
|
17
|
+
|
18
|
+
value = begin
|
19
|
+
Base64.strict_decode64(secret)
|
20
|
+
rescue ArgumentError
|
21
|
+
Base64.decode64(secret)
|
22
|
+
end
|
23
|
+
|
24
|
+
return value if value.bytesize == 16
|
25
|
+
|
26
|
+
raise InvalidSecret, "Secret must be 16 bytes, instead got #{value.bytesize}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module AttrKeyring
|
2
|
+
class Keyring
|
3
|
+
CIPHER_NAME = "AES-128-CBC".freeze
|
4
|
+
|
5
|
+
def initialize(keyring)
|
6
|
+
@keyring = keyring.map do |id, value|
|
7
|
+
Key.new(id, value)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def current_key
|
12
|
+
@keyring.max_by(&:id)
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](id)
|
16
|
+
key = @keyring.find {|k| k.id == id.to_i }
|
17
|
+
return key if key
|
18
|
+
|
19
|
+
raise UnknownKey, "key=#{id} is not available on keyring"
|
20
|
+
end
|
21
|
+
|
22
|
+
def []=(id, value)
|
23
|
+
@keyring << Key.new(id, value)
|
24
|
+
end
|
25
|
+
|
26
|
+
def clear
|
27
|
+
@keyring.clear
|
28
|
+
end
|
29
|
+
|
30
|
+
def encrypt(message, keyring_id = current_key.id)
|
31
|
+
cipher = OpenSSL::Cipher.new(CIPHER_NAME)
|
32
|
+
cipher.encrypt
|
33
|
+
iv = cipher.random_iv
|
34
|
+
cipher.iv = iv
|
35
|
+
cipher.key = self[keyring_id].value
|
36
|
+
iv + cipher.update(message) + cipher.final
|
37
|
+
end
|
38
|
+
|
39
|
+
def decrypt(secret, keyring_id)
|
40
|
+
decipher = OpenSSL::Cipher.new(CIPHER_NAME)
|
41
|
+
decipher.decrypt
|
42
|
+
|
43
|
+
iv = secret[0...decipher.iv_len]
|
44
|
+
encrypted = secret[decipher.iv_len..-1]
|
45
|
+
|
46
|
+
decipher.iv = iv
|
47
|
+
decipher.key = self[keyring_id].value
|
48
|
+
decipher.update(encrypted) + decipher.final
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_keyring
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nando Vieira
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: minitest-utils
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: pry-meta
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: sqlite3
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Simple encryption-at-rest plugin for ActiveRecord.
|
112
|
+
email:
|
113
|
+
- me@fnando.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rubocop.yml"
|
120
|
+
- ".travis.yml"
|
121
|
+
- CODE_OF_CONDUCT.md
|
122
|
+
- Gemfile
|
123
|
+
- Gemfile.lock
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- attr_keyring.gemspec
|
128
|
+
- bin/console
|
129
|
+
- bin/setup
|
130
|
+
- lib/attr_keyring.rb
|
131
|
+
- lib/attr_keyring/active_record.rb
|
132
|
+
- lib/attr_keyring/key.rb
|
133
|
+
- lib/attr_keyring/keyring.rb
|
134
|
+
- lib/attr_keyring/version.rb
|
135
|
+
homepage: https://github.com/fnando/attr_keyring
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 2.7.6
|
156
|
+
signing_key:
|
157
|
+
specification_version: 4
|
158
|
+
summary: Simple encryption-at-rest plugin for ActiveRecord.
|
159
|
+
test_files: []
|