Skip to content

Commit 8bf98fb

Browse files
authored
fix(x509source): ensure atomic snapshot of SVID and bundles (#397)
Signed-off-by: Max Lambrecht <maxlambrecht@gmail.com>
1 parent f9969c1 commit 8bf98fb

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

java-spiffe-core/src/main/java/io/spiffe/workloadapi/DefaultX509Source.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,17 @@ public final class DefaultX509Source implements X509Source {
4646
private static final String TIMEOUT_SYSTEM_PROPERTY = "spiffe.newX509Source.timeout";
4747
private static final Duration DEFAULT_TIMEOUT = Duration.parse(System.getProperty(TIMEOUT_SYSTEM_PROPERTY, "PT0S"));
4848

49-
private X509Svid svid;
50-
private X509BundleSet bundles;
49+
private static final class X509Snapshot {
50+
final X509Svid svid;
51+
final X509BundleSet bundles;
52+
53+
X509Snapshot(X509Svid svid, X509BundleSet bundles) {
54+
this.svid = svid;
55+
this.bundles = bundles;
56+
}
57+
}
58+
59+
private volatile X509Snapshot snapshot;
5160

5261
private final Function<List<X509Svid>, X509Svid> picker;
5362
private final WorkloadApiClient workloadApiClient;
@@ -134,7 +143,11 @@ public X509Svid getX509Svid() {
134143
if (isClosed()) {
135144
throw new IllegalStateException("X.509 SVID source is closed");
136145
}
137-
return svid;
146+
X509Snapshot snap = snapshot;
147+
if (snap == null) {
148+
throw new IllegalStateException("X.509 SVID source not initialized");
149+
}
150+
return snap.svid;
138151
}
139152

140153
/**
@@ -149,7 +162,11 @@ public X509Bundle getBundleForTrustDomain(TrustDomain trustDomain) throws Bundle
149162
if (isClosed()) {
150163
throw new IllegalStateException("X.509 bundle source is closed");
151164
}
152-
return bundles.getBundleForTrustDomain(trustDomain);
165+
X509Snapshot snap = snapshot;
166+
if (snap == null) {
167+
throw new IllegalStateException("X.509 bundle source not initialized");
168+
}
169+
return snap.bundles.getBundleForTrustDomain(trustDomain);
153170
}
154171

155172
/**
@@ -224,10 +241,13 @@ private void setX509Context(final X509Context update) {
224241
} else {
225242
svidUpdate = picker.apply(update.getX509Svids());
226243
}
227-
synchronized (this) {
228-
this.svid = svidUpdate;
229-
this.bundles = update.getX509BundleSet();
244+
245+
X509BundleSet bundleSet = update.getX509BundleSet();
246+
if (bundleSet == null) {
247+
throw new IllegalArgumentException("X509Context bundle set cannot be null");
230248
}
249+
250+
this.snapshot = new X509Snapshot(svidUpdate, bundleSet);
231251
}
232252

233253
private boolean isClosed() {

0 commit comments

Comments
 (0)