Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-6316: Autogenerate password, improvements #100

Merged
merged 3 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions documentation/api-accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ Use `drush helfi:update-api-secret` command to update the API secret.

Use `drush helfi:create-api-secret` command to create the API secret.

### Show current value

Call `drush helfi:reveal-api-secret {secret value}` to show the value of given secret.
1 change: 1 addition & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
services:
helfi_api_base.api_account_commands:
class: \Drupal\helfi_api_base\Commands\ApiAccountCommands
arguments: ['@password_generator']
tags:
- { name: drush.command }
helfi_api_base.deploy_commands:
Expand Down
51 changes: 42 additions & 9 deletions src/Commands/ApiAccountCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\helfi_api_base\Commands;

use Drupal\Core\Password\PasswordGeneratorInterface;
use Drush\Attributes\Command;
use Drush\Commands\DrushCommands;

Expand All @@ -12,6 +13,35 @@
*/
final class ApiAccountCommands extends DrushCommands {

/**
* Constructs a new instance.
*
* @param \Drupal\Core\Password\PasswordGeneratorInterface $passwordGenerator
* The password generator service.
*/
public function __construct(
private PasswordGeneratorInterface $passwordGenerator,
) {
}

/**
* Reveals the secret value.
*
* @param string $value
* The value.
*
* @return int
* The exit code.
*/
#[Command(name: 'helfi:reveal-api-secret')]
public function reveal(string $value) : int {
$currentValue = json_decode(base64_decode($value), flags: JSON_THROW_ON_ERROR);
$this->io()->note('Current value:');
$this->io()->writeln(json_encode($currentValue, flags: JSON_PRETTY_PRINT));

return DrushCommands::EXIT_SUCCESS;
}

/**
* Updates the base64 and json encoded Azure secret.
*
Expand All @@ -28,7 +58,7 @@ public function update() : int {
$values = json_decode(base64_decode($value), TRUE, flags: JSON_THROW_ON_ERROR);

$this->io()
->note(sprintf('Current value: %s', json_encode($values)));
->note(sprintf('Current value: %s', json_encode($values, flags: JSON_PRETTY_PRINT)));

$values = array_merge($values, [$this->processValues($type)]);

Expand Down Expand Up @@ -111,11 +141,12 @@ private function processValues(string $type) : array {

$value = $this->io()
->ask(sprintf('Provide %s [%s]', $name, $description), $defaultValue);
$value = $callback($value);

if (!$value) {
continue;
}
$values[$name] = $callback($value);
$values[$name] = $value;
}
return $values;
}
Expand All @@ -134,7 +165,7 @@ private function getFields(string $type) : array {
'DRUPAL_VAULT_ACCOUNTS' => [
'id' => [
'description' => 'An unique ID for given item',
'default_value' => NULL,
'default_value' => '',
'callback' => NULL,
],
'plugin' => [
Expand All @@ -144,29 +175,31 @@ private function getFields(string $type) : array {
],
'data' => [
'description' => 'The data. An authorization token or basic auth string for example.',
'default_value' => NULL,
'default_value' => '',
'callback' => NULL,
],
],
'DRUPAL_API_ACCOUNTS' => [
'username' => [
'description' => 'The username',
'default_value' => NULL,
'default_value' => '',
'callback' => NULL,
],
'password' => [
'description' => 'The password',
'description' => 'The password. Leave empty to generate a random password.',
'default_value' => NULL,
'callback' => NULL,
'callback' => function (?string $value) : string {
return $value ?: $this->passwordGenerator->generate(30);
},
],
'mail' => [
'description' => 'Leave empty to use automatically generated email',
'default_value' => NULL,
'default_value' => '',
'callback' => NULL,
],
'roles' => [
'description' => 'A comma separated list of roles',
'default_value' => NULL,
'default_value' => '',
'callback' => function (string $value) : array {
return array_map('trim', explode(',', $value));
},
Expand Down