Skip to content

Commit ecf16f3

Browse files
committed
Also accept util.Secret instances in FromVault constructor
1 parent d81e805 commit ecf16f3

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Via the `FromVault` class. Credentials are read from the backend mounted at `/se
4949
use security\credentials\{Credentials, FromVault};
5050

5151
// Set token to NULL to use VAULT_TOKEN from environment
52-
$token= '72698676-4988-94a4-...';
52+
$token= new Secret('72698676-4988-94a4-...');
5353

5454
$credentials= new Credentials(new FromVault('http://127.0.0.1:8200', $token));
5555
$secret= $credentials->named('ldap_password'); // Reads ldap_password key from /secret

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class FromVault implements Secrets {
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
13-
* @param string $token If omitted, defaults to `VAULT_TOKEN` environment variable
13+
* @param string|util.Secret $token If omitted, defaults to `VAULT_TOKEN` environment variable
1414
* @param string $group The secret group, e.g. "/vendor/name"
1515
*/
1616
public function __construct($endpoint= null, $token= null, $group= '/') {
@@ -20,7 +20,9 @@ public function __construct($endpoint= null, $token= null, $group= '/') {
2020
$this->endpoint= new Endpoint($endpoint ?: getenv('VAULT_ADDR'));
2121
}
2222

23-
if ($header= $token ?: getenv('VAULT_TOKEN')) {
23+
if ($token instanceof Secret) {
24+
$this->endpoint->with('X-Vault-Token', $token->reveal());
25+
} else if ($header= $token ?: getenv('VAULT_TOKEN')) {
2426
$this->endpoint->with('X-Vault-Token', $header);
2527
}
2628

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@ class FromVaultTest extends AbstractSecretsTest {
2626
['data' => ['test_db_password' => 'db', 'test_ldap_password' => 'ldap']],
2727
['data' => ['prod_master_key' => 'master']]
2828
],
29-
'from_subfolder' => [
30-
['data' => ['mysql' => 'test']],
31-
],
32-
'all_in_subfolder' => [
33-
['data' => ['mysql' => 'test']],
34-
],
35-
'using_group' => [
36-
['data' => ['credential' => 'test']],
37-
]
3829
];
3930

4031
/** @return security.vault.Secrets */
@@ -63,9 +54,9 @@ public function can_create_with($arg) {
6354
new FromVault($arg);
6455
}
6556

66-
#[@test]
67-
public function can_create_with_token() {
68-
new FromVault('http://vault:8200', 'SECRET_VAULT_TOKEN');
57+
#[@test, @values(['secret', new Secret('for-vault')])]
58+
public function can_create_with_token($token) {
59+
new FromVault('http://vault:8200', $token);
6960
}
7061

7162
#[@test]

0 commit comments

Comments
 (0)