rgpg 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
- require_relative 'rgpg/gem_info'
2
- require_relative 'rgpg/gpg_helper'
1
+ if RUBY_VERSION < '1.9.0'
2
+ require File.expand_path('../rgpg/gem_info', __FILE__)
3
+ require File.expand_path('../rgpg/gpg_helper', __FILE__)
4
+ else
5
+ require_relative 'rgpg/gem_info'
6
+ require_relative 'rgpg/gpg_helper'
7
+ end
3
8
 
@@ -2,7 +2,7 @@ module Rgpg
2
2
  module GemInfo
3
3
  MAJOR_VERSION = 0
4
4
  MINOR_VERSION = 2
5
- PATCH_VERSION = 3
5
+ PATCH_VERSION = 4
6
6
 
7
7
  def self.version_string
8
8
  [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION].join('.')
@@ -11,8 +11,10 @@ module Rgpg
11
11
  begin
12
12
  script_file.write(script)
13
13
  script_file.close
14
- result = system("gpg --batch --gen-key #{Shellwords.escape(script_file.path)}")
15
- raise RuntimeError.new('gpg failed') unless result
14
+ run_gpg_no_capture(
15
+ '--batch',
16
+ '--gen-key', script_file.path
17
+ )
16
18
  ensure
17
19
  script_file.close
18
20
  script_file.unlink
@@ -25,7 +27,7 @@ module Rgpg
25
27
 
26
28
  recipient = get_recipient(public_key_file_name)
27
29
  with_temporary_encrypt_keyring(public_key_file_name) do |keyring_file_name|
28
- run_gpg(
30
+ run_gpg_capture(
29
31
  '--keyring', keyring_file_name,
30
32
  '--output', output_file_name,
31
33
  '--encrypt',
@@ -44,7 +46,7 @@ module Rgpg
44
46
 
45
47
  recipient = get_recipient(private_key_file_name)
46
48
  with_temporary_decrypt_keyrings(public_key_file_name, private_key_file_name) do |keyring_file_name, secret_keyring_file_name|
47
- run_gpg(
49
+ run_gpg_capture(
48
50
  '--keyring', keyring_file_name,
49
51
  '--secret-keyring', secret_keyring_file_name,
50
52
  '--output', output_file_name,
@@ -58,21 +60,49 @@ module Rgpg
58
60
 
59
61
  private
60
62
 
61
- def self.run_gpg(*args)
63
+ def self.with_temp_home_dir
64
+ Dir.mktmpdir('.rgpg-tmp-', ENV['HOME']) do |home_dir|
65
+ yield home_dir
66
+ end
67
+ end
68
+
69
+ def self.build_safe_command_line(home_dir, *args)
62
70
  fragments = [
63
71
  'gpg',
72
+ '--homedir', home_dir,
64
73
  '--no-default-keyring'
65
74
  ] + args
66
- command_line = fragments.collect { |fragment| Shellwords.escape(fragment) }.join(' ')
75
+ fragments.collect { |fragment| Shellwords.escape(fragment) }.join(' ')
76
+ end
67
77
 
68
- output_file = Tempfile.new('gpg-output')
69
- begin
70
- output_file.close
71
- result = system("#{command_line} > #{Shellwords.escape(output_file.path)} 2>&1")
72
- ensure
73
- output_file.unlink
78
+ def self.run_gpg_no_capture(*args)
79
+ with_temp_home_dir do |home_dir|
80
+ command_line = build_safe_command_line(home_dir, *args)
81
+ result = system(command_line)
82
+ raise RuntimeError.new('gpg failed') unless result
83
+ end
84
+ end
85
+
86
+ def self.run_gpg_capture(*args)
87
+ with_temp_home_dir do |home_dir|
88
+ command_line = build_safe_command_line(home_dir, *args)
89
+
90
+ output_file = Tempfile.new('gpg-output')
91
+ begin
92
+ output_file.close
93
+ result = system("#{command_line} > #{Shellwords.escape(output_file.path)} 2>&1")
94
+
95
+ output = nil
96
+ File.open(output_file.path) do |f|
97
+ output = f.read
98
+ end
99
+ raise RuntimeError.new("gpg failed: #{output}") unless result
100
+
101
+ output.lines.collect(&:chomp)
102
+ ensure
103
+ output_file.unlink
104
+ end
74
105
  end
75
- raise RuntimeError.new('gpg failed') unless result
76
106
  end
77
107
 
78
108
  def self.generate_key_script(public_key_file_name, private_key_file_name, recipient, real_name)
@@ -95,17 +125,16 @@ module Rgpg
95
125
  end
96
126
 
97
127
  def self.get_recipient(key_file_name)
98
- result = `gpg #{key_file_name}`.lines.first.chomp
99
- raise RuntimeError.new('gpg failed') unless $?
100
- result =~ /^(pub|sec)\s+\d+D\/([0-9a-fA-F]{8}).+<(.+)>/ or raise RuntimeError.new('Invalid output')
128
+ lines = run_gpg_capture(key_file_name)
129
+ result = lines.detect { |line| line =~ /^(pub|sec)\s+\d+D\/([0-9a-fA-F]{8}).+<(.+)>/ }
130
+ raise RuntimeError.new('Invalid output') unless result
101
131
  key_id = $2
102
132
  recipient = $3
103
- recipient
104
133
  end
105
134
 
106
135
  def self.with_temporary_encrypt_keyring(public_key_file_name)
107
136
  with_temporary_keyring_file do |keyring_file_name|
108
- run_gpg(
137
+ run_gpg_capture(
109
138
  '--keyring', keyring_file_name,
110
139
  '--import', public_key_file_name
111
140
  )
@@ -116,7 +145,7 @@ module Rgpg
116
145
  def self.with_temporary_decrypt_keyrings(public_key_file_name, private_key_file_name)
117
146
  with_temporary_keyring_file do |keyring_file_name|
118
147
  with_temporary_keyring_file do |secret_keyring_file_name|
119
- run_gpg(
148
+ run_gpg_capture(
120
149
  '--keyring', keyring_file_name,
121
150
  '--secret-keyring', secret_keyring_file_name,
122
151
  '--import', private_key_file_name
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgpg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-01 00:00:00.000000000 Z
12
+ date: 2013-08-04 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Simple Ruby wrapper around "gpg" command for file encryption
15
15
  email: rcook@rcook.org