added certificates to export

This commit is contained in:
Roland Gruber 2020-04-13 15:40:33 +02:00
parent 00c5a014b4
commit e8d421ae04
4 changed files with 122 additions and 11 deletions

View File

@ -2698,6 +2698,7 @@ class LAMCfgMain {
* Imports configuration data. * Imports configuration data.
* *
* @param array $data config data * @param array $data config data
* @throws LAMException import error
*/ */
public function importData($data) { public function importData($data) {
foreach ($data as $dataKey => $dataValue) { foreach ($data as $dataKey => $dataValue) {
@ -2712,6 +2713,51 @@ class LAMCfgMain {
} }
} }
/**
* Returns the content of the server certificates file
*
* @return null|string certificates
*/
public function exportCertificates() {
$fileName = $this->getSSLCaCertPath();
if ($fileName === null) {
return null;
}
$content = null;
$handle = @fopen($fileName, "r");
if ($handle) {
$content = fread($handle, 10000000);
fclose($handle);
}
return $content;
}
/**
* Imports the server certificates.
*
* @param null|string $certsContent certificates
* @throws LAMException write to file failed
*/
public function importCertificates($certsContent) {
$fileName = $this->getSSLCaCertPath();
if (empty($certsContent)) {
if ($fileName !== null) {
unlink($fileName);
}
return;
}
$fileName = $this->getInternalSSLCaCertFileName();
$handle = @fopen($fileName, "wb");
if ($handle) {
fputs($handle, $certsContent);
fclose($handle);
@chmod($fileName, 0600);
}
else {
throw new LAMException(printf(_('Unable to write file %s.'), $fileName));
}
}
/** /**
* Reloads preferences from config file config.cfg * Reloads preferences from config file config.cfg
* *

View File

@ -42,21 +42,52 @@ class ConfigDataExporter {
* Exports LAM's configuration data in JSON format. * Exports LAM's configuration data in JSON format.
*/ */
public function exportAsJson() { public function exportAsJson() {
$mainCfg = $this->_getMainConfiguration();
$jsonData = array(); $jsonData = array();
$jsonData['mainConfig'] = $this->_getMainConfigData(); $jsonData['mainConfig'] = $this->_getMainConfigData($mainCfg);
$jsonData['certificates'] = $this->_getCertificates($mainCfg);
/**
* TODO
*
* webauthn
* server profiles
* server profile job database
* account profiles
* PDF profiles
* self service profiles
*/
return json_encode($jsonData); return json_encode($jsonData);
} }
/**
* Returns the main configuration.
*
* @return LAMCfgMain main config
*/
public function _getMainConfiguration() {
return new LAMCfgMain();
}
/** /**
* Internal function to read master configuration. * Internal function to read master configuration.
* *
* @param LAMCfgMain $mainCfg main config
* @return array data * @return array data
*/ */
public function _getMainConfigData() { public function _getMainConfigData($mainCfg) {
$mainCfg = new LAMCfgMain();
return $mainCfg->exportData(); return $mainCfg->exportData();
} }
/**
* Returns the certificate file content.
*
* @param LAMCfgMain $mainCfg main config
* @return array data
*/
public function _getCertificates($mainCfg) {
return $mainCfg->exportCertificates();
}
} }
/** /**
@ -82,6 +113,9 @@ class ConfigDataImporter {
case 'mainConfig': case 'mainConfig':
$steps[] = new ImporterStep(_('General settings'), 'mainConfig', $value); $steps[] = new ImporterStep(_('General settings'), 'mainConfig', $value);
break; break;
case 'certificates':
$steps[] = new ImporterStep(_('SSL certificates'), 'certificates', $value);
break;
default: default:
logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key); logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
} }
@ -96,7 +130,7 @@ class ConfigDataImporter {
* Runs the actual import. * Runs the actual import.
* *
* @param ImporterStep[] $steps import steps * @param ImporterStep[] $steps import steps
* @throws LAMException if error occured * @throws LAMException if error occurred
*/ */
public function runImport($steps) { public function runImport($steps) {
foreach ($steps as $step) { foreach ($steps as $step) {
@ -106,9 +140,10 @@ class ConfigDataImporter {
$key = $step->getKey(); $key = $step->getKey();
switch ($key) { switch ($key) {
case 'mainConfig': case 'mainConfig':
$cfgMain = new LAMCfgMain(); $this->importMainConfig($step->getValue());
$cfgMain->importData($step->getValue()); break;
$cfgMain->save(); case 'certificates':
$this->importCertificates($step->getValue());
break; break;
default: default:
logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key); logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key);
@ -116,6 +151,29 @@ class ConfigDataImporter {
} }
} }
/**
* Imports the main configuration.
*
* @param array $data main config data
* @throws LAMException error during import
*/
private function importMainConfig($data) {
$cfgMain = new LAMCfgMain();
$cfgMain->importData($data);
$cfgMain->save();
}
/**
* Imports the SSL certificates.
*
* @param null|string $data file content
* @throws LAMException error during import
*/
private function importCertificates($data) {
$cfgMain = new LAMCfgMain();
$cfgMain->importCertificates($data);
}
} }
/** /**

View File

@ -1,6 +1,7 @@
<?php <?php
namespace LAM\CONFIG; namespace LAM\CONFIG;
use htmlButton; use htmlButton;
use htmlGroup;
use htmlInputFileUpload; use htmlInputFileUpload;
use htmlLink; use htmlLink;
use htmlOutputText; use htmlOutputText;
@ -222,8 +223,10 @@ printHeaderContents(_("Import and export configuration"), '../..');
foreach ($importSteps as $importStep) { foreach ($importSteps as $importStep) {
$content->add(new htmlResponsiveInputCheckbox('step_' . $importStep->getKey(), true, $importStep->getLabel()), 12); $content->add(new htmlResponsiveInputCheckbox('step_' . $importStep->getKey(), true, $importStep->getLabel()), 12);
} }
$content->add(new htmlButton('importConfigConfirm', _('Import')), 12); $buttonGroup = new htmlGroup();
$content->add(new htmlButton('importCancel', _('Cancel')), 12); $buttonGroup->addElement(new htmlButton('importConfigConfirm', _('Import')));
$buttonGroup->addElement(new htmlButton('importCancel', _('Cancel')));
$content->add($buttonGroup, 12);
} }
elseif (isset($_POST['importConfigConfirm'])) { elseif (isset($_POST['importConfigConfirm'])) {
$handle = fopen($_SESSION['configImportFile'], "r"); $handle = fopen($_SESSION['configImportFile'], "r");

View File

@ -37,12 +37,16 @@ class ConfigDataExporterTest extends TestCase {
'confMainKey2' => 4, 'confMainKey2' => 4,
'confMainKey3' => '', 'confMainKey3' => '',
); );
$expectedJson = json_encode(array('mainConfig' => $mainData)); $expectedJson = json_encode(array(
'mainConfig' => $mainData,
'certificates' => 'certs'
));
$exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter') $exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter')
->setMethods(array('_getMainConfigData')) ->setMethods(array('_getMainConfigData', '_getCertificates'))
->getMock(); ->getMock();
$exporter->method('_getMainConfigData')->willReturn($mainData); $exporter->method('_getMainConfigData')->willReturn($mainData);
$exporter->method('_getCertificates')->willReturn('certs');
$json = $exporter->exportAsJson(); $json = $exporter->exportAsJson();