class Rultor::Encrypt

Encrypting command

Public Class Methods

new(name, file) click to toggle source
# File lib/rultor/encrypt.rb, line 41
def initialize(name, file)
  @key = 'rultor-key:' + name
  @dir = File.dirname(file)
  @file = File.basename(file)
end

Public Instance Methods

run() click to toggle source
# File lib/rultor/encrypt.rb, line 47
def run
  source = File.join(@dir, @file)
  target = File.join(@dir, @file + '.asc')
  if Gem.win_platform?
    windows(source, target)
  else
    unix(source, target)
  end
  fail 'PGP encryption failed' unless $CHILD_STATUS.exitstatus == 0
  Rultor.log.info "#{@file} encrypted into #{target}" \
    " (#{File.size(target)} bytes)"
end

Private Instance Methods

unix(source, target) click to toggle source
# File lib/rultor/encrypt.rb, line 62
def unix(source, target)
  system(
    "
    set -x
    set -e
    file=#{Shellwords.escape(source)}
    asc=#{Shellwords.escape(target)}
    if [ -e \"${asc}\" ]; then
      echo \"file already exists: ${asc}\"
      exit -1
    fi
    tmp=$(mktemp -t rultor-XXXX)
    rm -f \"${tmp}\"
    gpg --version
    gpg --symmetric --armor --verbose --batch --no-tty \
      --passphrase #{Shellwords.escape(@key)} \
      -o \"${tmp}\" \"${file}\"
    gpg --keyserver hkp://ipv4.pool.sks-keyservers.net \
      --verbose --recv-keys 9AF0FA4C
    gpg --trust-model always \
      --output \"${asc}\" \
      --batch --no-tty --armor --encrypt --verbose \
      --recipient 9AF0FA4C \"${tmp}\"
    rm -f \"${tmp}\"
    "
  )
end
windows(source, target) click to toggle source
# File lib/rultor/encrypt.rb, line 90
def windows(source, target)
  tmp = source + '.enc'
  system(
    [
      'gpg --version',
      'gpg --symmetric --armor --verbose --batch --no-tty' \
        " --passphrase #{Shellwords.escape(@key)}" \
        " -o #{Shellwords.escape(tmp)}" \
        " #{Shellwords.escape(source)}",
      'gpg --keyserver hkp://ipv4.pool.sks-keyservers.net' \
        ' --verbose --recv-keys 9AF0FA4C',
      'gpg --trust-model always' \
        " --output #{Shellwords.escape(target)}" \
        ' --batch --no-tty --armor --encrypt --verbose' \
        " --recipient 9AF0FA4C #{Shellwords.escape(tmp)}"
    ].join(' && ')
  )
  File.delete(tmp)
end