This commit is contained in:
Alexander Schneider 2018-03-29 22:08:04 +00:00 committed by GitHub
commit b4baf3f32c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 0 deletions

View file

@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Ssh;
use Mage\Task\Exception\ErrorException;
use Mage\Task\AbstractTask;
/**
* Ssh Task - Create key from base 64 encoded string
*
* @author Benjamin Gutmann <benjamin.gutmann@bestit-online.de>
* @author Alexander Schneider <alexanderschneider85@gmail.com>
*/
class CreateKeyFromBase64Task extends AbstractTask
{
public function getName()
{
return 'ssh/create-key-from-base64';
}
public function getDescription()
{
return '[SSH] Create ssh key from base64';
}
public function execute()
{
if (!array_key_exists('base64Key', $this->options)
&& !array_key_exists('base64KeyFromEnvVar', $this->options)
) {
throw new ErrorException('Parameter "base64Key" or "base64KeyFromEnvVar" is required.');
} elseif (array_key_exists('base64Key', $this->options)
&& array_key_exists('base64KeyFromEnvVar', $this->options)
) {
throw new ErrorException('Either "base64Key" or "base64KeyFromEnvVar" can be set.');
}
$home = getenv('HOME');
$sshDir = $home.DIRECTORY_SEPARATOR.'.ssh';
if (!is_dir($sshDir)) {
mkdir($sshDir);
}
$base64Key = isset($this->options['base64KeyFromEnvVar']) ?
getenv($this->options['base64KeyFromEnvVar']) : $this->options['base64Key'];
$keyFileName = isset($this->options['keyFileName']) ? $this->options['keyFileName'] : 'id_rsa';
$sshKey = base64_decode($base64Key);
$sshFile = $sshDir.DIRECTORY_SEPARATOR.$keyFileName;
$success = (file_put_contents($sshFile, $sshKey) !== false);
return ($success) ? chmod($sshFile, 0600) : false;
}
}

View file

@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Tests\Task\BuiltIn\Ssh;
use Mage\Task\BuiltIn\Ssh\CreateKeyFromBase64Task;
use PHPUnit_Framework_TestCase as TestCase;
class CreateKeyFromBase64TaskTest extends TestCase
{
public function testCreateKeyFromBase64Task()
{
$tmpDir = '/tmp/mage_ssh_test';
if (file_exists($tmpDir)) {
unlink($tmpDir);
}
mkdir($tmpDir, 0777, true);
putenv('HOME='.$tmpDir);
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Mage\Runtime\Runtime $runtime
*/
$runtime = $this->getMock('\Mage\Runtime\Runtime');
$task = new CreateKeyFromBase64Task();
$task->setOptions(['base64Key' => base64_encode('base64KeyValue')]);
$task->setRuntime($runtime);
$this->assertEquals('[SSH] Create ssh key from base64', $task->getDescription());
self::assertTrue($task->execute());
self::assertTrue(file_exists($tmpDir.'/.ssh/id_rsa'));
self::assertEquals('base64KeyValue', file_get_contents($tmpDir.'/.ssh/id_rsa'));
putenv('BASE64_KEY_ENV='.base64_encode('base64KeyFromEnvVarValue'));
$task->setOptions(['base64KeyFromEnvVar' => 'BASE64_KEY_ENV', 'keyFileName' => 'custom_key']);
self::assertTrue($task->execute());
self::assertTrue(file_exists($tmpDir.'/.ssh/custom_key'));
self::assertEquals('base64KeyFromEnvVarValue', file_get_contents($tmpDir.'/.ssh/custom_key'));
}
}