active_record-pgcrypto 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/main.workflow +18 -0
- data/.gitignore +11 -0
- data/.rubocop.yml +47 -0
- data/.yardstick.yml +29 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Dockerfile +17 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +92 -0
- data/LICENSE.txt +21 -0
- data/README.md +122 -0
- data/Rakefile +23 -0
- data/active_record-pgcrypto.gemspec +33 -0
- data/lib/active_record/pgcrypto.rb +15 -0
- data/lib/active_record/pgcrypto/log_subscriber.rb +23 -0
- data/lib/active_record/pgcrypto/symmetric_coder.rb +84 -0
- data/lib/active_record/pgcrypto/version.rb +5 -0
- metadata +199 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 06cbd7ee85626aad5756920cd6b4d49638c037942326c35596ecafbb20b2b7a0
|
4
|
+
data.tar.gz: 1c9776668981d59edf23c5744ec42fd143fa1a2072fe28c6eb5037f0e79980e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7f2a5b70c2f6c7a65c1bd7e88b830e0390b3da9d09c541111001c0eea0c7ebb5f9512a6fc0222627dbe67971ca1dc12518326ec3c8774b7b76c9bd596fb3e9a5
|
7
|
+
data.tar.gz: 17021f2dbcd875ad2e8f4bf3ebea817f41d4a1021a675ca303593f607efd2a7b79145ef0e9272e7cdd5d2b8d5bd3b50d20d5d3ea5876c804af55d0d85a5fee50
|
@@ -0,0 +1,18 @@
|
|
1
|
+
workflow "Docs/Tests" {
|
2
|
+
on = "push"
|
3
|
+
resolves = [
|
4
|
+
"Builds the Docker image",
|
5
|
+
"Runs the linters and tests"
|
6
|
+
]
|
7
|
+
}
|
8
|
+
|
9
|
+
action "Builds the Docker image" {
|
10
|
+
uses = "actions/docker/cli@master"
|
11
|
+
args = "build -f Dockerfile -t active_record-pgcrypto/ci:$GITHUB_SHA ."
|
12
|
+
}
|
13
|
+
|
14
|
+
action "Runs the linters and tests" {
|
15
|
+
uses = "actions/docker/cli@master"
|
16
|
+
needs = ["Builds the Docker image"]
|
17
|
+
args = "run active_record-pgcrypto/ci:$GITHUB_SHA"
|
18
|
+
}
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
RSpec:
|
6
|
+
Enabled: true
|
7
|
+
|
8
|
+
RSpec/MultipleExpectations:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Performance:
|
12
|
+
Enabled: true
|
13
|
+
|
14
|
+
Bundler:
|
15
|
+
Enabled: true
|
16
|
+
|
17
|
+
Gemspec:
|
18
|
+
Enabled: true
|
19
|
+
|
20
|
+
Style/StringLiterals:
|
21
|
+
Enabled: true
|
22
|
+
EnforcedStyle: single_quotes
|
23
|
+
|
24
|
+
Style/FrozenStringLiteralComment:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Metrics/LineLength:
|
28
|
+
Max: 80
|
29
|
+
|
30
|
+
Metrics/BlockLength:
|
31
|
+
Exclude:
|
32
|
+
- 'spec/**/*_spec.rb'
|
33
|
+
- '**/*.gemspec'
|
34
|
+
|
35
|
+
Layout/IndentationConsistency:
|
36
|
+
EnforcedStyle: normal
|
37
|
+
|
38
|
+
Style/BlockDelimiters:
|
39
|
+
Enabled: true
|
40
|
+
|
41
|
+
RSpec/FilePath:
|
42
|
+
Exclude:
|
43
|
+
- 'spec/**/*_spec.rb'
|
44
|
+
|
45
|
+
RSpec/DescribedClass:
|
46
|
+
Exclude:
|
47
|
+
- 'spec/integration/*_spec.rb'
|
data/.yardstick.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
---
|
2
|
+
path: ['lib/**/*.rb']
|
3
|
+
threshold: 100
|
4
|
+
rules:
|
5
|
+
ApiTag::Presence:
|
6
|
+
enabled: false
|
7
|
+
ApiTag::Inclusion:
|
8
|
+
enabled: false
|
9
|
+
ApiTag::ProtectedMethod:
|
10
|
+
enabled: false
|
11
|
+
ApiTag::PrivateMethod:
|
12
|
+
enabled: false
|
13
|
+
ExampleTag:
|
14
|
+
enabled: false
|
15
|
+
ReturnTag:
|
16
|
+
enabled: true
|
17
|
+
exclude: []
|
18
|
+
Summary::Presence:
|
19
|
+
enabled: true
|
20
|
+
exclude: []
|
21
|
+
Summary::Length:
|
22
|
+
enabled: true
|
23
|
+
exclude: []
|
24
|
+
Summary::Delimiter:
|
25
|
+
enabled: true
|
26
|
+
exclude: []
|
27
|
+
Summary::SingleLine:
|
28
|
+
enabled: true
|
29
|
+
exclude: []
|
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 stas@nerd.ro. 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/Dockerfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
FROM postgres:10.5-alpine
|
2
|
+
|
3
|
+
RUN apk add --no-cache git build-base ruby ruby-full ruby-dev
|
4
|
+
|
5
|
+
RUN gem install -q --no-ri --no-rdoc -v '~> 1' bundler
|
6
|
+
|
7
|
+
RUN mkdir /gem
|
8
|
+
WORKDIR /gem
|
9
|
+
|
10
|
+
COPY ./ ./
|
11
|
+
RUN bundle install --no-cache
|
12
|
+
|
13
|
+
ENV DATABASE_URL=postgresql://postgres@localhost/postgres?pool=5
|
14
|
+
|
15
|
+
ENTRYPOINT []
|
16
|
+
|
17
|
+
CMD ["sh", "-c", "(nohup /docker-entrypoint.sh postgres > /dev/null &) && sleep 3 && bundle exec rake"]
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
active_record-pgcrypto (0.1.1)
|
5
|
+
activerecord
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (5.2.3)
|
11
|
+
activesupport (= 5.2.3)
|
12
|
+
activerecord (5.2.3)
|
13
|
+
activemodel (= 5.2.3)
|
14
|
+
activesupport (= 5.2.3)
|
15
|
+
arel (>= 9.0)
|
16
|
+
activesupport (5.2.3)
|
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
|
+
concurrent-ruby (1.1.5)
|
24
|
+
diff-lcs (1.3)
|
25
|
+
docile (1.3.1)
|
26
|
+
ffaker (2.11.0)
|
27
|
+
i18n (1.6.0)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
jaro_winkler (1.5.2)
|
30
|
+
json (2.2.0)
|
31
|
+
minitest (5.11.3)
|
32
|
+
parallel (1.17.0)
|
33
|
+
parser (2.6.3.0)
|
34
|
+
ast (~> 2.4.0)
|
35
|
+
pg (1.1.4)
|
36
|
+
rainbow (3.0.0)
|
37
|
+
rake (12.3.2)
|
38
|
+
rspec (3.8.0)
|
39
|
+
rspec-core (~> 3.8.0)
|
40
|
+
rspec-expectations (~> 3.8.0)
|
41
|
+
rspec-mocks (~> 3.8.0)
|
42
|
+
rspec-core (3.8.0)
|
43
|
+
rspec-support (~> 3.8.0)
|
44
|
+
rspec-expectations (3.8.4)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.8.0)
|
47
|
+
rspec-mocks (3.8.0)
|
48
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
+
rspec-support (~> 3.8.0)
|
50
|
+
rspec-support (3.8.2)
|
51
|
+
rubocop (0.71.0)
|
52
|
+
jaro_winkler (~> 1.5.1)
|
53
|
+
parallel (~> 1.10)
|
54
|
+
parser (>= 2.6)
|
55
|
+
rainbow (>= 2.2.2, < 4.0)
|
56
|
+
ruby-progressbar (~> 1.7)
|
57
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
58
|
+
rubocop-performance (1.3.0)
|
59
|
+
rubocop (>= 0.68.0)
|
60
|
+
rubocop-rspec (1.33.0)
|
61
|
+
rubocop (>= 0.60.0)
|
62
|
+
ruby-progressbar (1.10.1)
|
63
|
+
simplecov (0.16.1)
|
64
|
+
docile (~> 1.1)
|
65
|
+
json (>= 1.8, < 3)
|
66
|
+
simplecov-html (~> 0.10.0)
|
67
|
+
simplecov-html (0.10.2)
|
68
|
+
thread_safe (0.3.6)
|
69
|
+
tzinfo (1.2.5)
|
70
|
+
thread_safe (~> 0.1)
|
71
|
+
unicode-display_width (1.6.0)
|
72
|
+
yard (0.9.19)
|
73
|
+
yardstick (0.9.9)
|
74
|
+
yard (~> 0.8, >= 0.8.7.2)
|
75
|
+
|
76
|
+
PLATFORMS
|
77
|
+
ruby
|
78
|
+
|
79
|
+
DEPENDENCIES
|
80
|
+
active_record-pgcrypto!
|
81
|
+
bundler
|
82
|
+
ffaker
|
83
|
+
pg
|
84
|
+
rake
|
85
|
+
rspec
|
86
|
+
rubocop-performance
|
87
|
+
rubocop-rspec
|
88
|
+
simplecov
|
89
|
+
yardstick
|
90
|
+
|
91
|
+
BUNDLED WITH
|
92
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Stas SUȘCOV
|
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,122 @@
|
|
1
|
+
# PGCrypto for ActiveRecord 🆊
|
2
|
+
|
3
|
+
[PostgreSQL PGCrypto](https://www.postgresql.org/docs/current/pgcrypto.html)
|
4
|
+
support for ActiveRecord models.
|
5
|
+
|
6
|
+
![Don't roll your own crypto](https://imgs.xkcd.com/comics/cryptography.png)
|
7
|
+
|
8
|
+
## About
|
9
|
+
|
10
|
+
The goal of this project is to provide a simple and efficient encryption
|
11
|
+
support for your application records.
|
12
|
+
|
13
|
+
Main goals:
|
14
|
+
* No _magic_ please
|
15
|
+
* No DSLs please
|
16
|
+
* Less code, less maintenance
|
17
|
+
* Good docs and test coverage
|
18
|
+
* Keep it up-to-date (or at least tell people this is no longer maintained)
|
19
|
+
|
20
|
+
The available features include:
|
21
|
+
* PostgreSQL `pgcrypto` native symmetric encryption using the
|
22
|
+
[attribute serialization](https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html#method-i-serialize) API and Arel API.
|
23
|
+
* Logger support for sensitive data query obfuscation.
|
24
|
+
|
25
|
+
### A bit of history...
|
26
|
+
|
27
|
+
The project was born after trying out `crypt_keeper` and having to deal with
|
28
|
+
the broken support for PostgreSQL where the stored data would be invalid for
|
29
|
+
native SQL functions.
|
30
|
+
|
31
|
+
I would like to thank Justin for his work, but I decided to move away from the
|
32
|
+
original work for arguably subjective reasons and the critical importance of
|
33
|
+
this functionality in my projects.
|
34
|
+
|
35
|
+
## Installation
|
36
|
+
|
37
|
+
Add this line to your application's Gemfile:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
gem 'active_record-pgcrypto'
|
41
|
+
```
|
42
|
+
|
43
|
+
And then execute:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
$ bundle
|
47
|
+
```
|
48
|
+
|
49
|
+
Or install it yourself as:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
$ gem install active_record-pgcrypto
|
53
|
+
```
|
54
|
+
|
55
|
+
## Usage
|
56
|
+
|
57
|
+
To start using it with ActiveRecord/Rails, add this to an initializer and
|
58
|
+
configure your keys:
|
59
|
+
```ruby
|
60
|
+
# config/initializers/pgcrypto.rb
|
61
|
+
require 'active_record/pgcrypto'
|
62
|
+
# Replace the default environment variable name with your own value/key.
|
63
|
+
ActiveRecord::PGCrypto::SymmetricCoder.pgcrypto_key = ENV['PGCRYPTO_SYM_KEY']
|
64
|
+
```
|
65
|
+
|
66
|
+
Now enable the coder for your model attributes:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class MyModel < ActiveRecord::Base
|
70
|
+
serialize(:email, ActiveRecord::PGCrypto::SymmetricCoder)
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
The coder provides a simple API to help you provide search support by leveraging
|
75
|
+
Arel API:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
class MyModel < ActiveRecord::Base
|
79
|
+
serialize(:email, ActiveRecord::PGCrypto::SymmetricCoder)
|
80
|
+
|
81
|
+
def self.decrypted_email
|
82
|
+
ActiveRecord::PGCrypto::SymmetricCoder.decrypted_arel(arel_table[:email])
|
83
|
+
end
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
Now you can use add it to your `ActiveRecord::Base#where` queries:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
MyModel.where(MyModel.decrypted_email.eq('keyword'))
|
91
|
+
```
|
92
|
+
|
93
|
+
## Development
|
94
|
+
|
95
|
+
After checking out the repo, run `bundle` to install dependencies.
|
96
|
+
|
97
|
+
Then, run `rake` to run the tests.
|
98
|
+
|
99
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
100
|
+
|
101
|
+
To release a new version, update the version number in `version.rb`, and then
|
102
|
+
run `bundle exec rake release`, which will create a git tag for the version,
|
103
|
+
push git commits and tags, and push the `.gem` file to
|
104
|
+
[rubygems.org](https://rubygems.org).
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
Bug reports and pull requests are welcome on GitHub at
|
109
|
+
https://github.com/stas/active_record-pgcrypto. This project is intended to be
|
110
|
+
a safe, welcoming space for collaboration, and contributors are expected to
|
111
|
+
adhere to the [Contributor Covenant](http://contributor-covenant.org) code of
|
112
|
+
conduct.
|
113
|
+
|
114
|
+
## License
|
115
|
+
|
116
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
117
|
+
|
118
|
+
## Code of Conduct
|
119
|
+
|
120
|
+
Everyone interacting with this project codebase, issue
|
121
|
+
tracker, chat rooms and mailing list is expected to follow the [code of
|
122
|
+
conduct](https://github.com/[USERNAME]/active_record-pgcrypto/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'rubocop/rake_task'
|
4
|
+
require 'yaml'
|
5
|
+
require 'yardstick/rake/verify'
|
6
|
+
|
7
|
+
desc('Documentation stats and measurements')
|
8
|
+
task('qa:docs') do
|
9
|
+
yaml = YAML.load_file('.yardstick.yml')
|
10
|
+
config = Yardstick::Config.coerce(yaml)
|
11
|
+
measure = Yardstick.measure(config)
|
12
|
+
measure.puts
|
13
|
+
coverage = Yardstick.round_percentage(measure.coverage * 100)
|
14
|
+
exit(1) if coverage < config.threshold
|
15
|
+
end
|
16
|
+
|
17
|
+
RuboCop::RakeTask.new('qa:code')
|
18
|
+
|
19
|
+
desc('Run QA tasks')
|
20
|
+
task(qa: ['qa:docs', 'qa:code'])
|
21
|
+
|
22
|
+
RSpec::Core::RakeTask.new(spec: :qa)
|
23
|
+
task(default: :spec)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'active_record/pgcrypto/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'active_record-pgcrypto'
|
7
|
+
spec.version = ActiveRecord::PGCrypto::VERSION
|
8
|
+
spec.authors = ['Stas SUȘCOV']
|
9
|
+
spec.email = ['stas@nerd.ro']
|
10
|
+
|
11
|
+
spec.summary = 'PGCrypto for ActiveRecord'
|
12
|
+
spec.description = 'PostgreSQL PGCrypto support for ActiveRecord models.'
|
13
|
+
spec.homepage = 'https://github.com/stas/active_record-pgcrypto'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
19
|
+
end
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'activerecord', ENV['RAILS_VERSION']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler'
|
25
|
+
spec.add_development_dependency 'ffaker'
|
26
|
+
spec.add_development_dependency 'pg'
|
27
|
+
spec.add_development_dependency 'rake'
|
28
|
+
spec.add_development_dependency 'rspec'
|
29
|
+
spec.add_development_dependency 'rubocop-performance'
|
30
|
+
spec.add_development_dependency 'rubocop-rspec'
|
31
|
+
spec.add_development_dependency 'simplecov'
|
32
|
+
spec.add_development_dependency 'yardstick'
|
33
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_record/pgcrypto/version'
|
2
|
+
require 'active_record/log_subscriber'
|
3
|
+
require 'active_record/pgcrypto/symmetric_coder'
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
# PostgreSQL PGCrypto support for [ActiveRecord]
|
7
|
+
module PGCrypto
|
8
|
+
# Enables the log scrubber
|
9
|
+
#
|
10
|
+
# @return [NilClass]
|
11
|
+
def self.enable_log_subscriber!
|
12
|
+
ActiveRecord::LogSubscriber.prepend(ActiveRecord::PGCrypto::LogSubscriber)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module PGCrypto
|
3
|
+
# Subscribes to the logger and obfuscates the sensitive queries.
|
4
|
+
module LogSubscriber
|
5
|
+
REGEXP = \
|
6
|
+
/(\(*)(?<operation>pgp_sym_(decrypt|encrypt)_bytea)(\(+.*\)+)/im.freeze
|
7
|
+
PLACEHOLDER = '[FILTERED]'.freeze
|
8
|
+
|
9
|
+
# Scrubs the log event from any sensitive SQL
|
10
|
+
#
|
11
|
+
# @return [NilClass]
|
12
|
+
def sql(event)
|
13
|
+
scrubbed_sql = event.payload[:sql].gsub(REGEXP) do |_|
|
14
|
+
"#{$LAST_MATCH_INFO[:operation]}(#{PLACEHOLDER})"
|
15
|
+
end
|
16
|
+
|
17
|
+
event.payload[:sql] = scrubbed_sql
|
18
|
+
|
19
|
+
super(event)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module PGCrypto
|
3
|
+
# PGCrypto symmetric encryption/decryption coder for attribute serialization
|
4
|
+
module SymmetricCoder
|
5
|
+
mattr_accessor :pgcrypto_key, default: ENV['PGCRYPTO_SYM_KEY']
|
6
|
+
mattr_accessor(
|
7
|
+
:pgcrypto_options,
|
8
|
+
default: (
|
9
|
+
ENV['PGCRYPTO_SYM_OPTIONS'] || 'cipher-algo=aes256, unicode-mode=1'
|
10
|
+
)
|
11
|
+
)
|
12
|
+
mattr_accessor(
|
13
|
+
:pgcrypto_encoding,
|
14
|
+
default: Encoding.find(ENV['PGCRYPTO_ENCODING'] || 'utf-8')
|
15
|
+
)
|
16
|
+
|
17
|
+
# Decrypts the requested value
|
18
|
+
#
|
19
|
+
# @return [String]
|
20
|
+
def self.load(value)
|
21
|
+
decrypt(value)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Encrypts the requested value
|
25
|
+
#
|
26
|
+
# @return [String] binary data
|
27
|
+
def self.dump(value)
|
28
|
+
encrypt(value)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Wraps the value into an [Arel::Node] with SQL calls for encryption
|
32
|
+
#
|
33
|
+
# @return [Arel::Node]
|
34
|
+
def self.encrypt(value)
|
35
|
+
return value if value.nil?
|
36
|
+
|
37
|
+
encrypted = Arel::Nodes::NamedFunction.new(
|
38
|
+
'PGP_SYM_ENCRYPT_BYTEA', [
|
39
|
+
Arel::Nodes::Quoted.new(value.to_s),
|
40
|
+
Arel::Nodes::Quoted.new(pgcrypto_key),
|
41
|
+
Arel::Nodes::Quoted.new(pgcrypto_options)
|
42
|
+
]
|
43
|
+
)
|
44
|
+
|
45
|
+
enc_val = arel_query(encrypted)
|
46
|
+
ActiveRecord::Base.connection.unescape_bytea(enc_val)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Wraps a node for decryption calls
|
50
|
+
#
|
51
|
+
# @return [Arel::Node]
|
52
|
+
def self.decrypted_arel(node)
|
53
|
+
Arel::Nodes::NamedFunction.new(
|
54
|
+
'PGP_SYM_DECRYPT_BYTEA', [
|
55
|
+
node, Arel::Nodes::Quoted.new(pgcrypto_key)
|
56
|
+
]
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Wraps the value into an [Arel::Node] with SQL calls for decryption
|
61
|
+
#
|
62
|
+
# @return [Arel::Node]
|
63
|
+
def self.decrypt(value)
|
64
|
+
return value if value.nil?
|
65
|
+
|
66
|
+
value = ActiveRecord::Base.connection.escape_bytea(value)
|
67
|
+
dec_value = arel_query(decrypted_arel(Arel::Nodes::Quoted.new(value)))
|
68
|
+
dec_value = ActiveRecord::Base.connection.unescape_bytea(dec_value)
|
69
|
+
dec_value.force_encoding(pgcrypto_encoding)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Executes the [Arel::Node] generated query
|
73
|
+
#
|
74
|
+
# @return [String] the first returned value
|
75
|
+
def self.arel_query(arel_nodes)
|
76
|
+
query = Arel::SelectManager.new(nil).project(arel_nodes).to_sql
|
77
|
+
|
78
|
+
::ActiveRecord::Base.connection.select_value(query)
|
79
|
+
end
|
80
|
+
|
81
|
+
private_class_method :encrypt, :decrypt, :arel_query
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record-pgcrypto
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stas SUȘCOV
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-30 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: ffaker
|
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: 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
|
+
- !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: rspec
|
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: rubocop-performance
|
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
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: yardstick
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: PostgreSQL PGCrypto support for ActiveRecord models.
|
154
|
+
email:
|
155
|
+
- stas@nerd.ro
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".github/main.workflow"
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rubocop.yml"
|
163
|
+
- ".yardstick.yml"
|
164
|
+
- CODE_OF_CONDUCT.md
|
165
|
+
- Dockerfile
|
166
|
+
- Gemfile
|
167
|
+
- Gemfile.lock
|
168
|
+
- LICENSE.txt
|
169
|
+
- README.md
|
170
|
+
- Rakefile
|
171
|
+
- active_record-pgcrypto.gemspec
|
172
|
+
- lib/active_record/pgcrypto.rb
|
173
|
+
- lib/active_record/pgcrypto/log_subscriber.rb
|
174
|
+
- lib/active_record/pgcrypto/symmetric_coder.rb
|
175
|
+
- lib/active_record/pgcrypto/version.rb
|
176
|
+
homepage: https://github.com/stas/active_record-pgcrypto
|
177
|
+
licenses:
|
178
|
+
- MIT
|
179
|
+
metadata: {}
|
180
|
+
post_install_message:
|
181
|
+
rdoc_options: []
|
182
|
+
require_paths:
|
183
|
+
- lib
|
184
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
185
|
+
requirements:
|
186
|
+
- - ">="
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
version: '0'
|
189
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
|
+
requirements:
|
191
|
+
- - ">="
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: '0'
|
194
|
+
requirements: []
|
195
|
+
rubygems_version: 3.0.1
|
196
|
+
signing_key:
|
197
|
+
specification_version: 4
|
198
|
+
summary: PGCrypto for ActiveRecord
|
199
|
+
test_files: []
|