-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeepManager.php
More file actions
executable file
·185 lines (149 loc) · 4.97 KB
/
KeepManager.php
File metadata and controls
executable file
·185 lines (149 loc) · 4.97 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace STS\Keep;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use STS\Keep\Data\Collections\VaultConfigCollection;
use STS\Keep\Data\Settings;
use STS\Keep\Services\LocalStorage;
use STS\Keep\Vaults\AbstractVault;
use STS\Keep\Vaults\AwsSecretsManagerVault;
use STS\Keep\Vaults\AwsSsmVault;
class KeepManager
{
protected array $availableVaults = [
AwsSsmVault::class,
AwsSecretsManagerVault::class,
];
protected array $loadedVaults = [];
public function __construct(protected ?Settings $settings, protected VaultConfigCollection $configuredVaults) {}
public function isInitialized(): bool
{
return $this->settings !== null;
}
public function getNamespace(): string
{
return $this->settings?->namespace() ?? 'default';
}
public function getSettings(): array
{
return $this->settings?->toArray() ?? [];
}
public function getSetting(string $key, mixed $default = null): mixed
{
return $this->settings?->get($key, $default) ?? $default;
}
public function getAvailableVaults(): array
{
return $this->availableVaults;
}
public function getConfiguredVaults(): Collection
{
return $this->filterByWorkspace($this->configuredVaults);
}
/**
* Get all configured vaults without workspace filtering
*/
public function getAllConfiguredVaults(): Collection
{
return $this->configuredVaults;
}
public function getVaultConfig(string $vaultName): ?\STS\Keep\Data\VaultConfig
{
return $this->configuredVaults->get($vaultName);
}
public function getDefaultVault(): ?string
{
return $this->settings?->defaultVault();
}
public function getEnvs(): array
{
$allEnvs = $this->getAllEnvs();
return $this->filterEnvsByWorkspace($allEnvs);
}
/**
* Get all envs without workspace filtering
*/
public function getAllEnvs(): array
{
if (!$this->isInitialized()) {
return [];
}
return $this->settings->envs();
}
public function vault(string $name, string $env): AbstractVault
{
$cacheKey = "{$name}:{$env}";
if (isset($this->loadedVaults[$cacheKey])) {
return $this->loadedVaults[$cacheKey];
}
if (! $this->configuredVaults->has($name)) {
throw new \InvalidArgumentException("Vault '{$name}' is not configured.");
}
$config = $this->configuredVaults->get($name);
$driver = $config->driver();
if (! $driver) {
throw new \InvalidArgumentException("Vault '{$name}' does not have a driver configured.");
}
if (! $driverClass = $this->driverClass($driver)) {
throw new \InvalidArgumentException("Vault driver '{$driver}' for '{$name}' is not available.");
}
if (!$driverClass::isAvailable()) {
throw new \RuntimeException(
"Vault driver '{$driver}' requires the AWS SDK. Install it with: composer require aws/aws-sdk-php"
);
}
$vault = new $driverClass($name, $config->toArray(), $env);
$this->loadedVaults[$cacheKey] = $vault;
return $vault;
}
protected function driverClass($name): ?string
{
return Arr::first($this->availableVaults, fn ($class) => $class::DRIVER === $name);
}
/**
* Add a vault driver class to available vaults (useful for testing)
*/
public function addVaultDriver(string $driverClass): static
{
$this->availableVaults[] = $driverClass;
return $this;
}
/**
* Clear the vault cache - useful for testing
*/
public function clearVaultCache(): static
{
$this->loadedVaults = [];
return $this;
}
/**
* Filter vaults based on workspace configuration
*/
protected function filterByWorkspace(Collection $vaults): Collection
{
$localStorage = new LocalStorage();
$workspace = $localStorage->getWorkspace();
// If no workspace is configured, return all vaults
if (empty($workspace) || empty($workspace['active_vaults'])) {
return $vaults;
}
// Filter to only include active vaults
return $vaults->filter(function ($vault) use ($workspace) {
return in_array($vault->slug(), $workspace['active_vaults']);
});
}
/**
* Filter envs based on workspace configuration
*/
protected function filterEnvsByWorkspace(array $envs): array
{
$localStorage = new LocalStorage();
$workspace = $localStorage->getWorkspace();
// If no workspace is configured, return all envs
if (empty($workspace) || empty($workspace['active_envs'])) {
return $envs;
}
// Filter to only include active envs
return array_values(array_intersect($envs, $workspace['active_envs']));
}
}