Skip to content

Commit

Permalink
feat(adapter): Adding new JSON File Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptiklemur committed Aug 4, 2019
1 parent 2c13a6c commit 4f218e1
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 36 deletions.
57 changes: 29 additions & 28 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
{
"name": "secretary/php",
"description": "Monorepo for Secretary's PHP implementation",
"type": "library",
"require-dev": {
"phpunit/phpunit": "^8.0 || ^7.0",
"mockery/mockery": "^1.2",
"symfony/options-resolver": "^4.0",
"aws/aws-sdk-php": "^3.91",
"guzzlehttp/guzzle": "^6.3"
},
"license": "MIT",
"authors": [
{
"name": "Aaron Scherer",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Secretary\\": "src/Core",
"Secretary\\Adapter\\": "src/Adapter",
"Secretary\\Bundle\\": "src/Bundle"
},
"exclude-from-classmap": [
"**/Tests/"
]
},
"require": {}
"name": "secretary/php",
"description": "Monorepo for Secretary's PHP implementation",
"type": "library",
"require-dev": {
"ext-json": "*",
"phpunit/phpunit": "^8.0 || ^7.0",
"mockery/mockery": "^1.2",
"symfony/options-resolver": "^4.0",
"aws/aws-sdk-php": "^3.91",
"guzzlehttp/guzzle": "^6.3"
},
"license": "MIT",
"authors": [
{
"name": "Aaron Scherer",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Secretary\\": "src/Core",
"Secretary\\Adapter\\": "src/Adapter",
"Secretary\\Bundle\\": "src/Bundle"
},
"exclude-from-classmap": [
"**/Tests/"
]
},
"require": {}
}
6 changes: 3 additions & 3 deletions examples/AWSSecretsManagerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

require_once __DIR__.'/../vendor/autoload.php';

use Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter;
use Secretary\Adapter\AWS\SecretsManager\LocalJSONFileAdapter;

$manager = new \Secretary\Manager(
new AWSSecretsManagerAdapter(
new LocalJSONFileAdapter(
[
'region' => 'us-east-1',
'version' => '2017-10-17',
Expand All @@ -34,4 +34,4 @@
);

$manager->deleteSecret($fooSecret, ['ForceDeleteWithoutRecovery' => true]);
$manager->deleteSecret($bazSecret, ['ForceDeleteWithoutRecovery' => true]);
$manager->deleteSecret($bazSecret, ['ForceDeleteWithoutRecovery' => true]);
21 changes: 21 additions & 0 deletions src/Adapter/Local/JSONFile/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Aaron Scherer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
159 changes: 159 additions & 0 deletions src/Adapter/Local/JSONFile/LocalJSONFileAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
declare(strict_types=1);

/**
* @author Aaron Scherer <[email protected]>
* @date 2019
* @license http://opensource.org/licenses/MIT
*/


namespace Secretary\Adapter\AWS\SecretsManager;


use Secretary\Adapter\AbstractAdapter;
use Secretary\Exception\SecretNotFoundException;
use Secretary\Helper\ArrayHelper;
use Secretary\Secret;

/**
* Class AWSSecretsManagerAdapter
*
* @package Secretary\Adapter\Local\JSONFile
*/
class LocalJSONFileAdapter extends AbstractAdapter
{
/**
* @param Secret $secret
* @param Secret[] $secrets
*
* @return array
*/
private static function updateValue(Secret $secret, array $secrets): array
{
$keys = array_column($secrets, 'key');
$index = array_search($secret->getKey(), $keys, true);
if ($index === false || $index === null) {
$secrets[] = $secret;
} else {
$secrets[$index] = $secrets[$index]->withValue($secret->getValue());
}

return $secrets;
}

/**
* @var string
*/
private $secretsFile;

/**
* @var array
*/
private $jsonOptions;

/**
* LocalJSONFileAdapter constructor.
*
* @param array $config
*
* @throws \Exception
*/
public function __construct(array $config)
{
if (!isset($config)) {
throw new \Exception('Configuration is required.');
}

if (!isset($config['file'])) {
throw new \Exception('`file` is a required config.');
}
if (isset($config['jsonOptions'])) {
if (!is_array($config['jsonOptions'])) {
throw new \Exception('`jsonOptions` must be an array');
}
} else {
$config['jsonOptions'] = [JSON_PRETTY_PRINT];
}

$this->secretsFile = $config['file'];
$this->jsonOptions = $config['jsonOptions'];
}

/**
* {@inheritdoc}
* @throws \Exception
*/
public function getSecret(string $key, ?array $options = []): Secret
{
$secrets = $this->loadSecrets();
$keys = array_column($secrets, 'key');
$index = array_search($key, $keys, true);
if ($index === false || $index === null) {
throw new SecretNotFoundException($key);
}

return $secrets[$index];
}

/**
* {@inheritdoc}
*/
public function putSecret(Secret $secret, ?array $options = []): Secret
{
$secrets = LocalJSONFileAdapter::updateValue($secret, $this->loadSecrets());
$this->saveSecrets($secrets);

return $secret;
}

/**
* {@inheritdoc}
*/
public function deleteSecretByKey(string $key, ?array $options = []): void
{
$secrets = $this->loadSecrets();
$keys = array_column($secrets, 'key');
$index = array_search($key, $keys, true);
if ($index === false || $index === null) {
throw new SecretNotFoundException($key);
}

array_splice($secrets, $index, 1);
$this->saveSecrets($secrets);
}

/**
* {@inheritdoc}
* @throws SecretNotFoundException
*/
public function deleteSecret(Secret $secret, ?array $options = []): void
{
$this->deleteSecretByKey($secret->getKey(), $options);
}

/**
* @return Secret[]
* @throws \Exception
*/
private function loadSecrets(): array
{
$contents = file_get_contents($this->secretsFile);
$json = json_decode($contents, true, 512, [JSON_THROW_ON_ERROR]);

return array_map(
function (array $secret) {
return new Secret($secret['key'], $secret['value'], $secret['metadata'] ?? null);
},
$json
);
}

/**
* @param array $secrets
*/
private function saveSecrets(array $secrets)
{
file_put_contents($this->secretsFile, json_encode($secrets, $this->jsonOptions));
}
}
32 changes: 32 additions & 0 deletions src/Adapter/Local/JSONFile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Secretary - JSON File Adapter

JSON File Adapter for [Secretary](https://github.com/secretary/php)

## Table of Contents

1. [Installation](#installation)
2. [Options](#options)

### Installation

```bash
$ composer require secretary/core secretary/local-json-file-adapter
```

### Secrets File Structure

```json
[
{
"key": "my-secret-key",
"value": "some secret"
},
{
"key": "some-other-secret",
"value": {
"a": "b"
},
"metadata": {"foo": "bar"}
}
]
```
37 changes: 37 additions & 0 deletions src/Adapter/Local/JSONFile/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "secretary/local-json-file-adapter",
"description": "JSON File Adapter for Secretary",
"type": "library",
"license": "MIT",
"keywords": [
"secrets",
"json",
"secretary"
],
"authors": [
{
"name": "Aaron Scherer",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"require": {
"php": "^7.1.0",
"ext-json": "*",
"secretary/core": "self.version"
},
"require-dev": {
"mockery/mockery": "^1.2",
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
"Secretary\\Adapter\\Local\\JSONFile\\": ""
}
},
"autoload-dev": {
"psr-4": {
"Secretary\\Adapter\\Local\\JSONFile\\Tests\\": "Tests/"
}
}
}
10 changes: 5 additions & 5 deletions src/Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ Pass in your desired adapter.
```php
<?php
use Secretary\Manager;
use Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter;
use Secretary\Adapter\AWS\SecretsManager\LocalJSONFileAdapter;

$manager = new Manager(
new AWSSecretsManagerAdapter([
new LocalJSONFileAdapter([
'region' => 'us-east-1',
'credentials' => [
'accessKeyId' => 'myAccessKeyId',
Expand All @@ -78,14 +78,14 @@ Optionally, you may wrap your adapter, with one of the two cache adapters.
```php
<?php
use Secretary\Manager;
use Secretary\Adapter\AWS\SecretsManager\AWSSecretsManagerAdapter;
use Secretary\Adapter\AWS\SecretsManager\LocalJSONFileAdapter;

use Secretary\Adapter\Cache\PSR6Cache\PSR6CacheAdapter;
use Cache\Adapter\Apc\ApcCachePool;

$manager = new Manager(
new PSR6CacheAdapter(
new AWSSecretsManagerAdapter([
new LocalJSONFileAdapter([
'region' => 'us-east-1',
'credentials' => [
'accessKeyId' => 'myAccessKeyId',
Expand Down Expand Up @@ -228,4 +228,4 @@ print_r($secret->getValue());
[psr-16-cache-adapter]: https://github.com/secretary/php-psr-16-cache-adapter
[secretary-bundle]: https://github.com/secretary/php-secretary-bundle
[Secretary\Manager::class]: https://github.com/secretary/php/blob/master/src/Core/src/Manager.php
[Secretary\Secret::class]: https://github.com/secretary/php/blob/master/src/Core/src/Secret.php
[Secretary\Secret::class]: https://github.com/secretary/php/blob/master/src/Core/src/Secret.php

0 comments on commit 4f218e1

Please sign in to comment.