Class: ForemanOpenscap::DataStreamValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/foreman_openscap/data_stream_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(data_stream_content) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/validators/foreman_openscap/data_stream_validator.rb', line 3

def validate(data_stream_content)
  return unless data_stream_content.scap_file_changed?

  content_type = data_type(data_stream_content)

  unless SmartProxy.with_features('Openscap').any?
    data_stream_content.errors.add(:base, _('No proxy with OpenSCAP feature was found.'))
    return false
  end

  if data_stream_content.proxy_url.nil?
    data_stream_content.errors.add(:base, _('No proxy with OpenSCAP feature is running.'))
    return false
  end

  begin
    api = ProxyAPI::Openscap.new(:url => data_stream_content.proxy_url)
    errors = api.validate_scap_file(data_stream_content.scap_file, content_type)
    if errors && errors['errors'].any?
      errors['errors'].each { |error| data_stream_content.errors.add(:scap_file, _(error)) }
      return false
    end
  rescue *ProxyAPI::Openscap::HTTP_ERRORS => e
    data_stream_content.errors.add(:base, _('No available proxy to validate. Returned with error: %s') % e)
    return false
  end

  is_scap_content = content_type == 'scap_content'

  if is_scap_content && !(data_stream_content.scap_content_profiles.map(&:profile_id) - data_stream_content.fetch_profiles.keys).empty?
    data_stream_content.errors.add(:scap_file, _('Changed file does not include existing SCAP content profiles'))
    return false
  end
end