Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge 9d2e216 into d0e3b75
Browse files Browse the repository at this point in the history
  • Loading branch information
RonRademaker committed May 23, 2016
2 parents d0e3b75 + 9d2e216 commit bda9c9c
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/Task/YamlConfigurationTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Accompli\EventDispatcher\Event\InstallReleaseEvent;
use Accompli\EventDispatcher\Event\LogEvent;
use Accompli\EventDispatcher\EventDispatcherInterface;
use Accompli\Utility\SecretGenerator;
use Accompli\Utility\ValueGeneratorInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -53,6 +55,13 @@ class YamlConfigurationTask extends AbstractConnectedTask
*/
private $environmentVariables;

/**
* Generator to generate values.
*
* @var ValueGeneratorInterface
*/
private $valueGenerator;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -123,6 +132,31 @@ public function onInstallReleaseCreateOrUpdateConfiguration(InstallReleaseEvent
}
}

/**
* Sets a value generator.
*
* @param ValueGeneratorInterface
*/
public function setValueGenerator(ValueGeneratorInterface $valueGenerator)
{
$this->valueGenerator = $valueGenerator;
}

/**
* Gets the value generator
* Defaults to SecretGenerator.
*
* @return ValueGeneratorInterface
*/
private function getValueGenerator()
{
if (!$this->valueGenerator instanceof ValueGeneratorInterface) {
$this->valueGenerator = new SecretGenerator();
}

return $this->valueGenerator;
}

/**
* Gathers environment variables to use in the YAML configuration.
*
Expand Down Expand Up @@ -188,7 +222,7 @@ private function findKeyAndGenerateValue(array &$configuration, array $parameter

$configuration[$key] = $value;
} elseif (is_scalar($value)) {
$configuration[$key] = sha1(uniqid());
$configuration[$key] = $this->getValueGenerator()->generate($key);
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/Utility/SecretGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Accompli\Utility;

/**
* Generatator to generate secrets.
*
* @author Ron Rademaker
*/
class SecretGenerator implements ValueGeneratorInterface
{
/**
* Generated values to prevent different secrets for the same key.
*
* @var array
*/
private $generatedValues = array();

/**
* {@inheritdoc}
*/
public function generate($key)
{
if (!array_key_exists($key, $this->generatedValues)) {
$this->generatedValues[$key] = sha1(uniqid());
}

return $this->generatedValues[$key];
}
}
20 changes: 20 additions & 0 deletions src/Utility/ValueGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Accompli\Utility;

/**
* Interface defining a generator for values.
*
* @author Ron Rademaker
*/
interface ValueGeneratorInterface
{
/**
* Generate a value for $key.
*
* @param string $key
*
* @return string
*/
public function generate($key);
}
12 changes: 12 additions & 0 deletions tests/Task/YamlConfigurationTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,16 @@ public function provideExpectedStageSpecificConfigurations()
array('production', "foo: bam\nbaz: ''\nbar:\n baz: ''\n"),
);
}

/**
* Test setting a value generator.
*/
public function testValueGeneratorSetter()
{
$task = new YamlConfigurationTask('/parameters.yml');
$generator = $this->getMockBuilder('Accompli\Utility\ValueGeneratorInterface')->getMock();
$task->setValueGenerator($generator);

$this->assertAttributeEquals($generator, 'valueGenerator', $task);
}
}
28 changes: 28 additions & 0 deletions tests/Utility/SecretGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Accompli\Test\Utility;

use Accompli\Utility\SecretGenerator;
use PHPUnit_Framework_TestCase;

/**
* Unit test for the secret generator.
*
* @author Ron Rademaker
*/
class SecretGeneratorTest extends PHPUnit_Framework_TestCase
{
/**
* Tests if generated values are not regenerated within the same process.
*/
public function testGeneratedValuesAreNotRegenerated()
{
$generator = new SecretGenerator();

$generated = $generator->generate('foobar');

$this->assertNotEmpty($generated);
$this->assertEquals($generated, $generator->generate('foobar'));
$this->assertNotEquals($generated, $generator->generate('baz'));
}
}

0 comments on commit bda9c9c

Please sign in to comment.