Skip to content

Commit a0cce43

Browse files
committed
Releasing 1.0.6
2 parents bcf227c + a923183 commit a0cce43

File tree

14 files changed

+87
-29
lines changed

14 files changed

+87
-29
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ composer install technicalguru/vault
1515
## By Package Download
1616
You can download the source code packages from [GitHub Release Page](https://github.com/technicalguru/php-vault/releases)
1717

18+
# Hashicorp Setup
19+
The procedure is best described at [Hashicorp Blog](https://www.hashicorp.com/blog/authenticating-applications-with-vault-approle). It describes
20+
how to create an `approle`. Here is the essence of it:
21+
22+
```
23+
# Enable the auth method for approle
24+
vault auth enable approle
25+
26+
# Create a file with your policy on the respective secret path:
27+
cat 'path "secret/my-secret" { capabilities = ["read", "list"] }' >app-policy.hcl
28+
29+
# Create the policy
30+
vault policy write my-app-policy app-policy.hcl
31+
32+
# Create the approle
33+
vault write auth/approle/role/my-approle secret_id_ttl=120m token_ttl=60m token_max_tll=120m policies="my-app-policy"
34+
35+
# Get the role ID printed
36+
vault read auth/approle/role/my-approle/role-id
37+
38+
# Create the secret ID and print it
39+
vault write -f auth/approle/role/my-approle/secret-id
40+
```
41+
1842
# Examples
1943
## Create a HashicorpVault
2044
Please note that this vault is actually a client to an existing Hashicorp Vault.
@@ -107,8 +131,8 @@ The secrets file (JSON) shall look like this:
107131

108132
```
109133
try {
110-
$mySecret1 = $vault->get('my/secret/number/1');
111-
$mySecret2 = $vault->get('my/secret/number/2');
134+
$mySecret1 = $vault->getSecret('my/secret/number/1');
135+
$mySecret2 = $vault->getSecret('my/secret/number/2');
112136
} catch (\TgVault\VaultException $e) {
113137
// secret was not found
114138
}

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525
"TgVault\\": "src/TgVault/"
2626
}
2727
},
28+
"extra": {
29+
"branch-alias": {
30+
"dev-master": "1.0-dev"
31+
}
32+
},
2833
"require-dev": {
2934
"phpunit/phpunit": "^9"
3035
}
31-
}
36+
}

src/TgVault/BaseVault.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct($logger = NULL) {
2828
* @return Secret
2929
* @throws VaultException when the secret cannot be found or retrieved.
3030
*/
31-
public function getSecret(string $path) {
31+
public function getSecret($path) {
3232
throw new VaultException(get_class().'::getSecret() must be implemented.', VAULT_ERR_INTERNAL);
3333
}
3434

@@ -47,7 +47,7 @@ public function setLogger($logger) {
4747
* @param $s - the string to be logged
4848
* @param $object - the object to be logged
4949
*/
50-
protected function debug(string $s, $object = NULL) {
50+
protected function debug($s, $object = NULL) {
5151
if ($this->logger != NULL) {
5252
$object = self::cleanObject($object);
5353
$psrInterface = '\\Psr\\Log\\LoggerInterface';
@@ -64,7 +64,7 @@ protected function debug(string $s, $object = NULL) {
6464
* @param $s - the string to be logged
6565
* @param $object - the object to be logged
6666
*/
67-
protected function warn(string $s, $object = NULL) {
67+
protected function warn($s, $object = NULL) {
6868
if ($this->logger != NULL) {
6969
$object = self::cleanObject($object);
7070
$psrInterface = '\\Psr\\Log\\LoggerInterface';
@@ -81,7 +81,7 @@ protected function warn(string $s, $object = NULL) {
8181
* @param $s - the string to be logged
8282
* @param $object - the object to be logged
8383
*/
84-
protected function info(string $s, $object = NULL) {
84+
protected function info($s, $object = NULL) {
8585
if ($this->logger != NULL) {
8686
$object = self::cleanObject($object);
8787
$psrInterface = '\\Psr\\Log\\LoggerInterface';
@@ -98,7 +98,7 @@ protected function info(string $s, $object = NULL) {
9898
* @param $s - the string to be logged
9999
* @param $object - the object to be logged
100100
*/
101-
protected function error(string $s, $object = NULL) {
101+
protected function error($s, $object = NULL) {
102102
if ($this->logger != NULL) {
103103
$object = self::cleanObject($object);
104104
$psrInterface = '\\Psr\\Log\\LoggerInterface';

src/TgVault/CredentialsProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CredentialsProvider extends SecretProvider implements \TgUtils\Auth\Creden
2424
* @param string $passwordKey - the key in the secret holding the password (default is 'password')
2525
* @throws VaultException when vault or path are NULL
2626
*/
27-
public function __construct(Vault $vault, string $path, string $usernameKey = NULL, string $passwordKey = NULL) {
27+
public function __construct($vault, $path, $usernameKey = NULL, $passwordKey = NULL) {
2828
parent::__construct($vault, $path);
2929
if (($usernameKey == NULL) || (trim($usernameKey) == '')) $usernameKey = 'username';
3030
if (($passwordKey == NULL) || (trim($passwordKey) == '')) $passwordKey = 'password';

src/TgVault/File/FileVault.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function load() {
6262
* @return Secret
6363
* @throws VaultException when the secret cannot be found or retrieved.
6464
*/
65-
public function getSecret(string $path) {
65+
public function getSecret($path) {
6666
$this->load();
6767
if (!isset($this->secrets[$path])) {
6868
throw new VaultException('Secret not available', VAULT_ERR_NOT_FOUND);

src/TgVault/Hashicorp/Cache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Cache {
2121
* @param string $cacheFile - where the cache is located in filesystem.
2222
* @param object $logger - a logger, either TgVault\Logger or Psr\Log\LoggerInterface
2323
*/
24-
public function __construct(string $cacheFile, $logger = NULL) {
24+
public function __construct($cacheFile, $logger = NULL) {
2525
$this->cacheFile = $cacheFile;
2626
$this->logger = $logger;
2727
}
@@ -64,7 +64,7 @@ protected function save() {
6464
* @param string $key - the key in the cache.
6565
* @return mixed - the data from the cache or NULL if not available.
6666
*/
67-
public function get(string $key) {
67+
public function get($key) {
6868
$this->load();
6969
if (isset($this->data->$key)) {
7070
return $this->data->$key;
@@ -77,7 +77,7 @@ public function get(string $key) {
7777
* @param string $key - the key in the cache.
7878
* @param mixed $value - the value to be stored.
7979
*/
80-
public function set(string $key, $value) {
80+
public function set($key, $value) {
8181
$this->load();
8282
$this->data->$key = $value;
8383
$this->save();

src/TgVault/Hashicorp/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private function check($valueKey, $errorMessage) {
7878
* @param string $roleId - the role ID in vault
7979
* @param string $secretId - the secret ID of the client
8080
*/
81-
public function setVaultCredentials(string $roleId, string $secretId) {
81+
public function setVaultCredentials($roleId, $secretId) {
8282
$this->roleId = $roleId;
8383
$this->secretId = $secretId;
8484
$this->check('roleId', 'Vault AppRole ID not set');

src/TgVault/Hashicorp/HashicorpVault.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class HashicorpVault extends BaseVault implements Vault {
2121
protected $isTls;
2222
protected $config;
2323
protected $lastResult;
24+
private $loggedToken;
2425
private $cache;
2526
private $token;
2627
private $secrets;
@@ -33,9 +34,10 @@ class HashicorpVault extends BaseVault implements Vault {
3334
public function __construct($config, $logger = NULL) {
3435
parent::__construct($logger);
3536
if ($config == NULL) throw new VaultException('Configuration must be set', VAULT_ERR_CONFIG_EMPTY);
36-
$this->config = new Config($config);
37-
$this->isTls = substr($this->config->uri, 0, 5) == 'https';
38-
$this->cache = new Cache($this->config->cacheFile, $logger);
37+
$this->config = new Config($config);
38+
$this->isTls = substr($this->config->uri, 0, 5) == 'https';
39+
$this->cache = new Cache($this->config->cacheFile, $logger);
40+
$this->loggedToken = FALSE;
3941
}
4042

4143
/**
@@ -52,18 +54,27 @@ public function removeToken() {
5254
* @return Secret
5355
* @throws VaultException when the secret cannot be found or retrieved.
5456
*/
55-
public function getSecret(string $path) {
57+
public function getSecret($path) {
5658
if (!isset($this->secrets[$path])) {
5759
$this->getToken();
5860
$rc = $this->GET($path);
5961
if (($rc->error == 0) && ($rc->http_code == 200) && is_object($rc->data->data)) {
60-
$this->secrets[$path] = new Secret($rc->data->data);
62+
// It's unclear why some vaults do answer with one level less (without metadata)
63+
if (isset($rc->data->data->data)) {
64+
$this->secrets[$path] = new Secret($rc->data->data);
65+
} else {
66+
$this->secrets[$path] = new Secret($rc->data);
67+
}
6168
} else {
6269
$this->secrets[$path] = $rc;
6370
}
6471
}
6572

66-
if (get_class($this->secrets[$path]) != 'TgVault\\Secret') throw new VaultException('Secret not available', VAULT_ERR_SECRET);
73+
if (get_class($this->secrets[$path]) != 'TgVault\\Secret') {
74+
$ex = new VaultException('Secret not available', VAULT_ERR_SECRET);
75+
$ex->setDetails($this->secrets[$path]);
76+
throw $ex;
77+
}
6778
return $this->secrets[$path];
6879
}
6980

@@ -175,7 +186,7 @@ protected function getToken() {
175186

176187
if (($this->token != NULL) && !$this->loggedToken) {
177188
$this->info('Using token: '.$this->token->getInfo());
178-
$this->loggedToken = true;
189+
$this->loggedToken = TRUE;
179190
}
180191

181192
return $this->token;
@@ -339,6 +350,7 @@ protected function request($curl, $path, $additionalHeaders = array()) {
339350
}
340351
}
341352
***********************************/
353+
$additionalHeaders[] = 'X-Vault-Request: true';
342354
if (($this->token != NULL) && isset($this->token->client_token)) {
343355
$additionalHeaders[] = 'X-Vault-Token: '.$this->token->client_token;
344356
}

src/TgVault/Memory/MemoryVault.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct($config, $logger = NULL) {
4242
* @return Secret
4343
* @throws VaultException when the secret cannot be found or retrieved.
4444
*/
45-
public function getSecret(string $path) {
45+
public function getSecret($path) {
4646
if (!isset($this->secrets[$path])) {
4747
throw new VaultException('Secret not available', VAULT_ERR_NOT_FOUND);
4848
}

src/TgVault/Secret.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct($data) {
3737
* @param string $key - the key of the value to be retrieved.
3838
* @return string the value or NULL if not set.
3939
*/
40-
public function get(string $key) {
40+
public function get($key) {
4141
if (isset($this->data->$key)) return $this->data->$key;
4242
return NULL;
4343
}

0 commit comments

Comments
 (0)