jerakia 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,56 @@
1
+ # Parts of this class are copied from https://github.com/TomPoulton/hiera-eyaml/blob/master/lib/hiera/backend/eyaml_backend.rb
2
+ # The MIT License (MIT)
3
+ #
4
+ # Copyright (c) 2013 Tom Poulton
5
+ #
6
+ # Other code Copyright (c) 2014 Craig Dunn, Apache 2.0 License.
7
+ #
8
+
9
+ require 'hiera/backend/eyaml/encryptor'
10
+ require 'hiera/backend/eyaml/utils'
11
+ require 'hiera/backend/eyaml/options'
12
+ require 'hiera/backend/eyaml/parser/parser'
13
+ require 'hiera/filecache'
14
+
15
+ require 'yaml'
16
+
17
+ class Jerakia::Response
18
+ module Filter
19
+ module Encryption
20
+
21
+ def filter_encryption(opts={})
22
+ parse_values do |val|
23
+ if val.is_a?(String)
24
+ decrypt val
25
+ end
26
+ val
27
+ end
28
+ end
29
+
30
+ def decrypt(data)
31
+ if encrypted?(data)
32
+ public_key = config["eyaml"]["public_key"]
33
+ private_key = config["eyaml"]["private_key"]
34
+ Hiera::Backend::Eyaml::Options[:pkcs7_private_key] = private_key
35
+ Hiera::Backend::Eyaml::Options[:pkcs7_public_key] = public_key
36
+ parser = Hiera::Backend::Eyaml::Parser::ParserFactory.hiera_backend_parser
37
+
38
+ tokens = parser.parse(data)
39
+ decrypted = tokens.map{ |token| token.to_plain_text }
40
+ plaintext = decrypted.join
41
+ Jerakia.log.debug(plaintext)
42
+ plaintext.chomp!
43
+ data.clear.insert(0,plaintext)
44
+ else
45
+ data
46
+ end
47
+ end
48
+
49
+ def encrypted?(data)
50
+ /.*ENC\[.*?\]/ =~ data ? true : false
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+
@@ -0,0 +1,35 @@
1
+ # strsub is in output filter that matches tags in data and replaces them
2
+ # for values in the scope. It mimics the hiera features of being able to
3
+ # embed %{::var} in YAML documents. This output filter may not provide
4
+ # 100% compatibility to hiera but it should cover most scenarios.
5
+ #
6
+ # Jerakia does not support method or literal interpolations, just straightforward %{var} and %{::var}
7
+ #
8
+ # ::var will be lookuped up as scope[:var]
9
+
10
+ class Jerakia::Response
11
+ module Filter
12
+ module Strsub
13
+
14
+ def filter_strsub(opts={})
15
+ parse_values do |val|
16
+ if val.is_a?(String)
17
+ do_substr(val)
18
+ end
19
+ val
20
+ end
21
+ end
22
+
23
+ def do_substr(data)
24
+ data.gsub!(/%\{([^\}]*)\}/) do |tag|
25
+ Jerakia.log.debug("matched substr #{tag}")
26
+ scopekey = tag.match(/\{([^\}]+)\}/)[1]
27
+ scopekey.gsub!(/^::/,'')
28
+ lookup.scope[scopekey.to_sym]
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+
@@ -0,0 +1,9 @@
1
+ class Jerakia::Response
2
+ module Filter
3
+ def filter!(name,opts)
4
+ Jerakia::Util.autoload('response/filter', name)
5
+ instance_eval "extend Jerakia::Response::Filter::#{name.to_s.capitalize}"
6
+ instance_eval "self.filter_#{name.to_s} (#{opts})"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,57 @@
1
+ class Jerakia::Response < Jerakia
2
+
3
+ attr_accessor :entries
4
+ attr_reader :lookup
5
+
6
+ def initialize(lookup)
7
+ @entries=[]
8
+ @lookup=lookup
9
+ require 'jerakia/response/filter'
10
+ extend Jerakia::Response::Filter
11
+ end
12
+
13
+ def want?
14
+ if lookup.request.lookup_type == :first && entries.length > 0
15
+ return false
16
+ else
17
+ return true
18
+ end
19
+ end
20
+
21
+ def submit(val)
22
+ Jerakia.log.debug "Backend submitted #{val}"
23
+ unless want?
24
+ no_more_answers
25
+ else
26
+ @entries << {
27
+ :value => val,
28
+ :datatype => val.class.to_s.downcase
29
+ }
30
+ Jerakia.log.debug "Added answer #{val}"
31
+ end
32
+ end
33
+
34
+ def values
35
+ Jerakia::Util.walk(@entries) do |entry|
36
+ yield entry
37
+ end
38
+ end
39
+
40
+ def parse_values
41
+ @entries.map! do |entry|
42
+ Jerakia::Util.walk(entry[:value]) do |v|
43
+ yield v
44
+ end
45
+ entry
46
+ end
47
+
48
+ end
49
+
50
+
51
+
52
+ def no_more_answers
53
+ Jerakia.log.debug "warning: backend tried to submit too many answers"
54
+ end
55
+
56
+ end
57
+
@@ -0,0 +1,18 @@
1
+ # Default scope handler, this handler creates the scope using the
2
+ # key value pairs from the metadata of the request object
3
+ #
4
+ # This is by far the simplest scope handler, others can be more
5
+ # complex and build the scope.value hash from MCollective, PuppetDB
6
+ # or other data sources
7
+ #
8
+ #
9
+ class Jerakia::Scope
10
+ module Metadata
11
+ def create
12
+ request.metadata.each_pair do |key, val|
13
+ value[key.to_sym] = val
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,14 @@
1
+ class Jerakia::Scope < Jerakia::Policy
2
+
3
+ attr_reader :value
4
+ attr_reader :handler
5
+
6
+ def initialize(handler=nil)
7
+ @value = {}
8
+ @handler ||= request.scope || :metadata
9
+ Jerakia::Util.autoload('scope', @handler)
10
+ instance_eval "extend Jerakia::Scope::#{@handler.to_s.capitalize}"
11
+ create
12
+ end
13
+
14
+ end
@@ -0,0 +1,302 @@
1
+ class apache ( $port='foo' ,
2
+ $foo1='',
3
+ $foo2='',
4
+ $foo3='',
5
+ $foo4='',
6
+ $foo5='',
7
+ $foo6='',
8
+ $foo7='',
9
+ $foo8='',
10
+ $foo9='',
11
+ $foo10='',
12
+ $foo11='',
13
+ $foo12='',
14
+ $foo13='',
15
+ $foo14='',
16
+ $foo15='',
17
+ $foo16='',
18
+ $foo17='',
19
+ $foo18='',
20
+ $foo19='',
21
+ $foo20='',
22
+ $foo21='',
23
+ $foo22='',
24
+ $foo23='',
25
+ $foo24='',
26
+ $foo26='',
27
+ $bar1='',
28
+ $bar2='',
29
+ $bar3='',
30
+ $bar4='',
31
+ $bar5='',
32
+ $bar6='',
33
+ $bar7='',
34
+ $bar8='',
35
+ $bar9='',
36
+ $bar10='',
37
+ $bar11='',
38
+ $bar12='',
39
+ $bar13='',
40
+ $bar14='',
41
+ $bar15='',
42
+ $bar16='',
43
+ $bar17='',
44
+ $bar18='',
45
+ $bar19='',
46
+ $bar20='',
47
+ $bar21='',
48
+ $bar22='',
49
+ $bar23='',
50
+ $bar24='',
51
+ $foooo1='',
52
+ $foooo2='',
53
+ $foooo3='',
54
+ $foooo4='',
55
+ $foooo5='',
56
+ $foooo6='',
57
+ $foooo7='',
58
+ $foooo8='',
59
+ $foooo9='',
60
+ $foooo10='',
61
+ $foooo11='',
62
+ $foooo12='',
63
+ $foooo13='',
64
+ $foooo14='',
65
+ $foooo15='',
66
+ $foooo16='',
67
+ $foooo17='',
68
+ $foooo18='',
69
+ $foooo19='',
70
+ $foooo20='',
71
+ $foooo21='',
72
+ $foooo22='',
73
+ $foooo23='',
74
+ $foooo24='',
75
+ $foooo26='',
76
+ $baaar1='',
77
+ $baaar2='',
78
+ $baaar3='',
79
+ $baaar4='',
80
+ $baaar5='',
81
+ $baaar6='',
82
+ $baaar7='',
83
+ $baaar8='',
84
+ $baaar9='',
85
+ $baaar10='',
86
+ $baaar11='',
87
+ $baaar12='',
88
+ $baaar13='',
89
+ $baaar14='',
90
+ $baaar15='',
91
+ $baaar16='',
92
+ $baaar17='',
93
+ $baaar18='',
94
+ $baaar19='',
95
+ $baaar20='',
96
+ $baaar21='',
97
+ $baaar22='',
98
+ $baaar23='',
99
+ $baaar24='',
100
+ $fooz1='',
101
+ $fooz2='',
102
+ $fooz3='',
103
+ $fooz4='',
104
+ $fooz5='',
105
+ $fooz6='',
106
+ $fooz7='',
107
+ $fooz8='',
108
+ $fooz9='',
109
+ $fooz10='',
110
+ $fooz11='',
111
+ $fooz12='',
112
+ $fooz13='',
113
+ $fooz14='',
114
+ $fooz15='',
115
+ $fooz16='',
116
+ $fooz17='',
117
+ $fooz18='',
118
+ $fooz19='',
119
+ $fooz20='',
120
+ $fooz21='',
121
+ $fooz22='',
122
+ $fooz23='',
123
+ $fooz24='',
124
+ $fooz26='',
125
+ $barz1='',
126
+ $barz2='',
127
+ $barz3='',
128
+ $barz4='',
129
+ $barz5='',
130
+ $barz6='',
131
+ $barz7='',
132
+ $barz8='',
133
+ $barz9='',
134
+ $barz10='',
135
+ $barz11='',
136
+ $barz12='',
137
+ $barz13='',
138
+ $barz14='',
139
+ $barz15='',
140
+ $barz16='',
141
+ $barz17='',
142
+ $barz18='',
143
+ $barz19='',
144
+ $barz20='',
145
+ $barz21='',
146
+ $barz22='',
147
+ $barz23='',
148
+ $barz24='',
149
+ $foo10='',
150
+ $foo20='',
151
+ $foo30='',
152
+ $foo40='',
153
+ $foo50='',
154
+ $foo60='',
155
+ $foo70='',
156
+ $foo80='',
157
+ $foo90='',
158
+ $foo100='',
159
+ $foo110='',
160
+ $foo120='',
161
+ $foo130='',
162
+ $foo140='',
163
+ $foo150='',
164
+ $foo160='',
165
+ $foo170='',
166
+ $foo180='',
167
+ $foo190='',
168
+ $foo200='',
169
+ $foo210='',
170
+ $foo220='',
171
+ $foo230='',
172
+ $foo240='',
173
+ $foo260='',
174
+ $bar10='',
175
+ $bar20='',
176
+ $bar30='',
177
+ $bar40='',
178
+ $bar50='',
179
+ $bar60='',
180
+ $bar70='',
181
+ $bar80='',
182
+ $bar90='',
183
+ $bar100='',
184
+ $bar110='',
185
+ $bar120='',
186
+ $bar130='',
187
+ $bar140='',
188
+ $bar150='',
189
+ $bar160='',
190
+ $bar170='',
191
+ $bar180='',
192
+ $bar190='',
193
+ $bar200='',
194
+ $bar210='',
195
+ $bar220='',
196
+ $bar230='',
197
+ $bar240='',
198
+ $foooo10='',
199
+ $foooo20='',
200
+ $foooo30='',
201
+ $foooo40='',
202
+ $foooo50='',
203
+ $foooo60='',
204
+ $foooo70='',
205
+ $foooo80='',
206
+ $foooo90='',
207
+ $foooo100='',
208
+ $foooo110='',
209
+ $foooo120='',
210
+ $foooo130='',
211
+ $foooo140='',
212
+ $foooo150='',
213
+ $foooo160='',
214
+ $foooo170='',
215
+ $foooo180='',
216
+ $foooo190='',
217
+ $foooo200='',
218
+ $foooo210='',
219
+ $foooo220='',
220
+ $foooo230='',
221
+ $foooo240='',
222
+ $foooo260='',
223
+ $baaar10='',
224
+ $baaar20='',
225
+ $baaar30='',
226
+ $baaar40='',
227
+ $baaar50='',
228
+ $baaar60='',
229
+ $baaar70='',
230
+ $baaar80='',
231
+ $baaar90='',
232
+ $baaar100='',
233
+ $baaar110='',
234
+ $baaar120='',
235
+ $baaar130='',
236
+ $baaar140='',
237
+ $baaar150='',
238
+ $baaar160='',
239
+ $baaar170='',
240
+ $baaar180='',
241
+ $baaar190='',
242
+ $baaar200='',
243
+ $baaar210='',
244
+ $baaar220='',
245
+ $baaar230='',
246
+ $baaar240='',
247
+ $fooz10='',
248
+ $fooz20='',
249
+ $fooz30='',
250
+ $fooz40='',
251
+ $fooz50='',
252
+ $fooz60='',
253
+ $fooz70='',
254
+ $fooz80='',
255
+ $fooz90='',
256
+ $fooz100='',
257
+ $fooz110='',
258
+ $fooz120='',
259
+ $fooz130='',
260
+ $fooz140='',
261
+ $fooz150='',
262
+ $fooz160='',
263
+ $fooz170='',
264
+ $fooz180='',
265
+ $fooz190='',
266
+ $fooz200='',
267
+ $fooz210='',
268
+ $fooz220='',
269
+ $fooz230='',
270
+ $fooz240='',
271
+ $fooz260='',
272
+ $barz10='',
273
+ $barz20='',
274
+ $barz30='',
275
+ $barz40='',
276
+ $barz50='',
277
+ $barz60='',
278
+ $barz70='',
279
+ $barz80='',
280
+ $barz90='',
281
+ $barz100='',
282
+ $barz110='',
283
+ $barz120='',
284
+ $barz130='',
285
+ $barz140='',
286
+ $barz150='',
287
+ $barz160='',
288
+ $barz170='',
289
+ $barz180='',
290
+ $barz190='',
291
+ $barz200='',
292
+ $barz210='',
293
+ $barz220='',
294
+ $barz230='',
295
+ $barz240='',
296
+ ) {
297
+
298
+
299
+ notify { $port: }
300
+ }
301
+
302
+ include apache
@@ -0,0 +1,52 @@
1
+ class Jerakia
2
+ module Util
3
+ class << self
4
+ def autoload(path,mod)
5
+ Jerakia.log.debug "autoloading #{path} #{mod}"
6
+ require "jerakia/#{path}/#{mod.to_s}"
7
+ end
8
+
9
+ def walk(data)
10
+ if data.is_a?(Hash)
11
+ walk_hash(data) do |target|
12
+ yield target
13
+ end
14
+ elsif data.is_a?(Array)
15
+ walk_array(data) do |target|
16
+ yield target
17
+ end
18
+ else
19
+ yield data
20
+ end
21
+ end
22
+
23
+ def walk_hash(data)
24
+ data.inject({}) do |h,(k,v)|
25
+ if v.is_a?(Hash)
26
+ walk_hash(v) { |x| yield x }
27
+ elsif v.is_a?(Array)
28
+ walk_array(v) { |x| yield x }
29
+ else
30
+ yield v
31
+ end
32
+ h
33
+ end
34
+ return data
35
+ end
36
+
37
+ def walk_array(data)
38
+ data.map! do |element|
39
+ if element.is_a?(Hash)
40
+ walk_hash(element) { |x| yield x }
41
+ elsif element.is_a?(Array)
42
+ walk_array(element) { |x| yield x }
43
+ else
44
+ yield element
45
+ end
46
+ element
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
data/lib/jerakia.rb ADDED
@@ -0,0 +1,72 @@
1
+ #require 'faster_require'
2
+
3
+ class Jerakia
4
+ require 'jerakia/policy'
5
+ require 'jerakia/lookup'
6
+ require 'jerakia/request'
7
+ require 'jerakia/log'
8
+ require 'jerakia/util'
9
+ require 'jerakia/config'
10
+ require 'jerakia/launcher'
11
+ require 'jerakia/cache'
12
+
13
+
14
+
15
+
16
+ def initialize(options={})
17
+ configfile = options[:config] || '/etc/jerakia/jerakia.yml'
18
+ @@config = Jerakia::Config.new(configfile)
19
+ @@filecache = {}
20
+ @@cache = Jerakia::Cache.new
21
+ loglevel = options[:loglevel] || @@config["loglevel"] || "info"
22
+ @@log = Jerakia::Log.new(loglevel.to_sym)
23
+ @@log.debug("Jerakia initialized")
24
+
25
+ end
26
+
27
+ def lookup(request)
28
+ res=Jerakia::Launcher.new(request)
29
+ return res.answer
30
+ end
31
+
32
+ def config
33
+ @@config
34
+ end
35
+
36
+
37
+ def self.fatal(msg,e)
38
+ stacktrace=e.backtrace.join("\n")
39
+ Jerakia.log.fatal msg
40
+ Jerakia.log.fatal "Full stacktrace output:\n#{$!}\n\n#{stacktrace}"
41
+ puts "Fatal error, check log output for details"
42
+ exit 1
43
+ end
44
+
45
+ def self.config
46
+ @@config
47
+ end
48
+
49
+ def self.filecache(name)
50
+ @@filecache[name] ||= File.read(name)
51
+ return @@filecache[name]
52
+ end
53
+
54
+ def add_to_filecache(name,data)
55
+ @@filecache[name] = data
56
+ end
57
+
58
+ def log
59
+ @@log
60
+ end
61
+
62
+ def self.log
63
+ @@log
64
+ end
65
+
66
+ def self.crit(msg)
67
+
68
+ puts msg
69
+ exit 1
70
+ end
71
+ end
72
+
@@ -0,0 +1,37 @@
1
+ require 'puppet/indirector/code'
2
+ require 'jerakia'
3
+ require 'json'
4
+
5
+ class Puppet::DataBinding::Jerakia < Puppet::Indirector::Code
6
+ desc "Data binding for Jerakia"
7
+
8
+ attr_reader :jerakia
9
+ attr_reader :policy
10
+
11
+ def initialize(*args)
12
+ @jerakia=::Jerakia.new
13
+
14
+ # Currently defaulting the policy to "puppet" - we should change this.
15
+ @default_policy = @jerakia::config["puppet"]["default_policy"] || "puppet"
16
+ super
17
+ end
18
+
19
+ def find(request)
20
+
21
+ lookupdata=request.key.split(/::/)
22
+ key=lookupdata.pop
23
+ namespace=lookupdata
24
+ metadata = request.options[:variables].to_hash
25
+ policy=metadata[:jerakia_policy] || @default_policy
26
+ jacreq = Jerakia::Request.new(
27
+ :key => key,
28
+ :namespace => namespace,
29
+ :policy => policy,
30
+ :lookup_type => :first,
31
+ :metadata => {}
32
+ )
33
+ answer = jerakia.lookup(jacreq)
34
+ answer.payload
35
+ end
36
+ end
37
+
@@ -0,0 +1,46 @@
1
+ # This is a proof of concept, and highly experimental to enable a data binding
2
+ # that talks to Jerakia's REST API. At present we only pass the environment
3
+ # from the scope as the whole scope hash is too large for the HTTP request.
4
+ #
5
+ # This may or may not be supported in future versions but feel free to contribute :)
6
+ #
7
+ require 'puppet/indirector/code'
8
+ require 'rest_client'
9
+ require 'jerakia'
10
+ require 'json'
11
+
12
+ class Puppet::DataBinding::Jerakia_rest < Puppet::Indirector::Code
13
+ desc "Data binding for Jerakia"
14
+
15
+ attr_reader :jerakia
16
+ attr_reader :jerakia_url
17
+ attr_reader :policy
18
+
19
+ def initialize(*args)
20
+ @jerakia=::Jerakia.new
21
+ @jerakia_url=@jerakia.config.server_url
22
+ @policy = "puppet"
23
+ super
24
+ end
25
+
26
+ def find(request)
27
+
28
+ lookupdata=request.key.split(/::/)
29
+ key=lookupdata.pop
30
+ namespace=lookupdata
31
+
32
+ #metadata = request.options[:variables].to_hash
33
+
34
+ metadata = {
35
+ :environment => request.options[:variables].environment,
36
+ }
37
+ payload={
38
+ :namespace => namespace,
39
+ :lookup_type => :first,
40
+ :metadata => metadata,
41
+ }.to_json
42
+ response = RestClient.get "#{jerakia_url}/#{policy}/#{key}", :params => { :payload => payload }
43
+ response
44
+ end
45
+ end
46
+