-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[downstream] Report failures for SignApp and SignCore #2724
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,16 +271,24 @@ private function createSignatureData(array $hashes, | |
public function writeAppSignature($path, | ||
X509 $certificate, | ||
RSA $privateKey) { | ||
if(!is_dir($path)) { | ||
throw new \Exception('Directory does not exist.'); | ||
} | ||
$appInfoDir = $path . '/appinfo'; | ||
$this->fileAccessHelper->assertDirectoryExists($path); | ||
$this->fileAccessHelper->assertDirectoryExists($appInfoDir); | ||
|
||
$iterator = $this->getFolderIterator($path); | ||
$hashes = $this->generateHashes($iterator, $path); | ||
$signature = $this->createSignatureData($hashes, $certificate, $privateKey); | ||
$this->fileAccessHelper->file_put_contents( | ||
$path . '/appinfo/signature.json', | ||
try { | ||
$this->fileAccessHelper->file_put_contents( | ||
$appInfoDir . '/signature.json', | ||
json_encode($signature, JSON_PRETTY_PRINT) | ||
); | ||
); | ||
} catch (\Exception $e){ | ||
if (!$this->fileAccessHelper->is_writeable($appInfoDir)){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
throw new \Exception($appInfoDir . ' is not writable'); | ||
} | ||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -289,17 +297,29 @@ public function writeAppSignature($path, | |
* @param X509 $certificate | ||
* @param RSA $rsa | ||
* @param string $path | ||
* @throws \Exception | ||
*/ | ||
public function writeCoreSignature(X509 $certificate, | ||
RSA $rsa, | ||
$path) { | ||
$coreDir = $path . '/core'; | ||
$this->fileAccessHelper->assertDirectoryExists($path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is_dir does not fail, when one of the parents doesn't exist, so this can be removed in favor of the next line |
||
$this->fileAccessHelper->assertDirectoryExists($coreDir); | ||
|
||
$iterator = $this->getFolderIterator($path, $path); | ||
$hashes = $this->generateHashes($iterator, $path); | ||
$signatureData = $this->createSignatureData($hashes, $certificate, $rsa); | ||
$this->fileAccessHelper->file_put_contents( | ||
$path . '/core/signature.json', | ||
try { | ||
$this->fileAccessHelper->file_put_contents( | ||
$coreDir . '/signature.json', | ||
json_encode($signatureData, JSON_PRETTY_PRINT) | ||
); | ||
); | ||
} catch (\Exception $e){ | ||
if (!$this->fileAccessHelper->is_writeable($coreDir)){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, check that |
||
throw new \Exception($coreDir . ' is not writable'); | ||
} | ||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,10 +53,33 @@ public function file_exists($filename) { | |
* Wrapper around file_put_contents($filename, $data) | ||
* | ||
* @param string $filename | ||
* @param $data | ||
* @return int|false | ||
* @param string $data | ||
* @return int | ||
* @throws \Exception | ||
*/ | ||
public function file_put_contents($filename, $data) { | ||
return file_put_contents($filename, $data); | ||
$bytesWritten = file_put_contents($filename, $data); | ||
if ($bytesWritten === false || $bytesWritten !== strlen($data)){ | ||
throw new \Exception('Failed to write into ' . $filename); | ||
} | ||
return $bytesWritten; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @return bool | ||
*/ | ||
public function is_writeable($path){ | ||
return is_writeable($path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this has been done to allow mocking here. Let me actually write tests for this and then that makes some more sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well no problem with the wrapper name, but the actual function in php is: function is_writeable($path) {
return is_writable($path);
} so killing the e will reduce nesting by 1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah. Gotcha :) |
||
} | ||
|
||
/** | ||
* @param string $path | ||
* @throws \Exception | ||
*/ | ||
public function assertDirectoryExists($path){ | ||
if (!is_dir($path)) { | ||
throw new \Exception('Directory ' . $path . ' does not exist.'); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_dir does not fail, when one of the parents doesn't exist, so this can be removed in favor of the next line