Class: ForemanOpenscap::BulkUpload

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_openscap/bulk_upload.rb

Instance Method Summary collapse

Constructor Details

#initializeBulkUpload

Returns a new instance of BulkUpload.



6
7
8
# File 'lib/foreman_openscap/bulk_upload.rb', line 6

def initialize
  @result = OpenStruct.new(:errors => [], :results => [])
end

Instance Method Details

#files_from_guideObject



10
11
12
# File 'lib/foreman_openscap/bulk_upload.rb', line 10

def files_from_guide
  `rpm -ql scap-security-guide | grep ds.xml`.split
end

#scap_guide_installed?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/foreman_openscap/bulk_upload.rb', line 14

def scap_guide_installed?
  `rpm -qa | grep scap-security-guide`.present?
end

#upload_from_directory(directory_path) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/foreman_openscap/bulk_upload.rb', line 64

def upload_from_directory(directory_path)
  unless directory_path && Dir.exist?(directory_path)
    @result[:errors].push("No such directory: #{directory_path}. Please check the path you have provided.")
    return @result
  end

  files_array = Dir["#{directory_path}/*-ds.xml"]
  upload_from_files(files_array)
end

#upload_from_files(files_array, from_scap_guide = false) ⇒ Object



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
# File 'lib/foreman_openscap/bulk_upload.rb', line 27

def upload_from_files(files_array, from_scap_guide = false)
  unless files_array.is_a? Array
    @result.errors.push("Expected an array of files to upload, got: #{files_array}.")
    return @result
  end

  files_array.each do |datastream|
    if File.directory?(datastream)
      @result.errors.push("#{datastream} is a directory, expecting file.")
      next
    end

    unless File.file?(datastream)
      @result.errors.push("#{datastream} does not exist, skipping.")
      next
    end

    file = File.open(datastream, 'rb').read
    digest = Digest::SHA2.hexdigest(datastream)
    title = content_name(datastream, from_scap_guide)
    filename = original_filename(datastream)
    scap_content = ScapContent.where(:title => title, :digest => digest).first_or_initialize
    next if scap_content.persisted?
    scap_content.scap_file = file
    scap_content.original_filename = filename
    scap_content.location_ids = Location.all.pluck(:id)
    scap_content.organization_ids = Organization.all.pluck(:id)

    if scap_content.save
      @result.results.push(scap_content)
    else
      @result.errors.push("Failed saving #{datastream}: #{scap_content.errors.full_messages.uniq.join(',')}")
    end
  end
  @result
end

#upload_from_scap_guideObject



18
19
20
21
22
23
24
25
# File 'lib/foreman_openscap/bulk_upload.rb', line 18

def upload_from_scap_guide
  unless scap_guide_installed?
    @result.errors.push("Can't find scap-security-guide RPM, are you sure it is installed on your server?")
    return @result
  end

  upload_from_files(files_from_guide, true)
end