Skip to content

Commit

Permalink
Change IDE SSH key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitmotghare committed Jun 9, 2021
1 parent 8d686f2 commit c74f356
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Command/Ide/Wizard/IdeWizardCommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function deleteLocalIdeSshKey(): void {
* @return string
*/
public function getSshKeyFilename(string $ide_uuid): string {
return 'id_rsa_acquia_ide_' . $ide_uuid;
return 'id_rsa';
}

}
35 changes: 17 additions & 18 deletions src/Command/Ide/Wizard/IdeWizardCreateSshKeyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$key_was_uploaded = FALSE;

// Create local SSH key.
if (!$this->localIdeSshKeyExists() || !$this->passPhraseFileExists()) {
if (!$this->localIdeSshKeyExists()) {
// Just in case the public key exists and the private doesn't, remove the public key.
$this->deleteLocalIdeSshKey();
// Just in case there's an orphaned key on the Cloud Platform for this Cloud IDE.
Expand All @@ -76,9 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->checklist->addItem('Creating a local SSH key');

// Create SSH key.
$password = md5(random_bytes(10));
$this->savePassPhraseToFile($password);
$this->createLocalSshKey($this->privateSshKeyFilename, $password);
$this->createLocalSshKey($this->privateSshKeyFilename);

$this->checklist->completePreviousItem();
$key_was_uploaded = TRUE;
Expand Down Expand Up @@ -107,7 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
// Add SSH key to local keychain.
if (!$this->sshKeyIsAddedToKeychain()) {
$this->checklist->addItem('Adding the SSH key to local keychain');
$this->addSshKeyToAgent($this->publicSshKeyFilepath, $this->getPassPhraseFromFile());
$this->addSshKeyToAgent($this->publicSshKeyFilepath);
$this->checklist->completePreviousItem();
}
else {
Expand Down Expand Up @@ -140,10 +138,11 @@ protected function localIdeSshKeyExists(): bool {
*
* @throws \Acquia\Cli\Exception\AcquiaCliException
*/
protected function addSshKeyToAgent($filepath, $password): void {
protected function addSshKeyToAgent($filepath,$password = ""): void {
// We must use a separate script to mimic user input due to the limitations of the `ssh-add` command.
// @see https://www.linux.com/topic/networking/manage-ssh-key-file-passphrase/
$temp_filepath = $this->localMachineHelper->getFilesystem()->tempnam(sys_get_temp_dir(), 'acli');

$this->localMachineHelper->writeFile($temp_filepath, <<<'EOT'
#!/usr/bin/env bash
echo $SSH_PASS
Expand Down Expand Up @@ -215,16 +214,16 @@ protected function getPassPhraseFromFile(): string {
protected function userHasUploadedIdeKeyToCloud(): bool {
$acquia_cloud_client = $this->cloudApiClientService->getClient();
$cloud_keys = $acquia_cloud_client->request('get', '/account/ssh-keys');
foreach ($cloud_keys as $index => $cloud_key) {
if (
$cloud_key->label === $this::getIdeSshKeyLabel($this->ide)
// Assert that a corresponding local key exists.
&& $this->localIdeSshKeyExists()
// Assert local public key contents match Cloud public key contents.
&& $this->normalizePublicSshKey($cloud_key->public_key) === $this->normalizePublicSshKey(file_get_contents($this->publicSshKeyFilepath))
) {
return TRUE;
}
foreach ($cloud_keys as $index => $cloud_key) {
if (
$cloud_key->label === $this::getIdeSshKeyLabel($this->ide)
// Assert that a corresponding local key exists.
&& $this->localIdeSshKeyExists()
// Assert local public key contents match Cloud public key contents.
&& $this->normalizePublicSshKey($cloud_key->public_key) === $this->normalizePublicSshKey(file_get_contents($this->publicSshKeyFilepath))
) {
return TRUE;
}
}
return FALSE;
}
Expand Down Expand Up @@ -295,8 +294,8 @@ protected function pollAcquiaCloudUntilSshSuccess(
* @throws \Acquia\Cli\Exception\AcquiaCliException
* @throws \Exception
*/
protected function createLocalSshKey(string $private_ssh_key_filename, string $password): int {
$return_code = $this->executeAcliCommand('ssh-key:create', [
protected function createLocalSshKey(string $private_ssh_key_filename, string $password = ""): int {
$return_code = $this->executeAcliCommand('ssh-key:create', [
'--filename' => $private_ssh_key_filename,
'--password' => $password,
]);
Expand Down
20 changes: 11 additions & 9 deletions src/Command/Ssh/SshKeyCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,21 @@ protected function validateFilename($filename) {
* @throws \Exception
*/
protected function determinePassword(InputInterface $input, OutputInterface $output): string {
$password = "";
if ($input->getOption('password')) {
$password = $input->getOption('password');
$this->validatePassword($password);
}
else {
$question = new Question('Enter a password for your SSH key');
$question->setHidden($this->localMachineHelper->useTty());
$question->setNormalizer(static function ($value) {
return $value ? trim($value) : '';
});
$question->setValidator(Closure::fromCallable([$this, 'validatePassword']));
$password = $this->io->askQuestion($question);
if ($input->getOption('password') !== "") {
$question = new Question('Enter a password for your SSH key');
$question->setHidden($this->localMachineHelper->useTty());
$question->setNormalizer(static function ($value) {
return $value ? trim($value) : '';
});
$question->setValidator(Closure::fromCallable([$this, 'validatePassword']));
$password = $this->io->askQuestion($question);
}
}

return $password;
Expand All @@ -152,8 +155,7 @@ protected function determinePassword(InputInterface $input, OutputInterface $out
*/
protected function validatePassword($password) {
$violations = Validation::createValidator()->validate($password, [
new Length(['min' => 5]),
new NotBlank(),
new Length(['min' => 0])
]);
if (count($violations)) {
throw new ValidatorException($violations->get(0)->getMessage());
Expand Down

0 comments on commit c74f356

Please sign in to comment.