-
Notifications
You must be signed in to change notification settings - Fork 0
/
AWSSecretsManagerAdapter.php
152 lines (123 loc) · 4.24 KB
/
AWSSecretsManagerAdapter.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
/*
* @author Aaron Scherer <[email protected]>
* @date 2019
* @license https://opensource.org/licenses/MIT
*/
namespace Secretary\Adapter\AWS\SecretsManager;
use Aws\SecretsManager\Exception\SecretsManagerException;
use Aws\SecretsManager\SecretsManagerClient;
use Secretary\Adapter\AbstractAdapter;
use Secretary\Exception\SecretNotFoundException;
use Secretary\Helper\ArrayHelper;
use Secretary\Secret;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Class AWSSecretsManagerAdapter.
*
* @package Secretary\Adapter\AWS\SecretsManager
*/
class AWSSecretsManagerAdapter extends AbstractAdapter
{
private ?SecretsManagerClient $client = null;
private array $config;
/**
* @throws \Exception
*/
public function __construct(array $config)
{
if (!class_exists(SecretsManagerClient::class)) {
throw new \Exception('aws/aws-sdk-php is required to use the AWSSecretsManagerAdapter');
}
$this->config = $config;
}
/**
* {@inheritdoc}
*/
public function getSecret(string $key, ?array $options = []): Secret
{
$options['SecretId'] = $key;
try {
$data = $this->getClient()->getSecretValue($options);
/** @var string $secretString */
$secretString = $data->get('SecretString');
return new Secret(
$key,
static::isJson($secretString) ? json_decode($data->get('SecretString'), true) : $secretString
);
} catch (SecretsManagerException $exception) {
$awsMsg = $exception->getAwsErrorMessage() ?? '';
if (str_contains($awsMsg, 'Secrets Manager can’t find the specified secret')) {
throw new SecretNotFoundException($key, $exception);
}
throw $exception;
}
}
/**
* {@inheritdoc}
*/
public function putSecret(Secret $secret, ?array $options = []): Secret
{
$options['SecretString'] = is_array($secret->getValue())
? json_encode($secret->getValue()) : $secret->getValue();
try {
$options = ArrayHelper::without($options, 'Tags');
$options['SecretId'] = $secret->getKey();
$this->getClient()->updateSecret($options);
} catch (\Exception $e) {
$options['Name'] = $secret->getKey();
$this->getClient()->createSecret($options);
}
return $secret;
}
/**
* {@inheritdoc}
*/
public function deleteSecretByKey(string $key, ?array $options = []): void
{
$options['SecretId'] = $key;
$this->getClient()->deleteSecret($options);
}
/**
* {@inheritdoc}
*/
public function deleteSecret(Secret $secret, ?array $options = []): void
{
$this->deleteSecretByKey($secret->getKey(), $options);
}
public function configureGetSecretOptions(OptionsResolver $resolver): void
{
parent::configureSharedOptions($resolver);
$resolver->setDefined(['VersionId', 'VersionStage'])
->setAllowedTypes('VersionId', 'string')
->setAllowedTypes('VersionStage', 'string');
}
public function configurePutSecretOptions(OptionsResolver $resolver): void
{
parent::configureSharedOptions($resolver);
$resolver->setDefined(['KmsKeyId', 'Tags', 'Description'])
->setAllowedTypes('KmsKeyId', 'string')
->setAllowedTypes('Description', 'string');
}
public function configureDeleteSecretOptions(OptionsResolver $resolver): void
{
parent::configureDeleteSecretOptions($resolver);
$resolver
->setDefined(['ForceDeleteWithoutRecovery', 'RecoveryWindowInDays'])
->setAllowedTypes('ForceDeleteWithoutRecovery', 'bool')
->setAllowedTypes('RecoveryWindowInDays', 'int');
}
private function getClient(): SecretsManagerClient
{
if (!$this->client instanceof SecretsManagerClient) {
$this->client = new SecretsManagerClient($this->config);
}
return $this->client;
}
private static function isJson(string $str)
{
$json = json_decode($str);
return $json && $str != $json;
}
}