diff --git a/CHANGELOG.md b/CHANGELOG.md index 319753b7..67efe103 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Added + +* Add `SpiffeIdVerifier.acceptIdPrefix()`, a factory method for verifiers that accept a SPIFFE ID and any SPIFFE ID under its path, matching on path segment boundaries in the same trust domain + ## [0.9.0] - 2026-11-24 ### Breaking changes diff --git a/java-spiffe-provider/README.md b/java-spiffe-provider/README.md index 70862729..4bb21f4e 100644 --- a/java-spiffe-provider/README.md +++ b/java-spiffe-provider/README.md @@ -62,6 +62,7 @@ handshake. |------------------------------------------------|----------------------------------------------------------| | `SpiffeIdVerifier.acceptAny()` | any SPIFFE ID | | `SpiffeIdVerifier.acceptId(spiffeId)` | only the given SPIFFE ID | +| `SpiffeIdVerifier.acceptIdPrefix(prefix)` | the given SPIFFE ID and any SPIFFE ID under its path | | `SpiffeIdVerifier.acceptOneOf(spiffeIds)` | any of the given SPIFFE IDs (varargs or a `Collection`) | | `SpiffeIdVerifier.acceptMemberOf(trustDomain)` | any SPIFFE ID in the given trust domain | @@ -77,16 +78,23 @@ For example, to accept any workload in the `example.org` trust domain: SSLContext sslContext = SpiffeSslContextFactory.getSslContext(options); ``` +`acceptIdPrefix` matches the prefix on path segment boundaries, in the trust domain of the prefix. For example, +`acceptIdPrefix(SpiffeId.parse("spiffe://example.org/backend"))` accepts `spiffe://example.org/backend` and +`spiffe://example.org/backend/api`, but not `spiffe://example.org/backend-admin` or `spiffe://other.org/backend/api`. +The prefix must have a path; to accept any SPIFFE ID in a trust domain, use `acceptMemberOf`. + `acceptOneOf` copies the SPIFFE IDs when it is called. To accept a set of SPIFFE IDs that changes over time, use `acceptedSpiffeIdsSupplier` instead. -For other rules, implement `SpiffeIdVerifier`. For example, to accept only workloads under the `/backend` path of the -`example.org` trust domain: +For other rules, implement `SpiffeIdVerifier`. For example, to accept the workloads that run as a Kubernetes service +account named `backend`, in any namespace, when the `example.org` trust domain issues SPIFFE IDs in the +`/ns//sa/` format: ``` TrustDomain trustDomain = TrustDomain.parse("example.org"); + Pattern backendServiceAccount = Pattern.compile("/ns/[^/]+/sa/backend"); SpiffeIdVerifier backendWorkloads = (spiffeId, verifiedChain) -> { - if (!spiffeId.memberOf(trustDomain) || !spiffeId.getPath().startsWith("/backend/")) { + if (!spiffeId.memberOf(trustDomain) || !backendServiceAccount.matcher(spiffeId.getPath()).matches()) { throw new SpiffeVerificationException( String.format("SPIFFE ID %s is not a backend workload of trust domain %s", spiffeId, trustDomain)); } diff --git a/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeIdVerifier.java b/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeIdVerifier.java index 855a4d5a..26705b68 100644 --- a/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeIdVerifier.java +++ b/java-spiffe-provider/src/main/java/io/spiffe/provider/SpiffeIdVerifier.java @@ -15,7 +15,8 @@ *

* A verifier is called after the peer's certificate chain has been verified against the X.509 bundles, and decides * whether the peer is authorized. The static factory methods return verifiers for the common cases: accepting any - * SPIFFE ID, a specific SPIFFE ID, one of a set of SPIFFE IDs, or any SPIFFE ID in a trust domain. + * SPIFFE ID, a specific SPIFFE ID, any SPIFFE ID under a SPIFFE ID prefix, one of a set of SPIFFE IDs, or any SPIFFE + * ID in a trust domain. */ public interface SpiffeIdVerifier { /** @@ -55,6 +56,41 @@ static SpiffeIdVerifier acceptId(final SpiffeId allowedSpiffeId) { }; } + /** + * Returns a verifier that accepts any SPIFFE ID under the given SPIFFE ID prefix. + *

+ * A SPIFFE ID is accepted if it is in the trust domain of the prefix and its path is either the path of the prefix + * or extends it with more segments. The prefix is matched on segment boundaries, so the prefix + * {@code spiffe://example.org/backend} accepts {@code spiffe://example.org/backend} and + * {@code spiffe://example.org/backend/api}, but not {@code spiffe://example.org/backend-admin}. Paths are compared + * case-sensitively. + *

+ * The prefix must have a path. To accept any SPIFFE ID in a trust domain, use {@link #acceptMemberOf(TrustDomain)}. + * + * @param prefix the SPIFFE ID prefix whose SPIFFE IDs are accepted + * @return a {@link SpiffeIdVerifier} that accepts only {@code prefix} and the SPIFFE IDs under it + * @throws NullPointerException if {@code prefix} is null + * @throws IllegalArgumentException if {@code prefix} has no path + */ + static SpiffeIdVerifier acceptIdPrefix(final SpiffeId prefix) { + Objects.requireNonNull(prefix, "prefix must not be null"); + final String prefixPath = prefix.getPath(); + if (prefixPath.isEmpty()) { + throw new IllegalArgumentException( + "prefix must have a path, use acceptMemberOf to accept any SPIFFE ID in a trust domain"); + } + final TrustDomain trustDomain = prefix.getTrustDomain(); + final String childPathPrefix = prefixPath + "/"; + return (spiffeId, verifiedChain) -> { + final boolean accepted = spiffeId != null && spiffeId.memberOf(trustDomain) + && (spiffeId.getPath().equals(prefixPath) || spiffeId.getPath().startsWith(childPathPrefix)); + if (!accepted) { + throw new SpiffeVerificationException(String.format( + "SPIFFE ID %s in X.509 certificate does not match the prefix %s", spiffeId, prefix)); + } + }; + } + /** * Returns a verifier that accepts any of the given SPIFFE IDs. *

diff --git a/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeIdVerifierTest.java b/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeIdVerifierTest.java index e1487f82..4e1a36b5 100644 --- a/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeIdVerifierTest.java +++ b/java-spiffe-provider/src/test/java/io/spiffe/provider/SpiffeIdVerifierTest.java @@ -58,6 +58,75 @@ void acceptId_nullSpiffeId() { assertEquals("allowedSpiffeId must not be null", e.getMessage()); } + @Test + void acceptIdPrefix_acceptsThePrefixAndSpiffeIdsUnderIt() { + SpiffeIdVerifier verifier = SpiffeIdVerifier.acceptIdPrefix(SpiffeId.parse("spiffe://example.org/backend")); + + assertDoesNotThrow(() -> verifier.verify(SpiffeId.parse("spiffe://example.org/backend"), CHAIN)); + assertDoesNotThrow(() -> verifier.verify(SpiffeId.parse("spiffe://example.org/backend/api"), CHAIN)); + assertDoesNotThrow(() -> verifier.verify(SpiffeId.parse("spiffe://EXAMPLE.org/backend/api/v1"), CHAIN)); + } + + @Test + void acceptIdPrefix_multiSegmentPrefix_matchesOnSegmentBoundaries() { + SpiffeIdVerifier verifier = SpiffeIdVerifier.acceptIdPrefix(SpiffeId.parse("spiffe://example.org/ns/prod")); + + assertDoesNotThrow(() -> verifier.verify(SpiffeId.parse("spiffe://example.org/ns/prod"), CHAIN)); + assertDoesNotThrow(() -> verifier.verify(SpiffeId.parse("spiffe://example.org/ns/prod/sa/web"), CHAIN)); + assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://example.org/ns/production/sa/web"), CHAIN)); + assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://example.org/ns"), CHAIN)); + assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://example.org/prod"), CHAIN)); + } + + @Test + void acceptIdPrefix_rejectsSpiffeIdsNotUnderThePrefix() { + SpiffeIdVerifier verifier = SpiffeIdVerifier.acceptIdPrefix(SpiffeId.parse("spiffe://example.org/backend")); + + SpiffeVerificationException e = assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://example.org/backend-admin"), CHAIN)); + assertEquals("SPIFFE ID spiffe://example.org/backend-admin in X.509 certificate does not match the prefix " + + "spiffe://example.org/backend", e.getMessage()); + assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://example.org/backendapi"), CHAIN)); + assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://example.org/Backend/api"), CHAIN)); + assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://example.org/frontend/backend"), CHAIN)); + assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(TRUST_DOMAIN.newSpiffeId(), CHAIN)); + } + + @Test + void acceptIdPrefix_rejectsSpiffeIdsInOtherTrustDomains() { + SpiffeIdVerifier verifier = SpiffeIdVerifier.acceptIdPrefix(SpiffeId.parse("spiffe://example.org/backend")); + + SpiffeVerificationException e = assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://other.org/backend/api"), CHAIN)); + assertEquals("SPIFFE ID spiffe://other.org/backend/api in X.509 certificate does not match the prefix " + + "spiffe://example.org/backend", e.getMessage()); + assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://other.org/backend"), CHAIN)); + assertThrows(SpiffeVerificationException.class, + () -> verifier.verify(SpiffeId.parse("spiffe://example.org.other/backend"), CHAIN)); + } + + @Test + void acceptIdPrefix_prefixWithoutPath() { + IllegalArgumentException e = assertThrows(IllegalArgumentException.class, + () -> SpiffeIdVerifier.acceptIdPrefix(TRUST_DOMAIN.newSpiffeId())); + assertEquals("prefix must have a path, use acceptMemberOf to accept any SPIFFE ID in a trust domain", + e.getMessage()); + } + + @Test + void acceptIdPrefix_nullPrefix() { + NullPointerException e = assertThrows(NullPointerException.class, () -> SpiffeIdVerifier.acceptIdPrefix(null)); + assertEquals("prefix must not be null", e.getMessage()); + } + @Test void acceptOneOf_array_acceptsTheGivenSpiffeIds() { SpiffeIdVerifier verifier = SpiffeIdVerifier.acceptOneOf(SPIFFE_ID_1, SPIFFE_ID_2); @@ -159,6 +228,8 @@ void acceptMemberOf_rejectsSpiffeIdsInOtherTrustDomains() { void verifiers_rejectNullSpiffeId() { assertThrows(SpiffeVerificationException.class, () -> SpiffeIdVerifier.acceptId(SPIFFE_ID_1).verify(null, CHAIN)); + assertThrows(SpiffeVerificationException.class, + () -> SpiffeIdVerifier.acceptIdPrefix(SPIFFE_ID_1).verify(null, CHAIN)); assertThrows(SpiffeVerificationException.class, () -> SpiffeIdVerifier.acceptOneOf(SPIFFE_ID_1).verify(null, CHAIN)); assertThrows(SpiffeVerificationException.class, 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 26910b2f..6b9c6383 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 @@ -293,6 +293,48 @@ void getSslContext_spiffeIdVerifierFactories() throws Exception { assertEquals("SPIFFE ID spiffe://example.org/server in X.509 certificate is not accepted", cause.getMessage()); } + @Test + void getSslContext_spiffeIdVerifierIdPrefix() throws Exception { + TrustDomain trustDomain = TrustDomain.parse("example.org"); + CertAndKeyPair rootCa = createRootCA("C = US, O = SPIFFE", trustDomain.toIdString()); + X509Source clientSource = newX509Source(trustDomain, rootCa, "client"); + X509Source serverSource = newX509Source(trustDomain, rootCa, "server"); + + SpiffeId serverPrefix = SpiffeId.parse("spiffe://example.org/server"); + SpiffeId clientPrefix = SpiffeId.parse("spiffe://example.org/client"); + // "serv" is a prefix of the server's path as a string, but not on a segment boundary + SpiffeId partialSegmentPrefix = SpiffeId.parse("spiffe://example.org/serv"); + + SSLContext clientContext = SpiffeSslContextFactory.getSslContext( + SpiffeSslContextFactory.SslContextOptions.builder() + .x509Source(clientSource) + .spiffeIdVerifier(SpiffeIdVerifier.acceptIdPrefix(serverPrefix)) + .build()); + SSLContext serverContext = SpiffeSslContextFactory.getSslContext( + SpiffeSslContextFactory.SslContextOptions.builder() + .x509Source(serverSource) + .spiffeIdVerifier(SpiffeIdVerifier.acceptIdPrefix(clientPrefix)) + .build()); + + handshake(clientContext, serverContext); + + SSLContext rejectingClientContext = SpiffeSslContextFactory.getSslContext( + SpiffeSslContextFactory.SslContextOptions.builder() + .x509Source(clientSource) + .spiffeIdVerifier(SpiffeIdVerifier.acceptIdPrefix(partialSegmentPrefix)) + .build()); + + SSLHandshakeException e = assertThrows(SSLHandshakeException.class, + () -> handshake(rejectingClientContext, serverContext)); + Throwable cause = e; + while (cause != null && !(cause instanceof SpiffeVerificationException)) { + cause = cause.getCause(); + } + assertNotNull(cause, "the handshake should fail because the SPIFFE ID verifier rejects the server"); + assertEquals("SPIFFE ID spiffe://example.org/server in X.509 certificate does not match the prefix " + + "spiffe://example.org/serv", cause.getMessage()); + } + private static X509Source newX509Source(TrustDomain trustDomain, CertAndKeyPair rootCa, String path) throws Exception { CertAndKeyPair leaf = createCertificate("C = US, O = SPIRE", "C = US, O = SPIFFE",