Skip to content

Commit 96b04fc

Browse files
committed
Throw exceptions if vault is down
1 parent 7b2c136 commit 96b04fc

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace security\credentials;
22

3+
use lang\IllegalAccessException;
34
use util\Secret;
45
use webservices\rest\Endpoint;
56

@@ -37,14 +38,20 @@ public function open() { return $this; }
3738
*
3839
* @param string $name
3940
* @return util.Secret
41+
* @throws lang.IllegalAccessException if vault backend fails
4042
*/
4143
public function named($name) {
42-
$response= $this->endpoint->resource('/v1/secret/'.$this->group)->get();
43-
if ($response->status() < 400) {
44-
$data= $response->value()['data'];
45-
return isset($data[$name]) ? new Secret($data[$name]) : null;
46-
} else {
47-
return null;
44+
$r= $this->endpoint->resource('/v1/secret/'.$this->group)->get();
45+
switch ($r->status()) {
46+
case 200:
47+
$data= $r->value()['data'];
48+
return isset($data[$name]) ? new Secret($data[$name]) : null;
49+
50+
case 404:
51+
return null;
52+
53+
default:
54+
throw new IllegalAccessException('Unexpected '.$r->status().': '.$r->error());
4855
}
4956
}
5057

@@ -55,12 +62,20 @@ public function named($name) {
5562
* @return iterable
5663
*/
5764
public function all($pattern) {
58-
$response= $this->endpoint->resource('/v1/secret/'.$this->group)->get();
59-
if ($response->status() < 400) {
60-
$match= substr($pattern, 0, strrpos($pattern, '*'));
61-
foreach ($response->value()['data'] as $name => $value) {
62-
if (0 === strncmp($name, $match, strlen($match))) yield $name => new Secret($value);
63-
}
65+
$r= $this->endpoint->resource('/v1/secret/'.$this->group)->get();
66+
switch ($r->status()) {
67+
case 200:
68+
$match= substr($pattern, 0, strrpos($pattern, '*'));
69+
foreach ($r->value()['data'] as $name => $value) {
70+
if (0 === strncmp($name, $match, strlen($match))) yield $name => new Secret($value);
71+
}
72+
return;
73+
74+
case 404:
75+
return;
76+
77+
default:
78+
throw new IllegalAccessException('Unexpected '.$r->status().': '.$r->error());
6479
}
6580
}
6681

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace security\credentials\unittest;
22

33
use io\streams\MemoryInputStream;
4-
use lang\FormatException;
4+
use lang\{FormatException, IllegalAccessException};
55
use peer\URL;
66
use peer\http\HttpResponse;
77
use security\credentials\FromVault;
@@ -79,6 +79,32 @@ public function fails_if_environment_variable_missing() {
7979
new FromVault();
8080
}
8181

82+
#[Test, Expect(IllegalAccessException::class)]
83+
public function named_on_vault_error() {
84+
$endpoint= newinstance(Endpoint::class, ['http://test'], [
85+
'execute' => function(RestRequest $request) {
86+
return newinstance(RestResponse::class, [503, 'Service unavailable'], [
87+
'error' => function($type= null) { return 'Database error'; }
88+
]);
89+
}
90+
]);
91+
92+
(new FromVault($endpoint))->named('credential');
93+
}
94+
95+
#[Test, Expect(IllegalAccessException::class)]
96+
public function all_on_vault_error() {
97+
$endpoint= newinstance(Endpoint::class, ['http://test'], [
98+
'execute' => function(RestRequest $request) {
99+
return newinstance(RestResponse::class, [503, 'Service unavailable'], [
100+
'error' => function($type= null) { return 'Database error'; }
101+
]);
102+
}
103+
]);
104+
105+
iterator_count((new FromVault($endpoint))->all('group*'));
106+
}
107+
82108
#[Test, Values(['map' => ['/' => '/', '/vendor/name' => '/vendor/name/', '/vendor/name/' => '/vendor/name/', 'vendor/name' => '/vendor/name/', 'vendor/name/' => '/vendor/name/',]])]
83109
public function using_group($group, $path) {
84110
$endpoint= newinstance(Endpoint::class, ['http://test'], [

0 commit comments

Comments
 (0)