sdb_dal 0.0.9 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,114 @@
1
+ require "openssl"
2
+
3
+
4
+ module SdbDal
5
+ class Configuration
6
+ include OpenSSL
7
+
8
+ include OpenSSL::Cipher
9
+
10
+ attr_reader :repository_class
11
+ attr_reader :domain_prefix
12
+ attr_reader :clob_bucket
13
+ attr_reader :aws_key_id
14
+ attr_reader :aws_secret_key
15
+ attr_reader :memcache_servers
16
+ attr_reader :append_table_to_domain
17
+ attr_reader :fail_fast
18
+
19
+ attr_reader :cipher_key_file
20
+ attr_reader :cipher_iv_file
21
+
22
+ def self.singleton
23
+ @singleton ||= SdbDal::Configuration.new
24
+ return @singleton
25
+ end
26
+
27
+ def initialize()
28
+ sdb_dal_config=find_config_section
29
+
30
+ if sdb_dal_config
31
+ if sdb_dal_config.has_key?("repository_class")
32
+ @repository_class=eval sdb_dal_config["repository_class"]
33
+ else
34
+ @repository_class=SdbDal::Repository
35
+ end
36
+ @domain_prefix= sdb_dal_config["sdb_domain_prefix"] || ""
37
+ @clob_bucket= sdb_dal_config["s3_clob_bucket"]
38
+ @aws_key_id = sdb_dal_config["aws_key_id"]
39
+ @aws_secret_key = sdb_dal_config["aws_secret_key"]
40
+ @memcache_servers= sdb_dal_config['memcache_servers']
41
+ @memcache_servers = memcache_servers.split(",") if memcache_servers
42
+
43
+ @append_table_to_domain=sdb_dal_config['append_table_to_domain']
44
+ @fail_fast=sdb_dal_config['fail_fast']
45
+
46
+ @cipher_key_file = sdb_dal_config['cipher_key_file']
47
+ @cipher_key_file ||= "./.cipher_key"
48
+ if @cipher_key_file and File.exists?(@cipher_key_file)
49
+ @cipher_key=File.open(@cipher_key_file).read
50
+ end
51
+
52
+ @cipher_iv_file ||= "./cipher_iv"
53
+ @cipher_iv_file=sdb_dal_config['cipher_iv_file']
54
+ if @cipher_iv_file and File.exists?(@cipher_iv_file)
55
+ @cipher_iv=File.open(@cipher_iv_file).read
56
+ end
57
+
58
+ end
59
+ end
60
+
61
+ def cipher_key
62
+ unless @cipher_key
63
+ @cipher = Cipher.new("AES-256-CBC")
64
+
65
+ @cipher_key = @cipher.random_key()
66
+ File.open(self.cipher_key_file,'w').write(@cipher_key)
67
+ end
68
+ return @cipher_key
69
+ end
70
+
71
+ def cipher_iv
72
+ unless @cipher_iv
73
+ @cipher = Cipher.new("AES-256-CBC")
74
+
75
+ @cipher_iv = @cipher.random_iv()
76
+ File.open(self.cipher_iv_file,'w').write(@cipher_iv)
77
+ end
78
+ return @cipher_iv
79
+ end
80
+
81
+ def find_config_section
82
+ return $sdb_dal_config if $sdb_dal_config
83
+ config_file_path=nil
84
+ config_section=nil
85
+
86
+ #when using rails use config/database.yml
87
+ if Object.const_defined?(:RAILS_ROOT) and ENV.has_key?('RAILS_ENV')
88
+ config_file_path = File.join("#{RAILS_ROOT}","config","database.yml")
89
+
90
+ config_section =ENV['RAILS_ENV']+"_sdb_dal"
91
+
92
+
93
+
94
+ else
95
+ #when not using rails use try database.yml then try config/database.yml
96
+
97
+ if File.exists?("database.yml")
98
+
99
+ config_file_path = "database.yml"
100
+ elsif File.exists?(File.join("config", "database.yml"))
101
+ config_file_path = File.join("config", "database.yml")
102
+ end
103
+
104
+ config_section =(ENV['SDB_DAL_ENV'] || "production")+"_sdb_dal"
105
+ end
106
+ if config_file_path and config_section
107
+ config_file = YAML.load(File.open( config_file_path))
108
+
109
+ $sdb_dal_config = config_file[config_section]
110
+ end
111
+ return $sdb_dal_config
112
+ end
113
+ end
114
+ end
@@ -1,29 +1,36 @@
1
1
  require "openssl"
2
- include OpenSSL
3
2
 
4
3
  module SdbDal
5
4
  class Crypto
5
+ include OpenSSL
6
+
6
7
  include OpenSSL::Cipher
7
8
  def initialize(options={})
8
- key_dir=options[:key_dir] if options.has_key?(:key_dir)
9
- key_dir ||="/tmp"
10
9
  @cipher = Cipher.new("AES-256-CBC")
11
10
 
12
- if File.exists?(key_dir+'/.cipher_key')
13
- @key=File.open(key_dir+'/.cipher_key').read
14
- else
15
- @key = @cipher.random_key()
16
- File.open(key_dir+"/.cipher_key",'w').write(@key)
11
+ @key=options[:cipher_key] if options.has_key?(:cipher_key)
12
+ @iv=options[:cipher_iv] if options.has_key?(:cipher_iv)
13
+
14
+ unless @key
15
+ key_dir=options[:key_dir] if options.has_key?(:key_dir)
16
+ key_dir ||="/tmp"
17
+
18
+ if File.exists?(key_dir+'/.cipher_key')
19
+ @key=File.open(key_dir+'/.cipher_key').read
20
+ else
21
+ @key = @cipher.random_key()
22
+ File.open(key_dir+"/.cipher_key",'w').write(@key)
23
+ end
17
24
  end
18
25
 
19
26
 
20
-
21
-
22
- if File.exists?(key_dir+'/.cipher_iv')
23
- @iv=File.open(key_dir+'/.cipher_iv').read
24
- else
25
- @iv = @cipher.random_iv()
26
- File.open(key_dir+"/.cipher_iv",'w').write(@iv)
27
+ unless @iv
28
+ if File.exists?(key_dir+'/.cipher_iv')
29
+ @iv=File.open(key_dir+'/.cipher_iv').read
30
+ else
31
+ @iv = @cipher.random_iv()
32
+ File.open(key_dir+"/.cipher_iv",'w').write(@iv)
33
+ end
27
34
  end
28
35
  end
29
36
 
@@ -1,5 +1,7 @@
1
1
  module SdbDal
2
2
  require File.dirname(__FILE__) +'/sdb_formatter.rb'
3
+ require File.dirname(__FILE__) +'/crypto.rb'
4
+ require File.dirname(__FILE__) +'/configuration.rb'
3
5
  class DomainAttributeDescription
4
6
  include SdbFormatter
5
7
  attr_accessor :name
@@ -9,7 +11,10 @@ class DomainAttributeDescription
9
11
  attr_accessor :is_encrypted
10
12
 
11
13
  def self.crypto
12
- @@crypto||=Crypto.new
14
+ @@crypto||=Crypto.new(
15
+ :cipher_key => Configuration.singleton.cipher_key,
16
+ :cipher_iv => Configuration.singleton.cipher_iv
17
+ )
13
18
  @@crypto
14
19
  end
15
20
 
@@ -5,7 +5,6 @@ require File.dirname(__FILE__) +"/reference.rb"
5
5
  require File.dirname(__FILE__) +"/index_description.rb"
6
6
  require File.dirname(__FILE__) +"/lazy_loading_text.rb"
7
7
  require File.dirname(__FILE__) +"/repository_factory.rb"
8
- require File.dirname(__FILE__) +"/crypto.rb"
9
8
  module SdbDal
10
9
  class DomainObject
11
10
  @@is_encrypted=false
@@ -259,6 +258,31 @@ module SdbDal
259
258
  find(#{scope},options)
260
259
  end
261
260
  GETTERDONE
261
+
262
+ if options.has_key?(:enum)
263
+ attribute_name=attribute_description.name.to_s
264
+ x=""
265
+ enum_val=1
266
+ options[:enum].each { |enum|
267
+ x<<"#{enum.to_s.upcase} = #{enum_val}\n"
268
+ enum_val+=1
269
+ class_eval <<-GETTERDONE
270
+ def is_#{attribute_name}_#{enum.to_s.downcase}?
271
+ #{attribute_name} == #{attribute_name.capitalize}::#{enum.to_s.upcase}
272
+ end
273
+ GETTERDONE
274
+
275
+ }
276
+ class_eval <<-GETTERDONE
277
+ class #{attribute_name.capitalize}
278
+ #{x}
279
+ end
280
+ GETTERDONE
281
+
282
+
283
+ end
284
+
285
+
262
286
  end
263
287
 
264
288
  def self.belongs_to(domain_class,foreign_key_attribute=nil,accesser_attribute_name=nil,options={})
@@ -310,7 +334,7 @@ module SdbDal
310
334
  GETTERDONE
311
335
  index("#{foreign_key_attribute}_index".to_sym,[foreign_key_attribute])
312
336
  end
313
-
337
+
314
338
  end
315
339
  def self.many_to_many(domain_class,
316
340
  through_class,
@@ -4,11 +4,6 @@ class IndexDescription < DomainAttributeDescription
4
4
  include SdbFormatter
5
5
  attr_accessor :columns
6
6
 
7
-
8
- def self.crypto
9
- @@crypto||=Crypto.new
10
- @@crypto
11
- end
12
7
 
13
8
  def initialize(name,columns,is_encrypted=false)
14
9
  self.name=name
@@ -1,4 +1,5 @@
1
1
  require "sdb_dal/repository.rb"
2
+ require "sdb_dal/configuration.rb"
2
3
  require "sdb_dal/memory_repository.rb"
3
4
 
4
5
  module SdbDal
@@ -8,98 +9,59 @@ module SdbDal
8
9
  $sdb_dal_config=nil
9
10
  $repository=nil
10
11
  end
12
+
11
13
  def self.instance=(repo)
12
14
  @repository=repo
13
- end
15
+ end
16
+
14
17
  def self.instance(options={})
18
+
15
19
  if options and options.has_key?(:repository)
16
20
  return options[:repository]
17
21
  end
18
- if @repository
19
- return @repository
20
- end
21
-
22
- sdb_dal_config=find_config_section
23
22
 
24
- if sdb_dal_config
25
- if sdb_dal_config.has_key?("repository_class")
26
- repo_class=eval sdb_dal_config["repository_class"]
27
- else
28
- repo_class=SdbDal::Repository
29
- end
30
- domain_prefix= sdb_dal_config["sdb_domain_prefix"] || ""
31
- clob_bucket= sdb_dal_config["s3_clob_bucket"]
32
- aws_key_id = sdb_dal_config["aws_key_id"]
33
- aws_secret_key = sdb_dal_config["aws_secret_key"]
34
- memcache_servers= sdb_dal_config['memcache_servers']
35
- memcache_servers = memcache_servers.split(",") if memcache_servers
23
+ return @repository if @repository
36
24
 
37
- append_table_to_domain=sdb_dal_config['append_table_to_domain']
38
- options={}
39
- if sdb_dal_config.has_key?('fail_fast')
40
- options[:fail_fast]=sdb_dal_config['fail_fast']
41
- end
42
- @repository= repo_class.new(domain_prefix,clob_bucket,aws_key_id,aws_secret_key,memcache_servers,nil,append_table_to_domain,options)
43
- end
25
+ config=SdbDal::Configuration.singleton
26
+
27
+ options[:fail_fast]=config.fail_fast
28
+ @repository= config.repository_class.new(
29
+ config.domain_prefix,
30
+ config.clob_bucket,
31
+ config.aws_key_id,
32
+ config.aws_secret_key,
33
+ config.memcache_servers,
34
+ nil,
35
+ config.append_table_to_domain,
36
+ options)
37
+
44
38
  end
45
39
 
46
40
 
47
41
 
48
- def self.find_config_section
49
- return $sdb_dal_config if $sdb_dal_config
50
- config_file_path=nil
51
- config_section=nil
52
-
53
- #when using rails use config/database.yml
54
- if const_defined?(:RAILS_ROOT) and ENV.has_key?('RAILS_ENV')
55
- config_file_path = File.join("#{RAILS_ROOT}","config","database.yml")
56
-
57
- config_section =ENV['RAILS_ENV']+"_sdb_dal"
58
-
59
-
60
-
61
- else
62
- #when not using rails use try database.yml then try config/database.yml
63
-
64
- if File.exists?("database.yml")
65
-
66
- config_file_path = "database.yml"
67
- elsif File.exists?(File.join("config", "database.yml"))
68
- config_file_path = File.join("config", "database.yml")
69
- end
70
-
71
- config_section =(ENV['SDB_DAL_ENV'] || "production")+"_sdb_dal"
72
- end
73
- if config_file_path and config_section
74
- config_file = YAML.load(File.open( config_file_path))
75
-
76
- $sdb_dal_config = config_file[config_section]
77
- end
78
- return $sdb_dal_config
79
- end
80
42
  def self.set_repository(repository)
81
43
  @repository=repository
82
44
  end
83
45
 
84
46
  end
85
- def self.qualified_const_get(str)
86
- path = str.to_s.split('::')
87
- from_root = path[0].empty?
88
- if from_root
89
- from_root = []
90
- path = path[1..-1]
91
- else
92
- start_ns = ((Class === self)||(Module === self)) ? self : self.class
93
- from_root = start_ns.to_s.split('::')
94
- end
95
- until from_root.empty?
96
- begin
97
- return (from_root+path).inject(Object) { |ns,name| ns.const_get(name) }
98
- rescue NameError
99
- from_root.delete_at(-1)
100
- end
101
- end
102
- path.inject(Object) { |ns,name| ns.const_get(name) }
103
- end
47
+ # def self.qualified_const_get(str)
48
+ # path = str.to_s.split('::')
49
+ # from_root = path[0].empty?
50
+ # if from_root
51
+ # from_root = []
52
+ # path = path[1..-1]
53
+ # else
54
+ # start_ns = ((Class === self)||(Module === self)) ? self : self.class
55
+ # from_root = start_ns.to_s.split('::')
56
+ # end
57
+ # until from_root.empty?
58
+ # begin
59
+ # return (from_root+path).inject(Object) { |ns,name| ns.const_get(name) }
60
+ # rescue NameError
61
+ # from_root.delete_at(-1)
62
+ # end
63
+ # end
64
+ # path.inject(Object) { |ns,name| ns.const_get(name) }
65
+ # end
104
66
  end
105
67
 
data/lib/sdb_dal/s3.rb CHANGED
@@ -40,7 +40,7 @@ module S3
40
40
  METADATA_PREFIX = 'x-amz-meta-'
41
41
  AMAZON_HEADER_PREFIX = 'x-amz-'
42
42
  AMAZON_TOKEN_HEADER_PREFIX = "x-amz-security-token"
43
- CANONICAL_STRING_WORTHY_PATH_ARGS=['acl','torrent','logging','requestPayment']
43
+
44
44
  # builds the canonical string for signing.
45
45
  def S3.canonical_string(method, bucket="", path="", path_args={}, headers={}, expires=nil)
46
46
  interesting_headers = {}
@@ -86,13 +86,14 @@ module S3
86
86
  # append a slash regardless
87
87
  buf << "/#{path}"
88
88
 
89
- # if there is an acl, logging, torrent, or requestPayment parameter
89
+ # if there is an acl, logging, or torrent parameter
90
90
  # add them to the string
91
- CANONICAL_STRING_WORTHY_PATH_ARGS.each do |arg_name|
92
- if path_args.has_key?(arg_name)
93
- buf << "?#{arg_name}"
94
- break
95
- end
91
+ if path_args.has_key?('acl')
92
+ buf << '?acl'
93
+ elsif path_args.has_key?('torrent')
94
+ buf << '?torrent'
95
+ elsif path_args.has_key?('logging')
96
+ buf << '?logging'
96
97
  end
97
98
 
98
99
  return buf
@@ -143,7 +144,7 @@ module S3
143
144
  is_secure=true,
144
145
  server=DEFAULT_HOST,
145
146
  port=PORTS_BY_SECURITY[is_secure],
146
- calling_format=CallingFormat::REGULAR)
147
+ calling_format=CallingFormat::SUBDOMAIN)
147
148
  @aws_access_key_id = aws_access_key_id
148
149
  @aws_secret_access_key = aws_secret_access_key
149
150
  @server = server
@@ -181,6 +182,26 @@ module S3
181
182
  make_request('PUT', bucket, CGI::escape(key), {}, headers, object.data, object.metadata)
182
183
  )
183
184
  end
185
+ def copy(source_bucket, source_key, destination_bucket,destination_key, headers={})
186
+ headers['x-amz-copy-source']="#{source_bucket}/#{source_key}"
187
+ headers['x-amz-metadata-directive']="REPLACE "
188
+ return GetResponse.new(make_request('PUT', destination_bucket, CGI::escape(destination_key),{}, headers))
189
+ end
190
+
191
+ def get_head(bucket, key, headers={})
192
+ return GetResponse.new(make_request('HEAD',bucket, CGI::escape(key),{}, headers))
193
+ end
194
+ def get_content_type(bucket, key, headers={})
195
+ response= get_head(bucket, key, headers)
196
+ if response.http_response.code=='404'
197
+ return nil
198
+
199
+ elsif response.http_response.code=='200'
200
+ return response.http_response.header.content_type
201
+ end
202
+ raise response.http_response.code
203
+ end
204
+
184
205
 
185
206
  def get(bucket, key, headers={})
186
207
  return GetResponse.new(make_request('GET', bucket, CGI::escape(key), {}, headers))
@@ -220,39 +241,6 @@ module S3
220
241
  )
221
242
  end
222
243
 
223
-
224
- #payer may be RequestPayer::REQUESTER or RequestPayer::BUCKET_OWNER
225
- def put_bucket_request_payment( bucket, payer=RequestPayer::REQUESTER, headers={})
226
-
227
- body = "<RequestPaymentConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Payer>#{payer}</Payer></RequestPaymentConfiguration>"
228
-
229
- result= make_request(
230
- 'PUT',
231
- bucket,
232
- '',
233
- {'requestPayment' => nil },
234
- headers,
235
- body,
236
- {} )
237
- return GetResponse.new(result)
238
-
239
- end
240
-
241
- def get_bucket_request_payment( bucket, headers={})
242
-
243
- result= make_request(
244
- 'GET',
245
- bucket,
246
- '',
247
- {'requestPayment' => nil },
248
- headers,
249
- '',
250
- {} )
251
- return GetResponse.new(result)
252
-
253
- end
254
-
255
-
256
244
  def list_all_my_buckets(headers={})
257
245
  return ListAllMyBucketsResponse.new(make_request('GET', '', '', {}, headers))
258
246
  end
@@ -306,6 +294,7 @@ module S3
306
294
  return http.request(req)
307
295
  end
308
296
  end
297
+
309
298
  end
310
299
 
311
300
  def method_to_request_class(method)
@@ -316,6 +305,8 @@ module S3
316
305
  return Net::HTTP::Put
317
306
  when 'DELETE'
318
307
  return Net::HTTP::Delete
308
+ when 'HEAD'
309
+ return Net::HTTP::Head
319
310
  else
320
311
  raise "Unsupported method #{method}"
321
312
  end
@@ -356,10 +347,6 @@ module S3
356
347
  end
357
348
  end
358
349
 
359
- module RequestPayer
360
- REQUESTER="Requester"
361
- BUCKET_OWNER="BucketOwner"
362
- end
363
350
  # class for storing calling format constants
364
351
  module CallingFormat
365
352
  REGULAR = 0 # http://s3.amazonaws.com/bucket/key
@@ -435,6 +422,7 @@ module S3
435
422
 
436
423
  # we have one, add him to the entries list
437
424
  def tag_end(name)
425
+ # this prefix is the one we echo back from the request
438
426
  # this prefix is the one we echo back from the request
439
427
  if name == 'Name'
440
428
  @properties.name = @curr_text
data/lib/sdb_dal.rb CHANGED
@@ -1,10 +1,10 @@
1
- require "sdb_dal/repository.rb"
2
- require "sdb_dal/repository_factory.rb"
3
- require "sdb_dal/acts_as_sdb_application.rb"
4
- require "sdb_dal/memory_repository.rb"
5
- require "sdb_dal/domain_object.rb"
6
- require "sdb_dal/storage.rb"
7
- require "sdb_dal/s3.rb"
8
- require "sdb_dal/attribute_range.rb"
9
- require "sdb_dal/and_condition.rb"
10
- require "sdb_dal/or_condition.rb"
1
+ require "#{File.dirname(__FILE__)}/sdb_dal/repository.rb"
2
+ require "#{File.dirname(__FILE__)}/sdb_dal/repository_factory.rb"
3
+ require "#{File.dirname(__FILE__)}/sdb_dal/acts_as_sdb_application.rb"
4
+ require "#{File.dirname(__FILE__)}/sdb_dal/memory_repository.rb"
5
+ require "#{File.dirname(__FILE__)}/sdb_dal/domain_object.rb"
6
+ require "#{File.dirname(__FILE__)}/sdb_dal/storage.rb"
7
+ require "#{File.dirname(__FILE__)}/sdb_dal/s3.rb"
8
+ require "#{File.dirname(__FILE__)}/sdb_dal/attribute_range.rb"
9
+ require "#{File.dirname(__FILE__)}/sdb_dal/and_condition.rb"
10
+ require "#{File.dirname(__FILE__)}/sdb_dal/or_condition.rb"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdb_dal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Knight
@@ -9,7 +9,7 @@ autorequire: sdb_dal
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-02 00:00:00 -08:00
12
+ date: 2009-02-10 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,6 +26,7 @@ files:
26
26
  - ./lib/sdb_dal/acts_as_sdb_application.rb
27
27
  - ./lib/sdb_dal/and_condition.rb
28
28
  - ./lib/sdb_dal/attribute_range.rb
29
+ - ./lib/sdb_dal/configuration.rb
29
30
  - ./lib/sdb_dal/crypto.rb
30
31
  - ./lib/sdb_dal/domain_attribute_description.rb
31
32
  - ./lib/sdb_dal/domain_object.rb