class Unified2::ConfigFile

Configuration file

Attributes

data[RW]
md5[RW]
path[RW]
type[RW]

Public Class Methods

new(type, path) click to toggle source

Initialize configuration file

@param [String, Symbol] type Configuration file type @param [String] path Configuration file path

# File lib/unified2/config_file.rb, line 18
def initialize(type, path)
  @type = type
  @path = path
  @data = {}
  @md5 = Digest::MD5.hexdigest(@path)
  import
end

Public Instance Methods

size() click to toggle source

Size

@return [Integer] Configuration size

# File lib/unified2/config_file.rb, line 31
def size
  @data.size
end

Private Instance Methods

import() click to toggle source

Configuration Import

Parse the configuration files and store them in memory as a hash.

# File lib/unified2/config_file.rb, line 43
def import
  file = File.open(@path)
  
  case @type.to_sym
  when :classifications

    count = 0
    file.each_line do |line|
      next if line[/^\#/]
      next unless line[/^config\s/]
      count += 1

      data = line.gsub!(/config classification: /, '')
      short, name, severity = data.to_s.split(',').map(&:strip)

      @data[count.to_s] = {
        :short => short,
        :name => name,
        :severity_id => severity.to_i
      }
    end

  when :generators

    file.each_line do |line|
      next if line[/^\#/]
      generator_id, alert_id, name = line.split(' || ').map(&:strip)
      id = "#{generator_id}.#{alert_id}"

      @data[id] = {
        :generator_id => generator_id.to_i,
        :name => name,
        :signature_id => alert_id.to_i
      }
    end

  when :signatures

    file.each_line do |line|
      next if line[/^\#/]
      id, body, *reference_data = line.split(' || ').map(&:strip)
      
      references = {}
      reference_data.each do |line|
        key, value = line.split(',')
        if references.has_key?(key.downcase.to_sym)
          references[key.downcase.to_sym] << value
        else
          references[key.downcase.to_sym] = [value]
        end
      end
      
      @data[id] = {
        :signature_id => id.to_i,
        :name => body,
        :generator_id => 1
      }
    end

  end
end