Skip to content

feat(provider): add SpiffeIdVerifier.acceptIdPrefix - #528

Open
NopeCharon wants to merge 1 commit into
spiffe:mainfrom
NopeCharon:feat/spiffe-id-verifier-id-prefix
Open

NopeCharon wants to merge 1 commit into
spiffe:mainfrom
NopeCharon:feat/spiffe-id-verifier-id-prefix

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

What

Adds SpiffeIdVerifier.acceptIdPrefix(SpiffeId prefix), a factory method for a verifier that accepts a SPIFFE ID and any SPIFFE ID under its path:

SslContextOptions options = SslContextOptions
        .builder()
        .x509Source(x509Source)
        .spiffeIdVerifier(SpiffeIdVerifier.acceptIdPrefix(SpiffeId.parse("spiffe://example.org/backend")))
        .build();
SSLContext sslContext = SpiffeSslContextFactory.getSslContext(options);

Details:

  • A SPIFFE ID is accepted if it is in the trust domain of the prefix and its path is the prefix's path or extends it with more segments. The prefix is matched on segment boundaries. spiffe://example.org/backend accepts spiffe://example.org/backend and spiffe://example.org/backend/api. It rejects spiffe://example.org/backend-admin, spiffe://example.org/Backend/api and spiffe://other.org/backend/api.
  • Paths are compared case-sensitively, as the SPIFFE ID spec requires. Trust domains are already normalized by SpiffeId.
  • Rejections throw SpiffeVerificationException: SPIFFE ID <id> in X.509 certificate does not match the prefix <prefix>.
  • A prefix without a path (spiffe://example.org) throws IllegalArgumentException when the verifier is created, and the message points to acceptMemberOf. go-spiffe's MatchIDPrefix returns a matcher that rejects every ID in this case, because its constructors can't fail. Failing at creation reports the mistake when the SSLContext is built instead of on every handshake. A null prefix throws NullPointerException, like the other factory methods.
  • The provider README lists the method in the factory table and explains the segment-boundary matching. The README's custom verifier example was a hand-written path prefix check (getPath().startsWith("/backend/")), so it now shows a rule the factories don't cover: a Kubernetes service account name in any namespace. The changelog lists the method under Unreleased.

Why

Accepting every workload under a path is a common authorization rule, for example the workloads of a Kubernetes namespace (/ns/prod) or the SPIRE agents under /spire/agent. None of the factories added in #524 covers it, so callers had to write the check themselves. The obvious check, spiffeId.getPath().startsWith(prefix), also accepts paths that only share a string prefix: /backend would accept /backend-admin. go-spiffe 2.9.0 added this rule as spiffeid.MatchIDPrefix and tlsconfig.AuthorizeIDPrefix, and this follows its semantics.

Compatibility: this only adds a static method to the SpiffeIdVerifier interface, which is binary and source compatible for existing implementations and lambdas. :java-spiffe-provider:japicmp passes.

How tested

  • SpiffeIdVerifierTest:
    • accepts the prefix itself and IDs under it, including one parsed from an upper-case trust domain
    • with a multi-segment prefix (/ns/prod), accepts /ns/prod/sa/web and rejects /ns/production/sa/web, /ns and /prod
    • rejects /backend-admin, /backendapi, /Backend/api, /frontend/backend, the trust domain's root ID and IDs in other trust domains (other.org, example.org.other), checking the message
    • a prefix without a path and a null prefix are rejected when the verifier is created; a null SPIFFE ID is rejected
  • SpiffeSslContextFactoryTest: mutual TLS handshake between two in-memory SSLEngines where each side accepts the other with acceptIdPrefix. With the prefix spiffe://example.org/serv, which shares a string prefix with the server's /server path but not a segment boundary, the handshake fails with an SSLHandshakeException caused by the verifier's SpiffeVerificationException.
  • Commands, on JDK 17: ./gradlew :java-spiffe-provider:test :java-spiffe-provider:javadoc :java-spiffe-provider:japicmp (115 provider tests pass).

Accepting every workload under a path is a common authorization rule
(the workloads of a Kubernetes namespace, the agents under
/spire/agent), but none of the SpiffeIdVerifier factory methods covers
it, so callers have to write it by hand. The obvious hand-written
check, spiffeId.getPath().startsWith(prefix), also accepts paths that
only share a string prefix: /backend accepts /backend-admin. go-spiffe
2.9.0 added this rule as spiffeid.MatchIDPrefix and
tlsconfig.AuthorizeIDPrefix.

Add SpiffeIdVerifier.acceptIdPrefix(SpiffeId prefix). It accepts a
SPIFFE ID that is in the trust domain of the prefix and whose path is
the path of the prefix or extends it with more segments, so the prefix
is matched on segment boundaries. Paths are compared case-sensitively,
as the SPIFFE ID spec requires. Rejections throw
SpiffeVerificationException naming the peer SPIFFE ID and the prefix.

A prefix without a path is rejected with an IllegalArgumentException
when the verifier is created, pointing to acceptMemberOf. go-spiffe
returns a matcher that rejects every ID in that case because its
constructors cannot fail; failing at creation surfaces the mistake
when the SSLContext is built instead of on every handshake. A null
prefix throws NullPointerException, like the other factory methods.

Document the method in the provider README. The README's example of a
custom verifier was the hand-written path prefix check, so replace it
with a rule the factory methods don't cover. Add a changelog entry.

Adding a static method to an interface is binary compatible, and
japicmp passes.

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant