rails_master_key_kms_decrypter 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +77 -0
- data/.spec_helper +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +174 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rails_master_key_kms_decrypter.rb +8 -0
- data/lib/rails_master_key_kms_decrypter/decryption.rb +20 -0
- data/lib/rails_master_key_kms_decrypter/kms_encrypted_configuration.rb +41 -0
- data/lib/rails_master_key_kms_decrypter/railtie.rb +20 -0
- data/lib/rails_master_key_kms_decrypter/version.rb +5 -0
- data/rails_master_key_kms_decrypter.gemspec +32 -0
- metadata +146 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 404cfff4aa982f0f310fe54cf113afe2e03136c17c137afd04386d4c9bfadbe8
|
|
4
|
+
data.tar.gz: c7a495d2b49ab78610b76ddf2e7268ecc74baeeb453e227256f18720641064ba
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 27794526219712820007cc1097be1e9965efe3a2ba81303b52d4fba38e7797c2369bbcacaa452add8b567168c0d5f94becbc53cf5f9f3545aea7b0e9ae2d7bfa
|
|
7
|
+
data.tar.gz: 26cc8a44b4f5f1693415c0de995b424bcca6ea7a0334731f575be50f42d69e7b591e3853100a1dc9accf97f3b902e39558bc17e59c0bf6f9901a347fb85b4578
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
Exclude:
|
|
3
|
+
- 'bin/*'
|
|
4
|
+
- 'db/**/*'
|
|
5
|
+
- 'vendor/**/*'
|
|
6
|
+
- '**/Rakefile'
|
|
7
|
+
- '**/config.ru'
|
|
8
|
+
- 'node_modules/**/*'
|
|
9
|
+
TargetRubyVersion: 2.5
|
|
10
|
+
DisplayCopNames: true
|
|
11
|
+
|
|
12
|
+
Performance:
|
|
13
|
+
Enabled: false
|
|
14
|
+
|
|
15
|
+
Bundler:
|
|
16
|
+
Enabled: false
|
|
17
|
+
|
|
18
|
+
Naming:
|
|
19
|
+
Enabled: false
|
|
20
|
+
|
|
21
|
+
Metrics/BlockNesting:
|
|
22
|
+
Enabled: false
|
|
23
|
+
|
|
24
|
+
Metrics/ClassLength:
|
|
25
|
+
Enabled: false
|
|
26
|
+
|
|
27
|
+
Metrics/LineLength:
|
|
28
|
+
Enabled: false
|
|
29
|
+
|
|
30
|
+
Metrics/MethodLength:
|
|
31
|
+
Enabled: false
|
|
32
|
+
|
|
33
|
+
Metrics/BlockLength:
|
|
34
|
+
Enabled: false
|
|
35
|
+
|
|
36
|
+
Metrics/ModuleLength:
|
|
37
|
+
Enabled: false
|
|
38
|
+
|
|
39
|
+
Style/AsciiComments:
|
|
40
|
+
Enabled: false
|
|
41
|
+
|
|
42
|
+
Style/BlockDelimiters:
|
|
43
|
+
Exclude:
|
|
44
|
+
- 'spec/**/*'
|
|
45
|
+
|
|
46
|
+
Style/Documentation:
|
|
47
|
+
Enabled: false
|
|
48
|
+
|
|
49
|
+
Style/BlockDelimiters:
|
|
50
|
+
Enabled: false
|
|
51
|
+
|
|
52
|
+
Style/DoubleNegation:
|
|
53
|
+
Enabled: false
|
|
54
|
+
|
|
55
|
+
Style/GuardClause:
|
|
56
|
+
Enabled: false
|
|
57
|
+
|
|
58
|
+
Style/ClassAndModuleChildren:
|
|
59
|
+
Enabled: false
|
|
60
|
+
|
|
61
|
+
Style/SpecialGlobalVars:
|
|
62
|
+
Enabled: false
|
|
63
|
+
|
|
64
|
+
Style/NumericPredicate:
|
|
65
|
+
Enabled: false
|
|
66
|
+
|
|
67
|
+
Style/Lambda:
|
|
68
|
+
Enabled: false
|
|
69
|
+
|
|
70
|
+
Layout/AlignParameters:
|
|
71
|
+
EnforcedStyle: with_fixed_indentation
|
|
72
|
+
|
|
73
|
+
Layout/MultilineMethodCallIndentation:
|
|
74
|
+
EnforcedStyle: indented
|
|
75
|
+
|
|
76
|
+
Lint/AmbiguousRegexpLiteral:
|
|
77
|
+
Enabled: false
|
data/.spec_helper
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--require spec_helper
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
rails_master_key_kms_decrypter (0.1.0)
|
|
5
|
+
aws-sdk-kms
|
|
6
|
+
rails (>= 5.2.0.rc1)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
actioncable (5.2.0.rc1)
|
|
12
|
+
actionpack (= 5.2.0.rc1)
|
|
13
|
+
nio4r (~> 2.0)
|
|
14
|
+
websocket-driver (>= 0.6.1)
|
|
15
|
+
actionmailer (5.2.0.rc1)
|
|
16
|
+
actionpack (= 5.2.0.rc1)
|
|
17
|
+
actionview (= 5.2.0.rc1)
|
|
18
|
+
activejob (= 5.2.0.rc1)
|
|
19
|
+
mail (~> 2.5, >= 2.5.4)
|
|
20
|
+
rails-dom-testing (~> 2.0)
|
|
21
|
+
actionpack (5.2.0.rc1)
|
|
22
|
+
actionview (= 5.2.0.rc1)
|
|
23
|
+
activesupport (= 5.2.0.rc1)
|
|
24
|
+
rack (~> 2.0)
|
|
25
|
+
rack-test (>= 0.6.3)
|
|
26
|
+
rails-dom-testing (~> 2.0)
|
|
27
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
|
28
|
+
actionview (5.2.0.rc1)
|
|
29
|
+
activesupport (= 5.2.0.rc1)
|
|
30
|
+
builder (~> 3.1)
|
|
31
|
+
erubi (~> 1.4)
|
|
32
|
+
rails-dom-testing (~> 2.0)
|
|
33
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
|
34
|
+
activejob (5.2.0.rc1)
|
|
35
|
+
activesupport (= 5.2.0.rc1)
|
|
36
|
+
globalid (>= 0.3.6)
|
|
37
|
+
activemodel (5.2.0.rc1)
|
|
38
|
+
activesupport (= 5.2.0.rc1)
|
|
39
|
+
activerecord (5.2.0.rc1)
|
|
40
|
+
activemodel (= 5.2.0.rc1)
|
|
41
|
+
activesupport (= 5.2.0.rc1)
|
|
42
|
+
arel (>= 9.0)
|
|
43
|
+
activestorage (5.2.0.rc1)
|
|
44
|
+
actionpack (= 5.2.0.rc1)
|
|
45
|
+
activerecord (= 5.2.0.rc1)
|
|
46
|
+
marcel (~> 0.3.1)
|
|
47
|
+
activesupport (5.2.0.rc1)
|
|
48
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
49
|
+
i18n (~> 0.7)
|
|
50
|
+
minitest (~> 5.1)
|
|
51
|
+
tzinfo (~> 1.1)
|
|
52
|
+
arel (9.0.0)
|
|
53
|
+
ast (2.4.0)
|
|
54
|
+
aws-partitions (1.63.0)
|
|
55
|
+
aws-sdk-core (3.15.0)
|
|
56
|
+
aws-partitions (~> 1.0)
|
|
57
|
+
aws-sigv4 (~> 1.0)
|
|
58
|
+
jmespath (~> 1.0)
|
|
59
|
+
aws-sdk-kms (1.5.0)
|
|
60
|
+
aws-sdk-core (~> 3)
|
|
61
|
+
aws-sigv4 (~> 1.0)
|
|
62
|
+
aws-sigv4 (1.0.2)
|
|
63
|
+
builder (3.2.3)
|
|
64
|
+
coderay (1.1.2)
|
|
65
|
+
concurrent-ruby (1.0.5)
|
|
66
|
+
crass (1.0.3)
|
|
67
|
+
diff-lcs (1.3)
|
|
68
|
+
erubi (1.7.0)
|
|
69
|
+
globalid (0.4.1)
|
|
70
|
+
activesupport (>= 4.2.0)
|
|
71
|
+
i18n (0.9.5)
|
|
72
|
+
concurrent-ruby (~> 1.0)
|
|
73
|
+
jmespath (1.3.1)
|
|
74
|
+
loofah (2.2.0)
|
|
75
|
+
crass (~> 1.0.2)
|
|
76
|
+
nokogiri (>= 1.5.9)
|
|
77
|
+
mail (2.7.0)
|
|
78
|
+
mini_mime (>= 0.1.1)
|
|
79
|
+
marcel (0.3.1)
|
|
80
|
+
mimemagic (~> 0.3.2)
|
|
81
|
+
method_source (0.9.0)
|
|
82
|
+
mimemagic (0.3.2)
|
|
83
|
+
mini_mime (1.0.0)
|
|
84
|
+
mini_portile2 (2.3.0)
|
|
85
|
+
minitest (5.11.3)
|
|
86
|
+
nio4r (2.2.0)
|
|
87
|
+
nokogiri (1.8.2)
|
|
88
|
+
mini_portile2 (~> 2.3.0)
|
|
89
|
+
parallel (1.12.1)
|
|
90
|
+
parser (2.5.0.0)
|
|
91
|
+
ast (~> 2.4.0)
|
|
92
|
+
powerpack (0.1.1)
|
|
93
|
+
pry (0.11.3)
|
|
94
|
+
coderay (~> 1.1.0)
|
|
95
|
+
method_source (~> 0.9.0)
|
|
96
|
+
rack (2.0.4)
|
|
97
|
+
rack-test (0.8.2)
|
|
98
|
+
rack (>= 1.0, < 3)
|
|
99
|
+
rails (5.2.0.rc1)
|
|
100
|
+
actioncable (= 5.2.0.rc1)
|
|
101
|
+
actionmailer (= 5.2.0.rc1)
|
|
102
|
+
actionpack (= 5.2.0.rc1)
|
|
103
|
+
actionview (= 5.2.0.rc1)
|
|
104
|
+
activejob (= 5.2.0.rc1)
|
|
105
|
+
activemodel (= 5.2.0.rc1)
|
|
106
|
+
activerecord (= 5.2.0.rc1)
|
|
107
|
+
activestorage (= 5.2.0.rc1)
|
|
108
|
+
activesupport (= 5.2.0.rc1)
|
|
109
|
+
bundler (>= 1.3.0)
|
|
110
|
+
railties (= 5.2.0.rc1)
|
|
111
|
+
sprockets-rails (>= 2.0.0)
|
|
112
|
+
rails-dom-testing (2.0.3)
|
|
113
|
+
activesupport (>= 4.2.0)
|
|
114
|
+
nokogiri (>= 1.6)
|
|
115
|
+
rails-html-sanitizer (1.0.3)
|
|
116
|
+
loofah (~> 2.0)
|
|
117
|
+
railties (5.2.0.rc1)
|
|
118
|
+
actionpack (= 5.2.0.rc1)
|
|
119
|
+
activesupport (= 5.2.0.rc1)
|
|
120
|
+
method_source
|
|
121
|
+
rake (>= 0.8.7)
|
|
122
|
+
thor (>= 0.18.1, < 2.0)
|
|
123
|
+
rainbow (3.0.0)
|
|
124
|
+
rake (10.5.0)
|
|
125
|
+
rspec (3.7.0)
|
|
126
|
+
rspec-core (~> 3.7.0)
|
|
127
|
+
rspec-expectations (~> 3.7.0)
|
|
128
|
+
rspec-mocks (~> 3.7.0)
|
|
129
|
+
rspec-core (3.7.1)
|
|
130
|
+
rspec-support (~> 3.7.0)
|
|
131
|
+
rspec-expectations (3.7.0)
|
|
132
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
133
|
+
rspec-support (~> 3.7.0)
|
|
134
|
+
rspec-mocks (3.7.0)
|
|
135
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
136
|
+
rspec-support (~> 3.7.0)
|
|
137
|
+
rspec-support (3.7.1)
|
|
138
|
+
rubocop (0.52.1)
|
|
139
|
+
parallel (~> 1.10)
|
|
140
|
+
parser (>= 2.4.0.2, < 3.0)
|
|
141
|
+
powerpack (~> 0.1)
|
|
142
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
143
|
+
ruby-progressbar (~> 1.7)
|
|
144
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
|
145
|
+
ruby-progressbar (1.9.0)
|
|
146
|
+
sprockets (3.7.1)
|
|
147
|
+
concurrent-ruby (~> 1.0)
|
|
148
|
+
rack (> 1, < 3)
|
|
149
|
+
sprockets-rails (3.2.1)
|
|
150
|
+
actionpack (>= 4.0)
|
|
151
|
+
activesupport (>= 4.0)
|
|
152
|
+
sprockets (>= 3.0.0)
|
|
153
|
+
thor (0.20.0)
|
|
154
|
+
thread_safe (0.3.6)
|
|
155
|
+
tzinfo (1.2.5)
|
|
156
|
+
thread_safe (~> 0.1)
|
|
157
|
+
unicode-display_width (1.3.0)
|
|
158
|
+
websocket-driver (0.7.0)
|
|
159
|
+
websocket-extensions (>= 0.1.0)
|
|
160
|
+
websocket-extensions (0.1.3)
|
|
161
|
+
|
|
162
|
+
PLATFORMS
|
|
163
|
+
ruby
|
|
164
|
+
|
|
165
|
+
DEPENDENCIES
|
|
166
|
+
bundler (~> 1.16)
|
|
167
|
+
pry
|
|
168
|
+
rails_master_key_kms_decrypter!
|
|
169
|
+
rake (~> 10.0)
|
|
170
|
+
rspec (~> 3.0)
|
|
171
|
+
rubocop
|
|
172
|
+
|
|
173
|
+
BUNDLED WITH
|
|
174
|
+
1.16.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 alpaca-tc
|
|
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,94 @@
|
|
|
1
|
+
# RailsMasterKeyKmsDecrypter
|
|
2
|
+
|
|
3
|
+
Dynamic decryptier of encrypted `config/master.key` on EC2.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Rails5.2 introduced [encrypted credentials](https://www.engineyard.com/blog/rails-encrypted-credentials-on-rails-5.2) 🙌
|
|
8
|
+
|
|
9
|
+
The key, located on `config/master.key` is created when you run rails new. It doesn't get committed to your repository.
|
|
10
|
+
|
|
11
|
+
If you using AWS and this gem, you can encrypt `config/master.key` to commit it.
|
|
12
|
+
After encryping the key, the encrypted key will be saved to `config/master.key.enc`.
|
|
13
|
+
|
|
14
|
+
The default rails credential decryptor decrypts `config/credentials.yml.enc` from raw master key.
|
|
15
|
+
After adding this gem, rails decrypts it from encrypted master key.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Add this line to your application's Gemfile:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
gem 'rails_master_key_kms_decrypter' # Recommended: `group: 'production'`
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Rails Application
|
|
28
|
+
|
|
29
|
+
#### 1. Get your KMS key-id from AWS
|
|
30
|
+
|
|
31
|
+
Create the key at your region on [KMS](https://console.aws.amazon.com/iam/home#/encryptionKeys/)
|
|
32
|
+
|
|
33
|
+
#### 2. Encrypt your master.key or `RAILS_MASTER_KEY`
|
|
34
|
+
|
|
35
|
+
Encrypt your `config/master.key`
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
# Create `config/master.key.enc`
|
|
39
|
+
aws kms encrypt --key-id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx --plaintext fileb://config/master.key --output text --query CiphertextBlob > config/master.key.enc
|
|
40
|
+
git add config/master.key.enc
|
|
41
|
+
git commit
|
|
42
|
+
|
|
43
|
+
# or define `ENCRYPTED_RAILS_MASTER_KEY`
|
|
44
|
+
ENCRYPTED_RAILS_MASTER_KEY=$(aws kms encrypt --key-id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx --plaintext fileb://config/master.key --output text --query CiphertextBlob)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
#### 3. Try to decrypt encrypted credentials
|
|
48
|
+
|
|
49
|
+
When rails credential decryption is succeeded, `rails_master_key_kms_decrypter` is ready.
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
ENCRYPTED_RAILS_MASTER_KEY=... ./bin/rails runner 'Rails.application.credentials.config.present? ? puts("👍") : puts("👎")'
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### AWS resource
|
|
56
|
+
|
|
57
|
+
#### 1. Create the policy to allow access to KMS
|
|
58
|
+
|
|
59
|
+
[Create policy](https://console.aws.amazon.com/iam/home#/policies$new)
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
# Resource is your key
|
|
63
|
+
{
|
|
64
|
+
"Version": "2012-10-17",
|
|
65
|
+
"Statement": [
|
|
66
|
+
{
|
|
67
|
+
"Sid": "",
|
|
68
|
+
"Effect": "Allow",
|
|
69
|
+
"Action": "kms:Decrypt",
|
|
70
|
+
"Resource": "arn:aws:kms:ap-northeast-1:012345678900:key/1234567a-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### 2. Create the role to allow access to KMS
|
|
77
|
+
|
|
78
|
+
[Create role](https://console.aws.amazon.com/iam/home#/roles$new)
|
|
79
|
+
Choose your resource and attach the policy from before.
|
|
80
|
+
|
|
81
|
+
#### 3. Attach IAM role to EC2
|
|
82
|
+
|
|
83
|
+
[Create EC2](https://ap-northeast-1.console.aws.amazon.com/ec2/v2/home?#LaunchInstanceWizard:) and deploy your application.
|
|
84
|
+
|
|
85
|
+
`RailsMasterKeyKmsDecrypter` need region information.
|
|
86
|
+
Please set `ENV["AWS_REGION"]` or `ENV["RAILS_MASTER_KEY_KMS_DECRYPTER_AWS_REGION"]`.
|
|
87
|
+
|
|
88
|
+
## Contributing
|
|
89
|
+
|
|
90
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/alpaca-tc/rails_master_key_kms_decrypter.
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "rails_master_key_kms_decrypter"
|
|
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
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsMasterKeyKmsDecrypter
|
|
4
|
+
require 'rails_master_key_kms_decrypter/version'
|
|
5
|
+
require 'rails_master_key_kms_decrypter/decryption'
|
|
6
|
+
require 'rails_master_key_kms_decrypter/railtie'
|
|
7
|
+
require 'rails_master_key_kms_decrypter/kms_encrypted_configuration'
|
|
8
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'base64'
|
|
4
|
+
require 'aws-sdk-kms'
|
|
5
|
+
|
|
6
|
+
module RailsMasterKeyKmsDecrypter
|
|
7
|
+
class Decryption
|
|
8
|
+
attr_reader :client
|
|
9
|
+
|
|
10
|
+
def initialize(client_options)
|
|
11
|
+
@client = Aws::KMS::Client.new(client_options)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def decrypt(value, **options)
|
|
15
|
+
decoded = Base64.strict_decode64(value)
|
|
16
|
+
response = client.decrypt(ciphertext_blob: decoded, **options)
|
|
17
|
+
response.plaintext
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsMasterKeyKmsDecrypter
|
|
4
|
+
class KmsEncryptedConfiguration < ActiveSupport::EncryptedConfiguration
|
|
5
|
+
attr_reader :encrypted_env_key, :encrypted_key_path
|
|
6
|
+
|
|
7
|
+
# def initialize(config_path:, key_path:, env_key:, raise_if_missing_key:)
|
|
8
|
+
def initialize(*)
|
|
9
|
+
super
|
|
10
|
+
|
|
11
|
+
@encrypted_env_key = "ENCRYPTED_#{env_key}"
|
|
12
|
+
@encrypted_key_path = "#{key_path}.enc"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def key
|
|
16
|
+
read_encrypted_env_key || read_encrypted_key_file || super
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def read_encrypted_env_key
|
|
22
|
+
from_encrypted_key(ENV[encrypted_env_key]) if ENV[encrypted_env_key]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def read_encrypted_key_file
|
|
26
|
+
return unless File.exist?(encrypted_key_path)
|
|
27
|
+
from_encrypted_key(IO.binread(encrypted_key_path).strip)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def from_encrypted_key(value)
|
|
31
|
+
decrypt_master_key(value) if value
|
|
32
|
+
rescue StandardError
|
|
33
|
+
nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def decrypt_master_key(value)
|
|
37
|
+
region = ENV['RAILS_MASTER_KEY_KMS_DECRYPTER_AWS_REGION'] || ENV['AWS_REGION']
|
|
38
|
+
RailsMasterKeyKmsDecrypter::Decryption.new(region: region).decrypt(value).strip
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rails'
|
|
4
|
+
|
|
5
|
+
module RailsMasterKeyKmsDecrypter
|
|
6
|
+
module WithKmsEncryptedConfiguration
|
|
7
|
+
def encrypted(path, key_path: 'config/master.key', env_key: 'RAILS_MASTER_KEY')
|
|
8
|
+
RailsMasterKeyKmsDecrypter::KmsEncryptedConfiguration.new(
|
|
9
|
+
config_path: Rails.root.join(path),
|
|
10
|
+
key_path: Rails.root.join(key_path),
|
|
11
|
+
env_key: env_key,
|
|
12
|
+
raise_if_missing_key: config.require_master_key
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class Railtie < ::Rails::Railtie
|
|
18
|
+
::Rails::Application.prepend(WithKmsEncryptedConfiguration)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
6
|
+
require 'rails_master_key_kms_decrypter/version'
|
|
7
|
+
|
|
8
|
+
Gem::Specification.new do |spec|
|
|
9
|
+
spec.name = 'rails_master_key_kms_decrypter'
|
|
10
|
+
spec.version = RailsMasterKeyKmsDecrypter::VERSION
|
|
11
|
+
spec.authors = ['alpaca-tc']
|
|
12
|
+
spec.email = ['alpaca-tc@alpaca.tc']
|
|
13
|
+
|
|
14
|
+
spec.summary = 'AWS KMS decryption for rails credentials'
|
|
15
|
+
spec.description = 'AWS KMS decryption for rails credentials'
|
|
16
|
+
spec.homepage = 'https://github.com/alpaca-tc/rails_master_key_kms_decrypter'
|
|
17
|
+
spec.license = 'MIT'
|
|
18
|
+
|
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
spec.require_paths = ['lib']
|
|
24
|
+
|
|
25
|
+
spec.add_dependency 'aws-sdk-kms'
|
|
26
|
+
spec.add_dependency 'rails', '>= 5.2.0.rc1'
|
|
27
|
+
|
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
31
|
+
spec.add_development_dependency 'rubocop'
|
|
32
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rails_master_key_kms_decrypter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- alpaca-tc
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-02-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: aws-sdk-kms
|
|
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: rails
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 5.2.0.rc1
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 5.2.0.rc1
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.16'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.16'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '10.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '10.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '3.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
|
+
description: AWS KMS decryption for rails credentials
|
|
98
|
+
email:
|
|
99
|
+
- alpaca-tc@alpaca.tc
|
|
100
|
+
executables: []
|
|
101
|
+
extensions: []
|
|
102
|
+
extra_rdoc_files: []
|
|
103
|
+
files:
|
|
104
|
+
- ".gitignore"
|
|
105
|
+
- ".rspec"
|
|
106
|
+
- ".rubocop.yml"
|
|
107
|
+
- ".spec_helper"
|
|
108
|
+
- ".travis.yml"
|
|
109
|
+
- Gemfile
|
|
110
|
+
- Gemfile.lock
|
|
111
|
+
- LICENSE.txt
|
|
112
|
+
- README.md
|
|
113
|
+
- Rakefile
|
|
114
|
+
- bin/console
|
|
115
|
+
- bin/setup
|
|
116
|
+
- lib/rails_master_key_kms_decrypter.rb
|
|
117
|
+
- lib/rails_master_key_kms_decrypter/decryption.rb
|
|
118
|
+
- lib/rails_master_key_kms_decrypter/kms_encrypted_configuration.rb
|
|
119
|
+
- lib/rails_master_key_kms_decrypter/railtie.rb
|
|
120
|
+
- lib/rails_master_key_kms_decrypter/version.rb
|
|
121
|
+
- rails_master_key_kms_decrypter.gemspec
|
|
122
|
+
homepage: https://github.com/alpaca-tc/rails_master_key_kms_decrypter
|
|
123
|
+
licenses:
|
|
124
|
+
- MIT
|
|
125
|
+
metadata: {}
|
|
126
|
+
post_install_message:
|
|
127
|
+
rdoc_options: []
|
|
128
|
+
require_paths:
|
|
129
|
+
- lib
|
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
131
|
+
requirements:
|
|
132
|
+
- - ">="
|
|
133
|
+
- !ruby/object:Gem::Version
|
|
134
|
+
version: '0'
|
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
|
+
requirements:
|
|
137
|
+
- - ">="
|
|
138
|
+
- !ruby/object:Gem::Version
|
|
139
|
+
version: '0'
|
|
140
|
+
requirements: []
|
|
141
|
+
rubyforge_project:
|
|
142
|
+
rubygems_version: 2.7.3
|
|
143
|
+
signing_key:
|
|
144
|
+
specification_version: 4
|
|
145
|
+
summary: AWS KMS decryption for rails credentials
|
|
146
|
+
test_files: []
|