fix: Java setup

is necessary to consider the linux distribution when install java

Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
Vitor Mattos 2024-07-12 00:44:55 -03:00 committed by backportbot-libresign[bot]
parent c56a627637
commit 7ba65ac085
5 changed files with 96 additions and 42 deletions

View file

@ -131,8 +131,8 @@ appstore:
if [ -f $(cert_dir)/$(app_name).key ]; then \
curl -o $(cert_dir)/$(app_name).crt \
"https://raw.githubusercontent.com/nextcloud/app-certificate-requests/master/$(app_name)/$(app_name).crt"; \
$(occ) libresign:install --all --architecture aarch64; \
$(occ) libresign:install --all --architecture x86_64; \
$(occ) libresign:install --all --all-distros --architecture=aarch64; \
$(occ) libresign:install --all --all-distros --architecture=x86_64; \
echo "Signing setup files…"; \
$(occ) config:system:set debug --value true --type boolean; \
$(occ) libresign:developer:sign-setup \

View file

@ -78,7 +78,14 @@ class SignSetup extends Base {
$this->signSetupService->setPrivateKey($rsa);
foreach ($this->signSetupService->getArchitectures() as $architecture) {
foreach ($this->installService->getAvailableResources() as $resource) {
$this->signSetupService->writeAppSignature($architecture, $resource);
if ($resource === 'java') {
foreach (['linux', 'alpine-linux'] as $distro) {
$this->installService->setDistro($distro);
$this->writeAppSignature($architecture, $resource);
}
continue;
}
$this->writeAppSignature($architecture, $resource);
}
}
$output->writeln('Successfully signed');
@ -88,4 +95,17 @@ class SignSetup extends Base {
}
return 0;
}
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);
}
}

View file

@ -81,6 +81,12 @@ class Install extends Base {
shortcut: null,
mode: InputOption::VALUE_REQUIRED,
description: 'x86_64 or aarch64'
)
->addOption(
name: 'all-distros',
shortcut: null,
mode: InputOption::VALUE_NONE,
description: 'Will download java to all available distros'
);
if ($this->config->getSystemValue('debug', false) === true) {
$this->addOption(
@ -106,8 +112,14 @@ class Install extends Base {
}
$all = $input->getOption('all');
if ($input->getOption('java') || $all) {
if ($all) {
foreach (['linux', 'alpine-linux'] as $distro) {
if ($input->getOption('all-distros')) {
$currentDistro = $this->installService->getLinuxDistributionToDownloadJava();
if ($currentDistro === 'linux') {
$distros = ['alpine-linux', 'linux'];
} else {
$distros = ['linux', 'alpine-linux'];
}
foreach ($distros as $distro) {
$this->installService->setDistro($distro);
$this->installService->installJava();
}

View file

@ -74,7 +74,7 @@ class InstallService {
'java',
'jsignpdf',
'pdftk',
'cfssl'
'cfssl',
];
private string $distro = '';
private string $architecture;
@ -100,8 +100,9 @@ class InstallService {
$this->output = $output;
}
public function setArchitecture(string $architecture): void {
public function setArchitecture(string $architecture): self {
$this->architecture = $architecture;
return $this;
}
private function getFolder(string $path = '', ?ISimpleFolder $folder = null, $needToBeEmpty = false): ISimpleFolder {
@ -378,6 +379,10 @@ class InstallService {
if (!$this->willUseLocalCert) {
return;
}
$this->signSetupService->setSignatureFileName(
$this->getSignatureFileName()
);
$this->signSetupService->writeAppSignature($this->architecture, $this->resource);
}
@ -439,10 +444,37 @@ class InstallService {
$this->distro = $distro;
}
public function getInstallPath(): string {
switch ($this->resource) {
case 'java':
$path = $this->appConfig->getAppValue('java_path');
return substr($path, 0, -strlen('/bin/java'));
case 'jsignpdf':
$path = $this->appConfig->getAppValue('jsignpdf_jar_path');
return substr($path, 0, -strlen('/JSignPdf.jar'));
case 'pdftk':
$path = $this->appConfig->getAppValue('pdftk_path');
return substr($path, 0, -strlen('/pdftk.jar'));
case 'cfssl':
$path = $this->appConfig->getAppValue('cfssl_bin');
return substr($path, 0, -strlen('/cfssl'));
}
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
*/
private function getLinuxDistributionToDownloadJava(): string {
public function getLinuxDistributionToDownloadJava(): string {
if ($this->distro) {
return $this->distro;
}

View file

@ -18,7 +18,6 @@ use OCP\App\IAppManager;
use OCP\Files\AppData\IAppDataFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
use phpseclib\Crypt\RSA;
use phpseclib\File\X509;
@ -34,6 +33,8 @@ class SignSetupService {
private string $resource;
private array $signatureData = [];
private bool $willUseLocalCert = false;
private string $signatureFileName = '';
private string $installPath = '';
private ?X509 $x509 = null;
private ?RSA $rsa = null;
public function __construct(
@ -107,23 +108,23 @@ class SignSetupService {
) {
$this->architecture = $architecture;
$this->resource = $resource;
$appInfoDir = $this->getAppInfoDirectory();
try {
$iterator = $this->getFolderIterator($this->getInstallPath());
$iterator = $this->getFolderIterator($this->installPath);
$hashes = $this->generateHashes($iterator);
$signature = $this->createSignatureData($hashes);
$this->fileAccessHelper->file_put_contents(
$appInfoDir . '/install-' . $this->architecture . '-' . $this->resource . '.json',
$this->getFileName(),
json_encode($signature, JSON_PRETTY_PRINT)
);
} catch (NotFoundException $e) {
throw new \Exception(sprintf(
"Folder %s not found.\nIs necessary to run this command first: occ libresign:install --%s --architecture %s",
"Folder %s not found.\nIs necessary to run this command first: occ libresign:install --%s --architecture=%s",
$e->getMessage(),
$this->resource,
$this->architecture,
));
} catch (\Exception $e) {
$appInfoDir = $this->getAppInfoDirectory();
if (!$this->fileAccessHelper->is_writable($appInfoDir)) {
throw new \Exception($appInfoDir . ' is not writable. Original error: ' . $e->getMessage());
}
@ -131,6 +132,21 @@ class SignSetupService {
}
}
public function setInstallPath(string $installPath): self {
$this->installPath = $installPath;
return $this;
}
public function setSignatureFileName(string $signatureFileName): self {
$this->signatureFileName = $signatureFileName;
return $this;
}
private function getFileName(): string {
$appInfoDir = $this->getAppInfoDirectory();
return $appInfoDir . '/' . $this->signatureFileName;
}
protected function getAppInfoDirectory(): string {
$appInfoDir = realpath(__DIR__ . '/../../../appinfo');
$this->fileAccessHelper->assertDirectoryExists($appInfoDir);
@ -153,9 +169,7 @@ class SignSetupService {
if (!empty($this->signatureData)) {
return $this->signatureData;
}
$appInfoDir = $this->getAppInfoDirectory();
$signaturePath = $appInfoDir . '/install-' . $this->architecture . '-' . $this->resource . '.json';
$content = $this->fileAccessHelper->file_get_contents($signaturePath);
$content = $this->fileAccessHelper->file_get_contents($this->getFileName());
$signatureData = null;
if (\is_string($content)) {
@ -235,7 +249,7 @@ class SignSetupService {
try {
$expectedHashes = $this->getHashesOfResource();
// Compare the list of files which are not identical
$installPath = $this->getInstallPath();
$installPath = $this->installPath;
$currentInstanceHashes = $this->generateHashes($this->getFolderIterator($installPath), $installPath);
} catch (EmptySignatureDataException $th) {
return [
@ -284,30 +298,6 @@ class SignSetupService {
return $differenceArray;
}
private function getDataDir(): string {
$dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
return $dataDir;
}
/**
* @todo check a best solution to don't use reflection
*/
protected function getInternalPathOfFolder(ISimpleFolder $node): string {
$reflection = new \ReflectionClass($node);
$reflectionProperty = $reflection->getProperty('folder');
$reflectionProperty->setAccessible(true);
$folder = $reflectionProperty->getValue($node);
$path = $folder->getInternalPath();
return $path;
}
private function getInstallPath(): string {
$folder = $this->getDataDir() . '/' .
$this->getInternalPathOfFolder($this->appData->getFolder($this->architecture . '/' . $this->resource));
return $folder;
}
/**
* Enumerates all files belonging to the folder. Sensible defaults are excluded.
*
@ -342,7 +332,7 @@ class SignSetupService {
private function generateHashes(\RecursiveIteratorIterator $iterator): array {
$hashes = [];
$baseDirectoryLength = \strlen($this->getInstallPath());
$baseDirectoryLength = \strlen($this->installPath);
foreach ($iterator as $filename => $data) {
/** @var \DirectoryIterator $data */
if ($data->isDir()) {