Class: KmsTools::CLI::Decrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/kms-tools/cli/decrypt.rb

Overview

Class for handling decrypt operations from the CLI

Instance Method Summary collapse

Constructor Details

#initialize(global_options, options) ⇒ Decrypt

Set up decryption handler

Parameters:

  • global_options (Hash)

Options Hash (global_options):

  • :master_key (String)

    Master key to use if provided by CLI options

  • :profile (String)

    AWS credential profile to us if provided by CLI options

  • :region (String)

    AWS region to use if provided by CLI options



12
13
14
15
# File 'lib/kms-tools/cli/decrypt.rb', line 12

def initialize(global_options, options)
  @dec = KmsTools::Decrypter.new(global_options)
  @output = options[:output]
end

Instance Method Details

#decrypt(source) ⇒ String

Handle decrypt command

Parameters:

  • source (String)

    Encrypted source to decrypt. Can be a string for direct decryption or a path to a KMS file to decrypt

Returns:

  • (String)

    Returns plaintext if Base64 encoded ciphertext is provided

  • (String)

    Returns path to decrypted file if a source path is provided



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kms-tools/cli/decrypt.rb', line 22

def decrypt(source)
  if File.file?(source)
    save_path = decrypt_file(source)
    Output.say("Decrypted file saved to: #{save_path}")
  elsif source.is_a?(String)
    decrypted_response = @dec.decrypt_string(source)
    if STDIN.tty?
      Output.say("Decrypted string:\n\n#{decrypted_response}\n\n")
    else
      print decrypted_response
    end
  else
    raise "Unknown input to encrypt..."
  end
  source = nil
end

#decrypt_file(source) ⇒ String

Decrypt a file from source path

Parameters:

  • source (String)

    path to file to decrypt

Returns:

  • (String)

    path to decrypted file



43
44
45
46
47
48
49
50
51
# File 'lib/kms-tools/cli/decrypt.rb', line 43

def decrypt_file(source)
  ef = KmsTools::EncryptedFile.new(path: source)
  save_path = Helpers::get_save_path({
    :prompt => "Save decrytped file to",
    :suggested_path => File.absolute_path(source.sub File.extname(source), ef.original_extension)
    })
  ef.save_decrypted(save_path)
  save_path
end