netrc 0.7.3 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Netrc
2
2
 
3
- This library reads and writes `.netrc` files.
3
+ This library reads and writes
4
+ [`.netrc` files](http://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-File.html).
4
5
 
5
6
  ## API
6
7
 
@@ -8,7 +9,9 @@ Read a netrc file:
8
9
 
9
10
  n = Netrc.read("sample.netrc")
10
11
 
11
- If the file doesn't exist, Netrc.read will return an empty object.
12
+ If the file doesn't exist, Netrc.read will return an empty object. If
13
+ the filename ends in ".gpg", it will be decrypted using
14
+ [GPG](http://www.gnupg.org/).
12
15
 
13
16
  Read the user's default netrc file (`$HOME/.netrc` on Unix;
14
17
  `%HOME%\_netrc` on Windows):
@@ -1,3 +1,8 @@
1
+ 0.7.4 06/04/12
2
+ ==============
3
+
4
+ * add support for encrypted files pgp netrc files
5
+
1
6
  0.7.3 06/04/12
2
7
  ==============
3
8
 
@@ -1,5 +1,5 @@
1
1
  class Netrc
2
- VERSION = "0.7.3"
2
+ VERSION = "0.7.4"
3
3
  CYGWIN = RUBY_PLATFORM =~ /cygwin/i
4
4
  WINDOWS = RUBY_PLATFORM =~ /win32|mingw32/i
5
5
 
@@ -11,14 +11,24 @@ class Netrc
11
11
  end
12
12
  end
13
13
 
14
- # Reads path and parses it as a .netrc file. If path doesn't
15
- # exist, returns an empty object.
16
- def self.read(path=default_path)
14
+ def self.check_permissions(path)
17
15
  perm = File.stat(path).mode & 0777
18
16
  if perm != 0600 && !(CYGWIN) && !(WINDOWS)
19
17
  raise Error, "Permission bits for '#{path}' should be 0600, but are "+perm.to_s(8)
20
18
  end
21
- new(path, parse(lex(File.readlines(path))))
19
+ end
20
+
21
+ # Reads path and parses it as a .netrc file. If path doesn't
22
+ # exist, returns an empty object. Decrypt paths ending in .gpg.
23
+ def self.read(path=default_path)
24
+ check_permissions(path)
25
+ if path =~ /\.gpg$/
26
+ decrypted = `gpg --batch --quiet --decrypt #{path}`
27
+ raise Error.new("Decrypting #{path} failed.") unless $?.success?
28
+ new(path, parse(lex(decrypted.split("\n"))))
29
+ else
30
+ new(path, parse(lex(File.readlines(path))))
31
+ end
22
32
  rescue Errno::ENOENT
23
33
  new(path, parse(lex([])))
24
34
  end
@@ -145,7 +155,17 @@ class Netrc
145
155
  end
146
156
 
147
157
  def save
148
- File.open(@path, 'w', 0600) {|file| file.print(unparse)}
158
+ if @path =~ /\.gpg$/
159
+ e = IO.popen("gpg -a --batch --default-recipient-self -e", "r+") do |gpg|
160
+ gpg.puts(unparse)
161
+ gpg.close_write
162
+ gpg.read
163
+ end
164
+ raise Error.new("Encrypting #{path} failed.") unless $?.success?
165
+ File.open(@path, 'w', 0600) {|file| file.print(e)}
166
+ else
167
+ File.open(@path, 'w', 0600) {|file| file.print(unparse)}
168
+ end
149
169
  end
150
170
 
151
171
  def unparse
@@ -100,4 +100,15 @@ class TestNetrc < Test::Unit::TestCase
100
100
  n.save
101
101
  assert_equal(0600, File.stat("/tmp/created.netrc").mode & 0777)
102
102
  end
103
+
104
+ def test_encrypted_roundtrip
105
+ if `gpg --list-keys 2> /dev/null` != ""
106
+ FileUtils.rm_f("/tmp/test.netrc.gpg")
107
+ n = Netrc.read("/tmp/test.netrc.gpg")
108
+ n["m"] = "a", "b"
109
+ n.save
110
+ assert_equal(0600, File.stat("/tmp/test.netrc.gpg").mode & 0777)
111
+ assert_equal(["a", "b"], Netrc.read("/tmp/test.netrc.gpg")["m"])
112
+ end
113
+ end
103
114
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netrc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.7.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2012-06-04 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: turn
17
- requirement: &70143678261780 !ruby/object:Gem::Requirement
17
+ requirement: &70101253021640 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70143678261780
25
+ version_requirements: *70101253021640
26
26
  description: This library can read and update netrc files, preserving formatting including
27
27
  comments and whitespace.
28
28
  email: geemus@gmail.com