From d5cdddedea412c54d7c9ac5c93d6680ce2fa2e21 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Fri, 23 Mar 2018 19:25:05 +0100 Subject: [PATCH] backup, move google drive backup and settings into backup framework, add basic field types in diag_backup.php to render backup forms and remove crontab dependency since remote_backup doesn't really do anything when none of the providers are enabled. for https://github.com/opnsense/core/pull/2251 --- src/etc/inc/system.inc | 4 +- .../app/library/OPNsense/Backup/GDrive.php | 84 +++++- src/www/diag_backup.php | 244 ++++++++---------- 3 files changed, 182 insertions(+), 150 deletions(-) diff --git a/src/etc/inc/system.inc b/src/etc/inc/system.inc index 636d1b008..197bc9f3f 100644 --- a/src/etc/inc/system.inc +++ b/src/etc/inc/system.inc @@ -1183,9 +1183,7 @@ function system_cron_configure($verbose = false, $defer = false) $autocron[] = generate_cron_job('/usr/local/etc/rc.backup_captiveportal', '0', '*/' . $config['system']['captiveportalbackup']); } - if (!empty($config['system']['remotebackup']['GDriveEnabled'])) { - $autocron[] = generate_cron_job('/usr/local/opnsense/scripts/remote_backup.php', 0, 1); - } + $autocron[] = generate_cron_job('/usr/local/opnsense/scripts/remote_backup.php', 0, 1); /* bogons fetch always set in default config.xml */ switch ($config['system']['bogons']['interval']) { diff --git a/src/opnsense/mvc/app/library/OPNsense/Backup/GDrive.php b/src/opnsense/mvc/app/library/OPNsense/Backup/GDrive.php index 8ba535bfa..becf22518 100644 --- a/src/opnsense/mvc/app/library/OPNsense/Backup/GDrive.php +++ b/src/opnsense/mvc/app/library/OPNsense/Backup/GDrive.php @@ -49,43 +49,72 @@ class Gdrive extends Base implements IBackupProvider $fields[] = array( "name" => "GDriveEnabled", "type" => "checkbox", - "label" => gettext("Enable") + "label" => gettext("Enable"), + "value" => null ); $fields[] = array( "name" => "GDriveEmail", "type" => "text", - "label" => gettext("Email Address") + "label" => gettext("Email Address"), + "help" => gettext("Client-ID in the Google cloud console"), + "value" => null ); $fields[] = array( - "name" => "GDriveP12file", + "name" => "GDriveP12key", "type" => "file", - "label" => gettext("P12 key (not loaded)") + "label" => gettext("P12 key"), + "help" => sprintf(gettext("You need a private key in p12 format to use Google Drive, ". + "instructions on how to aquire one can be found here %s "), + " + https://cloud.google.com/storage/docs/authentication#generating-a-private-key"), + "value" => null ); $fields[] = array( "name" => "GDriveFolderID", "type" => "text", - "label" => gettext("Folder ID") + "label" => gettext("Folder ID"), + "value" => null ); $fields[] = array( "name" => "GDrivePrefixHostname", - "type" => "text", - "label" => gettext("Prefix hostname to backupfile") + "type" => "checkbox", + "label" => gettext("Prefix hostname to backupfile"), + "help" => gettext("Normally the config xml will be written as config-stamp.xml, with this option set " . + "the filename will use the systems host and domain name."), + "value" => null ); $fields[] = array( "name" => "GDriveBackupCount", "type" => "text", - "label" => gettext("Backup Count") + "label" => gettext("Backup Count"), + "value" => 60 ); $fields[] = array( "name" => "GDrivePassword", "type" => "password", - "label" => gettext("Password") + "label" => gettext("Password"), + "value" => null ); $fields[] = array( "name" => "GDrivePasswordConfirm", "type" => "password", - "label" => gettext("Confirm") + "label" => gettext("Confirm"), + "value" => null ); + $cnf = Config::getInstance(); + if ($cnf->isValid()) { + $config = $cnf->object(); + foreach ($fields as &$field) { + $fieldname = $field['name']; + if (isset($config->system->remotebackup->$fieldname)) { + $field['value'] = (string)$config->system->remotebackup->$fieldname; + } elseif ($fieldname == "GDrivePasswordConfirm" && + isset($config->system->remotebackup->GDrivePassword)) { + $field['value'] = (string)$config->system->remotebackup->GDrivePassword; + } + } + } return $fields; } @@ -102,11 +131,42 @@ class Gdrive extends Base implements IBackupProvider /** * validate and set configuration * @param array $conf configuration array - * @return array of validation errors + * @return array of validation errors when not saved */ public function setConfiguration($conf) { - // TODO: Implement setConfiguration() method. + $input_errors = array(); + if ($conf['GDrivePasswordConfirm'] != $conf['GDrivePassword']) { + $input_errors[] = gettext("The supplied 'Password' and 'Confirm' field values must match."); + } + if (count($input_errors) == 0) { + $config = Config::getInstance()->object(); + if (!isset($config->system->remotebackup)) { + $config->system->remotebackup = array(); + } + foreach ($this->getConfigurationFields() as $field) { + $fieldname = $field['name']; + if ($field['type'] == 'file') { + if (!empty($conf[$field['name']])) { + $config->system->remotebackup->$fieldname = base64_encode($conf[$field['name']]); + } + } elseif ($field['name'] == 'GDrivePasswordConfirm') { + null; // skip password confirm field + } elseif (!empty($conf[$field['name']])) { + $config->system->remotebackup->$fieldname = $conf[$field['name']]; + } else { + unset($config->system->remotebackup->$fieldname); + } + } + // remove private key when disabled + if (empty($config->system->remotebackup->GDriveEnabled) && + isset($config->system->remotebackup->GDriveP12key)) { + unset($config->system->remotebackup->GDriveP12key); + } + Config::getInstance()->save(); + } + + return $input_errors; } /** diff --git a/src/www/diag_backup.php b/src/www/diag_backup.php index b30fe837d..04c3a08d1 100644 --- a/src/www/diag_backup.php +++ b/src/www/diag_backup.php @@ -113,25 +113,28 @@ $backupFactory = new OPNsense\Backup\BackupFactory(); if ($_SERVER['REQUEST_METHOD'] === 'GET') { $pconfig = array(); - $pconfig['GDriveEnabled'] = isset($config['system']['remotebackup']['GDriveEnabled']) ? $config['system']['remotebackup']['GDriveEnabled'] : null; - $pconfig['GDrivePrefixHostname'] = isset($config['system']['remotebackup']['GDrivePrefixHostname']) ? $config['system']['remotebackup']['GDrivePrefixHostname'] : null; - $pconfig['GDriveEmail'] = isset($config['system']['remotebackup']['GDriveEmail']) ? $config['system']['remotebackup']['GDriveEmail'] : null; - $pconfig['GDriveP12key'] = isset($config['system']['remotebackup']['GDriveP12key']) ? $config['system']['remotebackup']['GDriveP12key'] : null; - $pconfig['GDriveFolderID'] = isset($config['system']['remotebackup']['GDriveFolderID']) ? $config['system']['remotebackup']['GDriveFolderID'] : null; - $pconfig['GDriveBackupCount'] = isset($config['system']['remotebackup']['GDriveBackupCount']) ? $config['system']['remotebackup']['GDriveBackupCount'] : null; - $pconfig['GDrivePassword'] = isset($config['system']['remotebackup']['GDrivePassword']) ? $config['system']['remotebackup']['GDrivePassword'] : null; + // collect all settings from backup providers + foreach ($backupFactory->listProviders() as $providerId => $provider) { + foreach ($provider['handle']->getConfigurationFields() as $field) { + $fieldId = $providerId . "_" .$field['name']; + $pconfig[$fieldId] = $field['value']; + } + } } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { $input_errors = array(); $pconfig = $_POST; - - if (!empty($_POST['restore'])) { - $mode = "restore"; - } elseif (!empty($_POST['download'])) { - $mode = "download"; - } elseif (!empty($_POST['setup_gdrive'])) { - $mode = "setup_gdrive"; - } else { - $mode = false; + $mode = null; + foreach (array_keys($backupFactory->listProviders()) as $providerName) { + if (!empty($pconfig["setup_{$providerName}"])) { + $mode = "setup_{$providerName}"; + } + } + if (!empty($mode)) { + if (!empty($pconfig['restore'])) { + $mode = "restore"; + } elseif (!empty($pconfig['download'])) { + $mode = "download"; + } } if ($mode == "download") { @@ -250,58 +253,44 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') { $savemsg .= ' ' . gettext("The system is rebooting now. This may take one minute."); } } - } elseif ( $mode == "setup_gdrive" ){ - if (!isset($config['system']['remotebackup'])) { - $config['system']['remotebackup'] = array() ; - } - $config['system']['remotebackup']['GDriveEnabled'] = $_POST['GDriveEnabled']; - $config['system']['remotebackup']['GDrivePrefixHostname'] = $_POST['GDrivePrefixHostname']; - $config['system']['remotebackup']['GDriveEmail'] = $_POST['GDriveEmail'] ; - $config['system']['remotebackup']['GDriveFolderID'] = $_POST['GDriveFolderID']; - $config['system']['remotebackup']['GDrivePassword'] = $_POST['GDrivePassword']; - if (is_numeric($_POST['GDriveBackupCount'])) { - $config['system']['remotebackup']['GDriveBackupCount'] = $_POST['GDriveBackupCount']; - } else { - $config['system']['remotebackup']['GDriveBackupCount'] = 60; - } - - if ( $_POST['GDrivePasswordConfirm'] != $_POST['GDrivePassword'] ) { - // log error, but continue - $input_errors[] = gettext("The supplied 'Password' and 'Confirm' field values must match."); - } - - if (count($input_errors) == 0) { - if (is_uploaded_file($_FILES['GDriveP12file']['tmp_name'])) { - $data = file_get_contents($_FILES['GDriveP12file']['tmp_name']); - $config['system']['remotebackup']['GDriveP12key'] = base64_encode($data); - } elseif ($config['system']['remotebackup']['GDriveEnabled'] != "on") { - unset($config['system']['remotebackup']['GDriveP12key']); - } - - $savemsg = gettext("Google Drive backup settings have been saved."); - - write_config(); - system_cron_configure(); - - try { - $provider = $backupFactory->getProvider("GDrive"); - $filesInBackup = $provider['handle']->backup(); - } catch (Exception $e) { - $filesInBackup = array(); - } - - if (empty($config['system']['remotebackup']['GDriveEnabled'])) { - /* unused */ - } elseif (count($filesInBackup) == 0) { - $input_errors[] = gettext("Google Drive communication failure"); + } elseif (!empty($mode)){ + // setup backup provider, collect provider settings and save/validate + $providerId = substr($mode, 6); + $provider = $backupFactory->getProvider($providerId); + $providerSet = array(); + foreach ($provider['handle']->getConfigurationFields() as $field) { + $fieldId = $providerId . "_" .$field['name']; + if ($field['type'] == 'file') { + // extract file to sent to setConfiguration() + if (is_uploaded_file($_FILES[$fieldId]['tmp_name'])) { + $providerSet[$field['name']] = file_get_contents($_FILES[$fieldId]['tmp_name']); + } else { + $providerSet[$field['name']] = null; + } } else { - $input_messages = gettext("Backup successful, current file list:") . "
"; - foreach ($filesInBackup as $filename) { - $input_messages .= "
" . $filename; + $providerSet[$field['name']] = $pconfig[$fieldId]; + } + } + $input_errors = $provider['handle']->setConfiguration($providerSet); + if (count($input_errors) == 0) { + if ($provider['handle']->isEnabled()) { + try { + $filesInBackup = $provider['handle']->backup(); + } catch (Exception $e) { + $filesInBackup = array(); + } + + if (count($filesInBackup) == 0) { + $input_errors[] = gettext("communication failure"); + } else { + $input_messages = gettext("Backup successful, current file list:") . "
"; + foreach ($filesInBackup as $filename) { + $input_messages .= "
" . $filename; + } } } + system_cron_configure(); } - } } @@ -437,80 +426,65 @@ $( document ).ready(function() { -
+ + +listProviders() as $providerId => $provider):?> +
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + +getConfigurationFields() as $field): + $fieldId = $providerId . "_" .$field['name'];?> + + + + + + + + + + + +
- -
- > -
- -
- -
- -
- > -
- -
:
- -
- -
- " type="submit"> -
getName();?>
+ + "> + + + > + + + + + + + + + + +
+ getName());?>" + type="submit"> +
+