Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
* Add a `spiffeIdVerifier` option to `SpiffeSslContextFactory.SslContextOptions`, so an `SSLContext` can verify peer SPIFFE IDs with a custom `SpiffeIdVerifier` instead of a fixed set of accepted SPIFFE IDs
* Add `SpiffeIdVerifier.acceptAny()`, `acceptId()`, `acceptOneOf()` and `acceptMemberOf()` factory methods for verifiers that accept any SPIFFE ID, a specific SPIFFE ID, one of a set of SPIFFE IDs, or any SPIFFE ID in a trust domain

### Changed

* Retry Workload API `PERMISSION_DENIED` responses with backoff. With the default client and no initialization timeout, `DefaultX509Source.newSource()`, `DefaultJwtSource.newSource()` and `CachedJwtSource.newSource()` now wait indefinitely for an unregistered workload instead of failing immediately. Set a nonzero `initTimeout` or the `spiffe.newX509Source.timeout` / `spiffe.newJwtSource.timeout` system property to bound the wait.

### Fixed

* Always report terminal Workload API watch failures after a permission denial, including repeated denials, preserving the exception type and cause.
* Reject X.509-SVID leaf certificates with a root SPIFFE ID path in `X509SvidValidator.verifyChain`, which `SpiffeTrustManager` uses for peer certificates (#510)
* Ignore JWKs with an unsupported key type when parsing a JWT bundle instead of rejecting the whole bundle, per the SPIFFE bundle spec (#512)
* Serialize `java-spiffe-helper` KeyStore and TrustStore files to memory before writing them, so a store or encoding failure no longer leaves an empty or partial file (#490)
Expand Down
6 changes: 4 additions & 2 deletions java-spiffe-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ It allows to configure another SVID picker. By default, the first SVID is used.

The `X509Source newSource()` method blocks waiting until an X.509 context is fetched. The X.509 context fetch is retried
using an exponential backoff policy with this progression of delays between retries: 1 second, 2 seconds, 4, 8, 16, 32, 60, 60, 60...
It retries indefinitely unless a timeout is configured.
It retries indefinitely unless a timeout is configured. This includes `PERMISSION_DENIED`, so an unregistered
workload waits for registration instead of failing immediately. Set a nonzero `initTimeout` to bound this wait.

This timeout can be configured either with the `initTimeout` option of the `X509SourceOptions` passed to the
`newSource(X509SourceOptions options)` method:
Expand Down Expand Up @@ -125,7 +126,8 @@ the cached JWT-SVIDs have passed half of their lifetime.

The `JwtSource newSource()` method blocks until the JWT materials are fetched. The fetching process is retried
using an exponential backoff policy with this progression of delays between retries: 1 second, 2 seconds, 4, 8, 16, 32, 60, 60, 60...
It retries indefinitely unless a timeout is configured.
It retries indefinitely unless a timeout is configured. This includes `PERMISSION_DENIED`, so an unregistered
workload waits for registration instead of failing immediately. Set a nonzero `initTimeout` to bound this wait.

This timeout can be configured either with the `initTimeout` option of the `JwtSourceOptions` passed to the
`newSource(JwtSourceOptions options)` method:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ private CachedJwtSource(final WorkloadApiClient workloadApiClient) {
* through the system property `spiffe.newJwtSource.timeout` expires.
* If no timeout is configured, it blocks until it gets a JWT update from the Workload API.
* <p>
* With the default client, {@code PERMISSION_DENIED} is retried with backoff, so an unregistered
* workload can wait indefinitely. To bound this wait, use {@link #newSource(JwtSourceOptions)}
* with a nonzero {@code initTimeout}, or set the timeout system property.
* <p>
* It uses the default address socket endpoint from the environment variable to get the Workload API address.
*
* @return an instance of {@link DefaultJwtSource}, with the JWT bundles initialized
Expand All @@ -83,6 +87,9 @@ public static JwtSource newSource() throws JwtSourceException, SocketEndpointAdd
* system property `spiffe.newJwtSource.timeout`. If none is configured, this method will
* block until the JWT bundles can be retrieved from the Workload API.
* <p>
* With the default client, {@code PERMISSION_DENIED} is retried with backoff. Set a nonzero
* {@code initTimeout} to bound the wait for an unregistered workload.
* <p>
* The {@link WorkloadApiClient} can be provided in the options, if it is not,
* a new client is created.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ private DefaultJwtSource(final WorkloadApiClient workloadApiClient) {
* through the system property `spiffe.newJwtSource.timeout` expires.
* If no timeout is configured, it blocks until it gets a JWT update from the Workload API.
* <p>
* With the default client, {@code PERMISSION_DENIED} is retried with backoff, so an unregistered
* workload can wait indefinitely. To bound this wait, use {@link #newSource(JwtSourceOptions)}
* with a nonzero {@code initTimeout}, or set the timeout system property.
* <p>
* It uses the default address socket endpoint from the environment variable to get the Workload API address.
*
* @return an instance of {@link DefaultJwtSource}, with the JWT bundles initialized
Expand All @@ -70,6 +74,9 @@ public static JwtSource newSource() throws JwtSourceException, SocketEndpointAdd
* system property `spiffe.newJwtSource.timeout`. If none is configured, this method will
* block until the JWT bundles can be retrieved from the Workload API.
* <p>
* With the default client, {@code PERMISSION_DENIED} is retried with backoff. Set a nonzero
* {@code initTimeout} to bound the wait for an unregistered workload.
* <p>
* The {@link WorkloadApiClient} can be provided in the options, if it is not,
* a new client is created.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@
* <p>
* Supports one-shot calls and watch updates for X.509 and JWT SVIDs and bundles.
* <p>
* The watch for updates methods support retries using an exponential backoff policy to reestablish
* the stream connection to the Workload API.
* The watch methods retry eligible errors with exponential backoff. Before the first successfully
* delivered update, {@code PERMISSION_DENIED} is not reported while retries can be scheduled.
* After an update, the first denial is reported immediately to {@link Watcher#onError(Throwable)};
* retries continue if the callback returns normally and the retry policy permits them.
* Repeated denials are not reported again until another update is successfully delivered.
* <p>
* Retry exhaustion or scheduling failure always reports a terminal error with a {@code Cancelling}
* message, including when a permission denial has already been reported. A denial can therefore
* produce both an immediate withdrawal notification and a terminal error if no retry can be scheduled.
* <p>
* Rejected updates are reported to {@code onError}; if that callback returns normally, the watch
* remains active. An exception thrown by {@code onError} escapes the callback and prevents further retries.
*/
public final class DefaultWorkloadApiClient implements WorkloadApiClient {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ private DefaultX509Source(final Function<List<X509Svid>, X509Svid> svidPicker, f
* through the system property `spiffe.newX509Source.timeout` expires.
* If no timeout is configured, it blocks until it gets an X.509 update from the Workload API.
* <p>
* With the default client, {@code PERMISSION_DENIED} is retried with backoff, so an unregistered
* workload can wait indefinitely. To bound this wait, use {@link #newSource(X509SourceOptions)}
* with a nonzero {@code initTimeout}, or set the timeout system property.
* <p>
* It uses the default address socket endpoint from the environment variable to get the Workload API address.
* <p>
* It uses the default X.509 SVID (picks the first SVID that comes in the Workload API response).
Expand All @@ -98,6 +102,9 @@ public static DefaultX509Source newSource() throws SocketEndpointAddressExceptio
* system property `spiffe.newX509Source.timeout`. If none is configured, this method will
* block until the X.509 materials can be retrieved from the Workload API.
* <p>
* With the default client, {@code PERMISSION_DENIED} is retried with backoff. Set a nonzero
* {@code initTimeout} to bound the wait for an unregistered workload.
* <p>
* The {@link WorkloadApiClient} can be provided in the options, if it is not,
* a new client is created.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.spiffe.bundle.x509bundle.X509Bundle;
import io.spiffe.bundle.x509bundle.X509BundleSet;
import io.spiffe.exception.JwtBundleException;
import io.spiffe.exception.InvalidSpiffeIdException;
import io.spiffe.exception.X509BundleException;
import io.spiffe.exception.X509ContextException;
import io.spiffe.exception.X509SvidException;
Expand Down Expand Up @@ -113,7 +114,12 @@ private static List<X509Bundle> getListOfX509Bundles(final Workload.X509SVIDResp
// Process federated bundles
Set<Map.Entry<String, ByteString>> federatedBundles = x509SvidResponse.getFederatedBundlesMap().entrySet();
for (Map.Entry<String, ByteString> bundleEntry : federatedBundles) {
TrustDomain trustDomain = TrustDomain.parse(bundleEntry.getKey());
final TrustDomain trustDomain;
try {
trustDomain = TrustDomain.parse(bundleEntry.getKey());
} catch (InvalidSpiffeIdException | IllegalArgumentException e) {
throw new X509ContextException("X.509 federated bundle trust domain could not be processed", e);
}
byte[] bundleBytes = bundleEntry.getValue().toByteArray();
final X509Bundle bundle = parseX509Bundle(trustDomain, bundleBytes);
x509BundleList.add(bundle);
Expand Down Expand Up @@ -176,13 +182,23 @@ private static void validateSpiffeId(String spiffeIdSvid, String spiffeIdRespons
}

private static JwtBundle createJwtBundle(Map.Entry<String, ByteString> entry) throws JwtBundleException {
TrustDomain trustDomain = TrustDomain.parse(entry.getKey());
final TrustDomain trustDomain;
try {
trustDomain = TrustDomain.parse(entry.getKey());
} catch (InvalidSpiffeIdException | IllegalArgumentException e) {
throw new JwtBundleException("JWT bundle trust domain could not be processed", e);
}
byte[] bundleBytes = entry.getValue().toByteArray();
return JwtBundle.parse(trustDomain, bundleBytes);
}

private static X509Bundle createX509Bundle(Map.Entry<String, ByteString> bundleEntry) throws X509BundleException {
TrustDomain trustDomain = TrustDomain.parse(bundleEntry.getKey());
final TrustDomain trustDomain;
try {
trustDomain = TrustDomain.parse(bundleEntry.getKey());
} catch (InvalidSpiffeIdException | IllegalArgumentException e) {
throw new X509BundleException("X.509 bundle trust domain could not be processed", e);
}
byte[] bundleBytes = bundleEntry.getValue().toByteArray();
return X509Bundle.parse(trustDomain, bundleBytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ final class StreamObservers {
Logger.getLogger(StreamObservers.class.getName());

private static final String STREAM_IS_COMPLETED = "Workload API stream is completed";
// Retry only transient stream failures. Caller/client cancellation and terminal API errors fail the watch closed.
// PermissionDenied is retryable, but must be reported after an update has been delivered.
// Caller/client cancellation and terminal API errors fail the watch closed.
private static final Set<Status.Code> NON_RETRYABLE_CODES = EnumSet.of(
Status.Code.INVALID_ARGUMENT,
Status.Code.CANCELLED,
Status.Code.PERMISSION_DENIED,
Status.Code.UNAUTHENTICATED,
Status.Code.UNIMPLEMENTED,
Status.Code.FAILED_PRECONDITION);
Expand All @@ -42,11 +42,16 @@ static StreamObserver<Workload.X509SVIDResponse> getX509ContextStreamObserver(
final SpiffeWorkloadAPIGrpc.SpiffeWorkloadAPIStub workloadApiAsyncStub) {

return new StreamObserver<Workload.X509SVIDResponse>() {
private boolean updateDelivered;
private boolean permissionDeniedReported;

@Override
public void onNext(final Workload.X509SVIDResponse value) {
try {
final X509Context x509Context = GrpcConversionUtils.toX509Context(value);
watcher.onUpdate(x509Context);
updateDelivered = true;
permissionDeniedReported = false;
retryHandler.reset();
} catch (X509ContextException e) {
watcher.onError(new X509ContextException("Error processing X.509 Context update", e));
Expand All @@ -65,6 +70,11 @@ private void handleWatchX509ContextError(final Throwable t) {
if (isErrorNotRetryable(t)) {
watcher.onError(new X509ContextException("Cancelling X.509 Context watch", t));
} else {
if (updateDelivered && !permissionDeniedReported
&& Status.fromThrowable(t).getCode() == Status.Code.PERMISSION_DENIED) {
permissionDeniedReported = true;
watcher.onError(new X509ContextException("Permission denied for X.509 Context watch", t));
}
handleX509ContextRetry(t);
}
}
Expand Down Expand Up @@ -98,14 +108,19 @@ static StreamObserver<Workload.X509BundlesResponse> getX509BundlesStreamObserver
final SpiffeWorkloadAPIGrpc.SpiffeWorkloadAPIStub workloadApiAsyncStub) {

return new StreamObserver<Workload.X509BundlesResponse>() {
private boolean updateDelivered;
private boolean permissionDeniedReported;

@Override
public void onNext(final Workload.X509BundlesResponse value) {
try {
final X509BundleSet x509Context = GrpcConversionUtils.toX509BundleSet(value);
watcher.onUpdate(x509Context);
updateDelivered = true;
permissionDeniedReported = false;
retryHandler.reset();
} catch (X509BundleException e) {
watcher.onError(new X509ContextException("Error processing X.509 bundles update", e));
watcher.onError(new X509BundleException("Error processing X.509 bundles update", e));
}
}

Expand All @@ -119,8 +134,13 @@ public void onError(final Throwable t) {

private void handleWatchX509BundlesError(final Throwable t) {
if (isErrorNotRetryable(t)) {
watcher.onError(new X509ContextException("Cancelling X.509 bundles watch", t));
watcher.onError(new X509BundleException("Cancelling X.509 bundles watch", t));
} else {
if (updateDelivered && !permissionDeniedReported
&& Status.fromThrowable(t).getCode() == Status.Code.PERMISSION_DENIED) {
permissionDeniedReported = true;
watcher.onError(new X509BundleException("Permission denied for X.509 bundles watch", t));
}
handleX509BundlesRetry(t);
}
}
Expand Down Expand Up @@ -153,12 +173,16 @@ static StreamObserver<Workload.JWTBundlesResponse> getJwtBundleStreamObserver(
final Context.CancellableContext cancellableContext,
final SpiffeWorkloadAPIGrpc.SpiffeWorkloadAPIStub workloadApiAsyncStub) {
return new StreamObserver<Workload.JWTBundlesResponse>() {
private boolean updateDelivered;
private boolean permissionDeniedReported;

@Override
public void onNext(final Workload.JWTBundlesResponse value) {
try {
final JwtBundleSet jwtBundleSet = GrpcConversionUtils.toJwtBundleSet(value);
watcher.onUpdate(jwtBundleSet);
updateDelivered = true;
permissionDeniedReported = false;
retryHandler.reset();
} catch (JwtBundleException e) {
watcher.onError(new JwtBundleException("Error processing JWT bundles update", e));
Expand All @@ -177,6 +201,11 @@ private void handleWatchJwtBundleError(final Throwable t) {
if (isErrorNotRetryable(t)) {
watcher.onError(new JwtBundleException("Cancelling JWT Bundles watch", t));
} else {
if (updateDelivered && !permissionDeniedReported
&& Status.fromThrowable(t).getCode() == Status.Code.PERMISSION_DENIED) {
permissionDeniedReported = true;
watcher.onError(new JwtBundleException("Permission denied for JWT Bundles watch", t));
}
handleJwtBundleRetry(t);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public interface WorkloadApiClient extends Closeable {
* A new Stream to the Workload API is opened for each call to this method, so that the client starts getting
* updates immediately after the Stream is ready and doesn't have to wait until the Workload API dispatches
* the next update based on the SVIDs TTL.
* See {@link DefaultWorkloadApiClient} for retry and error callback behavior.
*
* @param watcher an instance that implements a {@link Watcher}.
*/
Expand All @@ -52,6 +53,7 @@ public interface WorkloadApiClient extends Closeable {
* A new Stream to the Workload API is opened for each call to this method, so that the client starts getting
* updates immediately after the Stream is ready and doesn't have to wait until the Workload API dispatches
* the next update.
* See {@link DefaultWorkloadApiClient} for retry and error callback behavior.
*
* @param watcher an instance that implements a {@link Watcher} for {@link X509BundleSet}.
*/
Expand Down Expand Up @@ -123,6 +125,7 @@ public interface WorkloadApiClient extends Closeable {
* A new Stream to the Workload API is opened for each call to this method, so that the client starts getting
* updates immediately after the Stream is ready and doesn't have to wait until the Workload API dispatches
* the next update based on the SVIDs TTL.
* See {@link DefaultWorkloadApiClient} for retry and error callback behavior.
*
* @param watcher receives the update for JwtBundles.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.concurrent.CountDownLatch;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.fail;

class DefaultWorkloadApiClientCorruptedResponsesTest {
Expand Down Expand Up @@ -85,6 +86,7 @@ public void onUpdate(X509BundleSet update) {

@Override
public void onError(Throwable e) {
assertInstanceOf(X509BundleException.class, e);
assertEquals("Error processing X.509 bundles update", e.getMessage());
done.countDown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.concurrent.CountDownLatch;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.fail;

class DefaultWorkloadApiClientEmptyResponseTest {
Expand Down Expand Up @@ -86,6 +87,7 @@ public void onUpdate(X509BundleSet update) {

@Override
public void onError(Throwable e) {
assertInstanceOf(X509BundleException.class, e);
assertEquals("Error processing X.509 bundles update", e.getMessage());
done.countDown();
}
Expand Down
Loading
Loading