-
Notifications
You must be signed in to change notification settings - Fork 55
Self verify command #223
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
Merged
Merged
Self verify command #223
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
648e8b2
Split component for FullPathToSelf
asgrim 5584f0b
Ensure PieVersion::get always returns a non-empty-string
asgrim 9ade56b
Added self-verify command
asgrim 8e17938
Use Command::FAILURE instead of static int
asgrim 40a9b99
Happy path should use info tag not error
asgrim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Php\Pie\Command; | ||
|
|
||
| use Composer\Util\AuthHelper; | ||
| use Composer\Util\HttpDownloader; | ||
| use Php\Pie\ComposerIntegration\PieComposerFactory; | ||
| use Php\Pie\ComposerIntegration\PieComposerRequest; | ||
| use Php\Pie\ComposerIntegration\QuieterConsoleIO; | ||
| use Php\Pie\File\BinaryFile; | ||
| use Php\Pie\File\FullPathToSelf; | ||
| use Php\Pie\SelfManage\Update\ReleaseMetadata; | ||
| use Php\Pie\SelfManage\Verify\FailedToVerifyRelease; | ||
| use Php\Pie\SelfManage\Verify\VerifyPieReleaseUsingAttestation; | ||
| use Php\Pie\Util\PieVersion; | ||
| use Psr\Container\ContainerInterface; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| use function sprintf; | ||
|
|
||
| #[AsCommand( | ||
| name: 'self-verify', | ||
| description: 'Self verify PIE', | ||
| )] | ||
| final class SelfVerifyCommand extends Command | ||
| { | ||
| /** @param non-empty-string $githubApiBaseUrl */ | ||
| public function __construct( | ||
| private readonly string $githubApiBaseUrl, | ||
| private readonly QuieterConsoleIO $io, | ||
| private readonly ContainerInterface $container, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| public function configure(): void | ||
| { | ||
| parent::configure(); | ||
|
|
||
| CommandHelper::configurePhpConfigOptions($this); | ||
| } | ||
|
|
||
| public function execute(InputInterface $input, OutputInterface $output): int | ||
| { | ||
| if (! PieVersion::isPharBuild()) { | ||
| $output->writeln('<comment>Aborting! You are not running a PHAR, cannot self-update.</comment>'); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output); | ||
| $composer = PieComposerFactory::createPieComposer( | ||
| $this->container, | ||
| PieComposerRequest::noOperation( | ||
| $output, | ||
| $targetPlatform, | ||
| ), | ||
| ); | ||
| $httpDownloader = new HttpDownloader($this->io, $composer->getConfig()); | ||
| $authHelper = new AuthHelper($this->io, $composer->getConfig()); | ||
| $latestRelease = new ReleaseMetadata(PieVersion::get(), 'blah'); | ||
| $pharFilename = BinaryFile::fromFileWithSha256Checksum((new FullPathToSelf())()); | ||
| $verifyPiePhar = VerifyPieReleaseUsingAttestation::factory($this->githubApiBaseUrl, $httpDownloader, $authHelper); | ||
|
|
||
| try { | ||
| $verifyPiePhar->verify($latestRelease, $pharFilename, $output); | ||
| } catch (FailedToVerifyRelease $failedToVerifyRelease) { | ||
| $output->writeln(sprintf( | ||
| '<error>❌ Failed to verify the pie.phar release %s: %s</error>', | ||
| $latestRelease->tag, | ||
| $failedToVerifyRelease->getMessage(), | ||
| )); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $output->writeln(sprintf( | ||
| '<info>✅ You are running an authentic PIE version %s.</info>', | ||
| $latestRelease->tag, | ||
| )); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Php\Pie\File; | ||
|
|
||
| use RuntimeException; | ||
|
|
||
| use function array_key_exists; | ||
| use function getcwd; | ||
| use function is_string; | ||
| use function preg_match; | ||
| use function realpath; | ||
|
|
||
| use const DIRECTORY_SEPARATOR; | ||
|
|
||
| /** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */ | ||
| class FullPathToSelf | ||
| { | ||
| /** @return non-empty-string */ | ||
| public function __invoke(): string | ||
| { | ||
| /** @psalm-suppress TypeDoesNotContainType */ | ||
| $phpSelf = array_key_exists('PHP_SELF', $_SERVER) && is_string($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : ''; | ||
| if ($phpSelf === '') { | ||
| throw new RuntimeException('Could not find PHP_SELF'); | ||
| } | ||
|
|
||
| return $this->isAbsolutePath($phpSelf) | ||
| ? $phpSelf | ||
| : (getcwd() . DIRECTORY_SEPARATOR . $phpSelf); | ||
| } | ||
|
|
||
| private function isAbsolutePath(string $path): bool | ||
| { | ||
| if (realpath($path) === $path) { | ||
| return true; | ||
| } | ||
|
|
||
| if ($path === '' || $path === '.') { | ||
| return false; | ||
| } | ||
|
|
||
| if (preg_match('#^[a-zA-Z]:\\\\#', $path)) { | ||
| return true; | ||
| } | ||
|
|
||
| return $path[0] === '/' || $path[0] === '\\'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.