Class: Hiera::Backend::Eyaml_backend

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/backend/eyaml_backend.rb

Instance Method Summary collapse

Constructor Details

#initializeEyaml_backend

Returns a new instance of Eyaml_backend.



10
11
# File 'lib/hiera/backend/eyaml_backend.rb', line 10

def initialize
end

Instance Method Details

#deblock(block_string) ⇒ Object



80
81
82
# File 'lib/hiera/backend/eyaml_backend.rb', line 80

def deblock block_string
  block_string.gsub(/[ \n]/, '')
end

#debug(msg) ⇒ Object



115
116
117
# File 'lib/hiera/backend/eyaml_backend.rb', line 115

def debug(msg)
  Hiera.debug("[eyaml_backend]: #{msg}")
end

#decrypt(key, value, scope) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hiera/backend/eyaml_backend.rb', line 84

def decrypt(key, value, scope)

  if encrypted? value

    debug "Attempting to decrypt: #{key}"
    
    Config[:eyaml].each do |config_key, config_value|
      config_value = Backend.parse_string(Config[:eyaml][config_key], scope)
      debug "Setting: #{config_key} = #{config_value}"
      Eyaml::Options[config_key] = config_value
    end

    Eyaml::Options[:source] = "hiera"

    plaintext = value.gsub( /ENC\[([^\]]*)\]/ ) { |match|
      Eyaml::Options[:input_data] = deblock match.to_s
      Eyaml::Options[:output] = "raw"
      Eyaml::Actions::DecryptAction.execute
    }

    plaintext.chomp

  else
    value
  end
end

#encrypted?(value) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/hiera/backend/eyaml_backend.rb', line 111

def encrypted?(value)
  if value.match(/.*ENC\[.*?\]/) then true else false end
end

#lookup(key, scope, order_override, resolution_type) ⇒ Object



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hiera/backend/eyaml_backend.rb', line 13

def lookup(key, scope, order_override, resolution_type)

  debug("Lookup called for key #{key}")
  answer = nil

  Backend.datasources(scope, order_override) do |source|
    eyaml_file = Backend.datafile(:eyaml, scope, source, "eyaml") || next

    debug("Processing datasource: #{eyaml_file}")

    data = YAML.load(File.read( eyaml_file ))

    next if data.nil? or data.empty?
    debug ("Data contains valid YAML")

    next unless data.include?(key)
    debug ("Key #{key} found in YAML document")

    parsed_answer = parse_answer(key, data[key], scope)

    begin
      case resolution_type
      when :array
        debug("Appending answer array")
        raise Exception, "Hiera type mismatch: expected Array and got #{parsed_answer.class}" unless parsed_answer.kind_of? Array or parsed_answer.kind_of? String
        answer ||= []
        answer << parsed_answer
      when :hash
        debug("Merging answer hash")
        raise Exception, "Hiera type mismatch: expected Hash and got #{parsed_answer.class}" unless parsed_answer.kind_of? Hash
        answer ||= {}
        answer = parsed_answer.merge answer
      else
        debug("Assigning answer variable")
        answer = parsed_answer
        break
      end
    rescue NoMethodError
      raise Exception, "Resolution type is #{resolution_type} but parsed_answer is a #{parsed_answer.class}"
    end
  end

  answer
end

#parse_answer(key, data, scope, extra_data = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hiera/backend/eyaml_backend.rb', line 58

def parse_answer(key, data, scope, extra_data={})
  if data.is_a?(Numeric) or data.is_a?(TrueClass) or data.is_a?(FalseClass)
    # Can't be encrypted
    data
  elsif data.is_a?(String)
    parsed_string = Backend.parse_string(data, scope)
    decrypt(key, parsed_string, scope)
  elsif data.is_a?(Hash)
    answer = {}
    data.each_pair do |key, val|
      answer[key] = parse_answer(key, val, scope, extra_data)
    end
    answer
  elsif data.is_a?(Array)
    answer = []
    data.each do |item|
      answer << parse_answer(key, item, scope, extra_data)
    end
    answer
  end
end

#warn(msg) ⇒ Object



119
120
121
# File 'lib/hiera/backend/eyaml_backend.rb', line 119

def warn(msg)
  Hiera.warn("[eyaml_backend]:  #{msg}")
end