Skip to content

Commit

Permalink
Merge pull request #3365 from LibreSign/fix/use-linux-distro-when-build
Browse files Browse the repository at this point in the history
fix: use linux distro when build
  • Loading branch information
vitormattos authored Jul 12, 2024
2 parents abf43f7 + 5fa8bf5 commit 1ddddbb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 60 deletions.
11 changes: 1 addition & 10 deletions lib/Command/Developer/SignSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
foreach ($this->installService->getAvailableResources() as $resource) {
if ($resource === 'java') {
foreach (['linux', 'alpine-linux'] as $distro) {
$this->installService->setDistro($distro);
$this->signSetupService->setDistro($distro);
$this->writeAppSignature($architecture, $resource);
}
continue;
Expand All @@ -97,15 +97,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

private function writeAppSignature(string $architecture, string $resource): void {
$this->installService
->setArchitecture($architecture)
->setResource($resource);
$this->signSetupService->setInstallPath(
$this->installService->getInstallPath()
);
$this->signSetupService->setSignatureFileName(
$this->installService->getSignatureFileName()
);
$this->signSetupService->writeAppSignature($architecture, $resource);
}
}
16 changes: 1 addition & 15 deletions lib/Service/Install/InstallService.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,7 @@ private function writeAppSignature(): void {
return;
}

$this->signSetupService->setSignatureFileName(
$this->getSignatureFileName()
);
$this->signSetupService->setInstallPath(
$this->getInstallPath()
);
$this->signSetupService->setDistro($this->getLinuxDistributionToDownloadJava());
$this->signSetupService->writeAppSignature($this->architecture, $this->resource);
}

Expand Down Expand Up @@ -451,15 +446,6 @@ public function getInstallPath(): string {
return '';
}

public function getSignatureFileName(): string {
$path[] = 'install-' . $this->architecture;
if ($this->resource === 'java') {
$path[] = $this->getLinuxDistributionToDownloadJava();
}
$path[] = $this->resource . '.json';
return implode('-', $path);
}

/**
* Return linux or alpine-linux
*/
Expand Down
85 changes: 67 additions & 18 deletions lib/Service/Install/SignSetupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
use OCA\Libresign\Exception\InvalidSignatureException;
use OCA\Libresign\Exception\SignatureDataNotFoundException;
use OCP\App\IAppManager;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\IAppData;
use OCP\AppFramework\Services\IAppConfig;
use OCP\Files\NotFoundException;
use OCP\IConfig;
use phpseclib\Crypt\RSA;
use phpseclib\File\X509;

class SignSetupService {
private IAppData $appData;
private array $exclude = [
'openssl_config',
'cfssl_config',
Expand All @@ -33,18 +31,16 @@ class SignSetupService {
private string $resource;
private array $signatureData = [];
private bool $willUseLocalCert = false;
private string $signatureFileName = '';
private string $installPath = '';
private string $distro = '';
private ?X509 $x509 = null;
private ?RSA $rsa = null;
public function __construct(
private EnvironmentHelper $environmentHelper,
private FileAccessHelper $fileAccessHelper,
private IConfig $config,
private IAppDataFactory $appDataFactory,
private IAppConfig $appConfig,
private IAppManager $appManager,
) {
$this->appData = $appDataFactory->get('libresign');
}

public function getArchitectures(): array {
Expand Down Expand Up @@ -109,7 +105,7 @@ public function writeAppSignature(
$this->architecture = $architecture;
$this->resource = $resource;
try {
$iterator = $this->getFolderIterator($this->installPath);
$iterator = $this->getFolderIterator($this->getInstallPath());
$hashes = $this->generateHashes($iterator);
$signature = $this->createSignatureData($hashes);
$this->fileAccessHelper->file_put_contents(
Expand All @@ -132,19 +128,72 @@ public function writeAppSignature(
}
}

public function setInstallPath(string $installPath): self {
$this->installPath = $installPath;
return $this;
public function getInstallPath(): string {
switch ($this->resource) {
case 'java':
$path = $this->appConfig->getAppValue('java_path');
$installPath = substr($path, 0, -strlen('/bin/java'));
if (!str_contains($installPath, $this->distro)) {
$installPath = preg_replace(
'/\/' . $this->architecture . '\/(\w+)/i',
'/' . $this->architecture . '/'.$this->distro,
$installPath
);
}
break;
case 'jsignpdf':
$path = $this->appConfig->getAppValue('jsignpdf_jar_path');
$installPath = substr($path, 0, -strlen('/JSignPdf.jar'));
break;
case 'pdftk':
$path = $this->appConfig->getAppValue('pdftk_path');
$installPath = substr($path, 0, -strlen('/pdftk.jar'));
break;
case 'cfssl':
$path = $this->appConfig->getAppValue('cfssl_bin');
$installPath = substr($path, 0, -strlen('/cfssl'));
break;
default:
$installPath = '';
}
if (!str_contains($installPath, $this->architecture)) {
$installPath = preg_replace('/\/libresign\/(\w+)/i', '/libresign/'.$this->architecture, $installPath);
}
return $installPath;
}

private function getFileName(): string {
$appInfoDir = $this->getAppInfoDirectory();
return $appInfoDir . '/' . $this->getSignatureFileName();
}

public function getSignatureFileName(): string {
$path[] = 'install-' . $this->architecture;
if ($this->resource === 'java') {
$path[] = $this->getLinuxDistributionToDownloadJava();
}
$path[] = $this->resource . '.json';
return implode('-', $path);
}

public function setSignatureFileName(string $signatureFileName): self {
$this->signatureFileName = $signatureFileName;
public function setDistro(string $distro): self {
$this->distro = $distro;
return $this;
}

private function getFileName(): string {
$appInfoDir = $this->getAppInfoDirectory();
return $appInfoDir . '/' . $this->signatureFileName;
/**
* Return linux or alpine-linux
*/
public function getLinuxDistributionToDownloadJava(): string {
if ($this->distro) {
return $this->distro;
}
$distribution = shell_exec('cat /etc/*-release');
preg_match('/^ID=(?<version>.*)$/m', $distribution, $matches);
if (isset($matches['version']) && strtolower($matches['version']) === 'alpine') {
return 'alpine-linux';
}
return 'linux';
}

protected function getAppInfoDirectory(): string {
Expand Down Expand Up @@ -249,7 +298,7 @@ public function verify(string $architecture, $resource): array {
try {
$expectedHashes = $this->getHashesOfResource();
// Compare the list of files which are not identical
$currentInstanceHashes = $this->generateHashes($this->getFolderIterator($this->installPath), $this->installPath);
$currentInstanceHashes = $this->generateHashes($this->getFolderIterator($this->getInstallPath()));
} catch (EmptySignatureDataException $th) {
return [
'EMPTY_SIGNATURE_DATA' => $th->getMessage(),
Expand Down Expand Up @@ -331,7 +380,7 @@ private function getFolderIterator(string $folderToIterate): \RecursiveIteratorI
private function generateHashes(\RecursiveIteratorIterator $iterator): array {
$hashes = [];

$baseDirectoryLength = \strlen($this->installPath);
$baseDirectoryLength = \strlen($this->getInstallPath());
foreach ($iterator as $filename => $data) {
/** @var \DirectoryIterator $data */
if ($data->isDir()) {
Expand Down
44 changes: 27 additions & 17 deletions tests/Unit/Service/Install/SignSetupServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use OC\IntegrityCheck\Helpers\FileAccessHelper;
use OCA\Libresign\Service\Install\SignSetupService;
use OCP\App\IAppManager;
use OCP\Files\AppData\IAppDataFactory;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IConfig;
use phpseclib\Crypt\RSA;
use phpseclib\File\X509;
Expand All @@ -23,15 +23,15 @@ final class SignSetupServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
private EnvironmentHelper&MockObject $environmentHelper;
private FileAccessHelper $fileAccessHelper;
private IConfig&MockObject $config;
private IAppDataFactory&MockObject $appDataFactory;
private IAppConfig&MockObject $appConfig;
private IAppManager&MockObject $appManager;

public function setUp(): void {
$this->environmentHelper = $this->createMock(EnvironmentHelper::class);
$this->fileAccessHelper = new FileAccessHelper();
$this->config = $this->createMock(IConfig::class);
$this->appDataFactory = $this->createMock(IAppDataFactory::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->config = $this->createMock(IConfig::class);
$this->appConfig = $this->createMock(IAppConfig::class);
}

/**
Expand All @@ -43,7 +43,7 @@ private function getInstance(array $methods = []) {
$this->environmentHelper,
$this->fileAccessHelper,
$this->config,
$this->appDataFactory,
$this->appConfig,
$this->appManager,
])
->onlyMethods($methods)
Expand Down Expand Up @@ -82,14 +82,20 @@ private function writeAppSignature(string $architecture, $resource): SignSetupSe
$this->environmentHelper->method('getServerRoot')
->willReturn('vfs://home');

$this->appConfig
->method('getAppValue')
->willReturnCallback(function ($key, $default) use ($architecture):string {
return match ($key) {
'java_path' => 'vfs://home/data/libresign/' . $architecture . '/linux/java/bin/java',
default => '',
};
});
$signSetupService = $this->getInstance([
'getAppInfoDirectory',
]);
$signSetupService->expects($this->any())
->method('getAppInfoDirectory')
->willReturn('vfs://home/appinfo');
$signSetupService->setSignatureFileName('install-' . $architecture . '-' . $resource . '.json');
$signSetupService->setInstallPath('vfs://home/data/libresign/' . $resource);

$this->appManager->method('getAppInfo')
->willReturn(['dependencies' => ['architecture' => [$architecture]]]);
Expand All @@ -105,10 +111,14 @@ private function writeAppSignature(string $architecture, $resource): SignSetupSe
$structure = [
'data' => [
'libresign' => [
'java' => [
'fakeFile01' => 'content',
'fakeFile02' => 'content',
],
$architecture => [
'linux' => [
'java' => [
'fakeFile01' => 'content',
'fakeFile02' => 'content',
],
]
]
],
],
'resources' => [
Expand All @@ -121,12 +131,12 @@ private function writeAppSignature(string $architecture, $resource): SignSetupSe
$root = vfsStream::setup('home', null, $structure);

$signSetupService->writeAppSignature($architecture, $resource);
$this->assertFileExists('vfs://home/appinfo/install-' . $architecture . '-' . $resource . '.json');
$json = file_get_contents('vfs://home/appinfo/install-' . $architecture . '-' . $resource . '.json');
$this->assertFileExists('vfs://home/appinfo/install-' . $architecture . '-linux-' . $resource . '.json');
$json = file_get_contents('vfs://home/appinfo/install-' . $architecture . '-linux-' . $resource . '.json');
$signatureContent = json_decode($json, true);
$this->assertArrayHasKey('hashes', $signatureContent);
$this->assertCount(2, $signatureContent['hashes']);
$expected = hash('sha512', $structure['data']['libresign'][$resource]['fakeFile01']);
$expected = hash('sha512', $structure['data']['libresign'][$architecture]['linux'][$resource]['fakeFile01']);
$this->assertArrayHasKey('fakeFile01', $signatureContent['hashes']);
$actual = $signatureContent['hashes']['fakeFile01'];
$this->assertEquals($expected, $actual);
Expand All @@ -152,9 +162,9 @@ public static function dataWriteAppSignature(): array {
public function testVerify(): void {
$architecture = 'x86_64';
$signSetupService = $this->writeAppSignature($architecture, 'java');
unlink('vfs://home/data/libresign/java/fakeFile01');
file_put_contents('vfs://home/data/libresign/java/fakeFile02', 'invalidContent');
file_put_contents('vfs://home/data/libresign/java/fakeFile03', 'invalidContent');
unlink('vfs://home/data/libresign/' . $architecture . '/linux/java/fakeFile01');
file_put_contents('vfs://home/data/libresign/' . $architecture . '/linux/java/fakeFile02', 'invalidContent');
file_put_contents('vfs://home/data/libresign/' . $architecture . '/linux/java/fakeFile03', 'invalidContent');
$expected = json_encode([
'FILE_MISSING' => [
'fakeFile01' => [
Expand Down

0 comments on commit 1ddddbb

Please sign in to comment.