diff --git a/lib/task_helpers/exports/scan_profiles.rb b/lib/task_helpers/exports/scan_profiles.rb new file mode 100644 index 000000000000..b55da9636ec9 --- /dev/null +++ b/lib/task_helpers/exports/scan_profiles.rb @@ -0,0 +1,40 @@ +module TaskHelpers + class Exports + class ScanProfiles + def export(options) + ScanItemSet.all.each do |p| + # Skip read only entries + if p.read_only + next + end + # Also skip any entries that are using file defaults... + uses_files = false + p.members.each do |m| + if m.filename + uses_files = true + end + end + if uses_files + next + end + + puts("Exporting Scan Profile: #{p.name} (#{p.description})") + + pname = TaskHelpers::Exports.safe_filename(p.name, options[:keep_spaces]) + fname = "ScanProfile_#{pname}.yaml" + + # Clean-up data + 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 + + s = profile.to_yaml + s.gsub!(/\n\s*guid:\s+\S+\n/,"\n") + File.write("#{options[:directory]}/#{fname}", s) + 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 000000000000..5e9a0fa302ea --- /dev/null +++ b/lib/task_helpers/imports/scan_profiles.rb @@ -0,0 +1,49 @@ +module TaskHelpers + class Imports + class ScanProfiles + def import(options) + Dir.glob("#{options[:source]}/ScanProfile_*yaml") do |filename| + puts("Importing Scan Profile: #{File.basename(filename, '.yaml').gsub(/^ScanProfile_/, '')} ....") + + hash = YAML.load_file(filename) + items = hash["definition"] + hash.delete("definition") + profile = ScanItemSet.find_by(:name => hash["name"]); + if profile.nil? + if hash["guid"].nil? + hash["guid"] = SecureRandom.uuid + end + profile = ScanItemSet.new(hash) + else + profile.attributes = hash + end + profile.save! + + # Delete existing members + profile.members.each do |one| + if one.filename + # OK, this was defined through yaml file... just skip it + next + #profile.delete(one) + else + one.destroy + end + end + items.each do |i| + if i['filename'] + # OK, this rules refers to a file, just use it... + next + else + if i['guid'].nil? + i['guid'] = SecureRandom.uuid + end + #puts i.inspect + si = ScanItem.create(i) + end + profile.add_member(si) + end + end + end + end + end +end diff --git a/lib/tasks/evm_export_import.rake b/lib/tasks/evm_export_import.rake index c2889aeb0139..d2a609a15bf5 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