Skip to content

Commit

Permalink
Added export/import of SmartState Analysis Profiles
Browse files Browse the repository at this point in the history
Fixes BZ #1344589
  • Loading branch information
Julian Cheal committed May 18, 2018
1 parent e915a3f commit ab4105c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
28 changes: 28 additions & 0 deletions lib/task_helpers/exports/scan_profiles.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions lib/task_helpers/imports/policies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def import(options = {})
begin
policies = YAML.load_file(fname)
import_policies(policies)
rescue => e
$stderr.puts "Error importing #{fname} : #{e.message}"
rescue
warn("Error importing #{options[:source]}")
end
end
end
Expand Down
46 changes: 46 additions & 0 deletions lib/task_helpers/imports/scan_profiles.rb
Original file line number Diff line number Diff line change
@@ -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 => e
$stderr.puts "Error importing #{filename} : #{e.message}"
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
12 changes: 12 additions & 0 deletions lib/tasks/evm_export_import.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ab4105c

Please sign in to comment.