-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LOPS-1652] Add secret:site:local-generate command. (#48)
* Add new local-generate command. * Add docs.
- Loading branch information
Showing
3 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Pantheon\TerminusSecretsManager\Commands; | ||
|
||
use Pantheon\Terminus\Commands\StructuredListTrait; | ||
use Pantheon\Terminus\Site\SiteAwareTrait; | ||
use Pantheon\Terminus\Site\SiteAwareInterface; | ||
use Consolidation\OutputFormatters\StructuredData\RowsOfFields; | ||
use Pantheon\Terminus\Request\RequestOperationResult; | ||
use Pantheon\Terminus\Exceptions\TerminusException; | ||
|
||
/** | ||
* Class SiteLocalGenerateCommand. | ||
* | ||
* Generate local secrets file for usage of secrets in local environment. | ||
* | ||
* @package Pantheon\TerminusSecretsManager\Commands | ||
*/ | ||
class SiteLocalGenerateCommand extends SecretBaseCommand implements SiteAwareInterface | ||
{ | ||
use StructuredListTrait; | ||
use SiteAwareTrait; | ||
|
||
/** | ||
* Generate json file for usage in local environment. | ||
* | ||
* @authorize | ||
* | ||
* @command secret:site:local-generate | ||
* @aliases secret:local-generate | ||
* | ||
* @option string $filepath Output to given file path. Default is to output to ./secrets.json | ||
* | ||
* @usage <site> Generate json file with secrets for local usage. | ||
* @usage <site> --filepath=/my/path/filename.json Generate json file in given location with secrets for local usage. | ||
* | ||
* @param string $site_id The name or UUID of a site to retrieve information on | ||
* @param array $options | ||
* | ||
* @return RowsOfFields | ||
* @throws \GuzzleHttp\Exception\GuzzleException | ||
* @throws \Pantheon\Terminus\Exceptions\TerminusException | ||
* @throws \Psr\Container\ContainerExceptionInterface | ||
* @throws \Psr\Container\NotFoundExceptionInterface | ||
*/ | ||
public function localGenerateSecrets($site_id, array $options = ['filepath' => 'secrets.json',]) | ||
{ | ||
$env_name = ''; | ||
if (strpos($site_id, '.') !== false) { | ||
list($site_id, $env_name) = explode('.', $site_id, 2); | ||
} | ||
$site = $this->getSite($site_id); | ||
$this->setupRequest(); | ||
$result = $this->secretsApi->fetchSecrets($site->id); | ||
if ($result->getStatusCode() != 200) { | ||
throw new TerminusException($result->getData()); | ||
} | ||
$data = $result->getData(); | ||
$json_contents = json_encode($data, JSON_PRETTY_PRINT); | ||
$filepath = $options['filepath']; | ||
$ret = file_put_contents($filepath, $json_contents); | ||
if ($ret === false) { | ||
throw new TerminusException('Unable to write to file: ' . $filepath); | ||
} | ||
$this->log()->notice(sprintf("Secrets file written to: %s. Please review this file and adjust accordingly for your local usage.", $filepath)); | ||
} | ||
|
||
/** | ||
* @param array $secrets Complete secret data | ||
* @param string $env_name Name of environment to pull secrets from | ||
* | ||
* @return array Secret data containing only secrets with overrides | ||
* in the specified environment. | ||
*/ | ||
protected function secretsForEnv(array $secrets, $env_name) | ||
{ | ||
$result = []; | ||
|
||
foreach ($secrets as $key => $data) { | ||
if (array_key_exists($env_name, $data['env-values'] ?? [])) { | ||
$result[$key] = [ | ||
'name' => $key, | ||
'type' => $data['type'], | ||
'value' => $data['env-values'][$env_name], | ||
'scopes' => $data['scopes'], | ||
]; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters