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

Use file name numbering on kit upload instead of overwriting files with the same name #1213

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[v#.#.#] ([month] [YYYY])
- [entity]:
- [future tense verb] [feature]
- Kit Import: Use file name sequencing when a template file with the same name exists
- Upgraded gems:
- [gem]
- Bugs fixes:
Expand Down
33 changes: 24 additions & 9 deletions app/jobs/kit_import_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class KitImportJob < ApplicationJob
'html_export' => ['html.erb'],
'word' => ['docm', 'docx']
}
TEMPLATE_TYPES = %w{ methodologies notes plugins projects }

queue_as :dradis_upload

Expand All @@ -17,6 +18,12 @@ def perform(file_or_folder, logger:, user_id: nil)
@logger = logger
@project = nil
@report_templates_dir = Configuration.paths_templates_reports
@templates_dirs = TEMPLATE_TYPES.map do |template_type|
caitmich marked this conversation as resolved.
Show resolved Hide resolved
[
template_type,
Pathname.new(Configuration.send("paths_templates_#{template_type}"))
]
Comment on lines +21 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to 🤓 out, you can do this in a single SQL query, by prebuilding the names from the TEMPLATE_TYPEs.

end.to_h
@working_dir = Dir.mktmpdir
@word_rtp = nil

Expand All @@ -42,7 +49,7 @@ def perform(file_or_folder, logger:, user_id: nil)
end

private
attr_reader :current_user, :logger, :report_templates_dir, :working_dir
attr_reader :current_user, :logger, :report_templates_dir, :templates_dirs, :working_dir
caitmich marked this conversation as resolved.
Show resolved Hide resolved

def assign_project_rtp
logger.info { 'Assigning RTP to project...' }
Expand Down Expand Up @@ -107,8 +114,6 @@ def import_project_package
end

def import_plugin_templates
return unless File.directory?("#{working_dir}/kit/templates/plugins/")

logger.info { 'Copying Plugin Manager templates...' }
import_templates('plugins')
end
Expand Down Expand Up @@ -175,13 +180,23 @@ def import_rules
end

def import_templates(template_type)
template_directory = "#{working_dir}/kit/templates/#{template_type}"
return unless Dir.exist?(template_directory)
kit_template_dir = "#{working_dir}/kit/templates/#{template_type}"
return unless Dir.exist?(kit_template_dir)
template_pwd = templates_dirs[template_type]

FileUtils.cp_r(
"#{template_directory}/.",
Configuration.send("paths_templates_#{template_type}")
)
if template_type == 'plugins'
FileUtils.cp_r("#{kit_template_dir}/.", template_pwd)
else
Dir["#{kit_template_dir}/*"].each do |file|
return unless File.file?(file)
file_name = NamingService.name_file(
original_filename: File.basename(file),
pathname: template_pwd
)

FileUtils.cp(file, "#{template_pwd}/#{file_name}")
end
end
caitmich marked this conversation as resolved.
Show resolved Hide resolved
end

def unzip(file)
Expand Down
27 changes: 27 additions & 0 deletions spec/jobs/kit_import_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,33 @@
expect(ProjectTemplate.find_template('dradis-template-no-methodologies')).to_not be_nil
end

it 'renames project templates if template with same name already exists' do
project_template = ProjectTemplate.new(filename: 'dradis-template-welcome')
project_template.save

described_class.new.perform(@tmp_file, logger: Log.new.write('Testing...'))

expect(ProjectTemplate.find_template('dradis-template-welcome_copy-01')).to_not be_nil
end

it 'renames note templates if template with same name already exists' do
note_template = NoteTemplate.new(filename: 'evidence')
note_template.save

described_class.new.perform(@tmp_file, logger: Log.new.write('Testing...'))

expect(NoteTemplate.find('evidence_copy-01')).to_not be_nil
end

it 'renames methodology templates if template with same name already exists' do
methodology = Methodology.new(filename: 'OWASPv4_Testing_Methodology')
methodology.save

described_class.new.perform(@tmp_file, logger: Log.new.write('Testing...'))

expect(Methodology.find('OWASPv4_Testing_Methodology_copy-01')).to_not be_nil
end

if defined?(Dradis::Pro)
it 'imports Pro-only content too' do
described_class.new.perform(@tmp_file, logger: Log.new.write('Testing...'))
Expand Down
Loading