foreman_openscap 4.0.6 → 4.1.0
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.
- checksums.yaml +4 -4
- data/app/controllers/api/v2/compliance/scap_contents_controller.rb +30 -1
- data/app/models/concerns/foreman_openscap/host_extensions.rb +5 -0
- data/app/models/foreman_openscap/arf_report.rb +1 -5
- data/app/models/foreman_openscap/compliance_status.rb +4 -0
- data/app/models/foreman_openscap/policy.rb +8 -2
- data/app/views/api/v2/compliance/scap_contents/bulk_upload.json.rabl +7 -0
- data/config/routes.rb +3 -0
- data/lib/foreman_openscap/bulk_upload.rb +46 -20
- data/lib/foreman_openscap/engine.rb +1 -1
- data/lib/foreman_openscap/version.rb +1 -1
- data/lib/tasks/foreman_openscap_tasks.rake +15 -3
- data/test/factories/compliance_log_factory.rb +0 -6
- data/test/functional/api/v2/compliance/arf_reports_controller_test.rb +4 -4
- data/test/lib/foreman_openscap/bulk_upload_test.rb +48 -0
- data/test/test_plugin_helper.rb +3 -3
- data/test/unit/policy_test.rb +24 -0
- metadata +3 -3
- data/db/migrate/20201202110213_update_puppet_port_param_type.rb +0 -24
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: b5074ed6694cddda4b32459825e324df98f9d4ca1425826adeb86a5983b08be2
         | 
| 4 | 
            +
              data.tar.gz: aea63d7dfe108a4262909e3110bdb92d83bd3d6aa5eb68da4fbc27f938195118
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: b9dc895ea62f9d607a76a9e81fcedc088f0562c8faa621ec2a5b0a510d472bd59814cf8c05050e65d01a68b50f4f871945bedd257d07328650d05c21cfb2ce3f
         | 
| 7 | 
            +
              data.tar.gz: 4ce6250f308c0bd843261d1dc541d65a1901b0e36ab97c46b007d92668201c2387798307695d8eb3173667d69270bb473f39ca093d20f009cbd2cc67d59784eb
         | 
| @@ -5,7 +5,11 @@ module Api::V2 | |
| 5 5 | 
             
                  include ForemanOpenscap::BodyLogExtensions
         | 
| 6 6 | 
             
                  include ForemanOpenscap::Api::V2::ScapApiControllerExtensions
         | 
| 7 7 |  | 
| 8 | 
            -
                   | 
| 8 | 
            +
                  def self.bulk_upload_types
         | 
| 9 | 
            +
                    ['files', 'directory', 'default']
         | 
| 10 | 
            +
                  end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  before_action :find_resource, :except => %w[index create bulk_upload]
         | 
| 9 13 |  | 
| 10 14 | 
             
                  api :GET, '/compliance/scap_contents', N_('List SCAP contents')
         | 
| 11 15 | 
             
                  param_group :search_and_pagination, ::Api::V2::BaseController
         | 
| @@ -61,6 +65,29 @@ module Api::V2 | |
| 61 65 | 
             
                    process_response @scap_content.destroy
         | 
| 62 66 | 
             
                  end
         | 
| 63 67 |  | 
| 68 | 
            +
                  api :POST, '/compliance/scap_contents/bulk_upload', N_('Upload scap contents in bulk')
         | 
| 69 | 
            +
                  param :type, bulk_upload_types, :required => true, :desc => N_('Type of the upload')
         | 
| 70 | 
            +
                  param :files, Array, :desc => N_('File paths to upload when using "files" upload type')
         | 
| 71 | 
            +
                  param :directory, String, :desc => N_('Directory to upload when using "directory" upload type')
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                  def bulk_upload
         | 
| 74 | 
            +
                    case params[:type]
         | 
| 75 | 
            +
                    when 'files'
         | 
| 76 | 
            +
                      @result = ForemanOpenscap::BulkUpload.new.upload_from_files(params[:files])
         | 
| 77 | 
            +
                    when 'directory'
         | 
| 78 | 
            +
                      @result = ForemanOpenscap::BulkUpload.new.upload_from_directory(params[:directory])
         | 
| 79 | 
            +
                    when 'default'
         | 
| 80 | 
            +
                      @result = ForemanOpenscap::BulkUpload.new.upload_from_scap_guide
         | 
| 81 | 
            +
                    else
         | 
| 82 | 
            +
                      return render :json => {
         | 
| 83 | 
            +
                        :errors => [
         | 
| 84 | 
            +
                          _("Please specify import type, received: %{received}, expected one of: %{expected}") %
         | 
| 85 | 
            +
                            { :expected => self.class.bulk_upload_types.join(', '), :received => params[:type] }
         | 
| 86 | 
            +
                        ]
         | 
| 87 | 
            +
                      }, :status => :unprocessable_entity
         | 
| 88 | 
            +
                    end
         | 
| 89 | 
            +
                  end
         | 
| 90 | 
            +
             | 
| 64 91 | 
             
                  private
         | 
| 65 92 |  | 
| 66 93 | 
             
                  def find_resource
         | 
| @@ -70,6 +97,8 @@ module Api::V2 | |
| 70 97 |  | 
| 71 98 | 
             
                  def action_permission
         | 
| 72 99 | 
             
                    case params[:action]
         | 
| 100 | 
            +
                    when 'bulk_upload'
         | 
| 101 | 
            +
                      :create
         | 
| 73 102 | 
             
                    when 'xml'
         | 
| 74 103 | 
             
                      :view
         | 
| 75 104 | 
             
                    else
         | 
| @@ -81,6 +81,11 @@ module ForemanOpenscap | |
| 81 81 | 
             
                  }
         | 
| 82 82 |  | 
| 83 83 | 
             
                  base.send :extend, ClassMethods
         | 
| 84 | 
            +
             | 
| 85 | 
            +
                  base.apipie :class do
         | 
| 86 | 
            +
                    property :policies_enc, String, desc: 'Returns JSON string containing policies for the host'
         | 
| 87 | 
            +
                    property :policies_enc_raw, array_of: Hash, desc: 'Returns a list with key:value objects containing policies for the host'
         | 
| 88 | 
            +
                  end
         | 
| 84 89 | 
             
                end
         | 
| 85 90 |  | 
| 86 91 | 
             
                def inherited_attributes
         | 
| @@ -125,11 +125,9 @@ module ForemanOpenscap | |
| 125 125 | 
             
                          msg = Log.where(:source_id => src.id).order(:id => :desc).first.message
         | 
| 126 126 | 
             
                          update_msg_with_changes(msg, log)
         | 
| 127 127 | 
             
                        else
         | 
| 128 | 
            -
                           | 
| 129 | 
            -
                          if (msg = Message.find_by(:digest => digest))
         | 
| 128 | 
            +
                          if (msg = Message.find_by(:value => log[:title]))
         | 
| 130 129 | 
             
                            msg.attributes = {
         | 
| 131 130 | 
             
                              :value => N_(log[:title]),
         | 
| 132 | 
            -
                              :digest => digest,
         | 
| 133 131 | 
             
                              :severity => log[:severity],
         | 
| 134 132 | 
             
                              :description => newline_to_space(log[:description]),
         | 
| 135 133 | 
             
                              :rationale => newline_to_space(log[:rationale]),
         | 
| @@ -137,7 +135,6 @@ module ForemanOpenscap | |
| 137 135 | 
             
                            }
         | 
| 138 136 | 
             
                          else
         | 
| 139 137 | 
             
                            msg = Message.new(:value => N_(log[:title]),
         | 
| 140 | 
            -
                                              :digest => digest,
         | 
| 141 138 | 
             
                                              :severity => log[:severity],
         | 
| 142 139 | 
             
                                              :description => newline_to_space(log[:description]),
         | 
| 143 140 | 
             
                                              :rationale => newline_to_space(log[:rationale]),
         | 
| @@ -233,7 +230,6 @@ module ForemanOpenscap | |
| 233 230 | 
             
                  msg.value = incoming_data['title']
         | 
| 234 231 |  | 
| 235 232 | 
             
                  return unless msg.changed?
         | 
| 236 | 
            -
                  msg.digest = Digest::SHA1.hexdigest(msg.value) if msg.value_changed?
         | 
| 237 233 | 
             
                  msg.save
         | 
| 238 234 | 
             
                end
         | 
| 239 235 | 
             
              end
         | 
| @@ -174,8 +174,14 @@ module ForemanOpenscap | |
| 174 174 | 
             
                end
         | 
| 175 175 |  | 
| 176 176 | 
             
                def unassign_hosts(hosts)
         | 
| 177 | 
            -
                   | 
| 178 | 
            -
             | 
| 177 | 
            +
                  policy_host_assets = ForemanOpenscap::Asset.joins(:asset_policies).where(
         | 
| 178 | 
            +
                    :assetable_type => 'Host::Base',
         | 
| 179 | 
            +
                    :assetable_id => hosts.map(&:id),
         | 
| 180 | 
            +
                    :foreman_openscap_asset_policies => { :policy_id => id }
         | 
| 181 | 
            +
                  ).pluck(:id)
         | 
| 182 | 
            +
             | 
| 183 | 
            +
                  self.asset_ids = self.asset_ids - policy_host_assets
         | 
| 184 | 
            +
                  ForemanOpenscap::Asset.where(:id => policy_host_assets).destroy_all
         | 
| 179 185 | 
             
                end
         | 
| 180 186 |  | 
| 181 187 | 
             
                def to_enc
         | 
    
        data/config/routes.rb
    CHANGED
    
    
| @@ -1,48 +1,74 @@ | |
| 1 1 | 
             
            require 'digest/sha2'
         | 
| 2 | 
            +
            require 'ostruct'
         | 
| 3 | 
            +
             | 
| 2 4 | 
             
            module ForemanOpenscap
         | 
| 3 5 | 
             
              class BulkUpload
         | 
| 4 | 
            -
                 | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 6 | 
            +
                def initialize
         | 
| 7 | 
            +
                  @result = OpenStruct.new(:errors => [], :results => [])
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def files_from_guide
         | 
| 11 | 
            +
                  `rpm -ql scap-security-guide | grep ds.xml`.split
         | 
| 7 12 | 
             
                end
         | 
| 8 13 |  | 
| 9 | 
            -
                def  | 
| 10 | 
            -
                   | 
| 14 | 
            +
                def scap_guide_installed?
         | 
| 15 | 
            +
                  `rpm -qa | grep scap-security-guide`.present?
         | 
| 16 | 
            +
                end
         | 
| 11 17 |  | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
                     | 
| 18 | 
            +
                def upload_from_scap_guide
         | 
| 19 | 
            +
                  unless scap_guide_installed?
         | 
| 20 | 
            +
                    @result.errors.push("Can't find scap-security-guide RPM, are you sure it is installed on your server?")
         | 
| 21 | 
            +
                    return @result
         | 
| 15 22 | 
             
                  end
         | 
| 16 23 |  | 
| 17 | 
            -
                   | 
| 18 | 
            -
                  upload_from_files(files_array) unless files_array.empty?
         | 
| 24 | 
            +
                  upload_from_files(files_from_guide, true)
         | 
| 19 25 | 
             
                end
         | 
| 20 26 |  | 
| 21 | 
            -
                def upload_from_files(files_array)
         | 
| 27 | 
            +
                def upload_from_files(files_array, from_scap_guide = false)
         | 
| 28 | 
            +
                  unless files_array.is_a? Array
         | 
| 29 | 
            +
                    @result.errors.push("Expected an array of files to upload, got: #{files_array}.")
         | 
| 30 | 
            +
                    return @result
         | 
| 31 | 
            +
                  end
         | 
| 32 | 
            +
             | 
| 22 33 | 
             
                  files_array.each do |datastream|
         | 
| 34 | 
            +
                    if File.directory?(datastream)
         | 
| 35 | 
            +
                      @result.errors.push("#{datastream} is a directory, expecting file.")
         | 
| 36 | 
            +
                      next
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                    unless File.file?(datastream)
         | 
| 40 | 
            +
                      @result.errors.push("#{datastream} does not exist, skipping.")
         | 
| 41 | 
            +
                      next
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
             | 
| 23 44 | 
             
                    file = File.open(datastream, 'rb').read
         | 
| 24 45 | 
             
                    digest = Digest::SHA2.hexdigest(datastream)
         | 
| 25 | 
            -
                    title = content_name(datastream)
         | 
| 46 | 
            +
                    title = content_name(datastream, from_scap_guide)
         | 
| 26 47 | 
             
                    filename = original_filename(datastream)
         | 
| 27 48 | 
             
                    scap_content = ScapContent.where(:title => title, :digest => digest).first_or_initialize
         | 
| 28 49 | 
             
                    next if scap_content.persisted?
         | 
| 29 50 | 
             
                    scap_content.scap_file = file
         | 
| 30 51 | 
             
                    scap_content.original_filename = filename
         | 
| 31 | 
            -
                    scap_content.location_ids = Location.all.map(&:id) | 
| 32 | 
            -
                    scap_content.organization_ids = Organization.all.map(&:id) | 
| 52 | 
            +
                    scap_content.location_ids = Location.all.map(&:id)
         | 
| 53 | 
            +
                    scap_content.organization_ids = Organization.all.map(&:id)
         | 
| 33 54 |  | 
| 34 | 
            -
                    next puts "## SCAP content is invalid: #{scap_content.errors.full_messages.uniq.join(',')} ##" unless scap_content.valid?
         | 
| 35 55 | 
             
                    if scap_content.save
         | 
| 36 | 
            -
                       | 
| 56 | 
            +
                      @result.results.push(scap_content)
         | 
| 37 57 | 
             
                    else
         | 
| 38 | 
            -
                       | 
| 58 | 
            +
                      @result.errors.push("Failed saving #{datastream}: #{scap_content.errors.full_messages.uniq.join(',')}")
         | 
| 39 59 | 
             
                    end
         | 
| 40 60 | 
             
                  end
         | 
| 61 | 
            +
                  @result
         | 
| 41 62 | 
             
                end
         | 
| 42 63 |  | 
| 43 64 | 
             
                def upload_from_directory(directory_path)
         | 
| 65 | 
            +
                  unless directory_path && Dir.exist?(directory_path)
         | 
| 66 | 
            +
                    @result[:errors].push("No such directory: #{directory_path}. Please check the path you have provided.")
         | 
| 67 | 
            +
                    return @result
         | 
| 68 | 
            +
                  end
         | 
| 69 | 
            +
             | 
| 44 70 | 
             
                  files_array = Dir["#{directory_path}/*-ds.xml"]
         | 
| 45 | 
            -
                  upload_from_files(files_array) | 
| 71 | 
            +
                  upload_from_files(files_array)
         | 
| 46 72 | 
             
                end
         | 
| 47 73 |  | 
| 48 74 | 
             
                private
         | 
| @@ -57,9 +83,9 @@ module ForemanOpenscap | |
| 57 83 | 
             
                  file.split('/').last
         | 
| 58 84 | 
             
                end
         | 
| 59 85 |  | 
| 60 | 
            -
                def content_name(datastream)
         | 
| 86 | 
            +
                def content_name(datastream, from_scap_guide)
         | 
| 61 87 | 
             
                  os_name = extract_name_from_file(datastream)
         | 
| 62 | 
            -
                   | 
| 88 | 
            +
                  from_scap_guide ? "Red Hat #{os_name} default content" : "#{os_name} content"
         | 
| 63 89 | 
             
                end
         | 
| 64 90 | 
             
              end
         | 
| 65 91 | 
             
            end
         | 
| @@ -92,7 +92,7 @@ module ForemanOpenscap | |
| 92 92 | 
             
                                                        'api/v2/compliance/scap_contents' => [:update] },
         | 
| 93 93 | 
             
                                 :resource_type => 'ForemanOpenscap::ScapContent'
         | 
| 94 94 | 
             
                      permission :create_scap_contents, { :scap_contents => %i[new create],
         | 
| 95 | 
            -
                                                          'api/v2/compliance/scap_contents' => [ | 
| 95 | 
            +
                                                          'api/v2/compliance/scap_contents' => %i[create bulk_upload] },
         | 
| 96 96 | 
             
                                 :resource_type => 'ForemanOpenscap::ScapContent'
         | 
| 97 97 | 
             
                      permission :destroy_scap_contents, { :scap_contents => [:destroy],
         | 
| 98 98 | 
             
                                                           'api/v2/compliance/scap_contents' => [:destroy] },
         | 
| @@ -6,23 +6,26 @@ namespace :foreman_openscap do | |
| 6 6 | 
             
              namespace :bulk_upload do
         | 
| 7 7 | 
             
                desc 'Bulk upload SCAP content from directory'
         | 
| 8 8 | 
             
                task :directory, [:directory] => [:environment] do |task, args|
         | 
| 9 | 
            +
                  deprecate_upload_from_rake
         | 
| 9 10 | 
             
                  abort("# No such directory, please check the path you have provided. #") unless args[:directory].blank? || Dir.exist?(args[:directory])
         | 
| 10 11 | 
             
                  User.current = User.anonymous_admin
         | 
| 11 | 
            -
                  ForemanOpenscap::BulkUpload.new.upload_from_directory(args[:directory])
         | 
| 12 | 
            +
                  print_upload_result ForemanOpenscap::BulkUpload.new.upload_from_directory(args[:directory])
         | 
| 12 13 | 
             
                end
         | 
| 13 14 |  | 
| 14 15 | 
             
                task :files, [:files] => [:environment] do |task, args|
         | 
| 16 | 
            +
                  deprecate_upload_from_rake
         | 
| 15 17 | 
             
                  files_array = args[:files].split(' ')
         | 
| 16 18 | 
             
                  files_array.each do |file|
         | 
| 17 19 | 
             
                    abort("# #{file} is a directory, expecting file. Try using 'rake foreman_openscap:bulk_upload:directory' with this directory. #") if File.directory?(file)
         | 
| 18 20 | 
             
                  end
         | 
| 19 21 | 
             
                  User.current = User.anonymous_admin
         | 
| 20 | 
            -
                  ForemanOpenscap::BulkUpload.new.upload_from_files(files_array)
         | 
| 22 | 
            +
                  print_upload_result ForemanOpenscap::BulkUpload.new.upload_from_files(files_array)
         | 
| 21 23 | 
             
                end
         | 
| 22 24 |  | 
| 23 25 | 
             
                task :default => [:environment] do
         | 
| 26 | 
            +
                  deprecate_upload_from_rake
         | 
| 24 27 | 
             
                  User.current = User.anonymous_admin
         | 
| 25 | 
            -
                  ForemanOpenscap::BulkUpload.new | 
| 28 | 
            +
                  print_upload_result ForemanOpenscap::BulkUpload.new.upload_from_scap_guide
         | 
| 26 29 | 
             
                end
         | 
| 27 30 | 
             
              end
         | 
| 28 31 |  | 
| @@ -67,6 +70,15 @@ namespace :foreman_openscap do | |
| 67 70 | 
             
              end
         | 
| 68 71 | 
             
            end
         | 
| 69 72 |  | 
| 73 | 
            +
            def deprecate_upload_from_rake
         | 
| 74 | 
            +
              puts 'DEPRECATION WARNING: Uploading scap contents using rake task is deprecated and will be removed in a future version. Please use API or CLI.'
         | 
| 75 | 
            +
            end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
            def print_upload_result(result)
         | 
| 78 | 
            +
              puts result.errors.join(' ') if result.errors.present?
         | 
| 79 | 
            +
              puts result.results.map { |sc| "Saved #{sc.original_filename} as #{sc.title}" }.join("\n") if result.results.present?
         | 
| 80 | 
            +
            end
         | 
| 81 | 
            +
             | 
| 70 82 | 
             
            # Tests
         | 
| 71 83 | 
             
            namespace :test do
         | 
| 72 84 | 
             
              desc "Test ForemanOpenscap"
         | 
| @@ -9,15 +9,9 @@ FactoryBot.define do | |
| 9 9 |  | 
| 10 10 | 
             
              factory :compliance_message, :class => :message do
         | 
| 11 11 | 
             
                sequence(:value) { |n| "message#{n}" }
         | 
| 12 | 
            -
                after(:build) do |msg|
         | 
| 13 | 
            -
                  msg.digest = Digest::SHA1.hexdigest(msg.value)
         | 
| 14 | 
            -
                end
         | 
| 15 12 | 
             
              end
         | 
| 16 13 |  | 
| 17 14 | 
             
              factory :compliance_source, :class => :source do
         | 
| 18 15 | 
             
                sequence(:value) { |n| "source#{n}" }
         | 
| 19 | 
            -
                after(:build) do |source|
         | 
| 20 | 
            -
                  source.digest = Digest::SHA1.hexdigest(source.value)
         | 
| 21 | 
            -
                end
         | 
| 22 16 | 
             
              end
         | 
| 23 17 | 
             
            end
         | 
| @@ -139,7 +139,7 @@ class Api::V2::Compliance::ArfReportsControllerTest < ActionController::TestCase | |
| 139 139 | 
             
                                                 :date => dates[1].to_i,
         | 
| 140 140 | 
             
                                                 :openscap_proxy_name => @proxy.name),
         | 
| 141 141 | 
             
                     :session => set_session_user
         | 
| 142 | 
            -
                assert_equal Message.where(: | 
| 142 | 
            +
                assert_equal Message.where(:value => ForemanOpenscap::ArfReport.unscoped.last.logs.first.message.value).count, 1
         | 
| 143 143 | 
             
              end
         | 
| 144 144 |  | 
| 145 145 | 
             
              test "should recognize changes in messages" do
         | 
| @@ -187,12 +187,12 @@ class Api::V2::Compliance::ArfReportsControllerTest < ActionController::TestCase | |
| 187 187 |  | 
| 188 188 | 
             
                reports = ForemanOpenscap::ArfReport.unscoped.all
         | 
| 189 189 | 
             
                assert_equal reports.count, 2
         | 
| 190 | 
            -
             | 
| 191 | 
            -
                new_msgs = Message.where(:value =>  | 
| 190 | 
            +
                msg_value = "Disable Firefox Configuration File ROT-13 Encoding Changed For Test"
         | 
| 191 | 
            +
                new_msgs = Message.where(:value => msg_value)
         | 
| 192 192 | 
             
                old_msgs = Message.where(:value => "Disable Firefox Configuration File ROT-13 Encoding")
         | 
| 193 193 | 
             
                assert_equal new_msgs.count, 1
         | 
| 194 194 | 
             
                assert_equal old_msgs.count, 0
         | 
| 195 | 
            -
                assert_equal new_msgs.first. | 
| 195 | 
            +
                assert_equal new_msgs.first.value, msg_value
         | 
| 196 196 | 
             
              end
         | 
| 197 197 |  | 
| 198 198 | 
             
              test "should find reports by policy name" do
         | 
| @@ -3,6 +3,7 @@ require 'test_plugin_helper' | |
| 3 3 | 
             
            class BulkUploadTest < ActiveSupport::TestCase
         | 
| 4 4 | 
             
              setup do
         | 
| 5 5 | 
             
                require 'foreman_openscap/bulk_upload'
         | 
| 6 | 
            +
                ForemanOpenscap::ScapContent.all.map(&:destroy)
         | 
| 6 7 | 
             
              end
         | 
| 7 8 |  | 
| 8 9 | 
             
              test 'upload_from_files should create only one scap content' do
         | 
| @@ -13,4 +14,51 @@ class BulkUploadTest < ActiveSupport::TestCase | |
| 13 14 | 
             
                  end
         | 
| 14 15 | 
             
                end
         | 
| 15 16 | 
             
              end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              test 'upload_from_files should not crash when scap files are not array' do
         | 
| 19 | 
            +
                scap_files = '/tmp/foo'
         | 
| 20 | 
            +
                res = ForemanOpenscap::BulkUpload.new.upload_from_files(scap_files)
         | 
| 21 | 
            +
                assert_equal "Expected an array of files to upload, got: #{scap_files}.", res.errors.first
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
              test 'upload_from_files should skip directories' do
         | 
| 25 | 
            +
                dir = "#{ForemanOpenscap::Engine.root}/test/files/scap_contents"
         | 
| 26 | 
            +
                res = ForemanOpenscap::BulkUpload.new.upload_from_files([dir])
         | 
| 27 | 
            +
                assert_equal "#{dir} is a directory, expecting file.", res.errors.first
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              test 'upload_from_files should skip files that does not exist' do
         | 
| 31 | 
            +
                file = "#{ForemanOpenscap::Engine.root}/test/files/scap_contents/foo-ds.xml"
         | 
| 32 | 
            +
                res = ForemanOpenscap::BulkUpload.new.upload_from_files([file])
         | 
| 33 | 
            +
                assert_equal "#{file} does not exist, skipping.", res.errors.first
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              test 'upload_from_directory should check if directory exists' do
         | 
| 37 | 
            +
                dir = "#{ForemanOpenscap::Engine.root}/test/files/scap_contents/foo"
         | 
| 38 | 
            +
                res = ForemanOpenscap::BulkUpload.new.upload_from_directory(dir)
         | 
| 39 | 
            +
                assert_equal "No such directory: #{dir}. Please check the path you have provided.", res.errors.first
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              test 'upload_from_directory should upload from directory' do
         | 
| 43 | 
            +
                dir = "#{ForemanOpenscap::Engine.root}/test/files/scap_contents"
         | 
| 44 | 
            +
                assert_difference('ForemanOpenscap::ScapContent.count', 1) do
         | 
| 45 | 
            +
                  ForemanOpenscap::BulkUpload.new.upload_from_directory(dir)
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              test 'should handle case when scap security guide is not installed' do
         | 
| 50 | 
            +
                upload = ForemanOpenscap::BulkUpload.new
         | 
| 51 | 
            +
                upload.stubs(:scap_guide_installed?).returns(false)
         | 
| 52 | 
            +
                res = upload.upload_from_scap_guide
         | 
| 53 | 
            +
                assert_equal "Can't find scap-security-guide RPM, are you sure it is installed on your server?", res.errors.first
         | 
| 54 | 
            +
              end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
              test 'should upload files from guide' do
         | 
| 57 | 
            +
                upload = ForemanOpenscap::BulkUpload.new
         | 
| 58 | 
            +
                upload.stubs(:scap_guide_installed?).returns(true)
         | 
| 59 | 
            +
                upload.stubs(:files_from_guide).returns(["#{ForemanOpenscap::Engine.root}/test/files/scap_contents/ssg-fedora-ds.xml"])
         | 
| 60 | 
            +
                assert_difference('ForemanOpenscap::ScapContent.count', 1) do
         | 
| 61 | 
            +
                  upload.upload_from_scap_guide
         | 
| 62 | 
            +
                end
         | 
| 63 | 
            +
              end
         | 
| 16 64 | 
             
            end
         | 
    
        data/test/test_plugin_helper.rb
    CHANGED
    
    | @@ -11,9 +11,9 @@ module ScapClientPuppetclass | |
| 11 11 | 
             
                Puppetclass.find_by(:name => puppet_config.puppetclass_name)&.destroy
         | 
| 12 12 |  | 
| 13 13 | 
             
                puppet_class = FactoryBot.create(:puppetclass, :name => puppet_config.puppetclass_name)
         | 
| 14 | 
            -
                server_param = FactoryBot.create(:puppetclass_lookup_key, :key => puppet_config.server_param, : | 
| 15 | 
            -
                port_param = FactoryBot.create(:puppetclass_lookup_key, :key => puppet_config.port_param, : | 
| 16 | 
            -
                policies_param = FactoryBot.create(:puppetclass_lookup_key, :key => puppet_config.policies_param, : | 
| 14 | 
            +
                server_param = FactoryBot.create(:puppetclass_lookup_key, :key => puppet_config.server_param, :default_value => nil)
         | 
| 15 | 
            +
                port_param = FactoryBot.create(:puppetclass_lookup_key, :key => puppet_config.port_param, :default_value => nil)
         | 
| 16 | 
            +
                policies_param = FactoryBot.create(:puppetclass_lookup_key, :key => puppet_config.policies_param, :default_value => nil)
         | 
| 17 17 |  | 
| 18 18 | 
             
                env = FactoryBot.create :environment
         | 
| 19 19 |  | 
    
        data/test/unit/policy_test.rb
    CHANGED
    
    | @@ -46,6 +46,30 @@ class PolicyTest < ActiveSupport::TestCase | |
| 46 46 | 
             
                assert_equal 1, policy.hosts.count
         | 
| 47 47 | 
             
              end
         | 
| 48 48 |  | 
| 49 | 
            +
              test "should delete assets when unassigning hosts" do
         | 
| 50 | 
            +
                host1 = FactoryBot.create(:compliance_host)
         | 
| 51 | 
            +
                host2 = FactoryBot.create(:compliance_host)
         | 
| 52 | 
            +
                asset1 = FactoryBot.create(:asset, :assetable_id => host1.id, :assetable_type => 'Host::Base')
         | 
| 53 | 
            +
                asset2 = FactoryBot.create(:asset, :assetable_id => host2.id, :assetable_type => 'Host::Base')
         | 
| 54 | 
            +
                policy = FactoryBot.create(:policy, :assets => [asset1, asset2], :scap_content => @scap_content, :scap_content_profile => @scap_profile)
         | 
| 55 | 
            +
                policy.unassign_hosts([host1, host2])
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                assert_nil ForemanOpenscap::Asset.find_by(:id => asset1.id)
         | 
| 58 | 
            +
                assert_nil ForemanOpenscap::Asset.find_by(:id => asset2.id)
         | 
| 59 | 
            +
              end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
              test "should delete assets only for selected policy when unassigning host" do
         | 
| 62 | 
            +
                host1 = FactoryBot.create(:compliance_host)
         | 
| 63 | 
            +
                asset1 = FactoryBot.create(:asset, :assetable_id => host1.id, :assetable_type => 'Host::Base')
         | 
| 64 | 
            +
                asset2 = FactoryBot.create(:asset, :assetable_id => host1.id, :assetable_type => 'Host::Base')
         | 
| 65 | 
            +
                policy1 = FactoryBot.create(:policy, :assets => [asset1], :scap_content => @scap_content, :scap_content_profile => @scap_profile)
         | 
| 66 | 
            +
                policy2 = FactoryBot.create(:policy, :assets => [asset2], :scap_content => @scap_content, :scap_content_profile => @scap_profile)
         | 
| 67 | 
            +
                policy1.unassign_hosts([host1])
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                assert_nil ForemanOpenscap::Asset.find_by(:id => asset1.id)
         | 
| 70 | 
            +
                assert_not_nil ForemanOpenscap::Asset.find_by(:id => asset2.id)
         | 
| 71 | 
            +
              end
         | 
| 72 | 
            +
             | 
| 49 73 | 
             
              test "should remove associated hostgroup" do
         | 
| 50 74 | 
             
                hg = FactoryBot.create(:hostgroup)
         | 
| 51 75 | 
             
                asset = FactoryBot.create(:asset, :assetable_id => hg.id, :assetable_type => 'Hostgroup')
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: foreman_openscap
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 4.0 | 
| 4 | 
            +
              version: 4.1.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - slukasik@redhat.com
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date:  | 
| 11 | 
            +
            date: 2020-11-05 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: rake
         | 
| @@ -130,6 +130,7 @@ files: | |
| 130 130 | 
             
            - app/views/api/v2/compliance/scap_content_profiles/index.json.rabl
         | 
| 131 131 | 
             
            - app/views/api/v2/compliance/scap_content_profiles/main.json.rabl
         | 
| 132 132 | 
             
            - app/views/api/v2/compliance/scap_contents/base.json.rabl
         | 
| 133 | 
            +
            - app/views/api/v2/compliance/scap_contents/bulk_upload.json.rabl
         | 
| 133 134 | 
             
            - app/views/api/v2/compliance/scap_contents/create.json.rabl
         | 
| 134 135 | 
             
            - app/views/api/v2/compliance/scap_contents/index.json.rabl
         | 
| 135 136 | 
             
            - app/views/api/v2/compliance/scap_contents/main.json.rabl
         | 
| @@ -245,7 +246,6 @@ files: | |
| 245 246 | 
             
            - db/migrate/20190103093409_add_deployment_option_to_policy.foreman_openscap.rb
         | 
| 246 247 | 
             
            - db/migrate/20200117135424_migrate_port_overrides_to_int.rb
         | 
| 247 248 | 
             
            - db/migrate/20200803065041_migrate_port_overrides_for_ansible.rb
         | 
| 248 | 
            -
            - db/migrate/20201202110213_update_puppet_port_param_type.rb
         | 
| 249 249 | 
             
            - db/seeds.d/75-job_templates.rb
         | 
| 250 250 | 
             
            - db/seeds.d/openscap_feature.rb
         | 
| 251 251 | 
             
            - db/seeds.d/openscap_policy_notification.rb
         | 
| @@ -1,24 +0,0 @@ | |
| 1 | 
            -
            class UpdatePuppetPortParamType < ActiveRecord::Migration[6.0]
         | 
| 2 | 
            -
              def up
         | 
| 3 | 
            -
                update_port_type :to_i
         | 
| 4 | 
            -
              end
         | 
| 5 | 
            -
             | 
| 6 | 
            -
              def down
         | 
| 7 | 
            -
                update_port_type :to_s
         | 
| 8 | 
            -
              end
         | 
| 9 | 
            -
             | 
| 10 | 
            -
              private
         | 
| 11 | 
            -
             | 
| 12 | 
            -
              def update_port_type(method)
         | 
| 13 | 
            -
                puppet_class = Puppetclass.find_by :name => 'foreman_scap_client'
         | 
| 14 | 
            -
                return unless puppet_class
         | 
| 15 | 
            -
                port_key = puppet_class.class_params.find_by :key => 'port'
         | 
| 16 | 
            -
                return unless port_key
         | 
| 17 | 
            -
             | 
| 18 | 
            -
                if method == :to_i
         | 
| 19 | 
            -
                  port_key.update_columns(:key_type => "integer", :default_value => port_key.default_value.to_i)
         | 
| 20 | 
            -
                else
         | 
| 21 | 
            -
                  port_key.update_columns(:key_type => "string", :default_value => port_key.default_value.to_s)
         | 
| 22 | 
            -
                end
         | 
| 23 | 
            -
              end
         | 
| 24 | 
            -
            end
         |