Sha256: 3d675150944a84c5619bb653ba18e29940b72efba8f5595aa6d76a567af65ba6

Contents?: true

Size: 1.54 KB

Versions: 37

Compression:

Stored size: 1.54 KB

Contents

module ForemanOpenscap
  module DataStreamContent
    require 'digest/sha2'

    extend ActiveSupport::Concern

    included do
      validates :digest, :presence => true
      validates :scap_file, :presence => true

      validates_with ForemanOpenscap::DataStreamValidator

      after_save :create_profiles, :if => lambda { |ds_content| ds_content.scap_file_previously_changed? }

      before_validation :redigest, :if => lambda { |ds_content| ds_content.persisted? && ds_content.scap_file_changed? }
      before_destroy ActiveRecord::Base::EnsureNotUsedBy.new(:policies)
    end

    def proxy_url
      @proxy_url ||= SmartProxy.with_features('Openscap').find do |proxy|
        available = ProxyAPI::AvailableProxy.new(:url => proxy.url)
        available.available?
      end.try(:url)
      @proxy_url
    end

    def digest
      self[:digest] ||= Digest::SHA256.hexdigest(scap_file.to_s)
    end

    def create_profiles
      fetch_profiles.each do |key, title|
        create_or_update_profile key, title
      end
    end

    def create_or_update_profile(profile_id, title)
      profile = ScapContentProfile.find_by(:profile_id => profile_id, "#{self.class.to_s.demodulize.underscore}_id".to_sym => id)
      return ScapContentProfile.create(:profile_id => profile_id, :title => title, "#{self.class.to_s.demodulize.underscore}_id".to_sym => id) unless profile
      profile.update(:title => title) unless profile.title == title
      profile
    end

    private

    def redigest
      self[:digest] = Digest::SHA256.hexdigest(scap_file.to_s)
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
foreman_openscap-4.1.3 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.0.6 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.2.0 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.0.5 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.1.2 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.1.1 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.1.0 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.0.4 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.0.3 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.0.2 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.0.1 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-3.0.1 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-4.0.0 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-3.0.0 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-2.0.2 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-1.0.10 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-2.0.1 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-1.0.9 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-2.0.0 app/models/concerns/foreman_openscap/data_stream_content.rb
foreman_openscap-1.0.8 app/models/concerns/foreman_openscap/data_stream_content.rb