diff --git a/lib/ripple-encryption/activation.rb b/lib/ripple-encryption/activation.rb deleted file mode 100644 index 3c9bcfe..0000000 --- a/lib/ripple-encryption/activation.rb +++ /dev/null @@ -1,19 +0,0 @@ -module Ripple - module Encryption - class Activation - - def initialize(path) - config = Ripple::Encryption::Config.new path - # short-circuit encryption via the config file if desired - if !Riak::Serializers['application/x-json-encrypted'] && (config.to_h['encryption'] != false) - Riak::Serializers['application/x-json-encrypted'] = Ripple::Encryption::Serializer.new config - end - @@is_activated = true - end - - def self.activated? - @@is_activated - end - end - end -end diff --git a/lib/ripple-encryption/config.rb b/lib/ripple-encryption/config.rb index 0d88541..5922651 100644 --- a/lib/ripple-encryption/config.rb +++ b/lib/ripple-encryption/config.rb @@ -30,8 +30,8 @@ def activate def validate_path(path) if !File.exists? path - raise ConfigError, < e + handle_invalid_encryption_config(e.message, e.backtrace) + end + encryptor.key = config['key'] if config['key'] + encryptor.iv = config['iv'] if config['iv'] + Riak::Serializers['application/x-json-encrypted'] = encryptor + @@is_activated = true + end + encryptor + end + + def self.activated + @@is_activated + end + end - def update_robject - robject.key = key if robject.key != key - robject.content_type ||= 'application/x-json-encrypted' - robject.data = attributes_for_persistence - end end end + +def handle_invalid_encryption_config(msg, trace) + puts <^ |`fp#ʼl_* \ No newline at end of file diff --git a/test/fixtures/test_document/v2_doc.riak b/test/fixtures/test_document/v2_doc.riak new file mode 100644 index 0000000..d0ddc23 --- /dev/null +++ b/test/fixtures/test_document/v2_doc.riak @@ -0,0 +1 @@ +{"version":"0.0.2","iv":"ABYLnUHWE/fIwE2gKYC6hg==\n","data":"MK0LuGThPhde4t0NfKSbhAvPjTuFmykhWVGNxPG++40=\n"} diff --git a/test/helper.rb b/test/helper.rb index 14ed507..b8ec797 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -17,16 +17,14 @@ client = Riak::Client.new(:nodes => [riak_config]) bucket = client.bucket("#{riak_config[:namespace].to_s}test") object = bucket.get_or_new("test") -rescue RuntimeError => e - raise e +rescue RuntimeError + raise RuntimeError, "Could not connect to the Riak test node." end - -# activate the library -Ripple::Encryption::Activation.new ENV['ENCRYPTION'] - +# define test Ripple Documents +Ripple::Encryption::Encryption.activate ENV['ENCRYPTION'] class TestDocument include Ripple::Document - include Ripple::Encryption + include Ripple::Encryption::Encryption property :message, String def self.bucket_name @@ -34,7 +32,9 @@ def self.bucket_name end end -# load Riak fixtures the raw way +TestDocument.bucket.get_index('$bucket', '_').each {|k| TestDocument.bucket.delete(k)} + +# load Riak fixtures FileList[File.expand_path(File.join('..','fixtures','*'),__FILE__)].each do |f| if Dir.exists? f fixture_type = File.basename(f) @@ -43,15 +43,11 @@ def self.bucket_name rescue NameError raise NameError, "Is a Ripple Document of type '#{fixture_type.classify}' defined for that fixture file?" end - # unencrypted jsons - FileList[File.join(f,'*.unencrypted.riak')].each do |r| - key = File.basename(r,'.unencrypted.riak') - `curl -s -H 'content-type: application/json' -XPUT http://#{Ripple.config[:host]}:#{Ripple.config[:http_port]}/buckets/#{Ripple.config[:namespace]}#{fixture_type.pluralize}/keys/#{key} --data-binary @#{r}` - end - # encrypted jsons - FileList[File.join(f,'*.encrypted.riak')].each do |r| - key = File.basename(r,'.encrypted.riak') - `curl -s -H 'content-type: application/x-json-encrypted' -XPUT http://#{Ripple.config[:host]}:#{Ripple.config[:http_port]}/buckets/#{Ripple.config[:namespace]}#{fixture_type.pluralize}/keys/#{key} --data-binary @#{r}` + FileList[File.join(f,'*.riak')].each do |r| + key = File.basename(r,'.riak') + content_type = (key == 'v0_doc' ? 'application/json' : 'application/x-json-encrypted') + `curl -s -H 'content-type: #{content_type}' -XPUT http://#{Ripple.config[:host]}:#{Ripple.config[:http_port]}/buckets/#{Ripple.config[:namespace]}#{fixture_type.pluralize}/keys/#{key} --data-binary @#{r}` end end end + diff --git a/test/test_config.rb b/test/test_config.rb index e2c4cc1..5991d61 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -2,10 +2,6 @@ class TestConfig < MiniTest::Spec context "Ripple::Encryption::Config" do - should 'confirm encryption is active' do - assert Ripple::Encryption::Activation.activated? - end - should "raise heck if the config file isn't found" do assert_raises Ripple::Encryption::ConfigError do config = Ripple::Encryption::Config.new('nowhere') diff --git a/test/test_encryptor.rb b/test/test_encryptor.rb index 39c2c1e..273c2ff 100644 --- a/test/test_encryptor.rb +++ b/test/test_encryptor.rb @@ -22,19 +22,19 @@ class TestEncryptor < MiniTest::Spec context "Ripple::Encryption::Encryptor with missing parameter" do should "raise an error if key is missing" do - assert_raises Ripple::Encryption::EncryptorError do + assert_raises Ripple::Encryption::EncryptorConfigError do Ripple::Encryption::Encryptor.new(:iv => 'iv', :cipher => 'AES-256-CBC') end end should "raise an error if iv is missing" do - assert_raises Ripple::Encryption::EncryptorError do + assert_raises Ripple::Encryption::EncryptorConfigError do Ripple::Encryption::Encryptor.new(:key => 'key', :cipher => 'AES-256-CBC') end end should "raise an error if cipher is missing" do - assert_raises Ripple::Encryption::EncryptorError do + assert_raises Ripple::Encryption::EncryptorConfigError do Ripple::Encryption::Encryptor.new(:key => 'key', :iv => 'iv') end end diff --git a/test/test_json_document.rb b/test/test_json_document.rb index 0f2b1d0..9970c6f 100644 --- a/test/test_json_document.rb +++ b/test/test_json_document.rb @@ -4,7 +4,7 @@ class TestJsonDocument < MiniTest::Spec context "Ripple::Encryption::JsonDocument" do setup do # get some encryption going - @config = Ripple::Encryption::Config.new ENV['ENCRYPTION'] + @config = Ripple::Encryption::Config.new ENV['ENCRYPTION'] encryptor = Ripple::Encryption::Encryptor.new @config.to_h # this is the data package that we want diff --git a/test/test_ripple.rb b/test/test_ripple.rb index aaeaddd..2da22cc 100644 --- a/test/test_ripple.rb +++ b/test/test_ripple.rb @@ -2,17 +2,16 @@ class TestRipple < MiniTest::Spec context "TestDocument" do - should "read the ripple document" do - assert doc = TestDocument.find('some_other_data') - assert_equal 'this is secret data', doc.message - end - should "write the ripple document" do document = TestDocument.new document.message = 'here is some new data' document.save same_document = TestDocument.find(document.key) assert_equal document.message, same_document.message + + # read the document back out + read_doc = TestDocument.find(document.key) + assert_equal 'here is some new data', read_doc.message end should "write the ripple document raw confirmation" do diff --git a/test/test_unencrypted_document.rb b/test/test_unencrypted_document.rb index 0008054..194fdf9 100644 --- a/test/test_unencrypted_document.rb +++ b/test/test_unencrypted_document.rb @@ -1,20 +1,24 @@ require 'helper' -class TestUnencryptedDocument < MiniTest::Spec +class TestMigrationV1ToV2 < MiniTest::Spec context "unencrypted GenericModel" do + setup do + end + should "read unencrypted document type" do - assert (doc = TestDocument.find('some_data')), "Could not find fixture." - assert_equal 'this is unencrypted data', doc.message + assert v0 = TestDocument.find('v0_doc') + assert_equal 'this is unencrypted data', v0.message end should "write unencrypted document type when content-type is plain" do document = TestDocument.new document.message = 'here is some new data' + Ripple::Encryption::Encryption.class_variable_set(:@@is_activated, false) document.robject.content_type = 'application/json' document.save - expected_doc_data = '{"message":"here is some new data","_type":"TestDocument"}' + expected_v2_data = '{"message":"here is some new data","_type":"TestDocument"}' raw_data = `curl -s -XGET http://#{Ripple.config[:host]}:#{Ripple.config[:http_port]}/buckets/#{TestDocument.bucket_name}/keys/#{document.key}` - assert_equal expected_doc_data, raw_data + assert_equal expected_v2_data, raw_data end end end