kms_encrypted 0.3.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -7
- data/LICENSE.txt +1 -1
- data/README.md +499 -15
- data/lib/kms_encrypted.rb +27 -2
- data/lib/kms_encrypted/box.rb +91 -0
- data/lib/kms_encrypted/client.rb +69 -0
- data/lib/kms_encrypted/clients/aws.rb +36 -0
- data/lib/kms_encrypted/clients/base.rb +45 -0
- data/lib/kms_encrypted/clients/google.rb +40 -0
- data/lib/kms_encrypted/clients/test.rb +29 -0
- data/lib/kms_encrypted/clients/vault.rb +48 -0
- data/lib/kms_encrypted/database.rb +63 -0
- data/lib/kms_encrypted/log_subscriber.rb +14 -6
- data/lib/kms_encrypted/model.rb +143 -128
- data/lib/kms_encrypted/version.rb +1 -1
- metadata +45 -19
- data/.gitignore +0 -9
- data/.travis.yml +0 -11
- data/Gemfile +0 -3
- data/Rakefile +0 -11
- data/guides/Amazon.md +0 -262
- data/guides/Google.md +0 -131
- data/guides/Vault.md +0 -143
- data/kms_encrypted.gemspec +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0f09d385a70037946d973c086b0eea95497df19cc8b572b899bdbb6188b55e2
|
4
|
+
data.tar.gz: 2165ab882704315f6321cdac7544ed7db5a1da256187f954d292b92b14dbb35b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 341db4f5ec475a23fce65729360fe625b8e7f6905f870654ff668e8651eb89d03852a0073a53c1c2c9200cd5b33da347d8236a1430b80cdf6c46f6835d995e63
|
7
|
+
data.tar.gz: ef0f312ed2695cb1f658968cd15349e9dc1d56982e99b8411e8070d0daa6cbd3e025cd613ddd920e6688f012dc8ad587c472f3f8377031e501ed62b8bd9be663
|
data/CHANGELOG.md
CHANGED
@@ -1,35 +1,65 @@
|
|
1
|
-
##
|
1
|
+
## 1.2.0 (2020-08-18)
|
2
|
+
|
3
|
+
- Raise error when trying to rotate key used for encrypted files
|
4
|
+
|
5
|
+
## 1.1.1 (2020-04-16)
|
6
|
+
|
7
|
+
- Fixed `SystemStackError` with `reload` and CarrierWave
|
8
|
+
|
9
|
+
## 1.1.0 (2019-07-09)
|
10
|
+
|
11
|
+
- Added support for Lockbox
|
12
|
+
- Dropped support for Rails 4.2
|
13
|
+
|
14
|
+
## 1.0.1 (2019-01-21)
|
15
|
+
|
16
|
+
- Added support for encryption and decryption outside models
|
17
|
+
- Added support for dynamic keys
|
18
|
+
- Fixed issue with inheritance
|
19
|
+
|
20
|
+
## 1.0.0 (2018-12-17)
|
21
|
+
|
22
|
+
- Added versioning
|
23
|
+
- Added `context_hash` method
|
24
|
+
|
25
|
+
Breaking changes
|
26
|
+
|
27
|
+
- There’s now a default encryption context with the model name and id
|
28
|
+
- ActiveSupport notifications were changed from `generate_data_key` and `decrypt_data_key` to `encrypt` and `decrypt`
|
29
|
+
- AWS KMS uses the `Encrypt` operation instead of `GenerateDataKey`
|
30
|
+
|
31
|
+
## 0.3.0 (2018-11-11)
|
2
32
|
|
3
33
|
- Added support for Vault
|
4
34
|
- Removed `KmsEncrypted.kms_client` and `KmsEncrypted.client_options` in favor of `KmsEncrypted.aws_client`
|
5
35
|
- Removed `KmsEncrypted::Google.kms_client` in favor of `KmsEncrypted.google_client`
|
6
36
|
|
7
|
-
## 0.2.0
|
37
|
+
## 0.2.0 (2018-02-23)
|
8
38
|
|
9
39
|
- Added support for Google KMS
|
10
40
|
|
11
|
-
## 0.1.4
|
41
|
+
## 0.1.4 (2017-12-03)
|
12
42
|
|
13
43
|
- Added `kms_keys` method to models
|
14
44
|
- Reset data keys when record is reloaded
|
15
45
|
- Added `kms_client`
|
16
46
|
- Added ActiveSupport notifications
|
17
47
|
|
18
|
-
## 0.1.3
|
48
|
+
## 0.1.3 (2017-12-01)
|
19
49
|
|
20
50
|
- Added test key
|
21
51
|
- Added `client_options`
|
22
52
|
- Allow private or protected `kms_encryption_context` method
|
23
53
|
|
24
|
-
## 0.1.2
|
54
|
+
## 0.1.2 (2017-09-25)
|
25
55
|
|
26
56
|
- Use `KMS_KEY_ID` env variable by default
|
27
57
|
|
28
|
-
## 0.1.1
|
58
|
+
## 0.1.1 (2017-09-23)
|
29
59
|
|
30
60
|
- Added key rotation
|
31
61
|
- Added support for multiple keys per record
|
32
62
|
|
33
|
-
## 0.1.0
|
63
|
+
## 0.1.0 (2017-09-23)
|
34
64
|
|
35
65
|
- First release
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,40 +1,515 @@
|
|
1
1
|
# KMS Encrypted
|
2
2
|
|
3
|
-
Simple, secure key management for [attr_encrypted](https://github.com/attr-encrypted/attr_encrypted)
|
3
|
+
Simple, secure key management for [Lockbox](https://github.com/ankane/lockbox) and [attr_encrypted](https://github.com/attr-encrypted/attr_encrypted)
|
4
4
|
|
5
|
-
|
5
|
+
With KMS Encrypted:
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
- Master encryption keys are not on application servers
|
8
|
+
- Encrypt and decrypt permissions can be granted separately
|
9
|
+
- There’s an immutable audit log of all activity
|
10
|
+
- Decryption can be disabled if an attack is detected
|
11
|
+
- It’s easy to rotate keys
|
11
12
|
|
12
|
-
|
13
|
+
Supports [AWS KMS](https://aws.amazon.com/kms/), [Google Cloud KMS](https://cloud.google.com/kms/), and [Vault](https://www.vaultproject.io/)
|
13
14
|
|
14
|
-
|
15
|
+
Check out [this post](https://ankane.org/sensitive-data-rails) for more info on securing sensitive data with Rails
|
15
16
|
|
16
17
|
[![Build Status](https://travis-ci.org/ankane/kms_encrypted.svg?branch=master)](https://travis-ci.org/ankane/kms_encrypted)
|
17
18
|
|
18
19
|
## How It Works
|
19
20
|
|
20
|
-
This approach uses KMS to manage encryption keys and attr_encrypted to do the encryption.
|
21
|
+
This approach uses a key management service (KMS) to manage encryption keys and Lockbox / attr_encrypted to do the encryption.
|
21
22
|
|
22
|
-
To encrypt an attribute, we first generate a data key and encrypt it with KMS. This is known as [envelope encryption](https://cloud.google.com/kms/docs/envelope-encryption). We pass the unencrypted version to
|
23
|
+
To encrypt an attribute, we first generate a data key and encrypt it with the KMS. This is known as [envelope encryption](https://cloud.google.com/kms/docs/envelope-encryption). We pass the unencrypted version to the encryption library and store the encrypted version in the `encrypted_kms_key` column. For each record, we generate a different data key.
|
23
24
|
|
24
|
-
To decrypt an attribute, we first decrypt the data key with KMS. Once we have the decrypted key, we pass it to
|
25
|
+
To decrypt an attribute, we first decrypt the data key with the KMS. Once we have the decrypted key, we pass it to the encryption library to decrypt the data. We can easily track decryptions since we have a different data key for each record.
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Add this line to your application’s Gemfile:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gem 'kms_encrypted'
|
33
|
+
```
|
34
|
+
|
35
|
+
And follow the instructions for your key management service:
|
36
|
+
|
37
|
+
- [AWS KMS](#aws-kms)
|
38
|
+
- [Google Cloud KMS](#google-cloud-kms)
|
39
|
+
- [Vault](#vault)
|
40
|
+
|
41
|
+
### AWS KMS
|
42
|
+
|
43
|
+
Add this line to your application’s Gemfile:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
gem 'aws-sdk-kms'
|
47
|
+
```
|
48
|
+
|
49
|
+
Create an [Amazon Web Services](https://aws.amazon.com/) account if you don’t have one. KMS works great whether or not you run your infrastructure on AWS.
|
50
|
+
|
51
|
+
Create a [KMS master key](https://console.aws.amazon.com/iam/home#/encryptionKeys) and set it in your environment along with your AWS credentials ([dotenv](https://github.com/bkeepers/dotenv) is great for this)
|
52
|
+
|
53
|
+
```sh
|
54
|
+
KMS_KEY_ID=arn:aws:kms:...
|
55
|
+
AWS_ACCESS_KEY_ID=...
|
56
|
+
AWS_SECRET_ACCESS_KEY=...
|
57
|
+
```
|
58
|
+
|
59
|
+
You can also use the alias
|
60
|
+
|
61
|
+
```sh
|
62
|
+
KMS_KEY_ID=alias/my-alias
|
63
|
+
```
|
64
|
+
|
65
|
+
### Google Cloud KMS
|
66
|
+
|
67
|
+
Add this line to your application’s Gemfile:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
gem 'google-api-client'
|
71
|
+
```
|
72
|
+
|
73
|
+
Create a [Google Cloud Platform](https://cloud.google.com/) account if you don’t have one. KMS works great whether or not you run your infrastructure on GCP.
|
74
|
+
|
75
|
+
Create a [KMS key ring and key](https://console.cloud.google.com/iam-admin/kms) and set it in your environment along with your GCP credentials ([dotenv](https://github.com/bkeepers/dotenv) is great for this)
|
76
|
+
|
77
|
+
```sh
|
78
|
+
KMS_KEY_ID=projects/.../locations/.../keyRings/.../cryptoKeys/...
|
79
|
+
```
|
80
|
+
|
81
|
+
The Google API client logs requests by default. Be sure to turn off the logger in production or it will leak the plaintext.
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
Google::Apis.logger = Logger.new(nil)
|
85
|
+
```
|
86
|
+
|
87
|
+
### Vault
|
88
|
+
|
89
|
+
Add this line to your application’s Gemfile:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
gem 'vault'
|
93
|
+
```
|
94
|
+
|
95
|
+
Enable the [transit](https://www.vaultproject.io/docs/secrets/transit/index.html) secrets engine
|
96
|
+
|
97
|
+
```sh
|
98
|
+
vault secrets enable transit
|
99
|
+
```
|
100
|
+
|
101
|
+
And create a key
|
102
|
+
|
103
|
+
```sh
|
104
|
+
vault write -f transit/keys/my-key derived=true
|
105
|
+
```
|
106
|
+
|
107
|
+
Set it in your environment along with your Vault credentials ([dotenv](https://github.com/bkeepers/dotenv) is great for this)
|
108
|
+
|
109
|
+
```sh
|
110
|
+
KMS_KEY_ID=vault/my-key
|
111
|
+
VAULT_ADDR=http://127.0.0.1:8200
|
112
|
+
VAULT_TOKEN=secret
|
113
|
+
```
|
25
114
|
|
26
115
|
## Getting Started
|
27
116
|
|
28
|
-
|
117
|
+
Create a migration to add a column for the encrypted KMS data keys
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
add_column :users, :encrypted_kms_key, :text
|
121
|
+
```
|
122
|
+
|
123
|
+
And update your model
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
class User < ApplicationRecord
|
127
|
+
has_kms_key
|
128
|
+
|
129
|
+
# Lockbox fields
|
130
|
+
encrypts :email, key: :kms_key
|
131
|
+
|
132
|
+
# Lockbox files
|
133
|
+
encrypts_attached :license, key: :kms_key
|
134
|
+
|
135
|
+
# attr_encrypted fields
|
136
|
+
attr_encrypted :email, key: :kms_key
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
140
|
+
For each encrypted attribute, use the `kms_key` method for its key.
|
141
|
+
|
142
|
+
## Auditing & Alerting
|
143
|
+
|
144
|
+
### Context
|
145
|
+
|
146
|
+
Encryption context is used in auditing to identify the data being decrypted. This is the model name and id by default. You can customize this with:
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
class User < ApplicationRecord
|
150
|
+
def kms_encryption_context
|
151
|
+
{
|
152
|
+
model_name: model_name.to_s,
|
153
|
+
model_id: id
|
154
|
+
}
|
155
|
+
end
|
156
|
+
end
|
157
|
+
```
|
158
|
+
|
159
|
+
The context is used as part of the encryption and decryption process, so it must be a value that doesn’t change. Otherwise, you won’t be able to decrypt. You can [rotate the context](#switching-context) without downtime if needed.
|
160
|
+
|
161
|
+
### Order of Events
|
162
|
+
|
163
|
+
Since the default context includes the id, the data key cannot be encrypted until the record has an id. For new records, the default flow is:
|
164
|
+
|
165
|
+
1. Start a database transaction
|
166
|
+
2. Insert the record, getting back the id
|
167
|
+
3. Call KMS to encrypt the data key, passing the id as part of the context
|
168
|
+
4. Update the `encrypted_kms_key` column
|
169
|
+
5. Commit the database transaction
|
170
|
+
|
171
|
+
With Postgres, you can avoid a network call inside a transaction with:
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
class User < ApplicationRecord
|
175
|
+
has_kms_key eager_encrypt: :fetch_id
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
This changes the flow to:
|
180
|
+
|
181
|
+
1. Prefetch the id with the Postgres `nextval` function
|
182
|
+
2. Call KMS to encrypt the data key, passing the id as part of the context
|
183
|
+
3. Insert the record with the id and encrypted data key
|
184
|
+
|
185
|
+
If you don’t need the id from the database for context, you can use:
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
class User < ApplicationRecord
|
189
|
+
has_kms_key eager_encrypt: true
|
190
|
+
end
|
191
|
+
```
|
192
|
+
|
193
|
+
### AWS KMS
|
194
|
+
|
195
|
+
[AWS CloudTrail](https://aws.amazon.com/cloudtrail/) logs all decryption calls. You can view them in the [CloudTrail console](https://console.aws.amazon.com/cloudtrail/home#/events?EventName=Decrypt). Note that it can take 20 minutes for events to show up. You can also use the AWS CLI.
|
196
|
+
|
197
|
+
```sh
|
198
|
+
aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=Decrypt
|
199
|
+
```
|
200
|
+
|
201
|
+
If you haven’t already, enable CloudTrail storage to S3 to ensure events are accessible after 90 days. Later, you can use Amazon Athena and this [table structure](https://www.1strategy.com/blog/2017/07/25/auditing-aws-activity-with-cloudtrail-and-athena/) to query them.
|
202
|
+
|
203
|
+
Read more about [encryption context here](https://docs.aws.amazon.com/kms/latest/developerguide/encryption-context.html).
|
204
|
+
|
205
|
+
#### Alerting
|
206
|
+
|
207
|
+
Set up alerts for suspicious behavior. To get near real-time alerts (20-30 second delay), use CloudWatch Events.
|
208
|
+
|
209
|
+
First, create a new SNS topic with a name like "decryptions". We’ll use this shortly.
|
210
|
+
|
211
|
+
Next, open [CloudWatch Events](https://console.aws.amazon.com/cloudwatch/home#rules:) and create a rule to match “Events by Service”. Choose “Key Management Service (KMS)” as the service name and “AWS API Call via CloudTrail” as the event type. For operations, select “Specific Operations” and enter “Decrypt”.
|
212
|
+
|
213
|
+
Select the SNS topic created earlier as the target and save the rule.
|
214
|
+
|
215
|
+
To set up an alarm, go to [this page](https://console.aws.amazon.com/cloudwatch/home?#metricsV2:graph=%7E();namespace=AWS/Events;dimensions=RuleName) in CloudWatch Metrics. Find the rule and check “Invocations”. On the “Graphed Metrics” tab, change the statistic to “Sum” and the period to “1 minute”. Finally, click the bell icon to create an alarm for high number of decryptions.
|
216
|
+
|
217
|
+
While the alarm we created isn’t super sophisticated, this setup provides a great foundation for alerting as your organization grows.
|
218
|
+
|
219
|
+
You can use the SNS topic or another target to send events to a log provider or [SIEM](https://en.wikipedia.org/wiki/Security_information_and_event_management), where can you do more advanced anomaly detection.
|
220
|
+
|
221
|
+
You should also use other tools to detect breaches, like an [IDS](https://www.alienvault.com/blogs/security-essentials/open-source-intrusion-detection-tools-a-quick-overview). You can use [Amazon GuardDuty](https://aws.amazon.com/guardduty/) if you run infrastructure on AWS.
|
222
|
+
|
223
|
+
### Google Cloud KMS
|
224
|
+
|
225
|
+
Follow the [instructions here](https://cloud.google.com/kms/docs/logging) to set up data access logging. There is not currently a way to see what data is being decrypted, since the additional authenticated data is not logged. For this reason, we recommend another KMS provider.
|
226
|
+
|
227
|
+
### Vault
|
228
|
+
|
229
|
+
Follow the [instructions here](https://www.vaultproject.io/docs/audit/) to set up data access logging.
|
230
|
+
|
231
|
+
**Note:** Vault will only verify this value if `derived` was set to true when creating the key. If this is not done, the context cannot be trusted.
|
232
|
+
|
233
|
+
Context will show up hashed in the audit logs. To get the hash for a record, use:
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
KmsEncrypted.context_hash(record.kms_encryption_context, path: "file")
|
237
|
+
```
|
238
|
+
|
239
|
+
The `path` option should point to your audit device. Common paths are `file`, `syslog`, and `socket`.
|
240
|
+
|
241
|
+
## Separate Permissions
|
242
|
+
|
243
|
+
A great feature of KMS is the ability to grant encryption and decryption permission separately.
|
29
244
|
|
30
|
-
|
31
|
-
|
32
|
-
|
245
|
+
Be extremely selective of servers you allow to decrypt.
|
246
|
+
|
247
|
+
For servers that can only encrypt, clear out the existing data and data key before assigning new values (otherwise, you’ll get a decryption error).
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
# Lockbox
|
251
|
+
user.email_ciphertext = nil
|
252
|
+
user.encrypted_kms_key = nil
|
253
|
+
|
254
|
+
# attr_encrypted
|
255
|
+
user.encrypted_email = nil
|
256
|
+
user.encrypted_email_iv = nil
|
257
|
+
user.encrypted_kms_key = nil
|
258
|
+
```
|
259
|
+
|
260
|
+
### AWS KMS
|
261
|
+
|
262
|
+
To encrypt the data, use an IAM policy with:
|
263
|
+
|
264
|
+
```json
|
265
|
+
{
|
266
|
+
"Version": "2012-10-17",
|
267
|
+
"Statement": [
|
268
|
+
{
|
269
|
+
"Sid": "EncryptData",
|
270
|
+
"Effect": "Allow",
|
271
|
+
"Action": "kms:Encrypt",
|
272
|
+
"Resource": "arn:aws:kms:..."
|
273
|
+
}
|
274
|
+
]
|
275
|
+
}
|
276
|
+
```
|
277
|
+
|
278
|
+
To decrypt the data, use an IAM policy with:
|
279
|
+
|
280
|
+
```json
|
281
|
+
{
|
282
|
+
"Version": "2012-10-17",
|
283
|
+
"Statement": [
|
284
|
+
{
|
285
|
+
"Sid": "DecryptData",
|
286
|
+
"Effect": "Allow",
|
287
|
+
"Action": "kms:Decrypt",
|
288
|
+
"Resource": "arn:aws:kms:..."
|
289
|
+
}
|
290
|
+
]
|
291
|
+
}
|
292
|
+
```
|
293
|
+
|
294
|
+
### Google Cloud KMS
|
295
|
+
|
296
|
+
todo: document
|
297
|
+
|
298
|
+
### Vault
|
299
|
+
|
300
|
+
To encrypt the data, use a policy with:
|
301
|
+
|
302
|
+
```hcl
|
303
|
+
path "transit/encrypt/my-key"
|
304
|
+
{
|
305
|
+
capabilities = ["create", "update"]
|
306
|
+
}
|
307
|
+
```
|
308
|
+
|
309
|
+
To decrypt the data, use a policy with:
|
310
|
+
|
311
|
+
```hcl
|
312
|
+
path "transit/decrypt/my-key"
|
313
|
+
{
|
314
|
+
capabilities = ["create", "update"]
|
315
|
+
}
|
316
|
+
```
|
317
|
+
|
318
|
+
Apply a policy with:
|
319
|
+
|
320
|
+
```sh
|
321
|
+
vault policy write encrypt encrypt.hcl
|
322
|
+
```
|
323
|
+
|
324
|
+
And create a token with specific policies with:
|
325
|
+
|
326
|
+
```sh
|
327
|
+
vault token create -policy=encrypt -policy=decrypt -no-default-policy
|
328
|
+
```
|
329
|
+
|
330
|
+
## Testing
|
331
|
+
|
332
|
+
For testing, you can prevent network calls to KMS by setting:
|
333
|
+
|
334
|
+
```sh
|
335
|
+
KMS_KEY_ID=insecure-test-key
|
336
|
+
```
|
337
|
+
|
338
|
+
## Key Rotation
|
339
|
+
|
340
|
+
Key management services allow you to rotate the master key.
|
341
|
+
|
342
|
+
AWS KMS supports [automatic key rotation](https://docs.aws.amazon.com/kms/latest/developerguide/rotate-keys.html).
|
343
|
+
|
344
|
+
For Google Cloud, use the Google Cloud Console or API.
|
345
|
+
|
346
|
+
For Vault, use:
|
347
|
+
|
348
|
+
```sh
|
349
|
+
vault write -f transit/keys/my-key/rotate
|
350
|
+
```
|
351
|
+
|
352
|
+
New data will be encrypted with the new master key version. To encrypt existing data with new master key version, run:
|
353
|
+
|
354
|
+
```ruby
|
355
|
+
User.find_each do |user|
|
356
|
+
user.rotate_kms_key!
|
357
|
+
end
|
358
|
+
```
|
359
|
+
|
360
|
+
**Note:** This method does not rotate encrypted files, so avoid calling `rotate_kms_key!` on models with file uploads for now.
|
361
|
+
|
362
|
+
### Switching Keys
|
363
|
+
|
364
|
+
You can change keys within your current KMS or move to a different KMS without downtime. Update your model:
|
365
|
+
|
366
|
+
```ruby
|
367
|
+
class User < ApplicationRecord
|
368
|
+
has_kms_key version: 2, key_id: ENV["KMS_KEY_ID_V2"],
|
369
|
+
previous_versions: {
|
370
|
+
1 => {key_id: ENV["KMS_KEY_ID"]}
|
371
|
+
}
|
372
|
+
end
|
373
|
+
```
|
374
|
+
|
375
|
+
New data will be encrypted with the new key. To update existing data, use:
|
376
|
+
|
377
|
+
```ruby
|
378
|
+
User.where("encrypted_kms_key NOT LIKE 'v2:%'").find_each do |user|
|
379
|
+
user.rotate_kms_key!
|
380
|
+
end
|
381
|
+
```
|
382
|
+
|
383
|
+
Once all data is updated, you can remove the `previous_versions` option.
|
384
|
+
|
385
|
+
### Switching Context
|
386
|
+
|
387
|
+
You can change your encryption context without downtime. Update your model:
|
388
|
+
|
389
|
+
```ruby
|
390
|
+
class User < ApplicationRecord
|
391
|
+
has_kms_key version: 2,
|
392
|
+
previous_versions: {
|
393
|
+
1 => {key_id: ENV["KMS_KEY_ID"]}
|
394
|
+
}
|
395
|
+
|
396
|
+
def kms_encryption_context(version:)
|
397
|
+
if version == 1
|
398
|
+
# previous context method
|
399
|
+
else
|
400
|
+
# new context method
|
401
|
+
end
|
402
|
+
end
|
403
|
+
end
|
404
|
+
```
|
405
|
+
|
406
|
+
New data will be encrypted with the new context. To update existing data, use:
|
407
|
+
|
408
|
+
```ruby
|
409
|
+
User.where("encrypted_kms_key NOT LIKE 'v2:%'").find_each do |user|
|
410
|
+
user.rotate_kms_key!
|
411
|
+
end
|
412
|
+
```
|
413
|
+
|
414
|
+
Once all data is updated, you can remove the `previous_versions` option.
|
415
|
+
|
416
|
+
## Multiple Keys Per Record
|
417
|
+
|
418
|
+
You may want to protect different columns with different data keys (or even master keys).
|
419
|
+
|
420
|
+
To do this, add another column
|
421
|
+
|
422
|
+
```ruby
|
423
|
+
add_column :users, :encrypted_kms_key_phone, :text
|
424
|
+
```
|
425
|
+
|
426
|
+
And update your model
|
427
|
+
|
428
|
+
```ruby
|
429
|
+
class User < ApplicationRecord
|
430
|
+
has_kms_key
|
431
|
+
has_kms_key name: :phone, key_id: "..."
|
432
|
+
|
433
|
+
# Lockbox
|
434
|
+
encrypts :email, key: :kms_key
|
435
|
+
encrypts :phone, key: :kms_key_phone
|
436
|
+
|
437
|
+
# attr_encrypted
|
438
|
+
attr_encrypted :email, key: :kms_key
|
439
|
+
attr_encrypted :phone, key: :kms_key_phone
|
440
|
+
end
|
441
|
+
```
|
442
|
+
|
443
|
+
To rotate keys, use:
|
444
|
+
|
445
|
+
```ruby
|
446
|
+
user.rotate_kms_key_phone!
|
447
|
+
```
|
448
|
+
|
449
|
+
For custom context, use:
|
450
|
+
|
451
|
+
```ruby
|
452
|
+
class User < ApplicationRecord
|
453
|
+
def kms_encryption_context_phone
|
454
|
+
# some hash
|
455
|
+
end
|
456
|
+
end
|
457
|
+
```
|
458
|
+
|
459
|
+
## Outside Models
|
460
|
+
|
461
|
+
To encrypt and decrypt outside of models, create a box:
|
462
|
+
|
463
|
+
```ruby
|
464
|
+
kms = KmsEncrypted::Box.new
|
465
|
+
```
|
466
|
+
|
467
|
+
You can pass `key_id`, `version`, and `previous_versions` if needed.
|
468
|
+
|
469
|
+
Encrypt
|
470
|
+
|
471
|
+
```ruby
|
472
|
+
kms.encrypt(message, context: {model_name: "User", model_id: 123})
|
473
|
+
```
|
474
|
+
|
475
|
+
Decrypt
|
476
|
+
|
477
|
+
```ruby
|
478
|
+
kms.decrypt(ciphertext, context: {model_name: "User", model_id: 123})
|
479
|
+
```
|
33
480
|
|
34
481
|
## Related Projects
|
35
482
|
|
36
483
|
To securely search encrypted data, check out [Blind Index](https://github.com/ankane/blind_index).
|
37
484
|
|
485
|
+
## Upgrading
|
486
|
+
|
487
|
+
### 1.0
|
488
|
+
|
489
|
+
KMS Encrypted 1.0 brings a number of improvements. Here are a few breaking changes to be aware of:
|
490
|
+
|
491
|
+
- There’s now a default encryption context with the model name and id
|
492
|
+
- ActiveSupport notifications were changed from `generate_data_key` and `decrypt_data_key` to `encrypt` and `decrypt`
|
493
|
+
- AWS KMS uses the `Encrypt` operation instead of `GenerateDataKey`
|
494
|
+
|
495
|
+
If you didn’t previously use encryption context, add the `upgrade_context` option to your models:
|
496
|
+
|
497
|
+
```ruby
|
498
|
+
class User < ApplicationRecord
|
499
|
+
has_kms_key upgrade_context: true
|
500
|
+
end
|
501
|
+
```
|
502
|
+
|
503
|
+
Then run:
|
504
|
+
|
505
|
+
```ruby
|
506
|
+
User.where("encrypted_kms_key NOT LIKE 'v1:%'").find_each do |user|
|
507
|
+
user.rotate_kms_key!
|
508
|
+
end
|
509
|
+
```
|
510
|
+
|
511
|
+
And remove the `upgrade_context` option.
|
512
|
+
|
38
513
|
## History
|
39
514
|
|
40
515
|
View the [changelog](CHANGELOG.md)
|
@@ -47,3 +522,12 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
|
|
47
522
|
- Fix bugs and [submit pull requests](https://github.com/ankane/kms_encrypted/pulls)
|
48
523
|
- Write, clarify, or fix documentation
|
49
524
|
- Suggest or add new features
|
525
|
+
|
526
|
+
To get started with development and testing:
|
527
|
+
|
528
|
+
```sh
|
529
|
+
git clone https://github.com/ankane/kms_encrypted.git
|
530
|
+
cd kms_encrypted
|
531
|
+
bundle install
|
532
|
+
bundle exec rake test
|
533
|
+
```
|