Class: Hiera::Backend::Eyaml::Subcommands::Encrypt

Inherits:
Hiera::Backend::Eyaml::Subcommand show all
Defined in:
lib/hiera/backend/eyaml/subcommands/encrypt.rb

Class Method Summary collapse

Methods inherited from Hiera::Backend::Eyaml::Subcommand

all_options, attach_option, find, hidden?, load_config_file, parse, prettyname

Class Method Details

.descriptionObject


44
45
46
# File 'lib/hiera/backend/eyaml/subcommands/encrypt.rb', line 44

def self.description
  "encrypt some data"
end

.executeObject


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hiera/backend/eyaml/subcommands/encrypt.rb', line 71

def self.execute
  case Eyaml::Options[:source]
    when :eyaml
      parser = Parser::ParserFactory.decrypted_parser
      tokens = parser.parse(Eyaml::Options[:input_data])
      encrypted = tokens.map{ |token| token.to_encrypted }
      encrypted.join
    else
      encryptor = Encryptor.find
      ciphertext = encryptor.encode( encryptor.encrypt(Eyaml::Options[:input_data]) )
      token = Parser::EncToken.new(:block, Eyaml::Options[:input_data], encryptor, ciphertext, nil, '    ')
      case Eyaml::Options[:output]
        when "block"
          token.to_encrypted :label => Eyaml::Options[:label], :use_chevron => !Eyaml::Options[:label].nil?, :format => :block
        when "string"
          token.to_encrypted :label => Eyaml::Options[:label], :format => :string
        when "examples"
          string = token.to_encrypted :label => Eyaml::Options[:label] || 'string', :format => :string
          block = token.to_encrypted :label => Eyaml::Options[:label] || 'block', :format => :block
          "#{string}\n\nOR\n\n#{block}"
        else
          token.to_encrypted :format => :string
      end
  end
end

.optionsObject


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hiera/backend/eyaml/subcommands/encrypt.rb', line 13

def self.options
  [{:name => :password, 
    :description => "Source input is a password entered on the terminal", 
    :short => 'p'},
   {:name => :string,
    :description => "Source input is a string provided as an argument",
    :short => 's', 
    :type => :string},
   {:name => :file,
    :description => "Source input is a regular file",
    :short => 'f',
    :type => :string},
   {:name => :stdin,
    :description => "Source input is taken from stdin",
    :short => :none},
   {:name => :eyaml,
    :description => "Source input is an eyaml file",
    :short => 'e',
    :type => :string},
   {:name => :output,
    :description => "Output format of final result (examples, block, string)",
    :type => :string,
    :short => 'o',
    :default => "examples"},
   {:name => :label,
    :description => "Apply a label to the encrypted result",
    :short => 'l',
    :type => :string}
  ]
end

.validate(options) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hiera/backend/eyaml/subcommands/encrypt.rb', line 48

def self.validate options
  sources = [:password, :string, :file, :stdin, :eyaml].collect {|x| x if options[x]}.compact
  Trollop::die "You must specify a source" if sources.count.zero?
  Trollop::die "You can only specify one of (#{sources.join(', ')})" if sources.count > 1
  options[:source] = sources.first

  options[:input_data] = case options[:source]
  when :password
    require 'hiera/backend/eyaml/highlinehelper'
    HighlineHelper.read_password
  when :string
    options[:string]
  when :file
    File.read options[:file]
  when :stdin
    STDIN.read
  when :eyaml
    File.read options[:eyaml]
  end
  options

end