Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
101 changes: 63 additions & 38 deletions lib/Service/InstallService.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,43 +352,32 @@ public function installCfssl(?bool $async = false): void {
$this->runAsync();
return;
}
if (PHP_OS_FAMILY !== 'Linux') {
throw new RuntimeException(sprintf('OS_FAMILY %s is incompatible with LibreSign.', PHP_OS_FAMILY));
}
$architecture = php_uname('m');
if ($architecture === 'x86_64') {
$this->installCfssl64();
} elseif ($architecture === 'aarch64') {
$this->installCfsslArm();
}
$this->removeDownloadProgress();
}

private function installCfssl64(): void {
$folder = $this->getFolder();
$version = '1.6.1';

if (PHP_OS_FAMILY === 'Windows') {
$downloads = [
[
'file' => 'cfssl_' . $version . '_windows_amd64.exe',
'destination' => 'cfssl.exe',
],
[
'file' => 'cfssljson_' . $version . '_windows_amd64.exe',
'destination' => 'cfssljson.exe',
],
];
} elseif (PHP_OS_FAMILY === 'Darwin') {
$downloads = [
[
'file' => 'cfssl_' . $version . '_darwin_amd64',
'destination' => 'cfssl',
],
[
'file' => 'cfssljson_' . $version . '_darwin_amd64',
'destination' => 'cfssljson',
],
];
} else {
$downloads = [
[
'file' => 'cfssl_' . $version . '_linux_amd64',
'destination' => 'cfssl',
],
[
'file' => 'cfssljson_' . $version . '_linux_amd64',
'destination' => 'cfssljson',
],
];
}
$downloads = [
[
'file' => 'cfssl_' . $version . '_linux_amd64',
'destination' => 'cfssl',
],
[
'file' => 'cfssljson_' . $version . '_linux_amd64',
'destination' => 'cfssljson',
],
];
$baseUrl = 'https://github.com/cloudflare/cfssl/releases/download/v' . $version . '/';
$checksumUrl = 'https://github.com/cloudflare/cfssl/releases/download/v' . $version . '/cfssl_' . $version . '_checksums.txt';
foreach ($downloads as $download) {
Expand All @@ -399,16 +388,49 @@ public function installCfssl(?bool $async = false): void {

$this->download($baseUrl . $download['file'], $download['destination'], $fullPath, $hash, 'sha256');

if (PHP_OS_FAMILY !== 'Windows') {
chmod($fullPath, 0700);
}
chmod($fullPath, 0700);
}

$cfsslBinPath = $this->getDataDir() . DIRECTORY_SEPARATOR .
$this->getFolder()->getInternalPath() . DIRECTORY_SEPARATOR .
$downloads[0]['destination'];
$this->config->setAppValue(Application::APP_ID, 'cfssl_bin', $cfsslBinPath);
$this->removeDownloadProgress();
}

private function installCfsslArm(): void {
$appFolder = $this->getFolder();
if ($appFolder->nodeExists('cfssl')) {
/** @var Folder */
$cfsslFolder = $appFolder->get('cfssl');
} else {
$cfsslFolder = $appFolder->newFolder('cfssl');
}
$compressedFileName = 'cfssl-1.6.3-1-aarch64.pkg.tar.xz';
$url = 'http://mirror.archlinuxarm.org/aarch64/community/' . $compressedFileName;
// Generated handmade with command sha256sum
$hash = '944a6c54e53b0e2ef04c9b22477eb5f637715271c74ccea9bb91d7ac0473b855';
if (!$cfsslFolder->nodeExists($compressedFileName)) {
$compressedFile = $cfsslFolder->newFile($compressedFileName);
} else {
$compressedFile = $cfsslFolder->get($compressedFileName);
}

$comporessedInternalFileName = $this->getDataDir() . DIRECTORY_SEPARATOR . $compressedFile->getInternalPath();

$this->download($url, 'cfssl', $comporessedInternalFileName, $hash, 'sha256');

$this->config->deleteAppValue(Application::APP_ID, 'cfssl_bin');
$extractor = new TAR($comporessedInternalFileName);

$extractDir = $this->getFullPath() . DIRECTORY_SEPARATOR . 'cfssl';
$result = $extractor->extract($extractDir);
if (!$result) {
throw new \RuntimeException('Error to extract xz file. Install xz. Read more: https://github.com/codemasher/php-ext-xz');
}
$cfsslBinPath = $this->getDataDir() . DIRECTORY_SEPARATOR .
$this->getFolder()->getInternalPath() . DIRECTORY_SEPARATOR .
'cfssl/usr/bin/cfssl';
$this->config->setAppValue(Application::APP_ID, 'cfssl_bin', $cfsslBinPath);
}

public function uninstallCfssl(): void {
Expand Down Expand Up @@ -489,6 +511,9 @@ protected function downloadCli(string $url, string $filename, string $path, ?str
$this->output->writeln('<error>Failure on download ' . $filename . ' try again</error>');
$this->output->writeln('<error>Invalid ' . $hash_algo . '</error>');
}
if (!file_exists($path)) {
$this->output->writeln('<error>Failure on download ' . $filename . ', empty file, try again</error>');
}
}

private function getHash(Folder $folder, string $type, string $file, string $version, string $checksumUrl): string {
Expand Down
134 changes: 134 additions & 0 deletions tests/Unit/Service/InstallServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* @copyright Copyright (c) 2022, Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Libresign\Tests\Unit\Service;

use OCA\Libresign\Handler\CfsslHandler;
use OCA\Libresign\Handler\CfsslServerHandler;
use OCA\Libresign\Service\InstallService;
use OCP\Files\IRootFolder;
use OCP\Http\Client\IClientService;
use OCP\ICacheFactory;
use OCP\IConfig;
use org\bovigo\vfs\vfsStream;
use Symfony\Component\Console\Output\BufferedOutput;

final class InstallServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
/** @var ICacheFactory|MockObject */
private $cacheFactory;
/** @var IClientService|MockObject */
private $clientService;
/** @var CfsslServerHandler|MockObject */
private $cfsslServerHandler;
/** @var CfsslHandler|MockObject */
private $cfsslHandler;
/** @var IConfig|MockObject */
private $config;
/** @var IRootFolder|MockObject */
private $rootFolder;

public function setUp(): void {
parent::setUp();
}

protected function getInstallService(): InstallService {
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->clientService = $this->createMock(IClientService::class);
$this->cfsslServerHandler = $this->createMock(CfsslServerHandler::class);
$this->cfsslHandler = $this->createMock(CfsslHandler::class);
$this->config = $this->createMock(IConfig::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
return new InstallService(
$this->cacheFactory,
$this->clientService,
$this->cfsslServerHandler,
$this->cfsslHandler,
$this->config,
$this->rootFolder
);
}

/**
* @dataProvider providerDownloadCli
*/
public function testDownloadCli(string $url, string $filename, string $path, string $hash, string $algorithm, string $expectedOutput): void {
$installService = $this->getInstallService();
$output = new BufferedOutput();
$installService->setOutput($output);
self::invokePrivate($installService, 'downloadCli', [$url, $filename, $path, $hash, $algorithm]);
$actual = $output->fetch();
$this->assertEquals($expectedOutput, $actual);
}

public function providerDownloadCli(): array {
vfsStream::setup('download');

$pathInvalid = 'vfs://download/appInvalid.svg';
file_put_contents($pathInvalid, 'invalidContent');
$pathValid = 'vfs://download/validContent.svg';
file_put_contents($pathValid, 'invalidContent');
return [
[
"http://localhost/apps/libresign/img/app.svg",
'app.svg',
'vfs://download/app.svg',
'',
'md5',
"Downloading app.svg...\n" .
" 0 [>---------------------------]\n".
"Failure on download app.svg, empty file, try again\n",
],
[
"http://localhost/apps/libresign/img/appInvalid.svg",
'appInvalid.svg',
$pathInvalid,
'hashInvalid',
'md5',
"Downloading appInvalid.svg...\n" .
" 0 [>---------------------------]\n" .
"Failure on download appInvalid.svg try again\n" .
"Invalid md5\n",
],
[
"http://localhost/apps/libresign/img/appInvalid.svg",
'appInvalid.svg',
$pathInvalid,
'hashInvalid',
'sha256',
"Downloading appInvalid.svg...\n" .
" 0 [>---------------------------]\n" .
"Failure on download appInvalid.svg try again\n" .
"Invalid sha256\n",
],
[
"http://localhost/apps/libresign/img/validContent.svg",
'validContent.svg',
$pathValid,
hash_file('sha256', $pathValid),
'sha256',
"Downloading validContent.svg...\n" .
" 0 [>---------------------------]\n",
],
];
}
}