Skip to content

Commit

Permalink
Avoid generating a cert twice during ssh-cert:load (via auto-login)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Dec 11, 2023
1 parent 9610e48 commit 9ae43d6
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/SshCert/Certifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Certifier
private $stdErr;
private $fileLock;

private static $disableAutoLoad = false;

public function __construct(Api $api, Config $config, Shell $shell, Filesystem $fs, OutputInterface $output, FileLock $fileLock)
{
$this->api = $api;
Expand All @@ -40,7 +42,7 @@ public function __construct(Api $api, Config $config, Shell $shell, Filesystem $
*/
public function isAutoLoadEnabled()
{
return (bool)$this->config->getWithDefault('api.auto_load_ssh_cert', false);
return !self::$disableAutoLoad && $this->config->getWithDefault('api.auto_load_ssh_cert', false);
}

/**
Expand All @@ -50,6 +52,16 @@ public function isAutoLoadEnabled()
*/
public function generateCertificate()
{
// Ensure the user is logged in to the API, so that an auto-login will
// not be triggered after we have generated keys (auto-login triggers a
// logout, which wipes keys).
try {
self::$disableAutoLoad = true;
$this->api->getClient();
} finally {
self::$disableAutoLoad = false;
}

// Acquire a lock to prevent race conditions when certificate and key
// files are changed at the same time in different CLI processes.
$lockName = 'ssh-cert--' . $this->config->getSessionIdSlug();
Expand Down Expand Up @@ -91,9 +103,6 @@ private function doGenerateCertificate()
$this->shell->execute(['ssh-add', '-d', $dir . DIRECTORY_SEPARATOR . self::PRIVATE_KEY_FILENAME], null, false, !$this->stdErr->isVeryVerbose());
}

// Ensure the user is logged in to the API, so that an auto-login will
// not be triggered after we have generated keys (auto-login triggers a
// logout, which wipes keys).
$apiClient = $this->api->getClient();

$sshPair = $this->generateSshKey($dir, true);
Expand Down

0 comments on commit 9ae43d6

Please sign in to comment.