-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(adapter): Adding new JSON File Adapter
- Loading branch information
1 parent
2c13a6c
commit 4f218e1
Showing
7 changed files
with
286 additions
and
36 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
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": {} | ||
} |
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,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. |
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,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)); | ||
} | ||
} |
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,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"} | ||
} | ||
] | ||
``` |
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,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/" | ||
} | ||
} | ||
} |
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