README.md in kms_encrypted-0.1.0 vs README.md in kms_encrypted-0.1.1

- old
+ new

@@ -5,14 +5,22 @@ The attr_encrypted gem is great for encryption, but: 1. Leaves you to manage the security of your keys 2. Doesn’t provide a great audit trail to see how data has been accessed -KMS addresses both of the issues and it’s easy to use them together. +KMS addresses both issues and it’s easy to use them together. **Note:** This has not been battle-tested in a production environment, so use with caution +## How It Works + +This approach uses KMS to manage encryption keys and attr_encrypted to do the encryption. + +To encrypt an attribute, we first generate a [data key](http://docs.aws.amazon.com/kms/latest/developerguide/concepts.html) from our KMS master key. KMS sends both encrypted and unencrypted versions of the data key. We pass the unencrypted version to attr_encrypted and store the encrypted version in the `encrypted_kms_key` column. For each record, we generate a different data key. + +To decrypt an attribute, we first decrypt the data key with KMS. Once we have the decrypted key, we pass it to attr_encrypted to decrypt the data. We can easily track decryptions since we have a different data key for each record. + ## Getting Started Add this line to your application’s Gemfile: ```ruby @@ -50,26 +58,151 @@ Add a `kms_encryption_context` method to your model. ```ruby class User < ApplicationRecord def kms_encryption_context + # some hash + end +end +``` + +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. Read more about [encryption context here](http://docs.aws.amazon.com/kms/latest/developerguide/encryption-context.html). + +The primary key is a good choice, but auto-generated ids aren’t available until a record is created, and we need to encrypt before this. One solution is to preload the primary key. Here’s what it looks like with Postgres: + +```ruby +class User < ApplicationRecord + def kms_encryption_context self.id ||= self.class.connection.execute("select nextval('#{self.class.sequence_name}')").first["nextval"] {"Record" => "#{model_name}/#{id}"} end end ``` -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. +We recommend [Amazon Athena](https://aws.amazon.com/athena/) for querying CloudTrail logs. Create a table (thanks to [this post](http://www.1strategy.com/blog/2017/07/25/auditing-aws-activity-with-cloudtrail-and-athena/) for the table structure) with: -Read more about [encryption context here](http://docs.aws.amazon.com/kms/latest/developerguide/encryption-context.html). +```sql +CREATE EXTERNAL TABLE cloudtrail_logs ( + eventversion STRING, + userIdentity STRUCT< + type:STRING, + principalid:STRING, + arn:STRING, + accountid:STRING, + invokedby:STRING, + accesskeyid:STRING, + userName:String, + sessioncontext:STRUCT< + attributes:STRUCT< + mfaauthenticated:STRING, + creationdate:STRING>, + sessionIssuer:STRUCT< + type:STRING, + principalId:STRING, + arn:STRING, + accountId:STRING, + userName:STRING>>>, + eventTime STRING, + eventSource STRING, + eventName STRING, + awsRegion STRING, + sourceIpAddress STRING, + userAgent STRING, + errorCode STRING, + errorMessage STRING, + requestId STRING, + eventId STRING, + resources ARRAY<STRUCT< + ARN:STRING, + accountId:STRING, + type:STRING>>, + eventType STRING, + apiVersion STRING, + readOnly BOOLEAN, + recipientAccountId STRING, + sharedEventID STRING, + vpcEndpointId STRING, + requestParameters STRING, + responseElements STRING, + additionalEventData STRING, + serviceEventDetails STRING +) +ROW FORMAT SERDE 'com.amazon.emr.hive.serde.CloudTrailSerde' +STORED AS INPUTFORMAT 'com.amazon.emr.cloudtrail.CloudTrailInputFormat' +OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' +LOCATION 's3://my-cloudtrail-logs/' +``` -## How It Works +Change the last line to point to your CloudTrail log bucket and query away -KMS allows you to generate data keys from your master key. Data keys are encrypted and stored with each record. Whenever you want to decrypt data, you need to first decrypt the data key with KMS. Once you have the decrypted key, you can then use it to decrypt the data with attr_encrypted. +```sql +SELECT + eventTime, + eventName, + userIdentity.userName, + requestParameters +FROM + cloudtrail_logs +WHERE + eventName = 'Decrypt' +ORDER BY 1 +``` -## TODO +## Key Rotation -- add support for multiple data keys per record +KMS supports [automatic key rotation](http://docs.aws.amazon.com/kms/latest/developerguide/rotate-keys.html). No action is required in this case. + +To manually rotate keys, replace the old KMS key id with the new key id in your model. Your app does not need the old key id to perform rotation (however, the key must still be enabled in your AWS account). + +```sh +KMS_KEY_ID=arn:aws:kms:... +``` + +and run + +```sh +User.find_each do |user| + user.rotate_kms_key! +end +``` + +## Multiple Keys Per Record + +You may want to protect different columns with different data keys (or even master keys). + +To do this, add more columns + +```ruby +add_column :users, :encrypted_kms_key_phone, :string +``` + +And update your model + +```ruby +class User < ApplicationRecord + has_kms_key ENV["KMS_KEY_ID"] + has_kms_key ENV["KMS_KEY_ID"], name: :phone + + attr_encrypted :email, key: :kms_key + attr_encrypted :phone, key: :kms_key_phone +end +``` + +For context, use: + +```ruby +class User < ApplicationRecord + def kms_encryption_context_phone + # some hash + end +end +``` + +To rotate keys, use: + +```ruby +user.rotate_kms_key_phone! +``` ## History View the [changelog](https://github.com/ankane/kms_encrypted/blob/master/CHANGELOG.md)