loyal3-sentry 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/CHANGELOG +58 -0
- data/MIT-LICENSE +20 -0
- data/README +94 -0
- data/RUNNING_UNIT_TESTS +42 -0
- data/Rakefile +192 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/active_record/sentry.rb +96 -0
- data/lib/sentry/asymmetric_sentry.rb +146 -0
- data/lib/sentry/asymmetric_sentry_callback.rb +17 -0
- data/lib/sentry/sha_sentry.rb +41 -0
- data/lib/sentry/symmetric_sentry.rb +79 -0
- data/lib/sentry/symmetric_sentry_callback.rb +17 -0
- data/lib/sentry.rb +47 -0
- data/sentry.gemspec +75 -0
- data/tasks/sentry.rake +9 -0
- data/test/abstract_unit.rb +44 -0
- data/test/asymmetric_sentry_callback_test.rb +106 -0
- data/test/asymmetric_sentry_test.rb +88 -0
- data/test/database.yml +18 -0
- data/test/fixtures/user.rb +26 -0
- data/test/fixtures/users.yml +9 -0
- data/test/keys/encrypted_private +12 -0
- data/test/keys/encrypted_public +4 -0
- data/test/keys/private +9 -0
- data/test/keys/public +4 -0
- data/test/schema.rb +10 -0
- data/test/sha_sentry_test.rb +35 -0
- data/test/symmetric_sentry_callback_test.rb +38 -0
- data/test/symmetric_sentry_test.rb +37 -0
- data/test/tests.rb +2 -0
- metadata +94 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require 'digest/sha1'
|
|
2
|
+
module Sentry
|
|
3
|
+
class ShaSentry
|
|
4
|
+
@@salt = 'salt'
|
|
5
|
+
attr_accessor :salt
|
|
6
|
+
|
|
7
|
+
# Encrypts data using SHA.
|
|
8
|
+
def encrypt(data)
|
|
9
|
+
self.class.encrypt(data + salt.to_s)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Initialize the class.
|
|
13
|
+
# Used by ActiveRecord::Base#generates_crypted to set up as a callback object for a model
|
|
14
|
+
def initialize(attribute = nil)
|
|
15
|
+
@attribute = attribute
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Performs encryption on before_validation Active Record callback
|
|
19
|
+
def before_validation(model)
|
|
20
|
+
return unless model.send(@attribute)
|
|
21
|
+
model.send("crypted_#{@attribute}=", encrypt(model.send(@attribute)))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class << self
|
|
25
|
+
# Gets the class salt value used when encrypting
|
|
26
|
+
def salt
|
|
27
|
+
@@salt
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Sets the class salt value used when encrypting
|
|
31
|
+
def salt=(value)
|
|
32
|
+
@@salt = value
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Encrypts the data
|
|
36
|
+
def encrypt(data)
|
|
37
|
+
Digest::SHA1.hexdigest(data + @@salt)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
module Sentry
|
|
2
|
+
class SymmetricSentry
|
|
3
|
+
@@default_algorithm = 'DES-EDE3-CBC'
|
|
4
|
+
@@default_key = nil
|
|
5
|
+
attr_accessor :algorithm
|
|
6
|
+
def initialize(options = {})
|
|
7
|
+
@algorithm = options[:algorithm] || @@default_algorithm
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def encrypt(data, key = nil)
|
|
11
|
+
key = check_for_key!(key)
|
|
12
|
+
des = encryptor
|
|
13
|
+
des.encrypt(key)
|
|
14
|
+
data = des.update(data)
|
|
15
|
+
data << des.final
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def encrypt_to_base64(text, key = nil)
|
|
19
|
+
Base64.encode64(encrypt(text, key))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def decrypt(data, key = nil)
|
|
23
|
+
key = check_for_key!(key)
|
|
24
|
+
des = encryptor
|
|
25
|
+
des.decrypt(key)
|
|
26
|
+
text = des.update(data)
|
|
27
|
+
text << des.final
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def decrypt_from_base64(text, key = nil)
|
|
31
|
+
decrypt(Base64.decode64(text), key)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class << self
|
|
35
|
+
def default_algorithm
|
|
36
|
+
@@default_algorithm
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def default_algorithm=(value)
|
|
40
|
+
@@default_algorithm = value
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def default_key
|
|
44
|
+
@@default_key
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def default_key=(value)
|
|
48
|
+
@@default_key = value
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def encrypt(data, key = nil)
|
|
52
|
+
self.new.encrypt(data, key)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def encrypt_to_base64(text, key = nil)
|
|
56
|
+
self.new.encrypt_to_base64(text, key)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def decrypt(data, key = nil)
|
|
60
|
+
self.new.decrypt(data, key)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def decrypt_from_base64(text, key = nil)
|
|
64
|
+
self.new.decrypt_from_base64(text, key)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
def encryptor
|
|
70
|
+
@encryptor ||= OpenSSL::Cipher::Cipher.new(@algorithm)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def check_for_key!(key)
|
|
74
|
+
valid_key = key || @@default_key
|
|
75
|
+
raise Sentry::NoKeyError if valid_key.nil?
|
|
76
|
+
valid_key
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Sentry
|
|
2
|
+
class SymmetricSentryCallback
|
|
3
|
+
def initialize(attr_name)
|
|
4
|
+
@attr_name = attr_name
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
## Performs encryption on before_validation Active Record callback
|
|
8
|
+
#def before_validation(model)
|
|
9
|
+
# return if model.send(@attr_name).blank?
|
|
10
|
+
# model.send("crypted_#{@attr_name}=", SymmetricSentry.encrypt_to_base64(model.send(@attr_name)))
|
|
11
|
+
#end
|
|
12
|
+
|
|
13
|
+
#def after_save(model)
|
|
14
|
+
# #model.send("#{@attr_name}=", nil)
|
|
15
|
+
#end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/sentry.rb
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#Copyright (c) 2005 Rick Olson
|
|
2
|
+
#
|
|
3
|
+
#Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
#a copy of this software and associated documentation files (the
|
|
5
|
+
#"Software"), to deal in the Software without restriction, including
|
|
6
|
+
#without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
#distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
#permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
#the following conditions:
|
|
10
|
+
#
|
|
11
|
+
#The above copyright notice and this permission notice shall be
|
|
12
|
+
#included in all copies or substantial portions of the Software.
|
|
13
|
+
#
|
|
14
|
+
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
#MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
#LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
#OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
#WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
|
22
|
+
require 'openssl'
|
|
23
|
+
require 'base64'
|
|
24
|
+
require 'sentry/symmetric_sentry'
|
|
25
|
+
require 'sentry/asymmetric_sentry'
|
|
26
|
+
require 'sentry/sha_sentry'
|
|
27
|
+
require 'sentry/symmetric_sentry_callback'
|
|
28
|
+
require 'sentry/asymmetric_sentry_callback'
|
|
29
|
+
|
|
30
|
+
module Sentry
|
|
31
|
+
class NoKeyError < StandardError
|
|
32
|
+
end
|
|
33
|
+
class NoPublicKeyError < StandardError
|
|
34
|
+
end
|
|
35
|
+
class NoPrivateKeyError < StandardError
|
|
36
|
+
end
|
|
37
|
+
mattr_accessor :default_key
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
begin
|
|
41
|
+
require 'active_record/sentry'
|
|
42
|
+
ActiveRecord::Base.class_eval do
|
|
43
|
+
include ActiveRecord::Sentry
|
|
44
|
+
end
|
|
45
|
+
rescue NameError
|
|
46
|
+
nil
|
|
47
|
+
end
|
data/sentry.gemspec
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = %q{sentry}
|
|
5
|
+
s.version = "0.4.2"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["John Pelly", "David Stevenson"]
|
|
9
|
+
s.date = %q{2009-07-30}
|
|
10
|
+
s.description = %q{Asymmetric encryption of active record fields}
|
|
11
|
+
s.email = %q{commoncode@pivotallabs.com}
|
|
12
|
+
s.extra_rdoc_files = [
|
|
13
|
+
"README"
|
|
14
|
+
]
|
|
15
|
+
s.files = [
|
|
16
|
+
".gitignore",
|
|
17
|
+
"CHANGELOG",
|
|
18
|
+
"MIT-LICENSE",
|
|
19
|
+
"README",
|
|
20
|
+
"RUNNING_UNIT_TESTS",
|
|
21
|
+
"Rakefile",
|
|
22
|
+
"VERSION",
|
|
23
|
+
"init.rb",
|
|
24
|
+
"lib/active_record/sentry.rb",
|
|
25
|
+
"lib/sentry.rb",
|
|
26
|
+
"lib/sentry/asymmetric_sentry.rb",
|
|
27
|
+
"lib/sentry/asymmetric_sentry_callback.rb",
|
|
28
|
+
"lib/sentry/sha_sentry.rb",
|
|
29
|
+
"lib/sentry/symmetric_sentry.rb",
|
|
30
|
+
"lib/sentry/symmetric_sentry_callback.rb",
|
|
31
|
+
"sentry.gemspec",
|
|
32
|
+
"tasks/sentry.rake",
|
|
33
|
+
"test/abstract_unit.rb",
|
|
34
|
+
"test/asymmetric_sentry_callback_test.rb",
|
|
35
|
+
"test/asymmetric_sentry_test.rb",
|
|
36
|
+
"test/database.yml",
|
|
37
|
+
"test/fixtures/user.rb",
|
|
38
|
+
"test/fixtures/users.yml",
|
|
39
|
+
"test/keys/encrypted_private",
|
|
40
|
+
"test/keys/encrypted_public",
|
|
41
|
+
"test/keys/private",
|
|
42
|
+
"test/keys/public",
|
|
43
|
+
"test/schema.rb",
|
|
44
|
+
"test/sha_sentry_test.rb",
|
|
45
|
+
"test/symmetric_sentry_callback_test.rb",
|
|
46
|
+
"test/symmetric_sentry_test.rb",
|
|
47
|
+
"test/tests.rb"
|
|
48
|
+
]
|
|
49
|
+
s.homepage = %q{http://github.com/pivotal/sentry}
|
|
50
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
51
|
+
s.require_paths = ["lib"]
|
|
52
|
+
s.rubygems_version = %q{1.3.5}
|
|
53
|
+
s.summary = %q{Asymmetric encryption of active record fields}
|
|
54
|
+
s.test_files = [
|
|
55
|
+
"test/abstract_unit.rb",
|
|
56
|
+
"test/asymmetric_sentry_callback_test.rb",
|
|
57
|
+
"test/asymmetric_sentry_test.rb",
|
|
58
|
+
"test/fixtures/user.rb",
|
|
59
|
+
"test/schema.rb",
|
|
60
|
+
"test/sha_sentry_test.rb",
|
|
61
|
+
"test/symmetric_sentry_callback_test.rb",
|
|
62
|
+
"test/symmetric_sentry_test.rb",
|
|
63
|
+
"test/tests.rb"
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
if s.respond_to? :specification_version then
|
|
67
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
68
|
+
s.specification_version = 3
|
|
69
|
+
|
|
70
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
71
|
+
else
|
|
72
|
+
end
|
|
73
|
+
else
|
|
74
|
+
end
|
|
75
|
+
end
|
data/tasks/sentry.rake
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require 'sentry'
|
|
2
|
+
|
|
3
|
+
desc "Creates a private/public key for asymmetric encryption: rake sentry_key PUB=/path/to/public.key PRIV=/path/to/priv.key [KEY=secret]"
|
|
4
|
+
task :sentry_key do
|
|
5
|
+
Sentry::AsymmetricSentry.save_random_rsa_key(
|
|
6
|
+
ENV['PRIV'] || 'private.key',
|
|
7
|
+
ENV['PUB'] || 'public.key',
|
|
8
|
+
:key => ENV['KEY'])
|
|
9
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'test/unit'
|
|
5
|
+
require 'active_record'
|
|
6
|
+
require 'active_record/fixtures'
|
|
7
|
+
require 'active_support/test_case'
|
|
8
|
+
#require 'active_support/binding_of_caller'
|
|
9
|
+
#require 'active_support/breakpoint'
|
|
10
|
+
require "#{File.dirname(__FILE__)}/../lib/sentry"
|
|
11
|
+
|
|
12
|
+
config_location = File.dirname(__FILE__) + '/database.yml'
|
|
13
|
+
|
|
14
|
+
config = YAML::load(IO.read(config_location))
|
|
15
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
|
16
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'mysql'])
|
|
17
|
+
ActiveRecord::Base.configurations["test"] = "lolcatz"
|
|
18
|
+
|
|
19
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
|
20
|
+
|
|
21
|
+
class ActiveSupport::TestCase #:nodoc:
|
|
22
|
+
include ActiveRecord::TestFixtures
|
|
23
|
+
#def create_fixtures(*table_names)
|
|
24
|
+
# if block_given?
|
|
25
|
+
# Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names) { yield }
|
|
26
|
+
# else
|
|
27
|
+
# Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names)
|
|
28
|
+
# end
|
|
29
|
+
#end
|
|
30
|
+
|
|
31
|
+
self.use_instantiated_fixtures = false
|
|
32
|
+
self.use_transactional_fixtures = true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def create_fixtures(*table_names, &block)
|
|
36
|
+
Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
ActiveSupport::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
|
|
42
|
+
ActiveSupport::TestCase.use_instantiated_fixtures = true
|
|
43
|
+
ActiveSupport::TestCase.use_transactional_fixtures = (ENV['AR_TX_FIXTURES'] == "yes")
|
|
44
|
+
$LOAD_PATH.unshift(ActiveSupport::TestCase.fixture_path)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
require 'abstract_unit'
|
|
2
|
+
require 'fixtures/user'
|
|
3
|
+
|
|
4
|
+
class AsymmetricSentryCallbackTest < ActiveSupport::TestCase
|
|
5
|
+
fixtures :users
|
|
6
|
+
|
|
7
|
+
def setup
|
|
8
|
+
super
|
|
9
|
+
@str = 'sentry'
|
|
10
|
+
@key = 'secret'
|
|
11
|
+
@public_key_file = File.dirname(__FILE__) + '/keys/public'
|
|
12
|
+
@private_key_file = File.dirname(__FILE__) + '/keys/private'
|
|
13
|
+
@encrypted_public_key_file = File.dirname(__FILE__) + '/keys/encrypted_public'
|
|
14
|
+
@encrypted_private_key_file = File.dirname(__FILE__) + '/keys/encrypted_private'
|
|
15
|
+
|
|
16
|
+
@orig = 'sentry'
|
|
17
|
+
Sentry::AsymmetricSentry.default_public_key_file = @public_key_file
|
|
18
|
+
Sentry::AsymmetricSentry.default_private_key_file = @private_key_file
|
|
19
|
+
Sentry::SymmetricSentry.default_key = @key
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def teardown
|
|
23
|
+
super
|
|
24
|
+
Sentry.default_key = nil
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_encryption_should_use_default_key_when_present
|
|
28
|
+
use_encrypted_keys
|
|
29
|
+
|
|
30
|
+
assert_nil users(:user_2).creditcard
|
|
31
|
+
Sentry.default_key = @key
|
|
32
|
+
|
|
33
|
+
assert_equal @orig, users(:user_2).creditcard
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_encryption_with_random_padding
|
|
37
|
+
# system works with unsaved record
|
|
38
|
+
u = User.new :login => 'jones'
|
|
39
|
+
u.creditcard = @orig
|
|
40
|
+
assert_equal @orig, u.creditcard
|
|
41
|
+
u.save!
|
|
42
|
+
|
|
43
|
+
# reload after save and check the decrypt works
|
|
44
|
+
u = User.find(u.id)
|
|
45
|
+
assert_equal @orig, u.creditcard
|
|
46
|
+
original_crypttext = u.crypted_creditcard
|
|
47
|
+
|
|
48
|
+
# set to same plaintext
|
|
49
|
+
u.creditcard = @orig
|
|
50
|
+
u.save!
|
|
51
|
+
|
|
52
|
+
# expect different crypttext (due to random padding)
|
|
53
|
+
assert_not_equal original_crypttext, u.crypted_creditcard
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def test_should_encrypt_creditcard
|
|
57
|
+
u = User.create :login => 'jones'
|
|
58
|
+
u.creditcard = @orig
|
|
59
|
+
assert u.save
|
|
60
|
+
assert !u.crypted_creditcard.empty?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_should_deal_with_before_typecast
|
|
64
|
+
u = User.create :login => 'jones'
|
|
65
|
+
u.creditcard = "123123"
|
|
66
|
+
assert_equal "123123", u.creditcard_before_type_cast
|
|
67
|
+
assert u.save
|
|
68
|
+
u.reload
|
|
69
|
+
assert_equal "123123", u.creditcard_before_type_cast
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def test_should_decrypt_creditcard
|
|
73
|
+
assert_equal @orig, users(:user_1).creditcard
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_should_not_decrypt_encrypted_creditcard_with_invalid_key
|
|
77
|
+
assert_nil users(:user_2).creditcard
|
|
78
|
+
assert_nil users(:user_2).creditcard(@key)
|
|
79
|
+
use_encrypted_keys
|
|
80
|
+
assert_nil users(:user_1).creditcard
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def test_should_not_decrypt_encrypted_creditcard
|
|
84
|
+
use_encrypted_keys
|
|
85
|
+
assert_nil users(:user_2).creditcard
|
|
86
|
+
assert_nil users(:user_2).creditcard('other secret')
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def test_should_encrypt_encrypted_creditcard
|
|
90
|
+
use_encrypted_keys
|
|
91
|
+
u = User.create :login => 'jones'
|
|
92
|
+
u.creditcard = @orig
|
|
93
|
+
assert u.save
|
|
94
|
+
assert !u.crypted_creditcard.empty?
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def test_should_decrypt_encrypted_creditcard
|
|
98
|
+
use_encrypted_keys
|
|
99
|
+
assert_equal @orig, users(:user_2).creditcard(@key)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def use_encrypted_keys
|
|
103
|
+
Sentry::AsymmetricSentry.default_public_key_file = @encrypted_public_key_file
|
|
104
|
+
Sentry::AsymmetricSentry.default_private_key_file = @encrypted_private_key_file
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
require 'abstract_unit'
|
|
2
|
+
|
|
3
|
+
class AsymmetricSentryTest < Test::Unit::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
@str = 'sentry'
|
|
6
|
+
@key = 'secret'
|
|
7
|
+
@public_key_file = File.dirname(__FILE__) + '/keys/public'
|
|
8
|
+
@private_key_file = File.dirname(__FILE__) + '/keys/private'
|
|
9
|
+
@encrypted_public_key_file = File.dirname(__FILE__) + '/keys/encrypted_public'
|
|
10
|
+
@encrypted_private_key_file = File.dirname(__FILE__) + '/keys/encrypted_private'
|
|
11
|
+
@sentry = Sentry::AsymmetricSentry.new
|
|
12
|
+
|
|
13
|
+
@orig = 'sentry'
|
|
14
|
+
@data = "vYfMxtVB8ezXmQKSNqTC9sPgi8TbsYRxWd7DVbpprzyuEdZ7gftJ/0IXsbXm\nXCU08bTAl0uEFm7dau+eJMXEJg==\n"
|
|
15
|
+
@encrypted_data = "q2obYAITmK93ylzVS01mJx1jSlnmylMX15nFpb4uKesVgnqvtzBRHZ/SK+Nm\nEzceIoAcJc3DHosVa4VUE/aK/A==\n"
|
|
16
|
+
Sentry::AsymmetricSentry.default_public_key_file = nil
|
|
17
|
+
Sentry::AsymmetricSentry.default_private_key_file = nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_should_decrypt_files
|
|
21
|
+
set_key_files @public_key_file, @private_key_file
|
|
22
|
+
assert_equal @orig, @sentry.decrypt_from_base64(@data)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_should_decrypt_files_with_encrypted_key
|
|
26
|
+
set_key_files @encrypted_public_key_file, @encrypted_private_key_file
|
|
27
|
+
assert_equal @orig, @sentry.decrypt_from_base64(@encrypted_data, @key)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_should_read_key_files
|
|
31
|
+
assert !@sentry.public?
|
|
32
|
+
assert !@sentry.private?
|
|
33
|
+
set_key_files @public_key_file, @private_key_file
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_should_read_encrypted_key_files
|
|
37
|
+
assert !@sentry.public?
|
|
38
|
+
assert !@sentry.private?
|
|
39
|
+
set_key_files @encrypted_public_key_file, @encrypted_private_key_file
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_should_decrypt_files_with_default_key
|
|
43
|
+
set_default_key_files @public_key_file, @private_key_file
|
|
44
|
+
assert_equal @orig, @sentry.decrypt_from_base64(@data)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_should_decrypt_files_with_default_encrypted_key
|
|
48
|
+
set_default_key_files @encrypted_public_key_file, @encrypted_private_key_file
|
|
49
|
+
assert_equal @orig, @sentry.decrypt_from_base64(@encrypted_data, @key)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_should_decrypt_files_with_default_key_using_class_method
|
|
53
|
+
set_default_key_files @public_key_file, @private_key_file
|
|
54
|
+
assert_equal @orig, Sentry::AsymmetricSentry.decrypt_from_base64(@data)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def test_should_decrypt_files_with_default_encrypted_key_using_class_method
|
|
58
|
+
set_default_key_files @encrypted_public_key_file, @encrypted_private_key_file
|
|
59
|
+
assert_equal @orig, Sentry::AsymmetricSentry.decrypt_from_base64(@encrypted_data, @key)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_should_read_key_files_with_default_key
|
|
63
|
+
assert !@sentry.public?
|
|
64
|
+
assert !@sentry.private?
|
|
65
|
+
set_default_key_files @public_key_file, @private_key_file
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def test_should_read_encrypted_key_files_with_default_key
|
|
69
|
+
assert !@sentry.public?
|
|
70
|
+
assert !@sentry.private?
|
|
71
|
+
set_default_key_files @encrypted_public_key_file, @encrypted_private_key_file
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
def set_key_files(public_key, private_key)
|
|
76
|
+
@sentry.public_key_file = public_key
|
|
77
|
+
@sentry.private_key_file = private_key
|
|
78
|
+
assert @sentry.private?
|
|
79
|
+
assert @sentry.public?
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def set_default_key_files(public_key, private_key)
|
|
83
|
+
Sentry::AsymmetricSentry.default_public_key_file = public_key
|
|
84
|
+
Sentry::AsymmetricSentry.default_private_key_file = private_key
|
|
85
|
+
assert @sentry.private?
|
|
86
|
+
assert @sentry.public?
|
|
87
|
+
end
|
|
88
|
+
end
|
data/test/database.yml
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
sqlite:
|
|
2
|
+
:adapter: sqlite
|
|
3
|
+
:dbfile: sentry_plugin.sqlite.db
|
|
4
|
+
sqlite3:
|
|
5
|
+
:adapter: sqlite3
|
|
6
|
+
:dbfile: sentry_plugin.sqlite3.db
|
|
7
|
+
postgresql:
|
|
8
|
+
:adapter: postgresql
|
|
9
|
+
:username: postgres
|
|
10
|
+
:password: postgres
|
|
11
|
+
:database: sentry_plugin_test
|
|
12
|
+
:min_messages: ERROR
|
|
13
|
+
mysql:
|
|
14
|
+
:adapter: mysql
|
|
15
|
+
:host: localhost
|
|
16
|
+
:username: root
|
|
17
|
+
:password: password
|
|
18
|
+
:database: sentry_plugin_test
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class User < ActiveRecord::Base
|
|
2
|
+
#define_read_methods
|
|
3
|
+
asymmetrically_encrypts :creditcard
|
|
4
|
+
|
|
5
|
+
#def self.validates_password
|
|
6
|
+
# validates_presence_of :password
|
|
7
|
+
# validates_presence_of :password, :on => :create
|
|
8
|
+
# validates_length_of :password, :in => 4..40
|
|
9
|
+
#end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
#class ShaUser < User
|
|
13
|
+
# validates_password
|
|
14
|
+
# validates_confirmation_of :password
|
|
15
|
+
# generates_crypted :password # sha is used by default
|
|
16
|
+
#end
|
|
17
|
+
#
|
|
18
|
+
#class DangerousUser < User # no password confirmation
|
|
19
|
+
## validates_password
|
|
20
|
+
# generates_crypted :password
|
|
21
|
+
#end
|
|
22
|
+
#
|
|
23
|
+
#class SymmetricUser < User
|
|
24
|
+
# validates_password
|
|
25
|
+
# generates_crypted :password, :mode => :symmetric
|
|
26
|
+
#end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
user_1:
|
|
2
|
+
id: 1
|
|
3
|
+
login: bob
|
|
4
|
+
password: "0XlmUuNpE2k=\n"
|
|
5
|
+
creditcard: "n0DW3do8BTv5caPgR52CVtZJ4IXh8Fmkpt7k2QnHaxIx63I9ILG+GKKzXuIM\nohtHRSnzcw3gk1gkeDpYml9CoQ==\n" # "sentry" with 8 characters of prepadding
|
|
6
|
+
user_2:
|
|
7
|
+
id: 2
|
|
8
|
+
login: fred
|
|
9
|
+
creditcard: "MiIZuZmc7znWzqZuPWYPVATXeJUrfxCNyVYibefs2hfvpna7Godk6RWILXfi\nqRRU0WehLLo1Mq5EuMTTnPi09A==\n" # "sentry" with 8 different characters of prepadding
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
OBNa1q8kbx8pyZZjIpr/pZV0oulE2czh5JlPW/13XsBvoz+A2zxA9gchhi6c
|
|
2
|
+
3yvfqgcZdojcsep+IiTqeg3gOPB2xNbedpP1lm+9tEfgdb9r1CLzRcURh7Hg
|
|
3
|
+
ufWgyEkS0lloz/YLy4hg9YDKetFNF9fnrk3xVwZPwFVuk4l/Unw1FTXLHsrq
|
|
4
|
+
KG27cR8mvNOow4bk4LVhk/avFSM85m3ITySEnyJsQQDzsI/RrWcQ7Js+8Ynv
|
|
5
|
+
esN51E/T0CYtkMEne2zSaD5qUTJlQ7Qtn4UUeZkpYjn4xQZPxw4OjL6zofg7
|
|
6
|
+
lsqElSv1/qP3QI8aKcQQklVsHRc5AgsxOFX4J6g6lo4kOGOwn0Ex8IRDfOej
|
|
7
|
+
pq4SUDh9IXz+6FBieQrObB/xEsKysVwRSzXre6ObHlPFsigg5ekFPyCv5ZTz
|
|
8
|
+
0iP8+xe/FJRrYdR3r3F5pRkOy0pw9EqlrLjmOx3/fgxhLq8FWmcSBbH3h3SG
|
|
9
|
+
GkJlfHNjF77FTJjnHKzRS+5VpdW4IHbsjL+NlI1z9Ol//czYvSGv85NdJvkq
|
|
10
|
+
PmH3o0+uYdwY5PeSMOPV21nJ3dwiKlm5IMFasL3C5yVJNVTVZTS7vWdcgZ4U
|
|
11
|
+
XfWQ9Y266ibbqXPluv4nxt1+kgjxmPbjPdYrlB5t7a2+unzT3oE3f4VGOG+k
|
|
12
|
+
YqFg0ErHN+fu
|
data/test/keys/private
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
-----BEGIN RSA PRIVATE KEY-----
|
|
2
|
+
MIIBOwIBAAJBAL/xeY6aqFx6z1ThNOwgPgxv3tsonTlCj8VkN3Ikumg6SzBuLxlV
|
|
3
|
+
i9gFQZ7K9Pv9o/7+xUTYODqBpVhwgLBeu2cCAwEAAQJAHyjFMfg7Yp/xLndMzxRA
|
|
4
|
+
3mX+yJckRtpeWo31TktWE3syks1r9OrfmxKiStM9kFRubeBHTihZrW92TYkROLxh
|
|
5
|
+
uQIhAPuftVTJZFDNxeYDKIMIMqwR8KZgtuf25cv4pTxYwPqLAiEAw0gNwDJHBkvo
|
|
6
|
+
da4402pZNQmBA6qCSf0svDXqoEoaShUCIGBma340Oe6LJ0pb42Vv+pnZtazIWMq9
|
|
7
|
+
2IQwmn1oM2bJAiEAhgP869mVRIzzi091UCG79tn+4DU0FPLasI+P5VD1mcECIQDb
|
|
8
|
+
3ndvbPcElVvdJgabxyWJJsNtBBNZYPsuc6NrQyShOw==
|
|
9
|
+
-----END RSA PRIVATE KEY-----
|
data/test/keys/public
ADDED
data/test/schema.rb
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
|
2
|
+
|
|
3
|
+
create_table "users", :force => true do |t|
|
|
4
|
+
t.column :password, :string, :limit => 255
|
|
5
|
+
t.column :creditcard, :string, :limit => 255
|
|
6
|
+
t.column :login, :string, :limit => 50
|
|
7
|
+
t.column :type, :string, :limit => 20
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'abstract_unit'
|
|
2
|
+
require 'fixtures/user'
|
|
3
|
+
|
|
4
|
+
class ShaSentryTest < Test::Unit::TestCase
|
|
5
|
+
def test_foo
|
|
6
|
+
assert true
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
#def setup
|
|
10
|
+
# Sentry::ShaSentry.salt = 'salt'
|
|
11
|
+
#end
|
|
12
|
+
#
|
|
13
|
+
#def test_should_encrypt
|
|
14
|
+
# assert_equal 'f438229716cab43569496f3a3630b3727524b81b', Sentry::ShaSentry.encrypt('test')
|
|
15
|
+
#end
|
|
16
|
+
#
|
|
17
|
+
#def test_should_encrypt_with_salt
|
|
18
|
+
# Sentry::ShaSentry.salt = 'different salt'
|
|
19
|
+
# assert_equal '18e3256d71529db8fa65b2eef24a69ddad7070f3', Sentry::ShaSentry.encrypt('test')
|
|
20
|
+
#end
|
|
21
|
+
#
|
|
22
|
+
#def test_should_encrypt_user_password
|
|
23
|
+
# u = ShaUser.new :login => 'bob'
|
|
24
|
+
# u.password = u.password_confirmation = 'test'
|
|
25
|
+
# assert u.save
|
|
26
|
+
# assert u.crypted_password = 'f438229716cab43569496f3a3630b3727524b81b'
|
|
27
|
+
#end
|
|
28
|
+
#
|
|
29
|
+
#def test_should_encrypt_user_password_without_confirmation
|
|
30
|
+
# u = DangerousUser.new :login => 'bob'
|
|
31
|
+
# u.password = 'test'
|
|
32
|
+
# assert u.save
|
|
33
|
+
# assert u.crypted_password = 'f438229716cab43569496f3a3630b3727524b81b'
|
|
34
|
+
#end
|
|
35
|
+
end
|