dbi-dbrc 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f38085a9c7abb4c8a3e53fb239196135d317f78baa14a33f415fbaeab0b03d6
4
- data.tar.gz: a591ba6877a994765c4ad4dc5eb540b149c98398fe11bfd93ff04be1dca4e562
3
+ metadata.gz: 813eb7a49bd78983624e1e91031b3f93d83a872a1edea9bc2a1bced57985af9a
4
+ data.tar.gz: f9a2a4b40d14ecdc5e36b02c8c8c14c5e5bf47cf34400f365b9e4b23b03a540c
5
5
  SHA512:
6
- metadata.gz: 607d4e9c54ceb5c98cfaa7dc1df75708b151daac57cca77bb244371d43a1950107334081cb65ea112ba46da85aa9b015700ad23df863a09f7cbd628b0627d069
7
- data.tar.gz: 3712726dcf66e0f4dc19c4dbe46115e1dc9ea5159a59a4e9e31bf250e643dd07f114a6b4b27585a31c7a4c34016bba22c3999592b831feaea3aeeacddb9a57f4
6
+ metadata.gz: b46c7b59855bfa381213fa8ce30d31dbe4646811a6fd085ec698a1d6d3504eda6838a9693355ee958fd13e5aad679b6f2f5a060050cfaa64a45ec5214e84c4ab
7
+ data.tar.gz: 7f222c7eda45b449a59ecff9fb6c9631ac3dd54e59f3c12a858333ba478a1fa1f4e0269bd96a4453c320a56c47826e49da2994656caa1e61bebbde78e5d54a80
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.7.0 - 19-Jun-2021
2
+ * Added a DBI::DBRC::JSON subclass, and corresponding specs, so you can now
3
+ store information in JSON format.
4
+ * Updated and/or removed old information from the README file.
5
+
1
6
  ## 1.6.0 - 9-Jun-2021
2
7
  * Now supports gpg encrypted files, with a 4th optional parameter where you
3
8
  can pass gpg options (usually a password, but whatever gpgme supports).
data/MANIFEST.md CHANGED
@@ -5,6 +5,9 @@
5
5
  * dbi-dbrc.gemspec
6
6
  * certs/djberg96_pub.pem
7
7
  * lib/dbi/dbrc.rb
8
+ * lib/dbi/dbrc/json.rb
9
+ * lib/dbi/dbrc/xml.rb
10
+ * lib/dbi/dbrc/yaml.rb
8
11
  * examples/plain/.dbrc
9
12
  * examples/plain/test.rb
10
13
  * examples/xml_examples/.dbrc
@@ -12,5 +15,6 @@
12
15
  * examples/yml_examples/.dbrc
13
16
  * examples/yml_examples/test_yml.rb
14
17
  * spec/dbi_dbrc_spec.rb
18
+ * spec/dbi_dbrc_json_spec.rb
15
19
  * spec/dbi_dbrc_xml_spec.rb
16
20
  * spec/dbi_dbrc_yml_spec.rb
data/README.md CHANGED
@@ -1,12 +1,20 @@
1
1
  ## Description
2
- This is a supplement to the dbi library, allowing you to avoid hard-coding
3
- passwords in your programs that make database connections. It can also
4
- be used as a general password storage mechanism for other types of
2
+ This is essentially a database connection configuration library, allowing you
3
+ to avoid hard-coding passwords in your programs, and store other generalized
4
+ information, when making database connections.
5
+
6
+ It can also be used as a general password storage manager for other types of
5
7
  connections, e.g. ssh, ftp, etc.
6
8
 
9
+ The name was originally based on the old DBI library (which in turn was based
10
+ on the Perl library of the same name), and was originally meant as a supplement
11
+ to that library. The DBI library is now defunct, but it not necessary to use
12
+ this library. But, that explains why there's an outer "DBI" namespace module.
13
+
7
14
  ## Requirements
8
- For most platforms there are no additional requirements. However, for MS
9
- Windows there are these additional requirements:
15
+ * gpgme - For GPG encrypted files (which you should be doing).
16
+
17
+ For MS Windows there are these additional requirements:
10
18
 
11
19
  * sys-admin
12
20
  * win32-file-attributes
@@ -33,10 +41,10 @@ puts dbrc.dsn
33
41
  ```
34
42
 
35
43
  ## Notes on the .dbrc file
36
- This module relies on a file in your home directory called ".dbrc", and it
37
- is meant to be analogous to the ".netrc" file used by programs such as
38
- telnet. The .dbrc file has several conditions that must be met by the
39
- module or it will fail:
44
+ This module relies on a file somewhere on your filesystem (normally your home
45
+ directory) called ".dbrc", and it is meant to be analogous to the ".netrc" file
46
+ used by programs such as telnet. The .dbrc file has several conditions that
47
+ must be met by the module or it will fail:
40
48
 
41
49
  * Permissions must be set to 600 (Unix only).
42
50
  * Must be hidden (MS Windows only).
@@ -54,8 +62,7 @@ You may include comments in the .dbrc file by starting the line with a
54
62
 
55
63
  A failure in any of the rules mentioned above will result in a `DBRC::Error`
56
64
  being raised. In addition, the file may also be encrypted on MS Windows
57
- systems, in which case the file will automatically be (temporarily)
58
- decrypted.
65
+ systems, in which case the file will automatically be (temporarily) decrypted.
59
66
 
60
67
  The format for XML (using the example above) is as follows:
61
68
 
@@ -83,6 +90,23 @@ The format for YAML is as follows:
83
90
  timeout: 10
84
91
  max_reconn: 2
85
92
  ```
93
+
94
+ The format for JSON is as follows:
95
+
96
+ ```json
97
+ [
98
+ {
99
+ "foo": {
100
+ "user": "dan",
101
+ "password": "mypass",
102
+ "driver": "oracle",
103
+ "interval": 30,
104
+ "timeout": 10,
105
+ "maximum_reconnects": 2
106
+ }
107
+ }
108
+ ]
109
+ ```
86
110
 
87
111
  ## Constants
88
112
  `VERSION`
@@ -90,7 +114,7 @@ The format for YAML is as follows:
90
114
  The current version of this library, returned as a String.
91
115
 
92
116
  ## Class Methods
93
- `DBRC.new(db, user=nil, dir=nil)`
117
+ `DBRC.new(db, user = nil, dir = nil, gpg_options = nil)`
94
118
 
95
119
  The constructor takes one to three arguments. The first argument is the
96
120
  database name. This *must* be provided. If only the database name is
@@ -105,6 +129,10 @@ The third argument, also optional, specifies the directory where DBRC will
105
129
  look for the .dbrc file. By default, it looks in the pwuid (present
106
130
  working user id) home directory. The rules for a .dbrc file still apply.
107
131
 
132
+ The fourth argument, if present, are options that are forwarded to the
133
+ `GPGME::Crypto.new` constructor for GPG encrypted files. Typically this
134
+ would at least be the :password option, but YMMV.
135
+
108
136
  MS Windows users should read the "Notes" section for how your home directory
109
137
  is determined.
110
138
 
@@ -209,7 +237,7 @@ not automatically reset the driver or database.
209
237
  ## Canonical Example
210
238
  ```ruby
211
239
  # This is a basic template for how I do things:
212
-
240
+ require 'dbi'
213
241
  require 'dbi/dbrc'
214
242
  require 'timeout'
215
243
 
@@ -233,10 +261,6 @@ end
233
261
  ```
234
262
 
235
263
  ## Notes for MS Windows Users
236
- The `home` directory for Win32 users is determined by `ENV['USERPROFILE']`.
237
- If that is not set then `ENV['HOME']` is used. If that is not set, then
238
- the directory found by the sys-admin library is used.
239
-
240
264
  To make your file hidden, right click on the .dbrc file in your Explorer
241
265
  window, select "Properties" and check the "Hidden" checkbox.
242
266
 
@@ -246,18 +270,9 @@ party, which is a bit much to expect. However, if the file is encrypted,
246
270
  DBRC will attempt to decrypt it, parse it, and encrypt it again when done
247
271
  parsing.
248
272
 
249
- ## Notes on running the test suite
250
- I cannot guarantee that the .dbrc files under the `examples`
251
- subdirectories maintain the appropriate properties. This can cause
252
- failures for the test suite (which uses these files).
253
-
254
- The only solution is to perform a `chmod 600 .dbrc` (on Unix) or set
255
- the properties to 'hidden' (on MS Windows) manually, for the file in
256
- question.
257
-
258
273
  ## Summary
259
- These "methods" don't really do anything. They're simply meant as a
260
- convenience mechanism for you dbi connections, plus a little bit of
274
+ These methods don't really "do" anything. They're simply meant as a
275
+ convenience mechanism for your database connections, plus a little bit of
261
276
  obfuscation (for passwords).
262
277
 
263
278
  ## Adding your own configuration
@@ -271,7 +286,7 @@ Take a look at the XML and YML subclasses in dbrc.rb for two examples that
271
286
  you can work from.
272
287
 
273
288
  ## Future Plans
274
- Add DBI::DBRC::JSON.
289
+ None at this time.
275
290
 
276
291
  ## Known Bugs
277
292
  I'm not positive about the dsn strings for databases other than Oracle.
data/dbi-dbrc.gemspec CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'dbi-dbrc'
5
- spec.version = '1.6.0'
5
+ spec.version = '1.7.0'
6
6
  spec.author = 'Daniel Berger'
7
7
  spec.email = 'djberg96@gmail.com'
8
8
  spec.license = 'Apache-2.0'
data/lib/dbi/dbrc.rb CHANGED
@@ -17,7 +17,7 @@ module DBI
17
17
  class Error < StandardError; end
18
18
 
19
19
  # The version of the dbi-dbrc library
20
- VERSION = '1.6.0'
20
+ VERSION = '1.7.0'
21
21
 
22
22
  WINDOWS = File::ALT_SEPARATOR # :no-doc:
23
23
 
@@ -104,7 +104,7 @@ module DBI
104
104
  # # Pass along a GPG password to decrypt the file.
105
105
  # DBI::DBRC.new('some_database', 'foo_usr', '/usr/local', :gpg_options => {:password => 'xxx'})
106
106
  #
107
- def initialize(database, user = nil, dbrc_dir = Dir.home, gpg_options = nil)
107
+ def initialize(database, user = nil, dbrc_dir = Dir.home, gpg_options = nil)
108
108
  if dbrc_dir.nil?
109
109
  # Default to the app data directory on Windows, or root on Unix, if
110
110
  # no home dir can be found.
@@ -245,62 +245,8 @@ module DBI
245
245
  end
246
246
  end
247
247
  end
248
-
249
- # A subclass of DBRC designed to handle .dbrc files in XML format. The
250
- # public methods of this class are identical to DBRC.
251
- class DBRC::XML < DBRC
252
- require 'rexml/document' # Good enough for small files
253
-
254
- private
255
-
256
- def parse_dbrc_config_file(file = @dbrc_file)
257
- file = file.is_a?(StringIO) ? file : File.new(file)
258
- doc = REXML::Document.new(file)
259
-
260
- fields = %w[user password driver interval timeout maximum_reconnects]
261
-
262
- doc.elements.each('/dbrc/database') do |element|
263
- next unless element.attributes['name'] == database
264
- next if @user && @user != element.elements['user'].text
265
-
266
- fields.each do |field|
267
- val = element.elements[field]
268
- send("#{field}=", val.text) unless val.nil?
269
- end
270
-
271
- break
272
- end
273
-
274
- raise Error, "No record found for #{@user}@#{@database}" unless @user && @database
275
- end
276
- end
277
-
278
- # A subclass of DBRC designed to handle .dbrc files in YAML format. The
279
- # public methods of this class are identical to DBRC.
280
- class DBRC::YML < DBRC
281
- require 'yaml'
282
-
283
- private
284
-
285
- def parse_dbrc_config_file(file = @dbrc_file)
286
- fh = file.is_a?(StringIO) ? file : File.open(file)
287
- config = YAML.safe_load(fh)
288
-
289
- config.each do |hash|
290
- hash.each do |db, info|
291
- next unless db == @database
292
- next if @user && @user != info['user']
293
- @user = info['user']
294
- @password = info['password']
295
- @driver = info['driver']
296
- @interval = info['interval']
297
- @timeout = info['timeout']
298
- @maximum_reconnects = info['maximum_reconnects']
299
- break
300
- end
301
- end
302
-
303
- raise Error, "No entry found for #{@user}@#{@database}" unless @user && @database
304
- end
305
- end
306
248
  end
249
+
250
+ require_relative 'dbrc/xml'
251
+ require_relative 'dbrc/yaml'
252
+ require_relative 'dbrc/json'
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../dbrc'
4
+
5
+ # The DBI module serves as namespace only.
6
+ module DBI
7
+ # A subclass of DBRC designed to handle .dbrc files in JSON format. The
8
+ # public methods of this class are identical to DBRC.
9
+ class DBRC::JSON < DBRC
10
+ require 'json'
11
+
12
+ private
13
+
14
+ def parse_dbrc_config_file(file = @dbrc_file)
15
+ begin
16
+ fh = file.is_a?(StringIO) ? file : File.open(file)
17
+ config = ::JSON.parse(fh.read)
18
+
19
+ config.each do |hash|
20
+ db = hash.keys.first
21
+ next unless db == @database
22
+ next if @user && @user != hash[db]['user']
23
+ @user = hash[db]['user']
24
+ @password = hash[db]['password']
25
+ @driver = hash[db]['driver']
26
+ @interval = hash[db]['interval']
27
+ @timeout = hash[db]['timeout']
28
+ @maximum_reconnects = hash[db]['maximum_reconnects']
29
+ break
30
+ end
31
+ ensure
32
+ fh.close if fh && fh.respond_to?(:close)
33
+ end
34
+
35
+ raise Error, "No entry found for #{@user}@#{@database}" unless @user && @database
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../dbrc'
4
+
5
+ # The DBI module serves as a namespace only.
6
+ module DBI
7
+ # A subclass of DBRC designed to handle .dbrc files in XML format. The
8
+ # public methods of this class are identical to DBRC.
9
+ class DBRC::XML < DBRC
10
+ require 'rexml/document' # Good enough for small files
11
+
12
+ private
13
+
14
+ def parse_dbrc_config_file(file = @dbrc_file)
15
+ file = file.is_a?(StringIO) ? file : File.new(file)
16
+ doc = REXML::Document.new(file)
17
+
18
+ fields = %w[user password driver interval timeout maximum_reconnects]
19
+
20
+ doc.elements.each('/dbrc/database') do |element|
21
+ next unless element.attributes['name'] == database
22
+ next if @user && @user != element.elements['user'].text
23
+
24
+ fields.each do |field|
25
+ val = element.elements[field]
26
+ send("#{field}=", val.text) unless val.nil?
27
+ end
28
+
29
+ break
30
+ end
31
+
32
+ raise Error, "No record found for #{@user}@#{@database}" unless @user && @database
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../dbrc'
4
+
5
+ # The DBI module serves as namespace only.
6
+ module DBI
7
+ # A subclass of DBRC designed to handle .dbrc files in YAML format. The
8
+ # public methods of this class are identical to DBRC.
9
+ class DBRC::YML < DBRC
10
+ require 'yaml'
11
+
12
+ private
13
+
14
+ def parse_dbrc_config_file(file = @dbrc_file)
15
+ fh = file.is_a?(StringIO) ? file : File.open(file)
16
+ config = ::YAML.safe_load(fh)
17
+
18
+ config.each do |hash|
19
+ hash.each do |db, info|
20
+ next unless db == @database
21
+ next if @user && @user != info['user']
22
+ @user = info['user']
23
+ @password = info['password']
24
+ @driver = info['driver']
25
+ @interval = info['interval']
26
+ @timeout = info['timeout']
27
+ @maximum_reconnects = info['maximum_reconnects']
28
+ break
29
+ end
30
+ end
31
+
32
+ raise Error, "No entry found for #{@user}@#{@database}" unless @user && @database
33
+ end
34
+ end
35
+ end
36
+
37
+ DBI::DBRC::YAML = DBI::DBRC::YML # Alias
@@ -0,0 +1,109 @@
1
+ ########################################################################
2
+ # dbi_dbrc_json_spec.rb
3
+ #
4
+ # Test suite for the JSON specific version of DBI::DBRC. This test case
5
+ # should be run via the 'rake test' task.
6
+ ########################################################################
7
+ require 'dbi/dbrc'
8
+ require 'rspec'
9
+ require 'pp' # Need this to avoid fakefs error
10
+ require 'fakefs/spec_helpers'
11
+
12
+ RSpec.describe DBI::DBRC::JSON, :json => true do
13
+ include FakeFS::SpecHelpers
14
+
15
+ let(:home) { File.join(Dir.pwd, 'home', 'someone') }
16
+ let(:dbrc) { File.join(home, '.dbrc') }
17
+
18
+ let(:db_foo){ 'foo' }
19
+ let(:user1) { 'user1' }
20
+
21
+ let(:json){
22
+ %q{
23
+ [
24
+ {
25
+ "foo": {
26
+ "user": "user1",
27
+ "password": "pwd1",
28
+ "driver": "Oracle",
29
+ "interval": 60,
30
+ "timeout": 40,
31
+ "maximum_reconnects": 3
32
+ }
33
+ },
34
+ {
35
+ "foo": {
36
+ "user": "user2",
37
+ "password": "pwd2",
38
+ "driver": "OCI8",
39
+ "interval": 60,
40
+ "timeout": 60,
41
+ "maximum_reconnects": 4
42
+ }
43
+ },
44
+ {
45
+ "bar": {
46
+ "user": "user1",
47
+ "password": "pwd3",
48
+ "driver": "Oracle",
49
+ "interval": 30,
50
+ "timeout": 30,
51
+ "maximum_reconnects": 2
52
+ }
53
+ },
54
+ {
55
+ "baz": {
56
+ "user": "user3",
57
+ "password": "pwd4"
58
+ }
59
+ }
60
+ ]
61
+ }
62
+ }
63
+
64
+ before do
65
+ allow(Dir).to receive(:home).and_return(home)
66
+
67
+ if File::ALT_SEPARATOR
68
+ allow(FakeFS::File).to receive(:hidden?).and_return(true)
69
+ allow(FakeFS::File).to receive(:encrypted?).and_return(false)
70
+ end
71
+
72
+ FileUtils.mkdir_p(home)
73
+ File.open(dbrc, 'w'){ |fh| fh.write(json) }
74
+ File.chmod(0600, dbrc)
75
+
76
+ # FakeFS doesn't implement this yet
77
+ allow_any_instance_of(FakeFS::File::Stat).to receive(:owned?).and_return(true)
78
+ end
79
+
80
+ context "instance methods" do
81
+ before do
82
+ @dbrc = described_class.new(db_foo, user1)
83
+ end
84
+
85
+ example "database method returns expected value" do
86
+ expect(@dbrc.database).to eq('foo')
87
+ end
88
+
89
+ example "password method returns expected value" do
90
+ expect(@dbrc.password).to eq('pwd1')
91
+ end
92
+
93
+ example "driver method returns expected value" do
94
+ expect(@dbrc.driver).to eq('Oracle')
95
+ end
96
+
97
+ example "interval method returns expected value" do
98
+ expect(@dbrc.interval).to eq(60)
99
+ end
100
+
101
+ example "timeout method returns expected value" do
102
+ expect(@dbrc.timeout).to eq(40)
103
+ end
104
+
105
+ example "maximum_reconnects method returns expected value" do
106
+ expect(@dbrc.maximum_reconnects).to eq(3)
107
+ end
108
+ end
109
+ end
@@ -43,7 +43,7 @@ RSpec.describe DBI::DBRC do
43
43
  end
44
44
 
45
45
  example "version" do
46
- expect(described_class::VERSION).to eq('1.6.0')
46
+ expect(described_class::VERSION).to eq('1.7.0')
47
47
  expect(described_class::VERSION).to be_frozen
48
48
  end
49
49
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbi-dbrc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Berger
@@ -35,7 +35,7 @@ cert_chain:
35
35
  ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
36
36
  WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
37
37
  -----END CERTIFICATE-----
38
- date: 2021-06-09 00:00:00.000000000 Z
38
+ date: 2021-06-19 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: gpgme
@@ -119,6 +119,10 @@ files:
119
119
  - examples/yml/test_yml.rb
120
120
  - lib/dbi-dbrc.rb
121
121
  - lib/dbi/dbrc.rb
122
+ - lib/dbi/dbrc/json.rb
123
+ - lib/dbi/dbrc/xml.rb
124
+ - lib/dbi/dbrc/yaml.rb
125
+ - spec/dbi_dbrc_json_spec.rb
122
126
  - spec/dbi_dbrc_spec.rb
123
127
  - spec/dbi_dbrc_xml_spec.rb
124
128
  - spec/dbi_dbrc_yml_spec.rb
metadata.gz.sig CHANGED
Binary file