Class: CertBot::RssHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/cert_bot/rss_handler.rb

Overview

This class handles the fetching, reading and parsing of the BSI rss feed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rss_feed, config_file) ⇒ RssHandler

initialization

Parameters:

  • rss_feed (String)

    the url to the rss feed

  • config_file (String)

    the file path to the configuration file



19
20
21
22
23
24
25
26
27
# File 'lib/cert_bot/rss_handler.rb', line 19

def initialize(rss_feed, config_file)
  @config_path = Pathname.new(config_file).dirname.expand_path
  @rss_feed = rss_feed
  @config_file = config_file

  if (CertBot.parameter_handler != nil && CertBot.parameter_handler.repository.parameters[:debug])
    @debug_log = File.open(@config_path.join("debug.log"), mode="a")
  end
end

Instance Attribute Details

#config_fileString (private)

Returns the string path to the config.json.

Returns:

  • (String)

    the string path to the config.json



69
70
71
# File 'lib/cert_bot/rss_handler.rb', line 69

def config_file
  @config_file
end

#config_pathPathname (private)

Returns the pathname to the configuration file.

Returns:

  • (Pathname)

    the pathname to the configuration file



63
64
65
# File 'lib/cert_bot/rss_handler.rb', line 63

def config_path
  @config_path
end

#debug_logFile (private)

Returns the file object to where debug output is written.

Returns:

  • (File)

    the file object to where debug output is written



65
66
67
# File 'lib/cert_bot/rss_handler.rb', line 65

def debug_log
  @debug_log
end

#rss_feedString (private)

Returns the URI to the rss feed.

Returns:

  • (String)

    the URI to the rss feed



67
68
69
# File 'lib/cert_bot/rss_handler.rb', line 67

def rss_feed
  @rss_feed
end

Instance Method Details

#contains_severity?(categories, severity_string) ⇒ Boolean (private)

method to determine if severity is contained in the severity list

Parameters:

  • categories (Array)

    the list of severities that needs to be sent

  • severity_string (String)

    the severity string from the rss item

Returns:

  • (Boolean)

    true, if the given severity is contained within the categories



95
96
97
# File 'lib/cert_bot/rss_handler.rb', line 95

def contains_severity?(categories, severity_string)
  categories.include?(CertBot::Data::Severity.get_mapping_for(severity_string))
end

#contains_values?(item_wid, item_timestamp, data) ⇒ Boolean (private)

method to check if a advisory already has been mailed

Parameters:

  • item_wid (String)

    the id of the advisory

  • item_timestamp (Time)

    the creation time of the advisory

  • data (Array)

    the list of items that already have been processed in previous script calls

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'lib/cert_bot/rss_handler.rb', line 75

def contains_values?(item_wid, item_timestamp, data)
  data.each { |line|
    return true if (line[0].eql?(item_wid) && Time.parse(line[1]).eql?(item_timestamp))
  }
  false
end

#contraints_fulfilled(item, csv_data, severities, is_updated) ⇒ Bool (private)

method to determine if all constraints for sending a mail are fulfilled

Parameters:

  • item (RSS:Item)

    the current rss item

  • csv_data (Array)

    the list of items that already have been processed in previous script calls

  • severities (Array)

    the list of severities that needs to be sent

  • is_updated (Bool)

    true if the parameter was set, nil otherwise

Returns:

  • (Bool)

    the boolean that shows if the contraints are fulfilled of not



105
106
107
108
109
110
111
112
113
114
# File 'lib/cert_bot/rss_handler.rb', line 105

def contraints_fulfilled(item, csv_data, severities, is_updated)
  item_wid = item.link.split("=")[1]
  return false if (contains_values?(item_wid, item.pubDate.localtime, csv_data))
  return false if (!contains_severity?(severities, item.category.content))
  if (is_updated == nil)
    update_status = CertBot::AdvisoryParser.retrieve_update_status(item_wid)
    return false if (CertBot::Data::UpdateStatus.get_mapping_for(update_status) != :new)
  end
  true
end

#init_csv_accessor(meta_path) ⇒ CertBot::CsvAccessor (private)

method to initialize the csv and read the data from the meta data filepath

Parameters:

  • meta_path (Strig)

    the file path to the meta data

Returns:



85
86
87
88
89
# File 'lib/cert_bot/rss_handler.rb', line 85

def init_csv_accessor(meta_path)
  csv_accessor = CertBot::CsvAccessor.new(meta_path, ";")
  csv_accessor.read_csv if meta_path.file?
  csv_accessor
end

#process_item(item, config_file) ⇒ Object (private)

method to generate the required output for a given item of the rss feed

Parameters:

  • item (RSS:Item)

    the rss item for a feed entry

  • config_file (String)

    the file path to the configuration file



119
120
121
122
123
124
125
126
127
# File 'lib/cert_bot/rss_handler.rb', line 119

def process_item(item, config_file)
  if (CertBot.parameter_handler != nil && CertBot.parameter_handler.repository.parameters[:json] != nil)
    output_dir = Pathname.new(CertBot.parameter_handler.repository.parameters[:json])
    CertBot::JsonGenerator.generate_json(item, output_dir.expand_path)
  else
    CertBot::MailAgent.send_mail(item, config_file)      
  end
  nil
end

#read_feed(severities, is_updated) ⇒ Object

method to parse the feed and generate mails for the required items

Parameters:

  • severities (Array)

    the list of severities that should be parsed from the feed

  • is_updated (Bool)

    true if the parameter was set, nil otherwise



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
# File 'lib/cert_bot/rss_handler.rb', line 32

def read_feed(severities, is_updated)
  write_debug_log("Starting rss parsing at #{Time.now}.")
  meta_path = Pathname.new(@config_path).join("meta_info").expand_path
  csv_accessor = init_csv_accessor(meta_path)

  URI(@rss_feed).open do |rss|
    feed = RSS::Parser.parse(rss)
    feed.items.each { |item|
      item_wid = item.link.split("=")[1]
      write_debug_log("Checking #{item_wid} (#{item.category.content}) at #{Time.now}")        
      if (contraints_fulfilled(item, csv_accessor.data, severities, is_updated))
        write_debug_log("Creating entry for #{item_wid} (#{item.category.content}) at #{Time.now}")
        csv_accessor.append_row([ item_wid, item.pubDate.localtime ])
        process_item(item, config_file)
      end
    }

    days_oldest_entry = ((Time.now - feed.items.last.pubDate.localtime)/ (3600 *24)).ceil
    CertBot::CacheCleaner.delete_old_entries(meta_path, days_oldest_entry)
  end

  write_debug_log("Finishing rss parsing at #{Time.now}.\n")
  if (CertBot.parameter_handler != nil && CertBot.parameter_handler.repository.parameters[:debug])
    @debug_log.close
  end
  nil
end

#write_debug_log(log_text) ⇒ Object (private)

method to write a given debug text into the logfile if the debug parameter is set

Parameters:

  • log_text (String)

    the logging string



131
132
133
134
135
136
# File 'lib/cert_bot/rss_handler.rb', line 131

def write_debug_log(log_text)
  if (CertBot.parameter_handler != nil && CertBot.parameter_handler.repository.parameters[:debug])
    @debug_log.puts(log_text)
  end
  nil
end