> acceptedSpiffeIdsSupplier) {
+ this.acceptedSpiffeIdsSupplier = acceptedSpiffeIdsSupplier;
+ return this;
+ }
+
+ public SslContextOptionsBuilder acceptAnySpiffeId() {
+ this.acceptAnySpiffeId = true;
+ return this;
+ }
+
+ public SslContextOptions build() {
+ return new SslContextOptions(sslProtocol, x509Source, acceptedSpiffeIdsSupplier, acceptAnySpiffeId);
+ }
+ }
}
}
diff --git a/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeTrustManager.java b/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeTrustManager.java
index 42a8245..cdb0611 100644
--- a/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeTrustManager.java
+++ b/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeTrustManager.java
@@ -51,15 +51,15 @@ public final class SpiffeTrustManager extends X509ExtendedTrustManager {
*
* Creates a {@link SpiffeTrustManager} with an X.509 bundle source used to provide the trusted bundles,
* and a flag to indicate that any SPIFFE ID will be accepted.
+ *
+ * Any SPIFFE ID will be accepted during peer SVID validation.
*
* @param x509BundleSource an implementation of a {@link BundleSource}
- * @param acceptAnySpiffeId a Supplier of a Set of accepted SPIFFE IDs.
*/
- public SpiffeTrustManager(@NonNull final BundleSource x509BundleSource,
- final boolean acceptAnySpiffeId) {
+ public SpiffeTrustManager(@NonNull final BundleSource x509BundleSource) {
this.x509BundleSource = x509BundleSource;
this.acceptedSpiffeIdsSupplier = Collections::emptySet;
- this.acceptAnySpiffeId = acceptAnySpiffeId;
+ this.acceptAnySpiffeId = true;
}
/**
diff --git a/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeTrustManagerFactory.java b/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeTrustManagerFactory.java
index 87c1ac9..8925b4f 100644
--- a/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeTrustManagerFactory.java
+++ b/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeTrustManagerFactory.java
@@ -70,7 +70,7 @@ public class SpiffeTrustManagerFactory extends TrustManagerFactorySpi {
final SpiffeTrustManager spiffeTrustManager;
if (ACCEPT_ANY_SPIFFE_ID) {
- spiffeTrustManager = new SpiffeTrustManager(x509Source, true);
+ spiffeTrustManager = new SpiffeTrustManager(x509Source);
} else {
spiffeTrustManager = new SpiffeTrustManager(x509Source, DEFAULT_SPIFFE_ID_SET_SUPPLIER);
}
@@ -92,8 +92,7 @@ public class SpiffeTrustManagerFactory extends TrustManagerFactorySpi {
final SpiffeTrustManager spiffeTrustManager;
if (ACCEPT_ANY_SPIFFE_ID) {
- // make explicit that all SPIFFE IDs will be accepted
- spiffeTrustManager = new SpiffeTrustManager(x509BundleSource, true);
+ spiffeTrustManager = new SpiffeTrustManager(x509BundleSource);
} else {
spiffeTrustManager = new SpiffeTrustManager(x509BundleSource, DEFAULT_SPIFFE_ID_SET_SUPPLIER);
}
@@ -108,7 +107,7 @@ public class SpiffeTrustManagerFactory extends TrustManagerFactorySpi {
* @return an instance of a {@link TrustManager} wrapped in an array. The actual type returned is {@link SpiffeTrustManager}
*/
public TrustManager[] engineGetTrustManagersAcceptAnySpiffeId(@NonNull final BundleSource x509BundleSource) {
- val spiffeTrustManager = new SpiffeTrustManager(x509BundleSource, true);
+ val spiffeTrustManager = new SpiffeTrustManager(x509BundleSource);
return new TrustManager[]{spiffeTrustManager};
}
diff --git a/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeSslContextFactoryTest.java b/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeSslContextFactoryTest.java
index 741d309..1015ba8 100644
--- a/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeSslContextFactoryTest.java
+++ b/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeSslContextFactoryTest.java
@@ -23,8 +23,12 @@ class SpiffeSslContextFactoryTest {
@Test
void getSslContext_withX509Source() {
- SpiffeSslContextFactory.SslContextOptions options = SpiffeSslContextFactory.SslContextOptions
- .builder().x509Source(x509Source).build();
+ SpiffeSslContextFactory.SslContextOptions options =
+ SpiffeSslContextFactory.SslContextOptions
+ .builder()
+ .x509Source(x509Source)
+ .acceptAnySpiffeId()
+ .build();
try {
assertNotNull(SpiffeSslContextFactory.getSslContext(options));
} catch (NoSuchAlgorithmException | KeyManagementException e) {
@@ -34,8 +38,12 @@ class SpiffeSslContextFactoryTest {
@Test
void getSslContext_withSupplierOfSpiffeIds() {
- SpiffeSslContextFactory.SslContextOptions options = SpiffeSslContextFactory.SslContextOptions
- .builder().x509Source(x509Source).acceptedSpiffeIdsSupplier(Collections::emptySet).build();
+ SpiffeSslContextFactory.SslContextOptions options =
+ SpiffeSslContextFactory.SslContextOptions
+ .builder()
+ .x509Source(x509Source)
+ .acceptedSpiffeIdsSupplier(Collections::emptySet)
+ .build();
try {
assertNotNull(SpiffeSslContextFactory.getSslContext(options));
} catch (NoSuchAlgorithmException | KeyManagementException e) {
@@ -45,8 +53,12 @@ class SpiffeSslContextFactoryTest {
@Test
void getSslContext_withAcceptAny() {
- SpiffeSslContextFactory.SslContextOptions options = SpiffeSslContextFactory.SslContextOptions
- .builder().x509Source(x509Source).acceptAnySpiffeId(true).build();
+ SpiffeSslContextFactory.SslContextOptions options =
+ SpiffeSslContextFactory.SslContextOptions
+ .builder()
+ .x509Source(x509Source)
+ .acceptAnySpiffeId()
+ .build();
try {
assertNotNull(SpiffeSslContextFactory.getSslContext(options));
} catch (NoSuchAlgorithmException | KeyManagementException e) {
@@ -56,8 +68,13 @@ class SpiffeSslContextFactoryTest {
@Test
void getSslContext_withOtherSslProtocol() {
- SpiffeSslContextFactory.SslContextOptions options = SpiffeSslContextFactory.SslContextOptions
- .builder().x509Source(x509Source).sslProtocol("TLSv1.1").build();
+ SpiffeSslContextFactory.SslContextOptions options =
+ SpiffeSslContextFactory.SslContextOptions
+ .builder()
+ .x509Source(x509Source)
+ .acceptAnySpiffeId()
+ .sslProtocol("TLSv1.1")
+ .build();
try {
assertNotNull(SpiffeSslContextFactory.getSslContext(options));
} catch (NoSuchAlgorithmException | KeyManagementException e) {
@@ -76,11 +93,33 @@ class SpiffeSslContextFactoryTest {
@Test
void getSslContext_nullX509Source() throws KeyManagementException, NoSuchAlgorithmException {
- SpiffeSslContextFactory.SslContextOptions options = SpiffeSslContextFactory.SslContextOptions.builder().build();
+ SpiffeSslContextFactory.SslContextOptions options =
+ SpiffeSslContextFactory.SslContextOptions
+ .builder()
+ .acceptAnySpiffeId()
+ .build();
try {
SpiffeSslContextFactory.getSslContext(options);
} catch (IllegalArgumentException e) {
assertEquals("x509Source option cannot be null, an X.509 Source must be provided", e.getMessage());
}
}
+
+ @Test
+ void getSslContext_noSupplierAndAcceptAnyNotSet() {
+ SpiffeSslContextFactory.SslContextOptions options =
+ SpiffeSslContextFactory.SslContextOptions
+ .builder()
+ .x509Source(x509Source)
+ .build();
+ try {
+ SpiffeSslContextFactory.getSslContext(options);
+ fail();
+ } catch (NoSuchAlgorithmException | KeyManagementException e) {
+ fail(e);
+ } catch (IllegalArgumentException e) {
+ assertEquals("SSL context should be configured either with a Supplier " +
+ "of accepted SPIFFE IDs or with acceptAnySpiffeId=true", e.getMessage());
+ }
+ }
}
\ No newline at end of file
diff --git a/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeSslSocketFactoryTest.java b/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeSslSocketFactoryTest.java
index 75d7da8..070d929 100644
--- a/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeSslSocketFactoryTest.java
+++ b/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeSslSocketFactoryTest.java
@@ -28,7 +28,12 @@ class SpiffeSslSocketFactoryTest {
@BeforeEach
void setup() throws NoSuchAlgorithmException, KeyManagementException {
X509SourceStub x509Source = new X509SourceStub();
- SpiffeSslContextFactory.SslContextOptions options = SpiffeSslContextFactory.SslContextOptions.builder().x509Source(x509Source).build();
+ SpiffeSslContextFactory.SslContextOptions options =
+ SpiffeSslContextFactory.SslContextOptions
+ .builder()
+ .x509Source(x509Source)
+ .acceptAnySpiffeId()
+ .build();
spiffeSslSocketFactory = new SpiffeSslSocketFactory(options);
SSLContext sslContext = SpiffeSslContextFactory.getSslContext(options);
socketFactory = sslContext.getSocketFactory();
diff --git a/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeTrustManagerTest.java b/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeTrustManagerTest.java
index 6968f8a..cebcf3d 100644
--- a/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeTrustManagerTest.java
+++ b/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeTrustManagerTest.java
@@ -75,7 +75,7 @@ public class SpiffeTrustManagerTest {
@Test
void testCreateSpiffeTrustManager_nullSource() {
try {
- new SpiffeTrustManager(null, true);
+ new SpiffeTrustManager(null);
fail();
} catch (Exception e) {
assertEquals("x509BundleSource is marked non-null but is null", e.getMessage());
@@ -277,7 +277,7 @@ public class SpiffeTrustManagerTest {
acceptedSpiffeIds = Collections.singleton(SpiffeId.parse("spiffe://example.org/other"));
when(bundleSource.getBundleForTrustDomain(TrustDomain.of("example.org"))).thenReturn(bundleKnown);
- spiffeTrustManager = new SpiffeTrustManager(bundleSource, true);
+ spiffeTrustManager = new SpiffeTrustManager(bundleSource);
try {
spiffeTrustManager.checkClientTrusted(chain, "");
@@ -291,7 +291,7 @@ public class SpiffeTrustManagerTest {
acceptedSpiffeIds = Collections.singleton(SpiffeId.parse("spiffe://example.org/other"));
when(bundleSource.getBundleForTrustDomain(TrustDomain.of("example.org"))).thenReturn(bundleKnown);
- spiffeTrustManager = new SpiffeTrustManager(bundleSource, true);
+ spiffeTrustManager = new SpiffeTrustManager(bundleSource);
try {
spiffeTrustManager.checkClientTrusted(chain, "");