Skip to content

Commit 49d736d

Browse files
NopeCharonmaxlambrecht
authored andcommitted
fix(helper): fail KeyStoreHelper.run when the X.509 context watch fails
KeyStoreHelper's X.509 context watcher released the run latch and then threw a WatcherException from onError. DefaultWorkloadApiClient invokes watchers on gRPC executor threads, so the exception never reached the thread blocked in run: run returned normally, as if the KeyStore had been written. run(false) therefore reported success when the first X.509 context could not be fetched or stored, and the CLI, which uses run(true), exited with status 0 after a failed watch (for example PERMISSION_DENIED from the Workload API, or an update that could not be stored). The existing error test passed only because its stub calls onError synchronously on the caller's thread. Record the first error reported while run is waiting and throw it from run after the latch is released, so run fails with KeyStoreHelperException as documented and the CLI exits with status 1. Errors arriving after run has returned are only logged; previously the exception thrown from onError could also cancel the gRPC stream. Fixes spiffe#488 Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
1 parent ae84260 commit 49d736d

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎java-spiffe-helper/src/main/java/io/spiffe/helper/keystore/KeyStoreHelper.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525
import java.util.Objects;
2626
import java.util.concurrent.CountDownLatch;
27+
import java.util.concurrent.atomic.AtomicReference;
2728
import java.util.logging.Level;
2829
import java.util.logging.Logger;
2930

@@ -166,6 +167,7 @@ private static WorkloadApiClient createNewClient(final String spiffeSocketPath)
166167

167168
private void setX509ContextWatcher(boolean keepRunning) {
168169
countDownLatch = new CountDownLatch(1);
170+
AtomicReference<Throwable> watchError = new AtomicReference<>();
169171
workloadApiClient.watchX509Context(new Watcher<X509Context>() {
170172
@Override
171173
public void onUpdate(X509Context update) {
@@ -183,12 +185,21 @@ public void onUpdate(X509Context update) {
183185
@Override
184186
public void onError(Throwable e) {
185187
log.log(Level.SEVERE, e.getMessage());
186-
countDownLatch.countDown();
187-
throw new WatcherException("Error processing X.509 context update", e);
188+
// The Workload API client reports errors on its own threads, so throwing here would not reach the
189+
// caller of run. Record the error so that run fails with it once the latch is released.
190+
if (countDownLatch.getCount() > 0) {
191+
watchError.compareAndSet(null, e);
192+
countDownLatch.countDown();
193+
}
188194
}
189195
});
190196

191197
await(countDownLatch);
198+
199+
Throwable error = watchError.get();
200+
if (error != null) {
201+
throw new WatcherException("Error processing X.509 context update", error);
202+
}
192203
}
193204

194205
private void storeX509ContextUpdate(final X509Context update) throws KeyStoreException {

‎java-spiffe-helper/src/test/java/io/spiffe/helper/keystore/KeyStoreHelperTest.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.spiffe.bundle.x509bundle.X509Bundle;
44
import io.spiffe.bundle.x509bundle.X509BundleSet;
55
import io.spiffe.exception.SocketEndpointAddressException;
6+
import io.spiffe.exception.X509ContextException;
67
import io.spiffe.helper.exception.KeyStoreHelperException;
78
import io.spiffe.internal.CertificateUtils;
89
import io.spiffe.spiffeid.SpiffeId;
@@ -375,6 +376,30 @@ void testCreateKeyStoreHelper_throwsExceptionWhenNoUpdateCanBeFetched() throws E
375376
}
376377
}
377378

379+
@Test
380+
void testRun_errorReportedAsynchronously_throwsException() throws Exception {
381+
KeyStoreHelper.KeyStoreOptions options = getKeyStoreValidOptions(newAsyncErrorClient());
382+
try (KeyStoreHelper helper = KeyStoreHelper.create(options)) {
383+
helper.run(false);
384+
fail();
385+
} catch (KeyStoreHelperException e) {
386+
assertEquals("Error running KeyStoreHelper", e.getMessage());
387+
assertEquals("Testing exception", e.getCause().getCause().getMessage());
388+
}
389+
}
390+
391+
@Test
392+
void testRun_keepRunning_errorReportedAsynchronously_throwsException() throws Exception {
393+
KeyStoreHelper.KeyStoreOptions options = getKeyStoreValidOptions(newAsyncErrorClient());
394+
try (KeyStoreHelper helper = KeyStoreHelper.create(options)) {
395+
helper.run(true);
396+
fail();
397+
} catch (KeyStoreHelperException e) {
398+
assertEquals("Error running KeyStoreHelper", e.getMessage());
399+
assertEquals("Testing exception", e.getCause().getCause().getMessage());
400+
}
401+
}
402+
378403
@Test
379404
void keyStoreHelperOptions_allNull() {
380405
try {
@@ -439,6 +464,16 @@ void keyStoreHelperOptions_keyPassNull() {
439464
}
440465

441466

467+
// Reports the error from another thread, as the Workload API client does for watch errors.
468+
private static WorkloadApiClient newAsyncErrorClient() {
469+
return new WorkloadApiClientErrorStub() {
470+
@Override
471+
public void watchX509Context(Watcher<X509Context> watcher) {
472+
new Thread(() -> watcher.onError(new X509ContextException("Testing exception"))).start();
473+
}
474+
};
475+
}
476+
442477
private KeyStoreHelper.KeyStoreOptions getKeyStoreValidOptions(WorkloadApiClient workloadApiClient) {
443478
String keyStorefileName = RandomStringUtils.randomAlphabetic(10);
444479
keyStoreFilePath = Paths.get(keyStorefileName);

0 commit comments

Comments
 (0)