Skip to content

Commit

Permalink
Add new way of handling secrets via new replacement pattern
Browse files Browse the repository at this point in the history
Secrets can be declared in a secrets section in the root of a
fabfile (similar to a question) and be used in any host-configuration
as `%secret.MY_SECRET_NAME%. Phab will inject the actual value when
the host configuration is requested. The actual value can be passed
via environment variable of command line argument `--secret key=value`
As a last ressort, the user is asked for the secret via shell.
  • Loading branch information
stmh committed Apr 6, 2021
1 parent 35d3e98 commit aa8be5d
Show file tree
Hide file tree
Showing 53 changed files with 489 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yaml]
indent_size = 2

[composer.{json,lock}]
indent_size = 4
2 changes: 1 addition & 1 deletion src/Command/AboutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->write($output, $this->getDockerConfig()->raw(), 2);
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$this->getMethods()->runTask('about', $this->getHostConfig(), $context);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/AppCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$copy_from = $this->getConfiguration()->getHostConfig($copy_from);
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$host_config = $this->getHostConfig();

$this->configuration->getMethodFactory()->runTask('appCheckExisting', $host_config, $context);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/AppDestroyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$host_config = $this->getHostConfig();

$this->configuration->getMethodFactory()->runTask('appCheckExisting', $host_config, $context);
Expand Down
3 changes: 2 additions & 1 deletion src/Command/AppScaffoldCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$context = $this->createContext($input, $output);

$url = $input->getArgument('scaffold-url');
$root_folder = empty($input->getOption('output')) ? getcwd() : $input->getOption('output');
$context = $this->createContext($input, $output);

$this->scaffold($url, $root_folder, $context, [], new Options());
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/AppUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();

try {
$this->getMethods()->runTask('appUpdate', $this->getHostConfig(), $context);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$context->set('what', array_map(function ($elem) {
return trim(strtolower($elem));
}, $input->getArgument('what')));
Expand Down
5 changes: 4 additions & 1 deletion src/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Phabalicious\Exception\ShellProviderNotFoundException;
use Phabalicious\Exception\ValidationFailedException;
use Phabalicious\Exception\MissingHostConfigException;
use Phabalicious\Method\TaskContext;
use Phabalicious\ShellCompletion\FishShellCompletionContext;
use Phabalicious\ShellProvider\ShellOptions;
use Phabalicious\ShellProvider\ShellProviderInterface;
Expand All @@ -35,6 +36,7 @@ abstract class BaseCommand extends BaseOptionsCommand
private $dockerConfig;



protected function configure()
{
$default_conf = getenv('PHABALICIOUS_DEFAULT_CONFIG');
Expand Down Expand Up @@ -126,7 +128,8 @@ public function completeOptionValues($optionName, CompletionContext $context)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$this->createContext($input, $output);
$io = $this->getContext()->io();

$this->checkAllRequiredOptionsAreNotEmpty($input);

Expand Down
22 changes: 20 additions & 2 deletions src/Command/BaseOptionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Phabalicious\Method\MethodFactory;
use Phabalicious\Method\ScriptMethod;
use Phabalicious\Method\TaskContext;
use Phabalicious\Method\TaskContextInterface;
use Phabalicious\Utilities\Utilities;
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
Expand All @@ -26,6 +27,9 @@ abstract class BaseOptionsCommand extends Command implements CompletionAwareInte
*/
protected $methods;

/** @var \Phabalicious\Method\TaskContextInterface */
private $context = null;


public function __construct(ConfigurationService $configuration, MethodFactory $method_factory, $name = null)
{
Expand Down Expand Up @@ -68,6 +72,13 @@ protected function configure()
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Pass optional arguments',
[]
)
->addOption(
'secret',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Pass optional secrets',
[]
);
}

Expand Down Expand Up @@ -171,7 +182,7 @@ protected function parseScriptArguments(array $defaults, $arguments_string)
*/
protected function createContext(InputInterface $input, OutputInterface $output, $default_arguments = [])
{
$context = new TaskContext($this, $input, $output);
$context = $this->context ? $this->context : new TaskContext($this, $input, $output);
$arguments = $this->parseScriptArguments($default_arguments, $input->getOption('arguments'));
$context->set('variables', $arguments);
$context->set('deployArguments', $arguments);
Expand All @@ -180,6 +191,13 @@ protected function createContext(InputInterface $input, OutputInterface $output,
$this->parseScriptArguments([], $input->getOption('arguments'))['arguments'] ?? []
);

return $context;
$this->context = $context;
return $this->context;
}

protected function getContext(): TaskContextInterface
{
assert($this->context);
return $this->context;
}
}
2 changes: 1 addition & 1 deletion src/Command/CopyFromCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$from = $this->configuration->getHostConfig($input->getArgument('from'));
if (empty($from['supportsCopyFrom'])) {
throw new \InvalidArgumentException('Source config does not support copy-from!');
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DeployCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();

// Override branch in config.
$branch = $input->getArgument('branch');
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DockerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$docker_config = $this->getDockerConfig();
$context->set('docker_config', $docker_config);

Expand Down
2 changes: 1 addition & 1 deletion src/Command/GetBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return trim(strtolower($elem));
}, $input->getArgument('what'));

$context = $this->createContext($input, $output);
$context = $this->getContext();
$context->set('what', $what);

$hash = $input->getArgument('hash');
Expand Down
2 changes: 1 addition & 1 deletion src/Command/GetFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$file = $input->getArgument('file');


$context = $this->createContext($input, $output);
$context = $this->getContext();
$context->set('sourceFile', $file);
$context->set('destFile', getcwd() . '/' . basename($file));

Expand Down
2 changes: 1 addition & 1 deletion src/Command/GetFilesDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();

$this->getMethods()->runTask('getFilesDump', $this->getHostConfig(), $context);
$to_copy = $context->getResult('files');
Expand Down
2 changes: 1 addition & 1 deletion src/Command/GetSqlDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();

$this->getMethods()->runTask('getSQLDump', $this->getHostConfig(), $context);
$to_copy = $context->getResult('files');
Expand Down
2 changes: 1 addition & 1 deletion src/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$host_config = $this->getHostConfig();

if ($host_config['supportsInstalls'] == false) {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/JiraCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->checkAllRequiredOptionsAreNotEmpty($input);
$this->readConfiguration($input);
$context = $this->createContext($input, $output);
$context = $this->getContext();

$jira_config = $this->configuration->getSetting('jira', []);
$errors = new ValidationErrorBag();
Expand Down
2 changes: 1 addition & 1 deletion src/Command/K8sCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$subcommands = $input->getArgument('k8s');
if (!is_array($subcommands)) {
$subcommands = [ $subcommands ];
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ListBackupsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$what = array_map(function ($elem) {
return trim(strtolower($elem));
}, $input->getArgument('what'));
Expand Down
2 changes: 1 addition & 1 deletion src/Command/NotifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($result = parent::execute($input, $output)) {
return $result;
}
$context = $this->createContext($input, $output);
$context = $this->getContext();
$context->set('message', $input->getArgument('message'));
$context->set('channel', $input->getOption('channel'));

Expand Down
10 changes: 8 additions & 2 deletions src/Command/OutputCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Phabalicious\Exception\MissingHostConfigException;
use Phabalicious\Exception\ShellProviderNotFoundException;
use Phabalicious\Exception\ValidationFailedException;
use Phabalicious\Method\TaskContext;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -70,6 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->readConfiguration($input);
$data = [];
$title = '';
$context = new TaskContext($this, $input, $output);

if ($what == 'blueprint') {
if (empty($blueprint)) {
Expand All @@ -82,9 +84,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
];
$title = 'Output of applied blueprint `' . $config . '`';
} elseif ($what == 'host') {
$data = $this->getConfiguration()->getHostConfig($config)->raw();
if (!empty($blueprint)) {
$data = $this->getConfiguration()->getHostConfigFromBlueprint($config, $blueprint);
} else {
$data = $this->getConfiguration()->getHostConfig($config);
}
$data = [
$data['configName'] => $data
$data['configName'] => $data->raw(),
];
$title = 'Output of host-configuration `' . $config . '`';
} elseif ($what == 'docker') {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/PlatformCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$context->set('command', implode(' ', $input->getArgument('platform')));

try {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/PutFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \RuntimeException('Could not find file `' . $file . '`!');
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$context->set('sourceFile', $file);

$context->io()->comment('Putting file `' . $file . '` to `' . $this->getHostConfig()['configName']. '`');
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();

try {
$this->getMethods()->runTask('reset', $this->getHostConfig(), $context);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/RestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$context->set('what', array_map(function ($elem) {
return trim(strtolower($elem));
}, $input->getArgument('what')));
Expand Down
2 changes: 1 addition & 1 deletion src/Command/RestoreSqlFromFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$file = $input->getArgument('file');
if (!file_exists($file)) {
throw new \InvalidArgumentException('Could not find file at `' . $file . '`');
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ScaffoldCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Phabalicious\Exception\MismatchedVersionException;
use Phabalicious\Exception\MissingScriptCallbackImplementation;
use Phabalicious\Exception\ValidationFailedException;
use Phabalicious\Method\TaskContext;
use Phabalicious\Scaffolder\Callbacks\TransformCallback;
use Phabalicious\Scaffolder\Options;
use Phabalicious\Utilities\PluginDiscovery;
Expand Down Expand Up @@ -61,6 +60,7 @@ protected function configure()
* @throws FabfileNotReadableException
* @throws FailedShellCommandException
* @throws MissingScriptCallbackImplementation
* @throws \Phabalicious\Exception\UnknownReplacementPatternException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ShellCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$host_config = $this->getHostConfig();

// Allow methods to override the used shellProvider:
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ShellCommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$host_config = $this->getHostConfig();

// Allow methods to override the used shellProvider:
Expand Down
2 changes: 1 addition & 1 deletion src/Command/SimpleExecutableInvocationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();

$arguments = $this->prepareArguments($input->getArgument('command-arguments'));
$context->set('command', $arguments);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/StartRemoteAccessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();
$host_config = $this->getHostConfig();
$this->getMethods()->runTask('startRemoteAccess', $host_config, $context);

Expand Down
2 changes: 1 addition & 1 deletion src/Command/VariablePull.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $result;
}

$context = $this->createContext($input, $output);
$context = $this->getContext();

$context->set('action', 'pull');
$filename = $input->getArgument('file');
Expand Down
Loading

0 comments on commit aa8be5d

Please sign in to comment.