Skip to content

feat(spiffeid): add SpiffeId.fromPath and path append and replace methods - #529

Open
NopeCharon wants to merge 1 commit into
spiffe:mainfrom
NopeCharon:feat/spiffe-id-path-methods
Open

NopeCharon wants to merge 1 commit into
spiffe:mainfrom
NopeCharon:feat/spiffe-id-path-methods

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

What

Adds methods to build a SpiffeId from a trust domain and a path, and to derive a SPIFFE ID with an extended or different path:

TrustDomain trustDomain = TrustDomain.parse("example.org");

SpiffeId backend = SpiffeId.fromPath(trustDomain, "/backend");   // spiffe://example.org/backend
SpiffeId api = backend.appendPath("/api/v1");                     // spiffe://example.org/backend/api/v1
SpiffeId sameApi = backend.appendSegments("api", "v1");           // spiffe://example.org/backend/api/v1
SpiffeId frontend = api.replacePath("/frontend");                 // spiffe://example.org/frontend
SpiffeId sameFrontend = api.replaceSegments("frontend");          // spiffe://example.org/frontend

Details:

  • SpiffeId.fromPath(TrustDomain, String) validates the path with validatePath, so the rules and error messages match parse: leading /, no trailing slash, no empty or dot segments, and only the allowed characters.
  • appendPath and appendSegments keep the trust domain and extend the path. replacePath and replaceSegments keep the trust domain and replace the path. SpiffeId stays immutable; each method returns a new instance.
  • As in go-spiffe, the empty path is the path of the trust domain's own SPIFFE ID. fromPath(td, "") and replacePath("") return spiffe://<trust-domain>, like fromSegments(td) and replaceSegments() with no segments, and appendPath("") returns the same SPIFFE ID. This keeps SpiffeId.fromPath(id.getTrustDomain(), id.getPath()) equal to id for every SPIFFE ID, including one without a path. validatePath itself is unchanged and still rejects an empty path.
  • Null arguments throw NullPointerException. fromSegments and appendSegments share a joinPathSegments helper, which now gives a null segments array a message (segments must not be null) instead of a bare NullPointerException. fromSegments is otherwise unchanged.
  • The core README gets a "SPIFFE IDs" section on parsing, building and deriving SPIFFE IDs, which it didn't cover. The changelog lists the methods under Unreleased.

Why

Paths usually come as strings: in configuration, in SPIRE registration entries, and in templates like /ns/<namespace>/sa/<service-account>. To turn one into a SpiffeId, callers had to concatenate trustDomain.toIdString() with the path and parse the result, or split the path into segments for fromSegments. Deriving one SPIFFE ID from another, such as a child of a workload's ID or a sibling in the same trust domain, had the same problem. go-spiffe provides these as spiffeid.FromPath and ID.AppendPath, AppendSegments, ReplacePath and ReplaceSegments. The Printf-style variants (FromPathf, ...) are left out because String.format covers them in Java.

Compatibility: this only adds methods to the final SpiffeId class. :java-spiffe-core:japicmp passes.

How tested

  • SpiffeIdTest:
    • fromPath with valid paths, including mixed case, ... segments and the empty path, returns the expected trust domain, path and string
    • fromPath, appendPath and replacePath reject every invalid path in the existing validatePath cases, with the same messages
    • appendSegments and replaceSegments reject null, empty, . and .. segments and segments with / or other invalid characters
    • fromPath(id.getTrustDomain(), id.getPath()) equals id for IDs with and without a path
    • append works on IDs with and without a path; append and replace leave the original ID unchanged and handle the empty path and no segments as described above
    • null trust domain, path and segments arguments throw NullPointerException with the expected messages
  • Commands, on JDK 17: ./gradlew :java-spiffe-core:test :java-spiffe-core:javadoc :java-spiffe-core:japicmp (683 core tests pass).

…hods

A SpiffeId can be parsed from a full string or built from path
segments, but not from a trust domain and a path such as
/ns/default/sa/backend, which is how paths are usually configured and
how other systems (SPIRE registration entries, Kubernetes templates)
hand them over. Callers have to concatenate the trust domain ID string
and parse the result, or split the path into segments themselves.
Deriving a SPIFFE ID from another one, for example a child of a
workload's ID or a sibling in the same trust domain, has the same
problem. go-spiffe provides these as spiffeid.FromPath and
ID.AppendPath, AppendSegments, ReplacePath and ReplaceSegments.

Add:

- SpiffeId.fromPath(TrustDomain, String): a SPIFFE ID in the trust
  domain with the given path, validated with validatePath.
- appendPath(String) and appendSegments(String...): a SPIFFE ID in the
  same trust domain with the path extended.
- replacePath(String) and replaceSegments(String...): a SPIFFE ID in
  the same trust domain with a different path.

Paths and segments are validated with the same rules and error
messages as parse and fromSegments. As in go-spiffe, an empty path is
the path of the trust domain's own SPIFFE ID: fromPath and replacePath
return spiffe://<trust-domain> for it, as fromSegments and
replaceSegments do with no segments, and appendPath returns the same
SPIFFE ID. This keeps fromPath(id.getTrustDomain(), id.getPath())
equal to id for every SPIFFE ID, including one without a path. Null
arguments throw NullPointerException.

fromSegments and appendSegments share a joinPathSegments helper, which
now rejects a null segments array with a message instead of a bare
NullPointerException. The behavior of fromSegments is otherwise
unchanged.

Document building and deriving SPIFFE IDs in the core README, which
did not cover SPIFFE IDs, and add a changelog entry. The new methods
only add API to a final class, 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