Class: BoltServer::TransportApp
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- BoltServer::TransportApp
- Defined in:
- lib/bolt_server/transport_app.rb
Constant Summary collapse
- PARTIAL_SCHEMAS =
These partial schemas are reused to build multiple request schemas
%w[target-any target-ssh target-winrm task].freeze
- REQUEST_SCHEMAS =
These schemas combine shared schemas to describe client requests
%w[ action-check_node_connections action-run_command action-run_task action-run_script action-upload_file transport-ssh transport-winrm ].freeze
- ACTIONS =
%w[ check_node_connections run_command run_task run_script upload_file ].freeze
Instance Method Summary collapse
- #allowed_helper(metadata, allowlist) ⇒ Object
- #build_puppetserver_uri(file_identifier, module_name, parameters) ⇒ Object
- #check_node_connections(targets, body) ⇒ Object
- #error_result(error) ⇒ Object
- #in_bolt_project(bolt_project) ⇒ Object
- #in_pe_pal_env(environment) ⇒ Object
-
#initialize(config) ⇒ TransportApp
constructor
A new instance of TransportApp.
- #make_ssh_target(target_hash) ⇒ Object
- #make_winrm_target(target_hash) ⇒ Object
- #pe_plan_info(pal, module_name, plan_name) ⇒ Object
- #pe_task_info(pal, module_name, task_name, parameters) ⇒ Object
- #plan_list(pal) ⇒ Object
-
#result_set_to_data(result_set, aggregate: false) ⇒ Object
Turns a Bolt::ResultSet object into a status hash that is fit to return to the client in a response.
- #run_command(target, body) ⇒ Object
- #run_script(target, body) ⇒ Object
- #run_task(target, body) ⇒ Object
- #scrub_stack_trace(result) ⇒ Object
- #task_list(pal) ⇒ Object
- #upload_file(target, body) ⇒ Object
- #validate_schema(schema, body) ⇒ Object
Constructor Details
#initialize(config) ⇒ TransportApp
Returns a new instance of TransportApp.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/bolt_server/transport_app.rb', line 38 def initialize(config) @config = config @schemas = Hash[REQUEST_SCHEMAS.map do |basename| [basename, JSON.parse(File.read(File.join(__dir__, ['schemas', "#{basename}.json"])))] end] PARTIAL_SCHEMAS.each do |basename| schema_content = JSON.parse(File.read(File.join(__dir__, ['schemas', 'partials', "#{basename}.json"]))) shared_schema = JSON::Schema.new(schema_content, Addressable::URI.parse("partial:#{basename}")) JSON::Validator.add_schema(shared_schema) end @executor = Bolt::Executor.new(0) @file_cache = BoltServer::FileCache.new(@config).setup # This is needed until the PAL is threadsafe. @pal_mutex = Mutex.new super(nil) end |
Instance Method Details
#allowed_helper(metadata, allowlist) ⇒ Object
295 296 297 298 |
# File 'lib/bolt_server/transport_app.rb', line 295 def allowed_helper(, allowlist) allowed = allowlist.nil? || allowlist.include?(['name']) ? true : false .merge({ 'allowed' => allowed }) end |
#build_puppetserver_uri(file_identifier, module_name, parameters) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/bolt_server/transport_app.rb', line 249 def build_puppetserver_uri(file_identifier, module_name, parameters) segments = file_identifier.split('/', 3) if segments.size == 1 { 'path' => "/puppet/v3/file_content/tasks/#{module_name}/#{file_identifier}", 'params' => parameters } else module_segment, mount_segment, name_segment = *segments { 'path' => case mount_segment when 'files' "/puppet/v3/file_content/modules/#{module_segment}/#{name_segment}" when 'tasks' "/puppet/v3/file_content/tasks/#{module_segment}/#{name_segment}" when 'lib' "/puppet/v3/file_content/plugins/#{name_segment}" end, 'params' => parameters } end end |
#check_node_connections(targets, body) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/bolt_server/transport_app.rb', line 127 def check_node_connections(targets, body) error = validate_schema(@schemas["action-check_node_connections"], body) return [], error unless error.nil? # Puppet Enterprise's orchestrator service uses the # check_node_connections endpoint to check whether nodes that should be # contacted over SSH or WinRM are responsive. The wait time here is 0 # because the endpoint is meant to be used for a single check of all # nodes; External implementations of wait_until_available (like # orchestrator's) should contact the endpoint in their own loop. [@executor.wait_until_available(targets, wait_time: 0), nil] end |
#error_result(error) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/bolt_server/transport_app.rb', line 67 def error_result(error) { 'status' => 'failure', 'value' => { '_error' => error.to_h } } end |
#in_bolt_project(bolt_project) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/bolt_server/transport_app.rb', line 209 def in_bolt_project(bolt_project) return [400, '`project_ref` is a required argument'] if bolt_project.nil? project_dir = File.join(@config['projects-dir'], bolt_project) return [400, "`project_ref`: #{project_dir} does not exist"] unless Dir.exist?(project_dir) @pal_mutex.synchronize do project = Bolt::Project.create_project(project_dir) bolt_config = Bolt::Config.from_project(project, { log: { 'bolt-debug.log' => 'disable' } }) pal = Bolt::PAL.new(bolt_config.modulepath, nil, nil, nil, nil, nil, bolt_config.project) module_path = [ BoltServer::PE::PAL::PE_BOLTLIB_PATH, Bolt::PAL::BOLTLIB_PATH, *bolt_config.modulepath, Bolt::PAL::MODULES_PATH ] # CODEREVIEW: I *think* this is the only thing we need to make different between bolt's PAL. Is it acceptable # to hack this? Modulepath is currently a readable attribute, could we make it writeable? pal.instance_variable_set(:@modulepath, module_path) context = { pal: pal, config: bolt_config } yield context rescue Bolt::Error => e [400, e.to_json] end end |
#in_pe_pal_env(environment) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/bolt_server/transport_app.rb', line 194 def in_pe_pal_env(environment) return [400, '`environment` is a required argument'] if environment.nil? @pal_mutex.synchronize do pal = BoltServer::PE::PAL.new({}, environment) yield pal rescue Puppet::Environments::EnvironmentNotFound [400, { "class" => 'bolt/unknown-environment', "message" => "Environment #{environment} not found" }.to_json] rescue Bolt::Error => e [400, e.to_json] end end |
#make_ssh_target(target_hash) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/bolt_server/transport_app.rb', line 342 def make_ssh_target(target_hash) defaults = { 'host-key-check' => false } overrides = { 'load-config' => false } opts = defaults.merge(target_hash).merge(overrides) if opts['private-key-content'] private_key_content = opts.delete('private-key-content') opts['private-key'] = { 'key-data' => private_key_content } end data = { 'uri' => target_hash['hostname'], 'config' => { 'transport' => 'ssh', 'ssh' => opts } } inventory = Bolt::Inventory.empty Bolt::Target.from_hash(data, inventory) end |
#make_winrm_target(target_hash) ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/bolt_server/transport_app.rb', line 390 def make_winrm_target(target_hash) defaults = { 'ssl' => false, 'ssl-verify' => false } opts = defaults.merge(target_hash) data = { 'uri' => target_hash['hostname'], 'config' => { 'transport' => 'winrm', 'winrm' => opts } } inventory = Bolt::Inventory.empty Bolt::Target.from_hash(data, inventory) end |
#pe_plan_info(pal, module_name, plan_name) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/bolt_server/transport_app.rb', line 236 def pe_plan_info(pal, module_name, plan_name) # Handle case where plan name is simply module name with special `init.pp` plan plan_name = if plan_name == 'init' || plan_name.nil? module_name else "#{module_name}::#{plan_name}" end plan_info = pal.get_plan_info(plan_name) # Path to module is meaningless in PE plan_info.delete('module') plan_info end |
#pe_task_info(pal, module_name, task_name, parameters) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/bolt_server/transport_app.rb', line 272 def pe_task_info(pal, module_name, task_name, parameters) # Handle case where task name is simply module name with special `init` task task_name = if task_name == 'init' || task_name.nil? module_name else "#{module_name}::#{task_name}" end task = pal.get_task(task_name) files = task.files.map do |file_hash| { 'filename' => file_hash['name'], 'sha256' => Digest::SHA256.hexdigest(File.read(file_hash['path'])), 'size_bytes' => File.size(file_hash['path']), 'uri' => build_puppetserver_uri(file_hash['name'], module_name, parameters) } end { 'metadata' => task., 'name' => task.name, 'files' => files } end |
#plan_list(pal) ⇒ Object
305 306 307 308 |
# File 'lib/bolt_server/transport_app.rb', line 305 def plan_list(pal) plans = pal.list_plans.flatten plans.map { |plan_name| { 'name' => plan_name } } end |
#result_set_to_data(result_set, aggregate: false) ⇒ Object
Turns a Bolt::ResultSet object into a status hash that is fit to return to the client in a response.
If the ‘result_set` has more than one result, the status hash will have a `status` value and a list of target `results`. If the `result_set` contains only one item, it will be returned as a single result object. Set `aggregate` to treat it as a set of results with length 1 instead.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/bolt_server/transport_app.rb', line 91 def result_set_to_data(result_set, aggregate: false) scrubbed_results = result_set.map do |result| scrub_stack_trace(result.to_data) end if aggregate || scrubbed_results.length > 1 # For actions that act on multiple targets, construct a status hash for the aggregate result all_succeeded = scrubbed_results.all? { |r| r['status'] == 'success' } { status: all_succeeded ? 'success' : 'failure', result: scrubbed_results } else # If there was only one target, return the first result on its own scrubbed_results.first end end |
#run_command(target, body) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/bolt_server/transport_app.rb', line 119 def run_command(target, body) error = validate_schema(@schemas["action-run_command"], body) return [], error unless error.nil? command = body['command'] [@executor.run_command(target, command), nil] end |
#run_script(target, body) ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'lib/bolt_server/transport_app.rb', line 184 def run_script(target, body) error = validate_schema(@schemas["action-run_script"], body) return [], error unless error.nil? # Download the file onto the machine. file_location = @file_cache.update_file(body['script']) [@executor.run_script(target, file_location, body['arguments'])] end |
#run_task(target, body) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/bolt_server/transport_app.rb', line 109 def run_task(target, body) error = validate_schema(@schemas["action-run_task"], body) return [], error unless error.nil? task_data = body['task'] task = Bolt::Task::PuppetServer.new(task_data['name'], task_data['metadata'], task_data['files'], @file_cache) parameters = body['parameters'] || {} [@executor.run_task(target, task, parameters), nil] end |
#scrub_stack_trace(result) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/bolt_server/transport_app.rb', line 60 def scrub_stack_trace(result) if result.dig('value', '_error', 'details', 'stack_trace') result['value']['_error']['details'].reject! { |k| k == 'stack_trace' } end result end |
#task_list(pal) ⇒ Object
300 301 302 303 |
# File 'lib/bolt_server/transport_app.rb', line 300 def task_list(pal) tasks = pal.list_tasks tasks.map { |task_name, _description| { 'name' => task_name } } end |
#upload_file(target, body) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/bolt_server/transport_app.rb', line 140 def upload_file(target, body) error = validate_schema(@schemas["action-upload_file"], body) return [], error unless error.nil? files = body['files'] destination = body['destination'] job_id = body['job_id'] cache_dir = @file_cache.create_cache_dir(job_id.to_s) FileUtils.mkdir_p(cache_dir) files.each do |file| relative_path = file['relative_path'] uri = file['uri'] sha256 = file['sha256'] kind = file['kind'] path = File.join(cache_dir, relative_path) case kind when 'file' # The parent should already be created by `directory` entries, # but this is to be on the safe side. parent = File.dirname(path) FileUtils.mkdir_p(parent) @file_cache.serial_execute { @file_cache.download_file(path, sha256, uri) } when 'directory' # Create directory in cache so we can move files in. FileUtils.mkdir_p(path) else return [400, Bolt::Error.new("Invalid `kind` of '#{kind}' supplied. Must be `file` or `directory`.", 'boltserver/schema-error').to_json] end end # We need to special case the scenario where only one file was # included in the request to download. Otherwise, the call to upload_file # will attempt to upload with a directory as a source and potentially a # filename as a destination on the host. In that case the end result will # be the file downloaded to a directory with the same name as the source # filename, rather than directly to the filename set in the destination. upload_source = if files.size == 1 && files[0]['kind'] == 'file' File.join(cache_dir, files[0]['relative_path']) else cache_dir end [@executor.upload_file(target, upload_source, destination), nil] end |
#validate_schema(schema, body) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/bolt_server/transport_app.rb', line 74 def validate_schema(schema, body) schema_error = JSON::Validator.fully_validate(schema, body) if schema_error.any? Bolt::Error.new("There was an error validating the request body.", 'boltserver/schema-error', schema_error) end end |