Skip to content

Commit

Permalink
Added the observer-like design pattern for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxNad committed Oct 9, 2018
1 parent a7bc2f4 commit cbd2b3f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
14 changes: 14 additions & 0 deletions helpers/plugin_listener.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# For more information on the behavior of this class, look at helpers/plugin_notifier.rb
class PluginListener
# By design, a PluginListener will return no information when notified.
# => This should be overridden by each plugin that need to add content into a report XML
def notify_report_generated(report_id)
return ""
end

# By design, a PluginListener will do no cleanup when notified.
# => This should be overridden by each plugin that need to cleanup his local database when reports are deleted.
def notify_report_deleted(report_id)
return
end
end
44 changes: 44 additions & 0 deletions helpers/plugin_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'singleton'

# This class is used to reproduce an Observer-like design pattern
# => Every plugin that inherits the PluginListener class will receive the events listed in this class
#
# => Do note that the methods in this class should remain generic. If you need something that only applies to 1YWoswqCayG3vIFHuRmnku8g
# plugin, consider doing it elsewhere.
class PluginNotifier
include Singleton

def initialize
@plugins = []
end

# Add a plugin PluginListener
# => The listener will be notified when a report is generated so he can add XML content into the report
# => He will also be notified when a report is deleted so he can cleanup his local database
def attach_plugin(observed_plugin)
if observed_plugin and observed_plugin.class <= PluginListener
@plugins.push observed_plugin
else
raise 'All observed classes must be non-null and inherit from the PluginListener class.'
end
end

def detach_plugin(observed_plugin)
@plugins.remove observed_plugin
end

def notify_report_generated(report_id)
returned_xml = "<plugins>\n"

@plugins.each { |observer|
returned_xml << observer.notify_report_generated(report_id)
}

returned_xml << "</plugins>\n"
return returned_xml
end

def notify_report_deleted(report_id)
@plugins.each { |observer| observer.notify_report_deleted(report_id) }
end
end
8 changes: 7 additions & 1 deletion routes/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@
findings.destroy
udos.destroy
report.destroy

PluginNotifier.instance.notify_report_deleted(id)
end
serpico_log("Report deleted, Report #{id}")
redirect to('/reports/list')
Expand Down Expand Up @@ -1371,8 +1373,12 @@

end
all_appendices_xml += "</appendices>\n"

# We notify all the plugins to get their output and add it to the report xml
plugins_xml = PluginNotifier.instance.notify_report_generated(id)

# we bring all xml together
report_xml = "<report>#{CGI.unescapeHTML(@report.to_xml)}#{udv}#{findings_xml}#{udo_xml}#{services_xml}#{hosts_xml}#{all_appendices_xml}</report>"
report_xml = "<report>#{CGI.unescapeHTML(@report.to_xml)}#{udv}#{findings_xml}#{udo_xml}#{services_xml}#{hosts_xml}#{all_appendices_xml}#{plugins_xml}</report>"
noko_report_xml = Nokogiri::XML(report_xml)
#no use to go on with report generation if report XML is malformed
if !noko_report_xml.errors.empty?
Expand Down

0 comments on commit cbd2b3f

Please sign in to comment.