public function testAddKey() { $key = SshPrivateKey::fromFile("{$this->keysDir}/id_nopass_rsa"); $agent = new SshAgent(); // Agent is not running so not possible to add a key to it $this->assertFalse($agent->addKey($key)); $agent->start(); $this->assertTrue($agent->addKey($key)); }
public function addKey(SshPrivateKey $key) { if (!$this->isRunning()) { // No point in trying to add a key to an agent that is not running // so abort early return false; } // Save the key to a temporary file $tmpKey = tempnam(sys_get_temp_dir(), 'codeaken_sshagent_'); file_put_contents($tmpKey, $key->getKeyData(SshKey::FORMAT_PKCS8)); $sshAdd = new Process("ssh-add {$tmpKey}", null, ['SSH_AUTH_SOCK' => $this->getSocket()], $key->getPassword() . "\n"); $sshAdd->setPty(true); $sshAdd->run(); if (!$sshAdd->isSuccessful()) { unlink($tmpKey); return false; } unlink($tmpKey); return true; }
public function testPrivateKeyGetPassword() { // Private key without a password $key = SshPrivateKey::fromFile("{$this->keysDir}/id_nopass_rsa"); $this->assertFalse($key->hasPassword()); $this->assertEquals('', $key->getPassword()); // Private key with password $key = SshPrivateKey::fromFile("{$this->keysDir}/id_pass_rsa", 'abc123'); $this->assertTrue($key->hasPassword()); $this->assertEquals('abc123', $key->getPassword()); }