dbi-dbrc 1.1.8-x86-mingw32 → 1.1.9-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'rake'
2
+ require 'rake/clean'
2
3
  require 'rake/testtask'
3
4
 
5
+ CLEAN.include("**/*.gem", "**/*.rbc")
6
+
4
7
  namespace :gem do
5
8
  desc "Remove any gem files."
6
9
  task :clean do
@@ -20,21 +23,23 @@ namespace :gem do
20
23
  end
21
24
  end
22
25
 
23
- Rake::TestTask.new do |t|
24
- t.warning = true
25
- t.verbose = true
26
- end
26
+ namespace :test do
27
+ Rake::TestTask.new(:all) do |t|
28
+ t.warning = true
29
+ t.verbose = true
30
+ end
27
31
 
28
- Rake::TestTask.new(:test_xml) do |t|
29
- t.warning = true
30
- t.verbose = true
31
- t.test_files = FileList['test/test_dbi_dbrc_xml.rb']
32
- end
32
+ Rake::TestTask.new(:xml) do |t|
33
+ t.warning = true
34
+ t.verbose = true
35
+ t.test_files = FileList['test/test_dbi_dbrc_xml.rb']
36
+ end
33
37
 
34
- Rake::TestTask.new(:test_yml) do |t|
35
- t.warning = true
36
- t.verbose = true
37
- t.test_files = FileList['test/test_dbi_dbrc_yml.rb']
38
+ Rake::TestTask.new(:yml) do |t|
39
+ t.warning = true
40
+ t.verbose = true
41
+ t.test_files = FileList['test/test_dbi_dbrc_yml.rb']
42
+ end
38
43
  end
39
44
 
40
- task :default => :test
45
+ task :default => 'test:all'
data/dbi-dbrc.gemspec CHANGED
@@ -2,12 +2,12 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'dbi-dbrc'
5
- spec.version = '1.1.8'
5
+ spec.version = '1.1.9'
6
6
  spec.author = 'Daniel Berger'
7
7
  spec.email = 'djberg96@gmail.com'
8
8
  spec.license = 'Artistic 2.0'
9
9
  spec.summary = 'A simple way to avoid hard-coding passwords with DBI'
10
- spec.homepage = 'http://www.rubyforge.org/projects/shards'
10
+ spec.homepage = 'https://github.com/djberg96/dbi-dbrc'
11
11
  spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
12
12
  spec.test_files = Dir['test/test*.rb']
13
13
 
@@ -17,11 +17,12 @@ Gem::Specification.new do |spec|
17
17
  spec.add_dependency('sys-admin', '>= 1.5.2')
18
18
  spec.add_development_dependency('test-unit')
19
19
 
20
- if Config::CONFIG['host_os'] =~ /mswin|msdos|win32|mingw|cygwin/i
20
+ if File::ALT_SEPARATOR
21
21
  spec.add_dependency('win32-file', '>= 0.6.6')
22
22
  spec.add_dependency('win32-dir', '>= 0.3.7')
23
23
  spec.add_dependency('win32-process', '>= 0.6.2')
24
24
  spec.platform = Gem::Platform::CURRENT
25
+ spec.platform.cpu = 'universal'
25
26
  end
26
27
 
27
28
  spec.description = <<-EOF
data/lib/dbi/dbrc.rb CHANGED
@@ -1,309 +1,307 @@
1
- require 'rbconfig'
2
- require 'rubygems'
3
-
4
- if Config::CONFIG['host_os'] =~ /mswin|msdos|win32|mingw|cygwin/i
5
- require 'win32/dir'
6
- require 'win32/file'
7
- require 'win32/process'
8
- end
9
-
10
- require 'sys/admin'
11
-
12
- # The DBI module serves as a namespace only.
13
- module DBI
14
-
15
- # The DBRC class encapsulates a database resource config file.
16
- class DBRC
17
-
18
- # This error is raised if anything fails trying to read the config file.
19
- class Error < StandardError; end
20
-
21
- # The version of the dbi-dbrc library
22
- VERSION = '1.1.8'
23
-
24
- @@windows = Config::CONFIG['host_os'] =~ /mswin|msdos|win32|mingw|cygwin/i
25
-
26
- # The database or host to be connected to.
27
- attr_accessor :database
28
-
29
- # The user name used for the database or host connection.
30
- attr_accessor :user
31
-
32
- # The password associated with the database or host.
33
- attr_accessor :password
34
-
35
- # The driver associated with the database. This is used to internally to
36
- # construct the DSN.
37
- attr_accessor :driver
38
-
39
- # Data source name, e.g. "dbi:OCI8:your_database".
40
- attr_accessor :dsn
41
-
42
- # The maximum number of reconnects a program should make before giving up.
43
- attr_accessor :maximum_reconnects
44
-
45
- # The timeout, in seconds, for each connection attempt.
46
- attr_accessor :timeout
47
-
48
- # The interval, in seconds, between each connection attempt.
49
- attr_accessor :interval
50
-
51
- # The directory where the .dbrc file is stored.
52
- attr_accessor :dbrc_dir
53
-
54
- # The full path to the .dbrc file.
55
- attr_accessor :dbrc_file
56
-
57
- # Returns a new DBI::DBRC object. The contents of the object depend on
58
- # the arguments passed to the constructor. If only a database name is
59
- # passed, then the first entry found in the .dbrc file that matches that
60
- # database is parsed. If a user name is also included, then the first
61
- # entry that matches both the database and user name is parsed.
62
- #
63
- # If a directory is passed as the third argument, then DBRC will look
64
- # in that directory, instead of the default directory, for the .dbrc
65
- # file.
66
- #
67
- # If an entry cannot be found for the database, or database plus user
68
- # combination, then a Error is raised. If the .dbrc file cannot be
69
- # found, or is setup improperly with regards to permissions or properties
70
- # then a DBI::DBRC::Error is raised.
71
- #
72
- # See the README for the rules regarding .dbrc files and permissions.
73
- #
74
- # Note that this library can also be used as a general password
75
- # storage mechanism. In that case simply treat the 'database' as the
76
- # host name, and ignore the DBI::DBRC#dsn and DBI::DBRC#driver methods.
77
- #
78
- # Examples:
79
- #
80
- # # Find the first match for 'some_database'
81
- # DBI::DBRC.new('some_database')
82
- #
83
- # # Find the first match for 'foo_user@some_database'
84
- # DBI::DBRC.new('some_database', 'foo_user')
85
- #
86
- # # Find the first match for 'foo_user@some_database' under /usr/local
87
- # DBI::DBRC.new('some_database', 'foo_usr', '/usr/local')
88
- #
89
- def initialize(database, user=nil, dbrc_dir=nil)
90
- if dbrc_dir.nil?
91
- uid = Process.uid
92
- home = ENV['HOME'] || ENV['USERPROFILE']
93
-
94
- if home.nil?
95
- if @@windows
96
- home ||= Sys::Admin.get_user(uid, :localaccount => true).dir
97
- else
98
- home ||= Sys::Admin.get_user(uid).dir
99
- end
100
- end
101
-
102
- # Default to the app data directory on Windows, or root on Unix, if
103
- # no home dir can be found.
104
- if home.nil?
105
- if @@windows
106
- home = Dir::APPDATA
107
- else
108
- home = '/'
109
- end
110
- end
111
-
112
- @dbrc_file = File.join(home, '.dbrc')
113
- else
114
- raise Error, 'bad directory' unless File.directory?(dbrc_dir)
115
- @dbrc_file = File.join(dbrc_dir, '.dbrc')
116
- end
117
-
118
- @dbrc_dir = dbrc_dir
119
- @database = database
120
- @user = user
121
- encrypted = false # Win32 only
122
-
123
- @driver = nil
124
- @interval = nil
125
- @timeout = nil
126
- @maximum_reconnects = nil
127
-
128
- check_file()
129
-
130
- # Decrypt and re-encrypt the file if we're on MS Windows and the
131
- # file is encrypted.
132
- begin
133
- if @@windows && File.encrypted?(@dbrc_file)
134
- file_was_encrypted = true
135
- File.decrypt(@dbrc_file)
136
- end
137
-
138
- parse_dbrc_config_file()
139
- validate_data()
140
- convert_numeric_strings()
141
- create_dsn_string()
142
- ensure
143
- if @@windows && file_was_encrypted
144
- File.encrypt(@dbrc_file)
145
- end
146
- end
147
- end
148
-
149
- # Inspection of the DBI::DBRC object. This is identical to the standard
150
- # Ruby Object#inspect, except that the password field is filtered.
151
- #
152
- def inspect
153
- str = instance_variables.map{ |iv|
154
- if iv == '@password'
155
- "#{iv}=[FILTERED]"
156
- else
157
- "#{iv}=#{instance_variable_get(iv).inspect}"
158
- end
159
- }.join(', ')
160
-
161
- "#<#{self.class}:0x#{(self.object_id*2).to_s(16)} " << str << ">"
162
- end
163
-
164
- private
165
-
166
- # Ensure that the user/password has been set
167
- def validate_data
168
- unless @user
169
- raise Error, "no user found associated with #{@database}"
170
- end
171
-
172
- unless @password
173
- raise Error, "password not defined for #{@user}@#{@database}"
174
- end
175
- end
176
-
177
- # Converts strings that should be numbers into actual numbers
178
- def convert_numeric_strings
179
- @interval = @interval.to_i if @interval
180
- @timeout = @timeout.to_i if @timeout
181
- @maximum_reconnects = @maximum_reconnects.to_i if @maximum_reconnects
182
- end
183
-
184
- # Create the dsn string if the driver is defined
185
- def create_dsn_string
186
- @dsn = "dbi:#{@driver}:#{@database}" if @driver
187
- end
188
-
189
- # Check ownership and permissions
190
- def check_file(file=@dbrc_file)
191
- File.open(file){ |f|
192
- # Permissions must be set to 600 or better on Unix systems.
193
- # Must be hidden on Win32 systems.
194
- if @@windows
195
- unless File.hidden?(file)
196
- raise Error, "The .dbrc file must be hidden"
197
- end
198
- else
199
- unless (f.stat.mode & 077) == 0
200
- raise Error, "Bad .dbrc file permissions"
201
- end
202
- end
203
-
204
- # Only the owner may use it
205
- unless f.stat.owned?
206
- raise Error, "Not owner of .dbrc file"
207
- end
208
- }
209
- end
210
-
211
- # Parse the text out of the .dbrc file. This is the only method you
212
- # need to redefine if writing your own config handler.
213
- def parse_dbrc_config_file(file=@dbrc_file)
214
- IO.foreach(file){ |line|
215
- next if line =~ /^#/ # Ignore comments
216
- db, user, pwd, driver, timeout, max, interval = line.split
217
-
218
- next unless @database == db
219
-
220
- if @user
221
- next unless @user == user
222
- end
223
-
224
- @user = user
225
- @password = pwd
226
- @driver = driver
227
- @timeout = timeout
228
- @maximum_reconnects = max
229
- @interval = interval
230
- return
231
- }
232
-
233
- # If we reach here it means the database and/or user wasn't found
234
- if @user
235
- err = "no record found for #{@user}@#{@database}"
236
- else
237
- err = "no record found for #{@database}"
238
- end
239
-
240
- raise Error, err
241
- end
242
-
243
- alias_method(:db, :database)
244
- alias_method(:db=, :database=)
245
- alias_method(:passwd, :password)
246
- alias_method(:passwd=, :password=)
247
- alias_method(:max_reconn, :maximum_reconnects)
248
- alias_method(:max_reconn=, :maximum_reconnects=)
249
- alias_method(:time_out, :timeout)
250
- alias_method(:time_out=, :timeout=)
251
- alias_method(:host, :database)
252
- end
253
-
254
- # A subclass of DBRC designed to handle .dbrc files in XML format. The
255
- # public methods of this class are identical to DBRC.
256
- class XML < DBRC
257
- require "rexml/document"
258
- include REXML
259
-
260
- private
261
-
262
- def parse_dbrc_config_file(file=@dbrc_file)
263
- doc = Document.new(File.new(file))
264
- fields = %w/user password driver interval timeout maximum_reconnects/
265
- doc.elements.each("/dbrc/database"){ |element|
266
- next unless element.attributes["name"] == database
267
- if @user
268
- next unless element.elements["user"].text == @user
269
- end
270
- fields.each{ |field|
271
- val = element.elements[field]
272
- unless val.nil?
273
- send("#{field}=",val.text)
274
- end
275
- }
276
- return
277
- }
278
- # If we reach here it means the database and/or user wasn't found
279
- raise Error, "No record found for #{@user}@#{@database}"
280
- end
281
- end
282
-
283
- # A subclass of DBRC designed to handle .dbrc files in YAML format. The
284
- # public methods of this class are identical to DBRC.
285
- class YML < DBRC
286
- require "yaml"
287
-
288
- private
289
-
290
- def parse_dbrc_config_file(file=@dbrc_file)
291
- config = YAML.load(File.open(file))
292
- config.each{ |hash|
293
- hash.each{ |db,info|
294
- next unless db == @database
295
- next unless @user == info["user"] if @user
296
- @user = info["user"]
297
- @password = info["password"]
298
- @driver = info["driver"]
299
- @interval = info["interval"]
300
- @timeout = info["timeout"]
301
- @maximum_reconnects = info["max_reconn"]
302
- return
303
- }
304
- }
305
- # If we reach this point, it means the database wasn't found
306
- raise Error, "No entry found for #{@user}@#{@database}"
307
- end
308
- end
309
- end
1
+ if File::ALT_SEPARATOR
2
+ require 'win32/dir'
3
+ require 'win32/file'
4
+ require 'win32/process'
5
+ end
6
+
7
+ require 'sys/admin'
8
+
9
+ # The DBI module serves as a namespace only.
10
+ module DBI
11
+
12
+ # The DBRC class encapsulates a database resource config file.
13
+ class DBRC
14
+
15
+ # This error is raised if anything fails trying to read the config file.
16
+ class Error < StandardError; end
17
+
18
+ # The version of the dbi-dbrc library
19
+ VERSION = '1.1.9'
20
+
21
+ @@windows = File::ALT_SEPARATOR
22
+
23
+ # The database or host to be connected to.
24
+ attr_accessor :database
25
+
26
+ # The user name used for the database or host connection.
27
+ attr_accessor :user
28
+
29
+ # The password associated with the database or host.
30
+ attr_accessor :password
31
+
32
+ # The driver associated with the database. This is used to internally to
33
+ # construct the DSN.
34
+ attr_accessor :driver
35
+
36
+ # Data source name, e.g. "dbi:OCI8:your_database".
37
+ attr_accessor :dsn
38
+
39
+ # The maximum number of reconnects a program should make before giving up.
40
+ attr_accessor :maximum_reconnects
41
+
42
+ # The timeout, in seconds, for each connection attempt.
43
+ attr_accessor :timeout
44
+
45
+ # The interval, in seconds, between each connection attempt.
46
+ attr_accessor :interval
47
+
48
+ # The directory where the .dbrc file is stored.
49
+ attr_accessor :dbrc_dir
50
+
51
+ # The full path to the .dbrc file.
52
+ attr_accessor :dbrc_file
53
+
54
+ # Returns a new DBI::DBRC object. The contents of the object depend on
55
+ # the arguments passed to the constructor. If only a database name is
56
+ # passed, then the first entry found in the .dbrc file that matches that
57
+ # database is parsed. If a user name is also included, then the first
58
+ # entry that matches both the database and user name is parsed.
59
+ #
60
+ # If a directory is passed as the third argument, then DBRC will look
61
+ # in that directory, instead of the default directory, for the .dbrc
62
+ # file.
63
+ #
64
+ # If an entry cannot be found for the database, or database plus user
65
+ # combination, then a Error is raised. If the .dbrc file cannot be
66
+ # found, or is setup improperly with regards to permissions or properties
67
+ # then a DBI::DBRC::Error is raised.
68
+ #
69
+ # See the README for the rules regarding .dbrc files and permissions.
70
+ #
71
+ # Note that this library can also be used as a general password
72
+ # storage mechanism. In that case simply treat the 'database' as the
73
+ # host name, and ignore the DBI::DBRC#dsn and DBI::DBRC#driver methods.
74
+ #
75
+ # Examples:
76
+ #
77
+ # # Find the first match for 'some_database'
78
+ # DBI::DBRC.new('some_database')
79
+ #
80
+ # # Find the first match for 'foo_user@some_database'
81
+ # DBI::DBRC.new('some_database', 'foo_user')
82
+ #
83
+ # # Find the first match for 'foo_user@some_database' under /usr/local
84
+ # DBI::DBRC.new('some_database', 'foo_usr', '/usr/local')
85
+ #
86
+ def initialize(database, user=nil, dbrc_dir=nil)
87
+ if dbrc_dir.nil?
88
+ uid = Process.uid
89
+ home = ENV['HOME'] || ENV['USERPROFILE']
90
+
91
+ if home.nil?
92
+ if @@windows
93
+ home ||= Sys::Admin.get_user(uid, :localaccount => true).dir
94
+ else
95
+ home ||= Sys::Admin.get_user(uid).dir
96
+ end
97
+ end
98
+
99
+ # Default to the app data directory on Windows, or root on Unix, if
100
+ # no home dir can be found.
101
+ if home.nil?
102
+ if @@windows
103
+ home = Dir::APPDATA
104
+ else
105
+ home = '/'
106
+ end
107
+ end
108
+
109
+ @dbrc_file = File.join(home, '.dbrc')
110
+ else
111
+ raise Error, 'bad directory' unless File.directory?(dbrc_dir)
112
+ @dbrc_file = File.join(dbrc_dir, '.dbrc')
113
+ end
114
+
115
+ @dbrc_dir = dbrc_dir
116
+ @database = database
117
+ @user = user
118
+
119
+ file_was_encrypted = false # Win32 only
120
+
121
+ @driver = nil
122
+ @interval = nil
123
+ @timeout = nil
124
+ @maximum_reconnects = nil
125
+
126
+ check_file()
127
+
128
+ # Decrypt and re-encrypt the file if we're on MS Windows and the
129
+ # file is encrypted.
130
+ begin
131
+ if @@windows && File.encrypted?(@dbrc_file)
132
+ file_was_encrypted = true
133
+ File.decrypt(@dbrc_file)
134
+ end
135
+
136
+ parse_dbrc_config_file()
137
+ validate_data()
138
+ convert_numeric_strings()
139
+ create_dsn_string()
140
+ ensure
141
+ if @@windows && file_was_encrypted
142
+ File.encrypt(@dbrc_file)
143
+ end
144
+ end
145
+ end
146
+
147
+ # Inspection of the DBI::DBRC object. This is identical to the standard
148
+ # Ruby Object#inspect, except that the password field is filtered.
149
+ #
150
+ def inspect
151
+ str = instance_variables.map{ |iv|
152
+ if iv == '@password'
153
+ "#{iv}=[FILTERED]"
154
+ else
155
+ "#{iv}=#{instance_variable_get(iv).inspect}"
156
+ end
157
+ }.join(', ')
158
+
159
+ "#<#{self.class}:0x#{(self.object_id*2).to_s(16)} " << str << ">"
160
+ end
161
+
162
+ private
163
+
164
+ # Ensure that the user/password has been set
165
+ def validate_data
166
+ unless @user
167
+ raise Error, "no user found associated with #{@database}"
168
+ end
169
+
170
+ unless @password
171
+ raise Error, "password not defined for #{@user}@#{@database}"
172
+ end
173
+ end
174
+
175
+ # Converts strings that should be numbers into actual numbers
176
+ def convert_numeric_strings
177
+ @interval = @interval.to_i if @interval
178
+ @timeout = @timeout.to_i if @timeout
179
+ @maximum_reconnects = @maximum_reconnects.to_i if @maximum_reconnects
180
+ end
181
+
182
+ # Create the dsn string if the driver is defined
183
+ def create_dsn_string
184
+ @dsn = "dbi:#{@driver}:#{@database}" if @driver
185
+ end
186
+
187
+ # Check ownership and permissions
188
+ def check_file(file=@dbrc_file)
189
+ File.open(file){ |f|
190
+ # Permissions must be set to 600 or better on Unix systems.
191
+ # Must be hidden on Win32 systems.
192
+ if @@windows
193
+ unless File.hidden?(file)
194
+ raise Error, "The .dbrc file must be hidden"
195
+ end
196
+ else
197
+ unless (f.stat.mode & 077) == 0
198
+ raise Error, "Bad .dbrc file permissions"
199
+ end
200
+ end
201
+
202
+ # Only the owner may use it
203
+ unless f.stat.owned?
204
+ raise Error, "Not owner of .dbrc file"
205
+ end
206
+ }
207
+ end
208
+
209
+ # Parse the text out of the .dbrc file. This is the only method you
210
+ # need to redefine if writing your own config handler.
211
+ def parse_dbrc_config_file(file=@dbrc_file)
212
+ IO.foreach(file){ |line|
213
+ next if line =~ /^#/ # Ignore comments
214
+ db, user, pwd, driver, timeout, max, interval = line.split
215
+
216
+ next unless @database == db
217
+
218
+ if @user
219
+ next unless @user == user
220
+ end
221
+
222
+ @user = user
223
+ @password = pwd
224
+ @driver = driver
225
+ @timeout = timeout
226
+ @maximum_reconnects = max
227
+ @interval = interval
228
+ return
229
+ }
230
+
231
+ # If we reach here it means the database and/or user wasn't found
232
+ if @user
233
+ err = "no record found for #{@user}@#{@database}"
234
+ else
235
+ err = "no record found for #{@database}"
236
+ end
237
+
238
+ raise Error, err
239
+ end
240
+
241
+ alias_method(:db, :database)
242
+ alias_method(:db=, :database=)
243
+ alias_method(:passwd, :password)
244
+ alias_method(:passwd=, :password=)
245
+ alias_method(:max_reconn, :maximum_reconnects)
246
+ alias_method(:max_reconn=, :maximum_reconnects=)
247
+ alias_method(:time_out, :timeout)
248
+ alias_method(:time_out=, :timeout=)
249
+ alias_method(:host, :database)
250
+ end
251
+
252
+ # A subclass of DBRC designed to handle .dbrc files in XML format. The
253
+ # public methods of this class are identical to DBRC.
254
+ class XML < DBRC
255
+ require "rexml/document"
256
+ include REXML
257
+
258
+ private
259
+
260
+ def parse_dbrc_config_file(file=@dbrc_file)
261
+ doc = Document.new(File.new(file))
262
+ fields = %w/user password driver interval timeout maximum_reconnects/
263
+ doc.elements.each("/dbrc/database"){ |element|
264
+ next unless element.attributes["name"] == database
265
+ if @user
266
+ next unless element.elements["user"].text == @user
267
+ end
268
+ fields.each{ |field|
269
+ val = element.elements[field]
270
+ unless val.nil?
271
+ send("#{field}=",val.text)
272
+ end
273
+ }
274
+ return
275
+ }
276
+ # If we reach here it means the database and/or user wasn't found
277
+ raise Error, "No record found for #{@user}@#{@database}"
278
+ end
279
+ end
280
+
281
+ # A subclass of DBRC designed to handle .dbrc files in YAML format. The
282
+ # public methods of this class are identical to DBRC.
283
+ class YML < DBRC
284
+ require "yaml"
285
+
286
+ private
287
+
288
+ def parse_dbrc_config_file(file=@dbrc_file)
289
+ config = YAML.load(File.open(file))
290
+ config.each{ |hash|
291
+ hash.each{ |db,info|
292
+ next unless db == @database
293
+ next unless @user == info["user"] if @user
294
+ @user = info["user"]
295
+ @password = info["password"]
296
+ @driver = info["driver"]
297
+ @interval = info["interval"]
298
+ @timeout = info["timeout"]
299
+ @maximum_reconnects = info["max_reconn"]
300
+ return
301
+ }
302
+ }
303
+ # If we reach this point, it means the database wasn't found
304
+ raise Error, "No entry found for #{@user}@#{@database}"
305
+ end
306
+ end
307
+ end