lib/rultor/encrypt.rb in rultor-0.1.1 vs lib/rultor/encrypt.rb in rultor-0.2.0
- old
+ new
@@ -26,33 +26,53 @@
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
+require 'shellwords'
require 'English'
# Rultor main module.
# Author:: Yegor Bugayenko (yegor@teamed.io)
# Copyright:: Copyright (c) 2014 Yegor Bugayenko
# License:: BSD
module Rultor
# Encrypting command
class Encrypt
def initialize(name, file)
- @name = name
- @file = file
- @dest = "#{@file}.asc"
+ @key = 'rultor-key:' + name
+ @dir = File.dirname(file)
+ @file = File.basename(file)
end
def run
system(
"
set -x
set -e
+ file=#{Shellwords.escape(@file)}
+ enc=#{Shellwords.escape(@file + '.enc')}
+ if [ -e \"${enc}\" ]; then
+ echo \"file already exists: ${enc}\"
+ exit -1
+ fi
+ asc=#{Shellwords.escape(@file + '.asc')}
+ if [ -e \"${asc}\" ]; then
+ echo \"file already exists: ${asc}\"
+ exit -1
+ fi
+ cd #{Shellwords.escape(@dir)}
+ echo #{Shellwords.escape(@key)} > \"${asc}\"
+ echo #{Shellwords.escape(@key)} >> \"${asc}\"
+ cat \"${asc}\" | bcrypt -r -o \"${file}\" > \"${enc}\"
+ rm \"${asc}\"
gpg --keyserver hkp://pool.sks-keyservers.net \
--verbose --recv-keys 9AF0FA4C
- gpg --trust-model always --output '#{@dest}' \
- --batch --armor --encrypt --verbose --recipient 9AF0FA4C '#{@file}'
+ gpg --trust-model always \
+ --output \"${asc}\" \
+ --batch --armor --encrypt --verbose \
+ --recipient 9AF0FA4C \"${enc}\"
+ rm -f \"${enc}\"
"
)
fail 'Failed to PGP encrypt' unless $CHILD_STATUS.exitstatus == 0
Rultor.log.info "#{@file} encrypted"
end