Skip to content

Commit 37fb491

Browse files
committed
Add optional "group" parameter to FromVault
1 parent c582b4e commit 37fb491

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ Via the `FromVault` class. Credentials are read from the backend mounted at `/se
4949
```php
5050
use security\credentials\{Credentials, FromVault};
5151

52-
$credentials= new Credentials(new FromVault('http://127.0.0.1:8200', '72698676-4988-94a4-...'));
52+
// Set token to NULL to use VAULT_TOKEN from environment
53+
$token= '72698676-4988-94a4-...';
54+
55+
$credentials= new Credentials(new FromVault('http://127.0.0.1:8200', $token));
5356
$secret= $credentials->named('ldap_password'); // Reads ldap_password key from /secret
54-
$secret= $credentials->named('vendor/name/mysql'); // Reads mysql key from /secret/vendor/name
57+
58+
$credentials= new Credentials(new FromVault('http://127.0.0.1:8200', $token, 'vendor/name'));
59+
$secret= $credentials->named('mysql'); // Reads mysql key from /secret/vendor/name
5560
```
5661

5762
### KeePass databases

src/main/php/security/credentials/FromVault.class.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
use webservices\rest\Endpoint;
55

66
class FromVault implements Secrets {
7-
private $endpoint;
7+
private $endpoint, $group;
88

99
/**
1010
* Creates a secrets source which reads credentials from a running vault service
1111
*
1212
* @param string|peer.URL|webservices.rest.Endpoint $endpoint If omitted, defaults to `VAULT_ADDR` environment variable
1313
* @param string $token If omitted, defaults to `VAULT_TOKEN` environment variable
14+
* @param string $group The secret group, e.g. "/vendor/name"
1415
*/
15-
public function __construct($endpoint= null, $token= null) {
16+
public function __construct($endpoint= null, $token= null, $group= '/') {
1617
if ($endpoint instanceof Endpoint) {
1718
$this->endpoint= $endpoint;
1819
} else {
@@ -22,6 +23,8 @@ public function __construct($endpoint= null, $token= null) {
2223
if ($header= $token ?: getenv('VAULT_TOKEN')) {
2324
$this->endpoint->with('X-Vault-Token', $header);
2425
}
26+
27+
$this->group= '/' === $group ? '' : trim($group, '/').'/';
2528
}
2629

2730
/** @return self */
@@ -35,7 +38,7 @@ public function open() { return $this; }
3538
*/
3639
public function named($name) {
3740
$p= strrpos($name, '/');
38-
$response= $this->endpoint->resource('/v1/secret/'.substr($name, 0, $p))->get();
41+
$response= $this->endpoint->resource('/v1/secret/'.$this->group.substr($name, 0, $p))->get();
3942
if ($response->status() < 400) {
4043
$data= $response->value()['data'];
4144
$key= ltrim(substr($name, $p), '/');
@@ -54,7 +57,7 @@ public function named($name) {
5457
public function all($pattern) {
5558
$p= strrpos($pattern, '/');
5659
$group= substr($pattern, 0, $p);
57-
$response= $this->endpoint->resource('/v1/secret/'.$group)->get();
60+
$response= $this->endpoint->resource('/v1/secret/'.$this->group.$group)->get();
5861
if ($response->status() < 400) {
5962
$key= ltrim(substr($pattern, $p), '/');
6063
$match= substr($key, 0, strrpos($key, '*'));

src/test/php/security/credentials/unittest/FromVaultTest.class.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class FromVaultTest extends AbstractSecretsTest {
3131
],
3232
'all_in_subfolder' => [
3333
['data' => ['mysql' => 'test']],
34+
],
35+
'using_group' => [
36+
['data' => ['credential' => 'test']],
3437
]
3538
];
3639

@@ -65,6 +68,11 @@ public function can_create_with_token() {
6568
new FromVault('http://vault:8200', 'SECRET_VAULT_TOKEN');
6669
}
6770

71+
#[@test]
72+
public function can_create_with_token_and_group() {
73+
new FromVault('http://vault:8200', 'SECRET_VAULT_TOKEN', '/vendor/name');
74+
}
75+
6876
#[@test]
6977
public function uses_environment_variable_by_default() {
7078
putenv('VAULT_ADDR=http://127.0.0.1:8200');
@@ -76,4 +84,23 @@ public function fails_if_environment_variable_missing() {
7684
putenv('VAULT_ADDR=');
7785
new FromVault();
7886
}
87+
88+
#[@test, @values(map= [
89+
# '/' => '/',
90+
# '/vendor/name' => '/vendor/name/',
91+
# '/vendor/name/' => '/vendor/name/',
92+
# 'vendor/name' => '/vendor/name/',
93+
# 'vendor/name/' => '/vendor/name/',
94+
#])]
95+
public function using_group($group, $path) {
96+
$endpoint= newinstance(Endpoint::class, ['http://test'], [
97+
'execute' => function(RestRequest $request) use(&$requested) {
98+
$requested= $request->path();
99+
return new RestResponse(404, 'Not found');
100+
}
101+
]);
102+
103+
(new FromVault($endpoint, 'SECRET_VAULT_TOKEN', $group))->named('credential');
104+
$this->assertEquals('/v1/secret'.$path, $requested);
105+
}
79106
}

0 commit comments

Comments
 (0)