-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashicorpVaultAdapter.php
76 lines (62 loc) · 1.87 KB
/
HashicorpVaultAdapter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
/*
* @author Aaron Scherer <[email protected]>
* @date 2019
* @license https://opensource.org/licenses/MIT
*/
namespace Secretary\Adapter\Hashicorp\Vault;
use GuzzleHttp\Client as GuzzleClient;
use Secretary\Adapter\AbstractAdapter;
use Secretary\Adapter\Hashicorp\Vault\Client\Client;
use Secretary\Secret;
/**
* Class HashicorpVaultAdapter.
*
* @package Secretary\Adapter\Hashicorp\Vault
*/
class HashicorpVaultAdapter extends AbstractAdapter
{
private Client $client;
/**
* @throws \Exception
*/
public function __construct(array $config = [])
{
if (!class_exists(GuzzleClient::class)) {
throw new \Exception('guzzlehttp/guzzle is required to use the HashicorpVaultAdapter');
}
$this->client = new Client($config);
}
/**
* {@inheritdoc}
*/
public function getSecret(string $key, ?array $options = []): Secret
{
$response = $this->client->getClient()->get('/v1/secret/'.$key);
$json = json_decode($response->getBody()->getContents(), true);
return new Secret($key, $json['data']);
}
/**
* {@inheritdoc}
*/
public function putSecret(Secret $secret, ?array $options = []): Secret
{
if (!is_array($secret->getValue())) {
throw new \Exception('Value for this adapter must be a key/value array');
}
$this->client->getClient()->post('/v1/secret/'.$secret->getKey(), ['json' => $secret->getValue()]);
return $secret;
}
/**
* {@inheritdoc}
*/
public function deleteSecret(Secret $secret, ?array $options = []): void
{
$this->deleteSecretByKey($secret->getKey(), $options);
}
public function deleteSecretByKey(string $key, ?array $options = []): void
{
$this->client->getClient()->delete('/v1/secret/'.$key);
}
}