Repository navigation
feat(spiffeid): add SpiffeId.fromPath and path append and replace methods - #529
Open
NopeCharon wants to merge 1 commit into
Open
NopeCharon wants to merge 1 commit into
NopeCharon wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds methods to build a
SpiffeIdfrom a trust domain and a path, and to derive a SPIFFE ID with an extended or different path:Details:
SpiffeId.fromPath(TrustDomain, String)validates the path withvalidatePath, so the rules and error messages matchparse: leading/, no trailing slash, no empty or dot segments, and only the allowed characters.appendPathandappendSegmentskeep the trust domain and extend the path.replacePathandreplaceSegmentskeep the trust domain and replace the path.SpiffeIdstays immutable; each method returns a new instance.fromPath(td, "")andreplacePath("")returnspiffe://<trust-domain>, likefromSegments(td)andreplaceSegments()with no segments, andappendPath("")returns the same SPIFFE ID. This keepsSpiffeId.fromPath(id.getTrustDomain(), id.getPath())equal toidfor every SPIFFE ID, including one without a path.validatePathitself is unchanged and still rejects an empty path.NullPointerException.fromSegmentsandappendSegmentsshare ajoinPathSegmentshelper, which now gives a null segments array a message (segments must not be null) instead of a bareNullPointerException.fromSegmentsis otherwise unchanged.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 aSpiffeId, callers had to concatenatetrustDomain.toIdString()with the path and parse the result, or split the path into segments forfromSegments. 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 asspiffeid.FromPathandID.AppendPath,AppendSegments,ReplacePathandReplaceSegments. ThePrintf-style variants (FromPathf, ...) are left out becauseString.formatcovers them in Java.Compatibility: this only adds methods to the final
SpiffeIdclass.:java-spiffe-core:japicmppasses.How tested
SpiffeIdTest:fromPathwith valid paths, including mixed case,...segments and the empty path, returns the expected trust domain, path and stringfromPath,appendPathandreplacePathreject every invalid path in the existingvalidatePathcases, with the same messagesappendSegmentsandreplaceSegmentsreject null, empty,.and..segments and segments with/or other invalid charactersfromPath(id.getTrustDomain(), id.getPath())equalsidfor IDs with and without a pathNullPointerExceptionwith the expected messages./gradlew :java-spiffe-core:test :java-spiffe-core:javadoc :java-spiffe-core:japicmp(683 core tests pass).