leap_cli 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. data/bin/leap +81 -0
  2. data/lib/core_ext/boolean.rb +14 -0
  3. data/lib/core_ext/hash.rb +35 -0
  4. data/lib/core_ext/json.rb +42 -0
  5. data/lib/core_ext/nil.rb +5 -0
  6. data/lib/core_ext/string.rb +14 -0
  7. data/lib/leap/platform.rb +52 -0
  8. data/lib/leap_cli/commands/ca.rb +430 -0
  9. data/lib/leap_cli/commands/clean.rb +16 -0
  10. data/lib/leap_cli/commands/compile.rb +134 -0
  11. data/lib/leap_cli/commands/deploy.rb +172 -0
  12. data/lib/leap_cli/commands/facts.rb +93 -0
  13. data/lib/leap_cli/commands/inspect.rb +140 -0
  14. data/lib/leap_cli/commands/list.rb +122 -0
  15. data/lib/leap_cli/commands/new.rb +126 -0
  16. data/lib/leap_cli/commands/node.rb +272 -0
  17. data/lib/leap_cli/commands/pre.rb +99 -0
  18. data/lib/leap_cli/commands/shell.rb +67 -0
  19. data/lib/leap_cli/commands/test.rb +55 -0
  20. data/lib/leap_cli/commands/user.rb +140 -0
  21. data/lib/leap_cli/commands/util.rb +50 -0
  22. data/lib/leap_cli/commands/vagrant.rb +201 -0
  23. data/lib/leap_cli/config/macros.rb +369 -0
  24. data/lib/leap_cli/config/manager.rb +369 -0
  25. data/lib/leap_cli/config/node.rb +37 -0
  26. data/lib/leap_cli/config/object.rb +336 -0
  27. data/lib/leap_cli/config/object_list.rb +174 -0
  28. data/lib/leap_cli/config/secrets.rb +43 -0
  29. data/lib/leap_cli/config/tag.rb +18 -0
  30. data/lib/leap_cli/constants.rb +7 -0
  31. data/lib/leap_cli/leapfile.rb +97 -0
  32. data/lib/leap_cli/load_paths.rb +15 -0
  33. data/lib/leap_cli/log.rb +166 -0
  34. data/lib/leap_cli/logger.rb +216 -0
  35. data/lib/leap_cli/markdown_document_listener.rb +134 -0
  36. data/lib/leap_cli/path.rb +84 -0
  37. data/lib/leap_cli/remote/leap_plugin.rb +204 -0
  38. data/lib/leap_cli/remote/puppet_plugin.rb +66 -0
  39. data/lib/leap_cli/remote/rsync_plugin.rb +35 -0
  40. data/lib/leap_cli/remote/tasks.rb +36 -0
  41. data/lib/leap_cli/requirements.rb +19 -0
  42. data/lib/leap_cli/ssh_key.rb +130 -0
  43. data/lib/leap_cli/util/remote_command.rb +110 -0
  44. data/lib/leap_cli/util/secret.rb +54 -0
  45. data/lib/leap_cli/util/x509.rb +32 -0
  46. data/lib/leap_cli/util.rb +431 -0
  47. data/lib/leap_cli/version.rb +9 -0
  48. data/lib/leap_cli.rb +46 -0
  49. data/lib/lib_ext/capistrano_connections.rb +16 -0
  50. data/lib/lib_ext/gli.rb +52 -0
  51. data/lib/lib_ext/markdown_document_listener.rb +122 -0
  52. data/vendor/certificate_authority/lib/certificate_authority/certificate.rb +200 -0
  53. data/vendor/certificate_authority/lib/certificate_authority/certificate_revocation_list.rb +77 -0
  54. data/vendor/certificate_authority/lib/certificate_authority/distinguished_name.rb +97 -0
  55. data/vendor/certificate_authority/lib/certificate_authority/extensions.rb +266 -0
  56. data/vendor/certificate_authority/lib/certificate_authority/key_material.rb +148 -0
  57. data/vendor/certificate_authority/lib/certificate_authority/ocsp_handler.rb +144 -0
  58. data/vendor/certificate_authority/lib/certificate_authority/pkcs11_key_material.rb +65 -0
  59. data/vendor/certificate_authority/lib/certificate_authority/revocable.rb +14 -0
  60. data/vendor/certificate_authority/lib/certificate_authority/serial_number.rb +10 -0
  61. data/vendor/certificate_authority/lib/certificate_authority/signing_entity.rb +16 -0
  62. data/vendor/certificate_authority/lib/certificate_authority/signing_request.rb +56 -0
  63. data/vendor/certificate_authority/lib/certificate_authority.rb +21 -0
  64. data/vendor/rsync_command/lib/rsync_command/ssh_options.rb +159 -0
  65. data/vendor/rsync_command/lib/rsync_command/thread_pool.rb +36 -0
  66. data/vendor/rsync_command/lib/rsync_command/version.rb +3 -0
  67. data/vendor/rsync_command/lib/rsync_command.rb +96 -0
  68. data/vendor/rsync_command/test/rsync_test.rb +74 -0
  69. data/vendor/rsync_command/test/ssh_options_test.rb +61 -0
  70. data/vendor/vagrant_ssh_keys/vagrant.key +27 -0
  71. data/vendor/vagrant_ssh_keys/vagrant.pub +1 -0
  72. metadata +345 -0
@@ -0,0 +1,369 @@
1
+ #
2
+ # MACROS
3
+ # these are methods available when eval'ing a value in the .json configuration
4
+ #
5
+ # This module is included in Config::Object
6
+ #
7
+
8
+ module LeapCli; module Config
9
+ module Macros
10
+ ##
11
+ ## NODES
12
+ ##
13
+
14
+ #
15
+ # the list of all the nodes
16
+ #
17
+ def nodes
18
+ global.nodes
19
+ end
20
+
21
+ #
22
+ # returns a list of nodes that match the same environment
23
+ #
24
+ # if @node.environment is not set, we return other nodes
25
+ # where environment is not set.
26
+ #
27
+ def nodes_like_me
28
+ nodes[:environment => @node.environment]
29
+ end
30
+
31
+ ##
32
+ ## FILES
33
+ ##
34
+
35
+ class FileMissing < Exception
36
+ attr_accessor :path, :options
37
+ def initialize(path, options={})
38
+ @path = path
39
+ @options = options
40
+ end
41
+ def to_s
42
+ @path
43
+ end
44
+ end
45
+
46
+ #
47
+ # inserts the contents of a file
48
+ #
49
+ def file(filename, options={})
50
+ if filename.is_a? Symbol
51
+ filename = [filename, @node.name]
52
+ end
53
+ filepath = Path.find_file(filename)
54
+ if filepath
55
+ if filepath =~ /\.erb$/
56
+ ERB.new(File.read(filepath), nil, '%<>').result(binding)
57
+ else
58
+ File.read(filepath)
59
+ end
60
+ else
61
+ raise FileMissing.new(Path.named_path(filename), options)
62
+ ""
63
+ end
64
+ end
65
+
66
+ #
67
+ # like #file, but allow missing files
68
+ #
69
+ def try_file(filename)
70
+ return file(filename)
71
+ rescue FileMissing
72
+ return nil
73
+ end
74
+
75
+ #
76
+ # returns what the file path will be, once the file is rsynced to the server.
77
+ # an internal list of discovered file paths is saved, in order to rsync these files when needed.
78
+ #
79
+ # notes:
80
+ #
81
+ # * argument 'path' is relative to Path.provider/files or Path.provider_base/files
82
+ # * the path returned by this method is absolute
83
+ # * the path stored for use later by rsync is relative to Path.provider
84
+ # * if the path does not exist locally, but exists in provider_base, then the default file from
85
+ # provider_base is copied locally. this is required for rsync to work correctly.
86
+ #
87
+ def file_path(path)
88
+ if path.is_a? Symbol
89
+ path = [path, @node.name]
90
+ end
91
+ actual_path = Path.find_file(path)
92
+ if actual_path.nil?
93
+ Util::log 2, :skipping, "file_path(\"#{path}\") because there is no such file."
94
+ nil
95
+ else
96
+ if actual_path =~ /^#{Regexp.escape(Path.provider_base)}/
97
+ # if file is under Path.provider_base, we must copy the default file to
98
+ # to Path.provider in order for rsync to be able to sync the file.
99
+ local_provider_path = actual_path.sub(/^#{Regexp.escape(Path.provider_base)}/, Path.provider)
100
+ FileUtils.mkdir_p File.dirname(local_provider_path), :mode => 0700
101
+ FileUtils.install actual_path, local_provider_path, :mode => 0600
102
+ Util.log :created, Path.relative_path(local_provider_path)
103
+ actual_path = local_provider_path
104
+ end
105
+ if File.directory?(actual_path) && actual_path !~ /\/$/
106
+ actual_path += '/' # ensure directories end with /, important for building rsync command
107
+ end
108
+ relative_path = Path.relative_path(actual_path)
109
+ @node.file_paths << relative_path
110
+ @node.manager.provider.hiera_sync_destination + '/' + relative_path
111
+ end
112
+ end
113
+
114
+ #
115
+ # inserts a named secret, generating it if needed.
116
+ #
117
+ # manager.export_secrets should be called later to capture any newly generated secrets.
118
+ #
119
+ # +length+ is the character length of the generated password.
120
+ #
121
+ def secret(name, length=32)
122
+ @manager.secrets.set(name, Util::Secret.generate(length))
123
+ end
124
+
125
+ #
126
+ # inserts an hexidecimal secret string, generating it if needed.
127
+ #
128
+ # +bit_length+ is the bits in the secret, (ie length of resulting hex string will be bit_length/4)
129
+ #
130
+ def hex_secret(name, bit_length=128)
131
+ @manager.secrets.set(name, Util::Secret.generate_hex(bit_length))
132
+ end
133
+
134
+ #
135
+ # return a fingerprint for a x509 certificate
136
+ #
137
+ def fingerprint(filename)
138
+ "SHA256: " + X509.fingerprint("SHA256", Path.named_path(filename))
139
+ end
140
+
141
+ ##
142
+ ## HOSTS
143
+ ##
144
+
145
+ #
146
+ # records the list of hosts that are encountered for this node
147
+ #
148
+ def hostnames(nodes)
149
+ @referenced_nodes ||= ObjectList.new
150
+ if nodes.is_a? Config::Object
151
+ nodes = ObjectList.new nodes
152
+ end
153
+ nodes.each_node do |node|
154
+ @referenced_nodes[node.name] ||= node
155
+ end
156
+ return nodes.values.collect {|node| node.domain.name}
157
+ end
158
+
159
+ #
160
+ # Generates entries needed for updating /etc/hosts on a node, but only including the IPs of the
161
+ # other nodes we have encountered. Also, for virtual machines, use the local address if this
162
+ # @node is in the same location.
163
+ #
164
+ def hosts_file
165
+ if @referenced_nodes && @referenced_nodes.any?
166
+ hosts = {}
167
+ my_location = @node['location'] ? @node['location']['name'] : nil
168
+ @referenced_nodes.each_node do |node|
169
+ next if node.name == @node.name
170
+ hosts[node.name] = {'ip_address' => node.ip_address, 'domain_internal' => node.domain.internal, 'domain_full' => node.domain.full}
171
+ node_location = node['location'] ? node['location']['name'] : nil
172
+ if my_location == node_location
173
+ if facts = @node.manager.facts[node.name]
174
+ if facts['ec2_public_ipv4']
175
+ hosts[node.name]['ip_address'] = facts['ec2_public_ipv4']
176
+ end
177
+ end
178
+ end
179
+ end
180
+ #hosts = @referenced_nodes.pick_fields("ip_address", "domain.internal", "domain.full")
181
+ return hosts
182
+ else
183
+ return nil
184
+ end
185
+ end
186
+
187
+ ##
188
+ ## STUNNEL
189
+ ##
190
+
191
+ #
192
+ # stunnel configuration for the client side.
193
+ #
194
+ # +node_list+ is a ObjectList of nodes running stunnel servers.
195
+ #
196
+ # +port+ is the real port of the ultimate service running on the servers
197
+ # that the client wants to connect to.
198
+ #
199
+ # About ths stunnel puppet names:
200
+ #
201
+ # * accept_port is the port on localhost to which local clients
202
+ # can connect. it is auto generated serially.
203
+ # * connect_port is the port on the stunnel server to connect to.
204
+ # it is auto generated from the +port+ argument.
205
+ #
206
+ # The network looks like this:
207
+ #
208
+ # |------ stunnel client ---------------| |--------- stunnel server -----------------------|
209
+ # consumer app -> localhost:accept_port -> server:connect_port -> server:port -> service app
210
+ #
211
+ # generates an entry appropriate to be passed directly to
212
+ # create_resources(stunnel::service, hiera('..'), defaults)
213
+ #
214
+ def stunnel_client(node_list, port, options={})
215
+ @next_stunnel_port ||= 4000
216
+ hostnames(node_list) # record the hosts
217
+ node_list.values.inject(Config::ObjectList.new) do |hsh, node|
218
+ if node.name != self.name || options[:include_self]
219
+ hsh["#{node.name}_#{port}"] = Config::Object[
220
+ 'accept_port', @next_stunnel_port,
221
+ 'connect', node.domain.internal,
222
+ 'connect_port', stunnel_port(port)
223
+ ]
224
+ @next_stunnel_port += 1
225
+ end
226
+ hsh
227
+ end
228
+ end
229
+
230
+ #
231
+ # generates a stunnel server entry.
232
+ #
233
+ # +port+ is the real port targeted service.
234
+ #
235
+ def stunnel_server(port)
236
+ {"accept" => stunnel_port(port), "connect" => "127.0.0.1:#{port}"}
237
+ end
238
+
239
+ #
240
+ # maps a real port to a stunnel port (used as the connect_port in the client config
241
+ # and the accept_port in the server config)
242
+ #
243
+ def stunnel_port(port)
244
+ port = port.to_i
245
+ if port < 50000
246
+ return port + 10000
247
+ else
248
+ return port - 10000
249
+ end
250
+ end
251
+
252
+ ##
253
+ ## HAPROXY
254
+ ##
255
+
256
+ #
257
+ # creates a hash suitable for configuring haproxy. the key is the node name of the server we are proxying to.
258
+ #
259
+ # * node_list - a hash of nodes for the haproxy servers
260
+ # * stunnel_client - contains the mappings to local ports for each server node.
261
+ # * non_stunnel_port - in case self is included in node_list, the port to connect to.
262
+ #
263
+ # 1000 weight is used for nodes in the same location.
264
+ # 100 otherwise.
265
+ #
266
+ def haproxy_servers(node_list, stunnel_clients, non_stunnel_port=nil)
267
+ default_weight = 10
268
+ local_weight = 100
269
+
270
+ # record the hosts_file
271
+ hostnames(node_list)
272
+
273
+ # create a simple map for node name -> local stunnel accept port
274
+ accept_ports = stunnel_clients.inject({}) do |hsh, stunnel_entry|
275
+ name = stunnel_entry.first.sub /_[0-9]+$/, ''
276
+ hsh[name] = stunnel_entry.last['accept_port']
277
+ hsh
278
+ end
279
+
280
+ # if one the nodes in the node list is ourself, then there will not be a stunnel to it,
281
+ # but we need to include it anyway in the haproxy config.
282
+ if node_list[self.name] && non_stunnel_port
283
+ accept_ports[self.name] = non_stunnel_port
284
+ end
285
+
286
+ # create the first pass of the servers hash
287
+ servers = node_list.values.inject(Config::ObjectList.new) do |hsh, node|
288
+ weight = default_weight
289
+ if self['location'] && node['location']
290
+ if self.location['name'] == node.location['name']
291
+ weight = local_weight
292
+ end
293
+ end
294
+ hsh[node.name] = Config::Object[
295
+ 'backup', false,
296
+ 'host', 'localhost',
297
+ 'port', accept_ports[node.name] || 0,
298
+ 'weight', weight
299
+ ]
300
+ hsh
301
+ end
302
+
303
+ # if there are some local servers, make the others backup
304
+ if servers.detect{|k,v| v.weight == local_weight}
305
+ servers.each do |k,server|
306
+ server['backup'] = server['weight'] == default_weight
307
+ end
308
+ end
309
+
310
+ return servers
311
+ end
312
+
313
+ ##
314
+ ## SSH
315
+ ##
316
+
317
+ #
318
+ # creates a hash from the ssh key info in users directory, for use in updating authorized_keys file
319
+ #
320
+ def authorized_keys
321
+ hash = {}
322
+ Dir.glob(Path.named_path([:user_ssh, '*'])).sort.each do |keyfile|
323
+ ssh_type, ssh_key = File.read(keyfile).strip.split(" ")
324
+ name = File.basename(File.dirname(keyfile))
325
+ hash[name] = {
326
+ "type" => ssh_type,
327
+ "key" => ssh_key
328
+ }
329
+ end
330
+ hash
331
+ end
332
+
333
+ def known_hosts_file
334
+ return nil unless @referenced_nodes
335
+ entries = []
336
+ @referenced_nodes.each_node do |node|
337
+ hostnames = [node.name, node.domain.internal, node.domain.full, node.ip_address].join(',')
338
+ pub_key = Util::read_file([:node_ssh_pub_key,node.name])
339
+ if pub_key
340
+ entries << [hostnames, pub_key].join(' ')
341
+ end
342
+ end
343
+ entries.join("\n")
344
+ end
345
+
346
+ ##
347
+ ## UTILITY
348
+ ##
349
+
350
+ class AssertionFailed < Exception
351
+ attr_accessor :assertion
352
+ def initialize(assertion)
353
+ @assertion = assertion
354
+ end
355
+ def to_s
356
+ @assertion
357
+ end
358
+ end
359
+
360
+ def assert(assertion)
361
+ if instance_eval(assertion)
362
+ true
363
+ else
364
+ raise AssertionFailed.new(assertion)
365
+ end
366
+ end
367
+
368
+ end
369
+ end; end