slosilo-migration 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 64909ad58a9ef1d6154c78e9a7d8d94cca09a483
4
+ data.tar.gz: 8a015d51699e3233bc34962f42ed8105afcc0cee
5
+ SHA512:
6
+ metadata.gz: c39d164804f28689a233d8deec6cd1b15d50408d0148590b0e527a2dddaf666a9af0f48363bdc938609366bab83a4ce17fd42cb0912b3086fc5efc6ff7ba0cf9
7
+ data.tar.gz: 758450fff2bb16ca35fe30bac0d3ed56de6c6fcd4ba823b18c91afad627b93e5768c0b522846923530c715e88b27e2d28d2049e427d37bc27cac5409580cc750
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .idea
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>slosilo-migration</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>com.aptana.ide.core.unifiedBuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>com.aptana.ruby.core.rubynature</nature>
16
+ <nature>com.aptana.projects.webnature</nature>
17
+ </natures>
18
+ </projectDescription>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --format RspecJunitFormatter
3
+ --out spec-results.xml
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ #ruby=ruby-2.1.5
4
+ #ruby-gemset=slosilo-migration
5
+
6
+ # Specify your gem's dependencies in slosilo-migration.gemspec
7
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kevin Gilpin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ # Slosilo::Migration
2
+
3
+ Provides legacy `aes-256-cbc` encryption for purposes of migrating from
4
+ [Slosilo](https://github.com/conjurinc/slosilo) 1.x to 2.x.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'slosilo-migration'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install slosilo-migration
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( https://github.com/[my-github-username]/slosilo-migration/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,17 @@
1
+ module Slosilo
2
+ unless defined? LEGACY_WARNED
3
+ puts """
4
+ WARNING!! You are loading compatibility Slosilo code. It has known potential
5
+ security problems and is deprecated; this code is ONLY meant to be used in
6
+ migrations and tests.
7
+
8
+ First loaded from:
9
+ """
10
+ puts caller.map{|s| "\t\t#{s}"}
11
+ LEGACY_WARNED = true
12
+ end
13
+ end
14
+
15
+ require 'slosilo/migration/symmetric'
16
+ require 'slosilo/migration/migrate_keys'
17
+ require 'slosilo/migration/attr_encrypted'
@@ -0,0 +1,49 @@
1
+ require 'slosilo/migration'
2
+
3
+ module Slosilo::Migration
4
+ # we don't trust the database to keep all backups safe from the prying eyes
5
+ # so we encrypt sensitive attributes before storing them
6
+ module EncryptedAttributes
7
+ module ClassMethods
8
+ def attr_encrypted *a
9
+ # push a module onto the inheritance hierarchy
10
+ # this allows calling super in classes
11
+ include(accessors = Module.new)
12
+ accessors.module_eval do
13
+ a.each do |attr|
14
+ define_method "#{attr}=" do |value|
15
+ super(EncryptedAttributes.encrypt value)
16
+ end
17
+ define_method attr do
18
+ EncryptedAttributes.decrypt(super())
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.included base
26
+ base.extend ClassMethods
27
+ end
28
+
29
+ class << self
30
+ def encrypt value
31
+ return nil unless value
32
+ cipher.encrypt value, key: key
33
+ end
34
+
35
+ def decrypt ctxt
36
+ return nil unless ctxt
37
+ cipher.decrypt ctxt, key: key
38
+ end
39
+
40
+ def key
41
+ Slosilo::encryption_key || (raise "Please set Slosilo::encryption_key")
42
+ end
43
+
44
+ def cipher
45
+ @cipher ||= Slosilo::Migration::Symmetric.new
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,76 @@
1
+ # Make sure we show the warning
2
+ require 'slosilo/migration'
3
+
4
+ module Slosilo
5
+ module Migration
6
+ module MigrateKeys
7
+ DEFAULT_KEYSTORE_TABLE = :slosilo_keystore
8
+
9
+ attr_writer :keystore_table
10
+
11
+ def keystore_table
12
+ @keystore_table ||= DEFAULT_KEYSTORE_TABLE
13
+ end
14
+
15
+ def upgrade! db
16
+ keystore = db[keystore_table]
17
+ return unless keystore.count > 0
18
+
19
+ key = Slosilo::encryption_key
20
+ if key.nil?
21
+ warn "Slosilo::encryption_key not set, assuming unencrypted key store"
22
+ return
23
+ end
24
+
25
+
26
+ old_cipher = Slosilo::Migration::Symmetric.new
27
+ new_cipher = Slosilo::Symmetric.new
28
+
29
+
30
+ progress = progress_bar keystore.count
31
+
32
+ keystore.each do |row|
33
+ begin
34
+ # try to decrypt using new cipher
35
+ new_cipher.decrypt row[:key], key: key, aad: row[:id]
36
+ # it worked, no need to update
37
+ rescue OpenSSL::Cipher::CipherError
38
+ # otherwise, needs to be upgraded.
39
+ ptext = old_cipher.decrypt row[:key], key: key
40
+ ctext = new_cipher.encrypt ptext, key: key, aad: row[:id]
41
+ keystore.where(id: row[:id]).update(key: Sequel.blob(ctext))
42
+ end
43
+ progress.increment
44
+ end
45
+ end
46
+
47
+
48
+ def progress_bar count
49
+ begin
50
+ require 'ruby-progressbar'
51
+ ProgressBar.create total: count, output: $stderr, format: '%t |%w>%i| %e'
52
+ rescue LoadError
53
+ Object.new.tap do |o|
54
+ def o.increment; $stderr << '.' end
55
+ end
56
+ end
57
+ end
58
+
59
+ end
60
+ end
61
+ end
62
+
63
+ # Usage:
64
+ # require 'slosilo/migration/migrate_keys'
65
+ # Sequel.migration do
66
+ # up do
67
+ # extend Slosilo::Migration::MigrateKeys
68
+ # self.keystore_table = :some_custom_table
69
+ # upgrade! self
70
+ # end
71
+ #
72
+ # down do
73
+ # raise "Irreversible!"
74
+ # end
75
+ # end
76
+ #
@@ -0,0 +1,38 @@
1
+ module Slosilo::Migration
2
+ class Symmetric
3
+ def initialize
4
+ @cipher = OpenSSL::Cipher.new 'AES-256-CBC'
5
+ end
6
+
7
+ # This lets us do a final sanity check in migrations from older encryption versions
8
+ def cipher_name
9
+ @cipher.name
10
+ end
11
+
12
+ def encrypt plaintext, opts = {}
13
+ @cipher.reset
14
+ @cipher.encrypt
15
+ @cipher.key = opts[:key]
16
+ @cipher.iv = iv = random_iv
17
+ ctxt = @cipher.update(plaintext)
18
+ iv + ctxt + @cipher.final
19
+ end
20
+
21
+ def decrypt ciphertext, opts = {}
22
+ @cipher.reset
23
+ @cipher.decrypt
24
+ @cipher.key = opts[:key]
25
+ @cipher.iv, ctxt = ciphertext.unpack("a#{@cipher.iv_len}a*")
26
+ ptxt = @cipher.update(ctxt)
27
+ ptxt + @cipher.final
28
+ end
29
+
30
+ def random_iv
31
+ @cipher.random_iv
32
+ end
33
+
34
+ def random_key
35
+ @cipher.random_key
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module Slosilo
2
+ module Migration
3
+ VERSION = "1.1.0"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slosilo/migration/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slosilo-migration"
8
+ spec.version = Slosilo::Migration::VERSION
9
+ spec.authors = ["Kevin Gilpin"]
10
+ spec.email = ["kgilpin@gmail.com"]
11
+ spec.summary = %q{Slosilo v1-compatible migration helper.}
12
+ spec.homepage = "https://github.com/conjurinc/slosilo-migration"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "ruby-progressbar"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "slosilo"
26
+ spec.add_development_dependency 'rspec_junit_formatter'
27
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ class Object
4
+ def metaclass
5
+ class << self; self; end
6
+ end
7
+ end
8
+
9
+ describe Slosilo::Migration::MigrateKeys do
10
+
11
+ subject { Class.new.extend(Slosilo::Migration::MigrateKeys) }
12
+
13
+ context 'for rows already encrypted properly' do
14
+
15
+ before(:each) { Slosilo::encryption_key = 'bed3033f87d71911a357cebe35ecb434' }
16
+
17
+ let (:row_id) { 'id' }
18
+ let (:db) {
19
+ encrypted_key = Slosilo::Symmetric.new.encrypt('foo', :key => Slosilo::encryption_key, :aad => row_id )
20
+
21
+ # Match the shape of the keystore object expected by MigrateKeys#upgrade!
22
+ {
23
+ Slosilo::Migration::MigrateKeys::DEFAULT_KEYSTORE_TABLE => [
24
+ {:key => encrypted_key, :id => row_id }
25
+ ]
26
+ }
27
+ }
28
+
29
+ it 'ignores them' do
30
+ expect { subject.upgrade!(db) }.not_to raise_exception
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+ require 'slosilo'
3
+ require 'slosilo/migration'
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Slosilo::Migration::Symmetric do
4
+ # TODO transform it to class methods only?
5
+ let(:plaintext) { "quick brown fox jumped over the lazy dog" }
6
+ let(:key) { "^\xBAIv\xDB1\x0Fi\x04\x11\xFD\x14\xA7\xCD\xDFf\x93\xFE\x93}\v\x01\x11\x98\x14\xE0;\xC1\xE2 v\xA5".force_encoding("ASCII-8BIT") }
7
+ let(:iv) { "\xA1\xFA#z\x16\x80R\xCC|\x0Fyc\xB7j\x17\xED".force_encoding("ASCII-8BIT") }
8
+ let(:ciphertext) { "\xA1\xFA#z\x16\x80R\xCC|\x0Fyc\xB7j\x17\xED\x15\xC9r\xC9\xEE\xB9\xBC5\xB7\ni\x0F\f\xC8X\x80 h\a\xF4\xA6\xE3\x15\x9D\xF1-\xE5\bs\xF6\x02Z\x0F\xCD|S\x1A\xAA\x9At\xEFT\x17\xA5lT\x8C\xF3".force_encoding("ASCII-8BIT") }
9
+ describe '#encrypt' do
10
+ it "encrypts with AES-256-CBC" do
11
+ allow(subject).to receive(:random_iv).and_return(iv)
12
+ expect(subject.encrypt(plaintext, key: key)).to eql(ciphertext)
13
+ end
14
+ end
15
+
16
+ describe '#decrypt' do
17
+ it "decrypts with AES-256-CBC" do
18
+ expect(subject.decrypt(ciphertext, key: key)).to eq(plaintext)
19
+ end
20
+
21
+ context "when ciphertext happens to end in a zero" do
22
+ let(:ciphertext) { "\x7F\xD6\xEAb\xE56\a\xD3\xC5\xF2J\n\x8C\x8Fg\xB7-\\\x8A\fh\x18\xC8\x91\xB9 \x97\xC9\x12\xE6\xA6\xAE\xB1I\x1E\x80\xAB\xD8\xDC\xBD\xB6\xCD\x9A\xA3MH\xA8\xB0\xC7\xDA\x87\xA7c\xD75,\xD2A\xB8\x9E\xE3o\x04\x00" }
23
+ let(:key) { "4pSuk1rAQyuHA5uUYaj0X0BsiPCFb9Nc8J03XA6V5/Y" }
24
+ it "works correctly" do
25
+ expect(subject.decrypt(ciphertext, key: key)).to eq("R6KNTQ4aUivojbaqhgAqj1I4PaF8h/5/YcENy4uNbfk=")
26
+ end
27
+ end
28
+
29
+ context "when the iv ends in space" do
30
+ let(:ciphertext) { "\xC0\xDA#\xE9\xE1\xFD\xEDJ\xADs4P\xA9\xD6\x92 \xF7\xF8_M\xF6\x16\xC2i$\x8BT^\b\xA1\xB2L&\xE9\x80\x02[]6i\x9B\xD3\xC3\xED\xA9\xD1\x94\xE8\x15\xFD\xDA\xFEUj\xC5upH*\xBF\x82\x15le" }
31
+ let(:key) { "4pSuk1rAQyuHA5uUYaj0X0BsiPCFb9Nc8J03XA6V5/Y" }
32
+ it "works correctly" do
33
+ expect(subject.decrypt(ciphertext, key: key)).to eq("zGptmL3vd4obi1vqSiWHt/Ias2k+6qDtuq9vdow8jNA=")
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#random_iv' do
39
+ it "generates a random iv" do
40
+ expect_any_instance_of(OpenSSL::Cipher).to receive(:random_iv).and_return :iv
41
+ expect(subject.random_iv).to eq(:iv)
42
+ end
43
+ end
44
+
45
+ describe '#random_key' do
46
+ it "generates a random key" do
47
+ expect_any_instance_of(OpenSSL::Cipher).to receive(:random_key).and_return :key
48
+ expect(subject.random_key).to eq(:key)
49
+ end
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slosilo-migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Gilpin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-progressbar
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: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
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: slosilo
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_junit_formatter
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:
98
+ email:
99
+ - kgilpin@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".project"
106
+ - ".rspec"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/slosilo/migration.rb
112
+ - lib/slosilo/migration/attr_encrypted.rb
113
+ - lib/slosilo/migration/migrate_keys.rb
114
+ - lib/slosilo/migration/symmetric.rb
115
+ - lib/slosilo/migration/version.rb
116
+ - slosilo-migration.gemspec
117
+ - spec/migrate_keys_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec/symmetric_spec.rb
120
+ homepage: https://github.com/conjurinc/slosilo-migration
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.4.8
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Slosilo v1-compatible migration helper.
144
+ test_files:
145
+ - spec/migrate_keys_spec.rb
146
+ - spec/spec_helper.rb
147
+ - spec/symmetric_spec.rb