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/CHANGES +145 -141
- data/MANIFEST +15 -15
- data/README +260 -260
- data/Rakefile +19 -14
- data/dbi-dbrc.gemspec +4 -3
- data/lib/dbi/dbrc.rb +307 -309
- data/test/test_dbi_dbrc.rb +1 -4
- data/test/test_dbi_dbrc_xml.rb +1 -4
- data/test/test_dbi_dbrc_yml.rb +1 -4
- metadata +91 -102
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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(:
|
29
|
-
|
30
|
-
|
31
|
-
|
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(:
|
35
|
-
|
36
|
-
|
37
|
-
|
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 => :
|
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.
|
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 = '
|
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
|
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
|
-
|
2
|
-
require '
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
# The
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
#
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
# The database or host
|
27
|
-
attr_accessor :
|
28
|
-
|
29
|
-
# The
|
30
|
-
attr_accessor :
|
31
|
-
|
32
|
-
# The
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
#
|
37
|
-
attr_accessor :
|
38
|
-
|
39
|
-
#
|
40
|
-
attr_accessor :
|
41
|
-
|
42
|
-
# The
|
43
|
-
attr_accessor :
|
44
|
-
|
45
|
-
# The
|
46
|
-
attr_accessor :
|
47
|
-
|
48
|
-
# The
|
49
|
-
attr_accessor :
|
50
|
-
|
51
|
-
# The
|
52
|
-
attr_accessor :
|
53
|
-
|
54
|
-
# The
|
55
|
-
|
56
|
-
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
# file
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
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', '
|
85
|
-
#
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
@dbrc_file = File.join(
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
@
|
124
|
-
@
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
#
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
"#{iv}
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
}.
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
@
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
@
|
225
|
-
@
|
226
|
-
@
|
227
|
-
@
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
err = "no record found for #{@
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
alias_method(:
|
244
|
-
alias_method(:
|
245
|
-
alias_method(:
|
246
|
-
alias_method(:
|
247
|
-
alias_method(:
|
248
|
-
alias_method(:
|
249
|
-
alias_method(:
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
doc
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
}
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
@
|
297
|
-
@
|
298
|
-
@
|
299
|
-
@
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
}
|
305
|
-
|
306
|
-
|
307
|
-
|
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
|