diff --git a/CHANGELOG.md b/CHANGELOG.md index 8922d30f..a5a1b917 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixed +* Reject an X.509 Context update in `DefaultX509Source` when the `svidPicker` returns null or throws. The source kept a null SVID, so `getX509Svid()` returned null, and an exception from the picker escaped the watcher and cancelled the watch, so later rotations were never applied. The current SVID and bundles are now kept, and `newSource` fails if the first update is rejected * Honor sub-second initialization timeouts in `DefaultX509Source`, `DefaultJwtSource` and `CachedJwtSource`. The `initTimeout` option and the `spiffe.newX509Source.timeout` and `spiffe.newJwtSource.timeout` system properties were truncated to whole seconds, so a timeout below one second failed `newSource` immediately * Fail `newSource` with `X509SourceException` or `JwtSourceException`, and keep the thread's interrupt status, when the calling thread is interrupted while waiting for the initial update. Without a timeout, `newSource` used to return a source that had no SVID or bundles yet diff --git a/java-spiffe-core/src/main/java/io/spiffe/workloadapi/DefaultX509Source.java b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/DefaultX509Source.java index 031a3c1d..e26e7f8a 100644 --- a/java-spiffe-core/src/main/java/io/spiffe/workloadapi/DefaultX509Source.java +++ b/java-spiffe-core/src/main/java/io/spiffe/workloadapi/DefaultX509Source.java @@ -6,6 +6,7 @@ import io.spiffe.exception.BundleNotFoundException; import io.spiffe.exception.SocketEndpointAddressException; import io.spiffe.exception.WatcherException; +import io.spiffe.exception.X509ContextException; import io.spiffe.exception.X509SourceException; import io.spiffe.spiffeid.TrustDomain; import io.spiffe.svid.x509svid.X509Svid; @@ -232,7 +233,14 @@ private void setX509ContextWatcher(final CountDownLatch done, final AtomicRefere public void onUpdate(final X509Context update) { String spiffeIds = update.getX509Svids().stream().map(s -> s.getSpiffeId().toString()).collect(Collectors.joining(", ")); log.log(Level.INFO, String.format("Received X509Context update: %s", spiffeIds)); - setX509Context(update); + try { + setX509Context(update); + } catch (RuntimeException e) { + // Rejected like a malformed update: an exception escaping here would cancel the gRPC watch, + // and later rotations would never be applied. + onError(new X509ContextException("Error applying X.509 Context update", e)); + return; + } done.countDown(); } @@ -257,6 +265,9 @@ private void setX509Context(final X509Context update) { } else { svidUpdate = picker.apply(update.getX509Svids()); } + if (svidUpdate == null) { + throw new IllegalStateException("No X.509 SVID was selected from the X.509 Context update"); + } X509BundleSet bundleSet = update.getX509BundleSet(); if (bundleSet == null) { @@ -281,7 +292,8 @@ private boolean isClosed() { * from the System property `spiffe.newX509Source.timeout'. If this is also not defined, no default timeout is applied. *
* svidPicker Function to choose the X.509 SVID from the list returned by the Workload API.
- * If it is not set, the default SVID is picked.
+ * If it is not set, the default SVID is picked. An update for which the picker returns null, or throws, is
+ * rejected: the source keeps its current SVID and bundles, or fails to initialize if it is the first update.
*
* workloadApiClient A custom instance of a {@link WorkloadApiClient}, if it is not set, a new client
* will be created.
diff --git a/java-spiffe-core/src/test/java/io/spiffe/workloadapi/DefaultX509SourceTest.java b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/DefaultX509SourceTest.java
index 30cfc643..7222930f 100644
--- a/java-spiffe-core/src/test/java/io/spiffe/workloadapi/DefaultX509SourceTest.java
+++ b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/DefaultX509SourceTest.java
@@ -26,8 +26,10 @@
import java.time.Duration;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
import static io.spiffe.utils.TestUtils.toUri;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -129,6 +131,48 @@ void testX509ContextWatcherError_afterInit_keepsX509ContextAndAppliesLaterUpdate
assertSame(updatedBundle, x509Source.getBundleForTrustDomain(trustDomain));
}
+ @Test
+ void testPickerReturnsNull_afterInit_keepsX509ContextAndAppliesLaterUpdates() throws Exception {
+ AtomicBoolean pickNothing = new AtomicBoolean(false);
+ DefaultX509Source.X509SourceOptions options = DefaultX509Source.X509SourceOptions
+ .builder()
+ .workloadApiClient(workloadApiClient)
+ .svidPicker(list -> pickNothing.get() ? null : list.get(0))
+ .build();
+ TrustDomain trustDomain = TrustDomain.parse("example.org");
+
+ try (DefaultX509Source source = DefaultX509Source.newSource(options)) {
+ X509Svid initialSvid = source.getX509Svid();
+ X509Bundle initialBundle = source.getBundleForTrustDomain(trustDomain);
+ X509Bundle updatedBundle = new X509Bundle(trustDomain);
+ X509Context update = X509Context.of(Collections.singletonList(initialSvid), X509BundleSet.of(Collections.singleton(updatedBundle)));
+
+ // The picker selects nothing: the update is rejected as a whole and does not escape the watcher.
+ pickNothing.set(true);
+ assertDoesNotThrow(() -> workloadApiClient.x509ContextWatcher.onUpdate(update));
+ assertSame(initialSvid, source.getX509Svid());
+ assertSame(initialBundle, source.getBundleForTrustDomain(trustDomain));
+
+ pickNothing.set(false);
+ workloadApiClient.x509ContextWatcher.onUpdate(update);
+ assertSame(updatedBundle, source.getBundleForTrustDomain(trustDomain));
+ }
+ }
+
+ @Test
+ void newSource_pickerReturnsNull_fails() {
+ DefaultX509Source.X509SourceOptions options = DefaultX509Source.X509SourceOptions
+ .builder()
+ .workloadApiClient(new WorkloadApiClientStub())
+ .svidPicker(list -> null)
+ .build();
+
+ X509SourceException e = assertThrows(X509SourceException.class, () -> DefaultX509Source.newSource(options));
+ assertEquals("Error creating X.509 source", e.getMessage());
+ assertEquals("Error in X509Context watcher", e.getCause().getMessage());
+ assertEquals("Error applying X.509 Context update", e.getCause().getCause().getMessage());
+ }
+
@Test
void testWatchX509Context_corruptedUpdate_laterUpdatesAreApplied() throws Exception {
byte[] svid = Files.readAllBytes(Paths.get(toUri("testdata/workloadapi/svid.der")));