Skip to content

Commit

Permalink
Merge pull request #17427 from juliancheal/export_analysis_profiles
Browse files Browse the repository at this point in the history
Added export/import of SmartState Analysis Profiles
  • Loading branch information
gtanzillo authored May 18, 2018
2 parents 6f8f0cd + 207fcaa commit 6480221
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 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
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
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
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 6480221

Please sign in to comment.