-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from City-of-Helsinki/UHF-8525
UHF-8525: Refactored PubSub service to use Vault
- Loading branch information
Showing
11 changed files
with
238 additions
and
67 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 |
---|---|---|
|
@@ -8,11 +8,28 @@ This can be used to: | |
|
||
## Managing local API accounts | ||
|
||
This is used to ensure that local API accounts retain the credentials. Any missing accounts are created and the password is reset to the one defined in configuration. | ||
This is used to ensure that local API accounts retain the credentials. Any missing accounts are created, and the password is reset to whatever is defined in the configuration. | ||
|
||
### Configuration | ||
|
||
Define an array of `username`, `password` and an optional `roles` and `mail` pairs: | ||
|
||
```php | ||
$config['helfi_api_base.api_accounts']['accounts'][] = [ | ||
'username' => 'account1', | ||
'password' => 'password1', | ||
'roles' => ['role1', 'role2'], | ||
'mail' => '[email protected]', | ||
]; | ||
``` | ||
|
||
If no `mail` is provided, an autogenerated email address like `[email protected]` is used. For example: `[email protected]`. | ||
|
||
### Using environment variable to define accounts | ||
|
||
Define an environment variable called `DRUPAL_API_ACCOUNTS`. These accounts are read and mapped in [settings.php](https://github.com/City-of-Helsinki/drupal-helfi-platform/blob/main/public/sites/default/settings.php) file shipped with `City-of-Helsinki/drupal-helfi-platform`. | ||
|
||
The value should be a base64 encoded JSON string that contains an array of `username`, `password` and an optional `roles` and `mail` pairs: | ||
The value should be a base64 encoded JSON string of whatever is defined in `helfi_api_base.api_accounts.accounts` configuration, for example: | ||
|
||
```bash | ||
php -r "print base64_encode('[{"username":"account1","password":"password1","roles":["role1","role2"]},{"username":"account2","password":"password2","mail":"[email protected]"}]');" | ||
|
@@ -23,8 +40,6 @@ Then map the given output to `DRUPAL_API_ACCOUNTS` environment variable: | |
DRUPAL_API_ACCOUNTS=W3t1c2VybmFtZTphY2NvdW50MSxwYXNzd29yZDpwYXNzd29yZDEscm9sZXM6W3JvbGUxLHJvbGUyXX0se3VzZXJuYW1lOmFjY291bnQyLHBhc3N3b3JkOnBhc3N3b3JkMixtYWlsOnNvbWUtZW1haWxAZXhhbXBsZS5jb219XQ== | ||
``` | ||
|
||
If no `mail` is provided, an email address like `[email protected]` is used. For example: `[email protected]`. | ||
|
||
### Usage | ||
|
||
We hook into `helfi_api_base.post_deploy` event ([src/EventSubscriber/EnsureApiAccountsSubscriber.php](/src/EventSubscriber/EnsureApiAccountsSubscriber.php)), triggered by `drush helfi:post-deploy` command executed as a part of deployment tasks: [https://github.com/City-of-Helsinki/drupal-helfi-platform/blob/main/docker/openshift/entrypoints/20-deploy.sh](https://github.com/City-of-Helsinki/drupal-helfi-platform/blob/main/docker/openshift/entrypoints/20-deploy.sh) | ||
|
@@ -50,12 +65,36 @@ $config['helfi_api_base.api_accounts']['accounts'] = $api_accounts; | |
|
||
This is used to store external API credentials. | ||
|
||
### Configuration | ||
|
||
Define an array of `id`, `plugin`, and `data` pairs: | ||
|
||
```php | ||
$config['helfi_api_base.api_accounts']['vault'][] = [ | ||
'id' => 'pubsub', | ||
'plugin' => 'json', | ||
'data' => '{"endpoint": "xxx.docker.so", "hub": "local", "group": "invalidate_cache", "access_key": "<access-key>"}', | ||
]; | ||
$config['helfi_api_base.api_accounts']['vault'][] = [ | ||
'id' => 'global_navigation', | ||
'plugin' => 'authorization_token', | ||
'data' => 'aGVsZmktYWRtaW46MTIz', | ||
]; | ||
``` | ||
|
||
The value of `data` field depends on `plugin` value: | ||
|
||
- Authorization token (`authorization_token`): A simple string. For example `aGVsZmktYWRtaW46MTIz`. | ||
- JSON (`json`): A JSON string. For example `{"endpoint": "xxxx.docker.so", "key": "value"}`. | ||
|
||
### Using environment variable to define Vault items | ||
|
||
Define an environment variable called `DRUPAL_VAULT_ACCOUNTS`. These accounts are read and mapped in [settings.php](https://github.com/City-of-Helsinki/drupal-helfi-platform/blob/main/public/sites/default/settings.php) file shipped with `City-of-Helsinki/drupal-helfi-platform`. | ||
|
||
The value should be a base64 encoded JSON string that contains an array of `id`, `plugin` and `data` pairs: | ||
The value should be a base64 encoded JSON string of whatever is defined in `helfi_api_base.api_accounts.vault` configuration, for example: | ||
|
||
```bash | ||
php -r "print base64_encode('[{"id": "etusivu_local", "plugin": "authorization_token": "data": "aGVsZmktYWRtaW46MTIz"}]');" | ||
php -r "print base64_encode('[{"id": "global_navigation", "plugin": "authorization_token": "data": "aGVsZmktYWRtaW46MTIz"}]');" | ||
``` | ||
|
||
Then map the given output to `DRUPAL_VAULT_ACCOUNTS` environment variable: | ||
|
@@ -70,8 +109,8 @@ DRUPAL_VAULT_ACCOUNTS=W3tpZDogZXR1c2l2dV9sb2NhbCwgcGx1Z2luOiBhdXRob3JpemF0aW9uX3 | |
/** @var \Drupal\helfi_api_base\Vault\VaultManager $service */ | ||
$service = \Drupal::service('helfi_api_base.vault_manager'); | ||
/** @var \Drupal\helfi_api_base\Vault\VaultItemInterface $item */ | ||
$item = $service->get('etusivu_local'); // 'etusivu_local' is the ID previously defined in DRUPAL_VAULT_ACCOUNTS. | ||
$id = $item->id(); // $id = 'etusivu_local'. | ||
$item = $service->get('global_navigation'); // 'global_navigation' is the ID previously defined in DRUPAL_VAULT_ACCOUNTS. | ||
$id = $item->id(); // $id = 'global_navigation'. | ||
$data = $item->data() // $data = 'aGVsZmktYWRtaW46MTIz'. This is a base64 encoded basic auth token (helfi-admin:123). | ||
``` | ||
|
||
|
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
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
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\helfi_api_base\Vault; | ||
|
||
/** | ||
* A value object to store string item vault items. | ||
*/ | ||
final class Json implements VaultItemInterface { | ||
|
||
public const PLUGIN = 'json'; | ||
|
||
/** | ||
* The json decoded data. | ||
* | ||
* @var object|mixed | ||
*/ | ||
private readonly object $data; | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @param string $id | ||
* The ID. | ||
* @param string $string | ||
* The JSON string. | ||
* | ||
* @throws \JsonException | ||
*/ | ||
public function __construct( | ||
private readonly string $id, | ||
string $string, | ||
) { | ||
$this->data = json_decode($string, flags: JSON_THROW_ON_ERROR); | ||
} | ||
|
||
/** | ||
* Gets the id. | ||
* | ||
* @return string | ||
* The ID. | ||
*/ | ||
public function id() : string { | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* Gets the data. | ||
* | ||
* @return object | ||
* The data. | ||
*/ | ||
public function data() : object { | ||
return $this->data; | ||
} | ||
|
||
} |
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
Oops, something went wrong.