Class: Emasser::Artifacts

Inherits:
SubCommandBase show all
Defined in:
lib/emasser/get.rb,
lib/emasser/put.rb,
lib/emasser/post.rb,
lib/emasser/delete.rb

Overview

Remove one or many artifacts in a system

Endpoint:

/api/systems/{systemId}/artifacts - Delete one or more artifacts (files) from a system

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SubCommandBase

banner

Methods included from OutputConverters

#change_to_datetime, #to_output_hash

Methods included from InputConverters

#to_input_hash

Methods included from OptionsParser

#optional_options, #required_options

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


404
405
406
# File 'lib/emasser/get.rb', line 404

def self.exit_on_failure?
  true
end

Instance Method Details

#exportObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/emasser/get.rb', line 447

def export
  optional_options_keys = optional_options(@_initializer).keys
  optional_options = to_input_hash(optional_options_keys, options)

  if options[:printToStdout]
    optional_options.merge!(Emasser::GET_ARTIFACTS_RETURN_TYPE)
    if options[:compress]
      puts 'Output to stdout - compress'.yellow
    else
      puts 'Output to stdout - plain text'.yellow
    end
  else
    # The api method get_system_artifacts_export default return type is
    # file, and it will output the file to the configured EMASSER_DOWNLOAD_DIR
    # or to the default output folder 'eMASSerDownloads'
    export_dir = ENV.fetch('EMASSER_DOWNLOAD_DIR', '')
    export_dir = 'eMASSerDownloads' if export_dir.empty?
    puts "Output to #{export_dir} directory".yellow
  end

  result = EmassClient::ArtifactsExportApi.new.get_system_artifacts_export(
    options[:systemId], options[:filename], optional_options
  )

  unless File.extname(options[:filename]).eql? '.zip'
    # rubocop:disable Style/SoleNestedConditional, Metrics/BlockNesting
    if options[:printToStdout]
      if options[:compress]
        puts result.green
      else
        begin
          puts JSON.pretty_generate(JSON.parse(result)).green
        rescue StandardError
          puts result.red
        end
      end
    end
  end
  # rubocop:enable Style/SoleNestedConditional, Metrics/BlockNesting
rescue EmassClient::ApiError => e
  puts 'Exception when calling ArtifactsApi->get_system_artifacts_export'.red
  puts to_output_hash(e)
end

#forSystemObject



420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/emasser/get.rb', line 420

def forSystem
  optional_options_keys = optional_options(@_initializer).keys
  optional_options = to_input_hash(optional_options_keys, options)

  begin
    # Get one or many artifacts in a system
    result = EmassClient::ArtifactsApi.new.get_system_artifacts(options[:systemId],
                                                                optional_options)
    puts to_output_hash(result).green
  rescue EmassClient::ApiError => e
    puts 'Exception when calling ArtifactsApi->get_system_artifacts'.red
    puts to_output_hash(e)
  end
end

#removeObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/emasser/delete.rb', line 120

def remove
  body_array = []
  options[:files].each do |file|
    obj = {}
    obj[:filename] = file
    body_array << obj
  end

  result = EmassClient::ArtifactsApi.new.delete_artifact(options[:systemId], body_array)
  puts to_output_hash(result).green
rescue EmassClient::ApiError => e
  puts 'Exception when calling ArtifactsApi->delete_artifact'.red
  puts to_output_hash(e)
end

#updateObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/emasser/put.rb', line 580

def update
  require_fields = EmassClient::ArtifactsRequiredFields.new
  require_fields.filename = options[:filename]
  require_fields.type = options[:type]
  require_fields.category = options[:category]
  require_fields.is_template = options[:isTemplate]

  # Optional fields
  optional_fields = EmassClient::ArtifactsOptionalFields.new
  optional_fields.name = options[:name] if options[:name]
  optional_fields.description = options[:description] if options[:description]
  optional_fields.reference_page_number = options[:referencePageNumber] if options[:referencePageNumber]
  optional_fields.controls = options[:controls] if options[:controls]
  optional_fields.assessment_procedures = options[:assessmentProcedures] if options[:assessmentProcedures]
  optional_fields.expiration_date = options[:expirationDate] if options[:expirationDate]
  optional_fields.last_reviewed_date = options[:lastReviewedDate] if options[:lastReviewedDate]
  optional_fields.signed_date = options[:signedDate] if options[:signedDate]

  # Build the request body
  body = {}
  body = body.merge(require_fields)
  body = body.merge(optional_fields)
  body_array = Array.new(1, body)

  begin
    result = EmassClient::ArtifactsApi.new.update_artifact_by_system_id(options[:systemId], body_array)
    puts to_output_hash(result).green
  rescue EmassClient::ApiError => e
    puts 'Exception when calling ArtifactsApi->update_artifact_by_system_id'.red
    puts to_output_hash(e)
  end
end

#uploadObject



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/emasser/post.rb', line 443

def upload
  optional_options_keys = optional_options(@_initializer).keys
  optional_options = to_input_hash(optional_options_keys, options)
  # Remove isBulk as it is an options parameter sent to the API.
  optional_options.delete(:is_bulk)

  # Options contain the default values (type, category, and isTemplate)
  # They are sent to the API in the form_params option
  opts = {}
  opts[:is_bulk] = options[:isBulk]
  opts[:form_params] = optional_options

  # Configure the upload file(s)
  remove_temp_file = false
  begin
    # If we have a single file, could be a zip file
    if options[:files].length == 1
      tempfile = File.open(options[:files][0], 'r')
    # if we have multiple files zip them into a zip file
    elsif options[:files].length > 1
      remove_temp_file = true
      tempfile = Tempfile.create(['artifacts', '.zip'])

      Zip::OutputStream.open(tempfile.path) do |z|
        options[:files].each do |file|
          # Add file name to the archive: Don't use the full path
          z.put_next_entry(File.basename(file))
          # Add the file to the archive
          z.print File.read(file)
        end
      end
    else
      puts 'No file(s) provided!'.yellow
    end
  rescue Errno::ENOENT => e
    warn "File open exception: #{e}".red
    exit 1
  end

  # Call the API
  begin
    result = EmassClient::ArtifactsApi
             .new
             .add_artifacts_by_system_id(options[:systemId], tempfile, opts)
    puts to_output_hash(result).green
  rescue EmassClient::ApiError => e
    puts 'Exception when calling ArtifactsApi->add_artifacts_by_system_id'.red
    puts to_output_hash(e)
  ensure
    # Close the file
    tempfile.close
    # Delete the temp file
    if remove_temp_file
      FileUtils.remove_file(tempfile, true)
    end
  end
end