Skip to content

Commit

Permalink
Refactor thor tasks
Browse files Browse the repository at this point in the history
Break them into different files for easier management.
  • Loading branch information
brianp committed Nov 7, 2019
1 parent d073385 commit 639e2ed
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 158 deletions.
1 change: 1 addition & 0 deletions Thorfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require 'config/environment'
# add the dradis core tasks, and define the namespaces for import, export, and
# upload tasks
require File.expand_path('../lib/tasks/thorfile', __FILE__)
Dir['./lib/tasks/thor/**/*.rb'].sort.each { |f| require f }

# a gemified plugin can also add Thor tasks
puts 'Loaded add-ons:'
Expand Down
14 changes: 14 additions & 0 deletions lib/tasks/thor/logs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class DradisTasks < Thor
class Logs < Thor
namespace 'dradis:logs'

desc 'clean DAYS', 'delete all logs older than DAYS days (default 7)'
def clean(days=7)
puts 'Clearing old Logs...'
logs = Log.where('created_at < (?)', days.to_i.days.ago)
count = logs.count
logs.destroy_all
puts "Deleted #{count} Log#{"s" if count != 1}"
end
end
end
51 changes: 51 additions & 0 deletions lib/tasks/thor/reset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class DradisTasks < Thor
class Reset < Thor
namespace 'dradis:reset'

desc 'attachments', 'removes all attachments'
def attachments
print '** Deleting all attachments... '
FileUtils.rm_rf(Dir.glob( Attachment::AttachmentPwd.join('*')) )
puts(Dir.glob( Attachment::AttachmentPwd.join('*')).empty? ? '[ DONE ]' : '[ FAILED ]')
end

desc 'database', 'removes all data from a dradis repository, except configurations'
def database
return if defined?(Dradis::Pro)

require 'config/environment'
print '** Cleaning database... '

Rails.application.eager_load!
(ApplicationRecord.descendants - [Configuration]).each do |model|
ActiveRecord::Base.connection.execute("DELETE FROM #{model.table_name}")
ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence WHERE name='#{model.table_name}'")
end

puts '[ DONE ]'
end

desc 'logs', 'removes all log files'
def logs
print '** Deleting all log files... '
FileUtils.rm_rf(Dir.glob('log/*.log'))
puts(Dir.glob('log/*.log').empty? ? '[ DONE ]' : '[ FAILED ]')
end

desc 'password', 'Set a new shared password to access the web interface'
def password()
require 'config/environment'

say 'Changing password for Dradis server.'
password = ask 'Enter new Dradis password:'
confirmation = ask 'Retype new Dradis password:'

if !password.blank? && password == confirmation
Configuration.find_or_create_by(name: 'admin:password').update_attribute(:value, ::BCrypt::Password.create(password))
say('Password Changed.', Thor::Shell::Color::GREEN)
else
say('Passwords do not match. Password Unchanged.', Thor::Shell::Color::RED)
end
end
end
end
94 changes: 94 additions & 0 deletions lib/tasks/thor/setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class DradisTasks < Thor
class Setup < Thor
include Thor::Actions
include ::Rails.application.config.dradis.thor_helper_module

namespace 'dradis:setup'

def self.source_root
File.join(File.dirname(__FILE__), 'templates')
end

desc 'configure', 'Creates the Dradis configuration files from their templates (see config/*.yml.template)'
def configure
# init the config files
init_all = false
Dir['config/*.template'].each do |template|
config = File.join( 'config', File.basename(template, '.template') )
if !(File.exists?( config ))
if (init_all)
puts "Initilizing #{config}..."
FileUtils.cp(template, config)
else
puts "The config file [#{template}] was found not to be ready to use."
puts 'Do you want to initialize it? [y]es | [N]o | initialize [a]ll'
response = STDIN.gets.chomp.downcase
response = 'Y' if ( response.empty? || !['y', 'n', 'a'].include?(response) )

if response == 'n'
next
else
puts "Initilizing #{config}..."
FileUtils.cp(template, config)
if (response == 'a')
init_all = true
end
end
end
end
end
end

desc 'migrate', 'ensures the database schema is up-to-date'
def migrate
require 'config/environment'

print '** Checking database migrations... '
ActiveRecord::Migrator.migrate('db/migrate/', nil)
puts '[ DONE ]'
end

desc 'seed', 'adds initial values to the database (i.e., categories and configurations)'
def seed
require 'config/environment'

print '** Seeding database... '
require 'db/seeds'
puts '[ DONE ]'
end

desc 'welcome', 'adds initial content to the repo for demonstration purposes'
def welcome
# --------------------------------------------------------- Note template
if NoteTemplate.pwd.exist?
say 'Note templates folder already exists. Skipping.'
else
template 'note.txt', NoteTemplate.pwd.join('basic_fields.txt')
end

# ----------------------------------------------------------- Methodology
if Methodology.pwd.exist?
say 'Methodology templates folder already exists. Skipping.'
else
template 'methodology.xml', Methodology.pwd.join('owasp2017.xml')
end

# ---------------------------------------------------------- Project data
detect_and_set_project_scope

task_options.merge!({
plugin: Dradis::Plugins::Projects::Upload::Template,
default_user_id: 1
})

importer = Dradis::Plugins::Projects::Upload::Template::Importer.new(task_options)
importer.import(file: File.expand_path('../templates/project.xml', __FILE__))

# dradis:reset:database truncates the tables and resets the :id column so
# we know the right node ID we're going to get based on the project.xml
# structure.
Dir.mkdir(Attachment.pwd.join('5'))
template 'command-01.png', Attachment.pwd.join('5/command-01.png')
end
end
end
158 changes: 0 additions & 158 deletions lib/tasks/thorfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,165 +56,7 @@ def version
puts Core::Pro::VERSION::string
end


class Import < Thor; end
class Export < Thor; end
class Upload < Thor; end


class Setup < Thor
include Thor::Actions
include ::Rails.application.config.dradis.thor_helper_module

namespace 'dradis:setup'

def self.source_root
File.join(File.dirname(__FILE__), 'templates')
end

desc 'configure', 'Creates the Dradis configuration files from their templates (see config/*.yml.template)'
def configure
# init the config files
init_all = false
Dir['config/*.template'].each do |template|
config = File.join( 'config', File.basename(template, '.template') )
if !(File.exists?( config ))
if (init_all)
puts "Initilizing #{config}..."
FileUtils.cp(template, config)
else
puts "The config file [#{template}] was found not to be ready to use."
puts 'Do you want to initialize it? [y]es | [N]o | initialize [a]ll'
response = STDIN.gets.chomp.downcase
response = 'Y' if ( response.empty? || !['y', 'n', 'a'].include?(response) )

if response == 'n'
next
else
puts "Initilizing #{config}..."
FileUtils.cp(template, config)
if (response == 'a')
init_all = true
end
end
end
end
end
end

desc 'migrate', 'ensures the database schema is up-to-date'
def migrate
require 'config/environment'

print '** Checking database migrations... '
ActiveRecord::Migrator.migrate('db/migrate/', nil)
puts '[ DONE ]'
end

desc 'seed', 'adds initial values to the database (i.e., categories and configurations)'
def seed
require 'config/environment'

print '** Seeding database... '
require 'db/seeds'
puts '[ DONE ]'
end

desc 'welcome', 'adds initial content to the repo for demonstration purposes'
def welcome
# --------------------------------------------------------- Note template
if NoteTemplate.pwd.exist?
say 'Note templates folder already exists. Skipping.'
else
template 'note.txt', NoteTemplate.pwd.join('basic_fields.txt')
end

# ----------------------------------------------------------- Methodology
if Methodology.pwd.exist?
say 'Methodology templates folder already exists. Skipping.'
else
template 'methodology.xml', Methodology.pwd.join('owasp2017.xml')
end

# ---------------------------------------------------------- Project data
detect_and_set_project_scope

task_options.merge!({
plugin: Dradis::Plugins::Projects::Upload::Template,
default_user_id: 1
})

importer = Dradis::Plugins::Projects::Upload::Template::Importer.new(task_options)
importer.import(file: File.expand_path('../templates/project.xml', __FILE__))

# dradis:reset:database truncates the tables and resets the :id column so
# we know the right node ID we're going to get based on the project.xml
# structure.
Dir.mkdir(Attachment.pwd.join('5'))
template 'command-01.png', Attachment.pwd.join('5/command-01.png')
end
end

class Logs < Thor
namespace 'dradis:logs'

desc 'clean DAYS', 'delete all logs older than DAYS days (default 7)'
def clean(days=7)
puts 'Clearing old Logs...'
logs = Log.where('created_at < (?)', days.to_i.days.ago)
count = logs.count
logs.destroy_all
puts "Deleted #{count} Log#{"s" if count != 1}"
end
end

class Reset < Thor
namespace 'dradis:reset'

desc 'attachments', 'removes all attachments'
def attachments
print '** Deleting all attachments... '
FileUtils.rm_rf(Dir.glob( Attachment::AttachmentPwd.join('*')) )
puts(Dir.glob( Attachment::AttachmentPwd.join('*')).empty? ? '[ DONE ]' : '[ FAILED ]')
end

desc 'database', 'removes all data from a dradis repository, except configurations'
def database
return if defined?(Dradis::Pro)

require 'config/environment'
print '** Cleaning database... '

Rails.application.eager_load!
(ApplicationRecord.descendants - [Configuration]).each do |model|
ActiveRecord::Base.connection.execute("DELETE FROM #{model.table_name}")
ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence WHERE name='#{model.table_name}'")
end

puts '[ DONE ]'
end

desc 'logs', 'removes all log files'
def logs
print '** Deleting all log files... '
FileUtils.rm_rf(Dir.glob('log/*.log'))
puts(Dir.glob('log/*.log').empty? ? '[ DONE ]' : '[ FAILED ]')
end

desc 'password', 'Set a new shared password to access the web interface'
def password()
require 'config/environment'

say 'Changing password for Dradis server.'
password = ask 'Enter new Dradis password:'
confirmation = ask 'Retype new Dradis password:'

if !password.blank? && password == confirmation
Configuration.find_or_create_by(name: 'admin:password').update_attribute(:value, ::BCrypt::Password.create(password))
say('Password Changed.', Thor::Shell::Color::GREEN)
else
say('Passwords do not match. Password Unchanged.', Thor::Shell::Color::RED)
end
end
end
end

0 comments on commit 639e2ed

Please sign in to comment.