Class: Hiera::Backend::Gpg_backend

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

Instance Method Summary collapse

Constructor Details

#initializeGpg_backend

Returns a new instance of Gpg_backend.



5
6
7
8
# File 'lib/hiera/backend/gpg_backend.rb', line 5

def initialize 
    require 'gpgme'
    debug ("Loaded gpg_backend")
end

Instance Method Details

#debug(msg) ⇒ Object



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

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

#decrypt(file, gnupghome) ⇒ Object



76
77
78
79
80
81
82
83
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/gpg_backend.rb', line 76

def decrypt(file, gnupghome)

ENV["GNUPGHOME"]=gnupghome
debug("GNUPGHOME is #{ENV['GNUPGHOME']}")

ctx = GPGME::Ctx.new

open(file) do |cipher|
    debug("loaded cipher: #{file}")

    ctx = GPGME::Ctx.new

        if !ctx.keys.empty?
            raw = GPGME::Data.new(cipher)
            txt = GPGME::Data.new

            begin
                txt = ctx.decrypt(raw)
            rescue GPGME::Error::DecryptFailed
                warn("Warning: GPG Decryption failed, check your GPG settings")
            rescue
                warn("Warning: General exception decrypting GPG file")
            end
            
            txt.seek 0
            result = txt.read

            debug("result is a #{result.class} ctx #{ctx} txt #{txt}")
            return result
        else
            warn("No usable keys found in #{gnupghome}. Check :key_dir value in hiera.yaml is correct")
        end
    end
end

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



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hiera/backend/gpg_backend.rb', line 19

def lookup(key, scope, order_override, resolution_type)

    debug("Lookup called, key #{key} resolution type is #{resolution_type}")
    answer = nil

    # This should compute ~ on both *nix and *doze
    homes = ["HOME", "HOMEPATH"]
    real_home = homes.detect { |h| ENV[h] != nil }

    ## key_dir is the location of our GPG private keys
    ## default: ~/.gnupg
    key_dir = Config[:gpg][:key_dir] || "#{ENV[real_home]}/.gnupg"


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

        plain = decrypt(gpgfile, key_dir)
        next if !plain
        next if plain.empty?
        debug("GPG decrypt returned valid data")

        data = YAML.load(plain)
        next if !data
        next if data.empty?
        debug ("Data contains valid YAML")

        next unless data.include?(key)
        debug ("Key #{key} found in YAML document, Passing answer to hiera")

        parsed_answer = Backend.parse_answer(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
    return answer
end

#warn(msg) ⇒ Object



14
15
16
# File 'lib/hiera/backend/gpg_backend.rb', line 14

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