Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added export/import of SmartState Analysis Profiles #17427

Merged
merged 1 commit into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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