From 207fcaa2af4cc70693c8539f2250fbb69eda753c Mon Sep 17 00:00:00 2001 From: Julian Cheal Date: Wed, 16 May 2018 10:25:40 +0200 Subject: [PATCH] Added export/import of SmartState Analysis Profiles Fixes BZ #1344589 --- lib/task_helpers/exports/scan_profiles.rb | 28 ++++++++++++++ lib/task_helpers/imports/scan_profiles.rb | 46 +++++++++++++++++++++++ lib/tasks/evm_export_import.rake | 12 ++++++ 3 files changed, 86 insertions(+) create mode 100644 lib/task_helpers/exports/scan_profiles.rb create mode 100644 lib/task_helpers/imports/scan_profiles.rb diff --git a/lib/task_helpers/exports/scan_profiles.rb b/lib/task_helpers/exports/scan_profiles.rb new file mode 100644 index 00000000000..bd96668490d --- /dev/null +++ b/lib/task_helpers/exports/scan_profiles.rb @@ -0,0 +1,28 @@ +module TaskHelpers + class Exports + class ScanProfiles + def export(options = {}) + export_dir = options[:directory] + + ScanItemSet.all.each do |p| + next if p.read_only + next if p.members.map { |m| m.slice(:filename) } + + $log.send(level, "Exporting Scan Profile: #{p.name} (#{p.description})") + + profile = ScanItem.get_profile(p.name).first.dup + + %w(id created_on updated_on).each { |k| profile.delete(k) } + profile['definition'].each do |dd| + %w(id created_on updated_on description).each { |k| dd.delete(k) } + end + + scan_profile = profile.to_yaml + + file = Exports.safe_filename(p.name, options[:keep_spaces]) + File.write("#{export_dir}/ScanProfile_#{file}.yaml", scan_profile) + end + end + end + end +end diff --git a/lib/task_helpers/imports/scan_profiles.rb b/lib/task_helpers/imports/scan_profiles.rb new file mode 100644 index 00000000000..bd799c02d3d --- /dev/null +++ b/lib/task_helpers/imports/scan_profiles.rb @@ -0,0 +1,46 @@ +module TaskHelpers + class Imports + class ScanProfiles + def import(options) + return unless options[:source] + + glob = File.file?(options[:source]) ? options[:source] : "#{options[:source]}/ScanProfile_*.yaml" + Dir.glob(glob) do |filename| + begin + import_scan_profile(filename) + rescue + warn("Error importing #{options[:source]}") + end + end + end + + private + + def import_scan_profile(filename) + scan_profiles = YAML.load_file(filename) + items = scan_profiles.delete("definition") + + profile = ScanItemSet.find_by(:name => scan_profiles["name"]) + + if profile.nil? + if scan_profiles["guid"].nil? + scan_profiles["guid"] = SecureRandom.uuid + end + profile = ScanItemSet.new(scan_profiles) + else + profile.attributes = scan_profiles + end + profile.save! + + items.each do |item| + next if item['filename'] + if item['guid'].nil? + item['guid'] = SecureRandom.uuid + end + scan_item = ScanItem.create(item) + profile.add_member(scan_item) + end + end + end + end +end diff --git a/lib/tasks/evm_export_import.rake b/lib/tasks/evm_export_import.rake index c2889aeb013..3c55988b13d 100644 --- a/lib/tasks/evm_export_import.rake +++ b/lib/tasks/evm_export_import.rake @@ -47,6 +47,12 @@ namespace :evm do exit # exit so that parameters to the first rake task are not run as rake tasks end + desc 'Exports all scan profiles to individual YAML files' + task :scan_profiles => [:environment] do + options = TaskHelpers::Exports.parse_options + TaskHelpers::Exports::ScanProfiles.new.export(options) + end + desc 'Exports all tags to individual YAML files' task :tags => :environment do options = TaskHelpers::Exports.parse_options @@ -105,6 +111,12 @@ namespace :evm do exit # exit so that parameters to the first rake task are not run as rake tasks end + desc 'Imports all scan profiles from individual YAML files' + task :scan_profiles => [:environment] do + options = TaskHelpers::Imports.parse_options + TaskHelpers::Imports::ScanProfiles.new.import(options) + end + desc 'Imports all tags to individual YAML files' task :tags => :environment do options = TaskHelpers::Imports.parse_options