From d20e1d5b57146688cbd48b04b8c61c68ae894e6f Mon Sep 17 00:00:00 2001 From: sgreen-r7 Date: Mon, 25 Sep 2017 17:56:00 -0700 Subject: [PATCH 1/3] adding the ability to include a password when restoring a backup --- lib/nexpose/maint.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/nexpose/maint.rb b/lib/nexpose/maint.rb index 7da7bba0..8ee50bd0 100644 --- a/lib/nexpose/maint.rb +++ b/lib/nexpose/maint.rb @@ -96,12 +96,15 @@ def initialize(name, date, description, version, independent, size) # It will restart the console after acknowledging receiving the request. # # @param [Connection] nsc An active connection to a Nexpose console. + # @param [String] (Optional) The password to use when restoring the backup. # @return [Boolean] Whether the request was received. # - def restore(nsc) + def restore(nsc, password = nil) + raise "Supplied Password is incorrect for restoring this Backup." if invalid_backup_password?(nsc, password) parameters = { 'backupid' => @name, 'cmd' => 'restore', - 'targetTask' => 'backupRestore' } + 'targetTask' => 'backupRestore', + 'password' => password } xml = AJAX.form_post(nsc, '/admin/global/maintenance/maintCmd.txml', parameters) if !!(xml =~ /succeded="true"/) nsc._maintenance_restart @@ -129,5 +132,23 @@ def self.parse(hash) hash['Platform-Independent'], hash['Size']) end + + private + + def invalid_backup_password?(nsc, password) + !correct_backup_password?(nsc, password) if backup_need_password?(nsc) + end + + def backup_need_password?(nsc) + resp = Nexpose::AJAX.get(nsc, '/data/admin/backups/password', Nexpose::AJAX::CONTENT_TYPE::JSON, 'backupID' => name) + resp == 'true' + end + + def correct_backup_password?(nsc, password) + raise 'This Backup files requires a Password. Please include a password during the restore command.' if password.nil? + resp = Nexpose::AJAX.post(nsc, "/data/admin/backups/password?backupID=#{name}&password=#{password}", nil, Nexpose::AJAX::CONTENT_TYPE::JSON) + resp == 'true' + end + end end From d829157b51e7e0c68656bab23ed4d67aa7a665f7 Mon Sep 17 00:00:00 2001 From: sgreen-r7 Date: Mon, 25 Sep 2017 18:00:39 -0700 Subject: [PATCH 2/3] grammar adjustment for error message --- lib/nexpose/maint.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nexpose/maint.rb b/lib/nexpose/maint.rb index 8ee50bd0..9c2e6c77 100644 --- a/lib/nexpose/maint.rb +++ b/lib/nexpose/maint.rb @@ -145,7 +145,7 @@ def backup_need_password?(nsc) end def correct_backup_password?(nsc, password) - raise 'This Backup files requires a Password. Please include a password during the restore command.' if password.nil? + raise 'This Backup file requires a Password. Please include a password during the restore command.' if password.nil? resp = Nexpose::AJAX.post(nsc, "/data/admin/backups/password?backupID=#{name}&password=#{password}", nil, Nexpose::AJAX::CONTENT_TYPE::JSON) resp == 'true' end From c6650a45a96b83a9abbb3c3b4855050aca187431 Mon Sep 17 00:00:00 2001 From: sgreen-r7 Date: Mon, 25 Sep 2017 18:01:39 -0700 Subject: [PATCH 3/3] altering double to single quotes --- lib/nexpose/maint.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nexpose/maint.rb b/lib/nexpose/maint.rb index 9c2e6c77..c7f07f53 100644 --- a/lib/nexpose/maint.rb +++ b/lib/nexpose/maint.rb @@ -100,7 +100,7 @@ def initialize(name, date, description, version, independent, size) # @return [Boolean] Whether the request was received. # def restore(nsc, password = nil) - raise "Supplied Password is incorrect for restoring this Backup." if invalid_backup_password?(nsc, password) + raise 'Supplied Password is incorrect for restoring this Backup.' if invalid_backup_password?(nsc, password) parameters = { 'backupid' => @name, 'cmd' => 'restore', 'targetTask' => 'backupRestore',