Top Banner
Rails Encryption with SymmetricEncryption By Reid Morrison, Software Architect @reidmorrison
17

Rails encryption with SymmetricEncryption

Jan 11, 2015

Download

Technology

Reid Morrison

Encrypting Sensitive Data in Rails applications using SymmetricEncryption. As seen at RailsConf 2012 and Tampa Ruby Meetup May 2012.
Encrypting data in the database and passwords in configuration files. Using SymmetricEncryption to help meet PCI compliance.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rails encryption with SymmetricEncryption

Rails Encryption with

SymmetricEncryption

By Reid Morrison, Software Architect

@reidmorrison

Page 2: Rails encryption with SymmetricEncryption

Encrypting Sensitive Data

Sensitive Data in the database

ActiveRecord attributes

Mongoid fields

Passwords in configuration files

MySQL password (database.yml)

MongoDB passwords (mongoid.yml)

External supplier web services passwords

Page 3: Rails encryption with SymmetricEncryption

Encryption Example

SymmetricEncryption.encrypt("Keep me safe")

=> "gIIubGAQqXNrpvacvfrohw==\n"

Page 4: Rails encryption with SymmetricEncryption

Decryption Example

SymmetricEncryption.decrypt("gIIubGAQqXNrpvacvfrohw==\

n")

=> “Keep me safe”

Page 5: Rails encryption with SymmetricEncryption

ActiveRecord Example

class Person < ActiveRecord::Base

attr_encrypted :ssn

end

person = Person.new

person.ssn = ‘123456789’

person.encrypted_ssn

"95kcRwKStvgkVd+LogCn4Q==\n”

# add_column :people, :encrypted_ssn, :string

Page 6: Rails encryption with SymmetricEncryption

Mongoid Example

class Person

include Mongoid::Document

field :name, :type => String

field :encrypted_ssn, :type => String, :encrypted => true

end

person = Person.new

person.ssn = ‘123456789’

person.encrypted_ssn

=> "95kcRwKStvgkVd+LogCn4Q==\n”

Page 7: Rails encryption with SymmetricEncryption

config/database.yml

production:

  adapter:  mysql

  host:     db1primary

  database: myapp_production

  username: myapp 

  password: <%= SymmetricEncryption.try_decrypt

"JqLJOi6dNjWI9kX9lSL1XQ==\n" %>

Page 8: Rails encryption with SymmetricEncryption

PCI Compliance Requirements

Remove Encryption key from:

Source Code Repository

Development team access

Change encryption keys every 12 months

Re-encrypt existing data

Zero downtime

Encrypt with new key, decrypt with new and old keys

Destroy old keys after re-encryption

Page 9: Rails encryption with SymmetricEncryption

Options

shuber/attr_encrypted

Adds encryption methods and attributes to Object

Already in production encrypting data

Hours digging through github and google searches

Similar and different to attr_encrypted

None addressed PCI requirements

Built symmetric-encryption

Page 10: Rails encryption with SymmetricEncryption

What symmetric-encryption does for you

DatabaseEncrypted Data

Secured &RSA Encrypted“Encryption Key File”/etc/myapp/keySecured by OS Security

AES-256 bitEncryption

keyencrypt

decrypt

unlock

2048 bit RSA Key / “Pass Phrase”config/symmetric-encryption.yml

Page 11: Rails encryption with SymmetricEncryption

File Encryption

Large File Encryption and decryption

“On the fly”

Streaming API

Compression

Header

Compressed?

Encryption Key Version

Page 12: Rails encryption with SymmetricEncryption

Writing

SymmetricEncryption::Writer.open(

’filename', :compress => true) do |file|

file.write "Hello World\n"

file.write "Keep this safe and secure\n"

end

Page 13: Rails encryption with SymmetricEncryption

Reading

SymmetricEncryption::Reader.open(’filename') do |file|

file.each_line { |line| puts line }

end

Page 14: Rails encryption with SymmetricEncryption

Features

Not just for PCI compliance – Good practice

Lightweight and simple to use

Secures Passwords in configuration files

Waterfall decryption to support older data

Multiple Keys and versioning

ORM: ActiveRecord & Mongoid

Can be used standalone without Rails

File Streaming API to encrypt files on the fly

Rake tasks for Operations to generate keys and random passwords

Page 15: Rails encryption with SymmetricEncryption

InstallationFor Bundler, add to Gemfile:

gem ‘symmetric-encryption’

• Remove ‘attr_encrypted’ if present

bundle install

Otherwise

gem install symmetric-encryption

require ‘symmetric-encryption’

Create config file

config/symmetric-encryption.yml

Page 17: Rails encryption with SymmetricEncryption

Other Gemsactive_record_slave

Replacement for read from slave

Supports dynamic SQL calls, AREL, etc

Highly performant with no overhead for calls to master/primary

sync_attrThread-safe Synchronized attributes and class variables for lazy loading and/or default values

Don't have to stick everything into a Rails initializer

Jms4jrubyJMS API for JRuby to talk to ActiveMQ, HornetQ, WebSphere MQ, Oracle AQ, any JMS provider.

hyperic-mongodbMonitoring a MongoDB sharded cluster using Hyperic HQ

RubyWMQRuby MRI gem for communicating with IBM WebSphere MQ