Path: blob/master/src/applications/auth/management/PhabricatorAuthManagementCachePKCS8Workflow.php
12256 views
<?php12final class PhabricatorAuthManagementCachePKCS8Workflow3extends PhabricatorAuthManagementWorkflow {45protected function didConstruct() {6$this7->setName('cache-pkcs8')8->setExamples('**cache-pkcs8** --public __keyfile__ --pkcs8 __keyfile__')9->setSynopsis(10pht(11'Cache the PKCS8 format of a public key. When developing on OSX, '.12'this can be used to work around issues with ssh-keygen. Use '.13'`%s` to generate a PKCS8 key to feed to this command.',14'ssh-keygen -e -m PKCS8 -f key.pub'))15->setArguments(16array(17array(18'name' => 'public',19'param' => 'keyfile',20'help' => pht('Path to public keyfile.'),21),22array(23'name' => 'pkcs8',24'param' => 'keyfile',25'help' => pht('Path to corresponding PKCS8 key.'),26),27));28}2930public function execute(PhutilArgumentParser $args) {31$console = PhutilConsole::getConsole();3233$public_keyfile = $args->getArg('public');34if (!strlen($public_keyfile)) {35throw new PhutilArgumentUsageException(36pht(37'You must specify the path to a public keyfile with %s.',38'--public'));39}4041if (!Filesystem::pathExists($public_keyfile)) {42throw new PhutilArgumentUsageException(43pht(44'Specified public keyfile "%s" does not exist!',45$public_keyfile));46}4748$public_key = Filesystem::readFile($public_keyfile);4950$pkcs8_keyfile = $args->getArg('pkcs8');51if (!strlen($pkcs8_keyfile)) {52throw new PhutilArgumentUsageException(53pht(54'You must specify the path to a pkcs8 keyfile with %s.',55'--pkc8s'));56}5758if (!Filesystem::pathExists($pkcs8_keyfile)) {59throw new PhutilArgumentUsageException(60pht(61'Specified pkcs8 keyfile "%s" does not exist!',62$pkcs8_keyfile));63}6465$pkcs8_key = Filesystem::readFile($pkcs8_keyfile);6667$warning = pht(68'Adding a PKCS8 keyfile to the cache can be very dangerous. If the '.69'PKCS8 file really encodes a different public key than the one '.70'specified, an attacker could use it to gain unauthorized access.'.71"\n\n".72'Generally, you should use this option only in a development '.73'environment where ssh-keygen is broken and it is inconvenient to '.74'fix it, and only if you are certain you understand the risks. You '.75'should never cache a PKCS8 file you did not generate yourself.');7677$console->writeOut(78"%s\n",79phutil_console_wrap($warning));8081$prompt = pht('Really trust this PKCS8 keyfile?');82if (!phutil_console_confirm($prompt)) {83throw new PhutilArgumentUsageException(84pht('Aborted workflow.'));85}8687$key = PhabricatorAuthSSHPublicKey::newFromRawKey($public_key);88$key->forcePopulatePKCS8Cache($pkcs8_key);8990$console->writeOut(91"%s\n",92pht('Cached PKCS8 key for public key.'));9394return 0;95}9697}9899100