@@ -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 }
0 commit comments