[CPS] Add @kbn/cps-server-utils package#254085
Merged
Merged
Conversation
Introduces a new server-side package that exposes `getSpaceNPRE`, a utility
to compute the Named Project Routing Expression (NPRE) for a given Kibana
space. Accepts either a space ID string or a `KibanaRequest` (extracting the
space from the URL path without depending on the `spaces` plugin).
The NPRE follows the convention `kibana_space_${spaceId}_default`, mapping
to the default data view for a given space.
Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
|
Pinging @elastic/kibana-core (Team:Core) |
Contributor
💚 Build Succeeded
Metrics [docs]Public APIs missing comments
History
|
afharo
approved these changes
Feb 20, 2026
afharo
reviewed
Feb 20, 2026
| export function deleteAction(drilldownState: DrilldownActionState) { | ||
| const { actionId, trigger } = drilldownState; | ||
| if (!uiActions.hasAction(actionId)) return; | ||
| export function getSpaceNPRE(spaceIdOrRequest: string | KibanaRequest): string { |
Member
Author
There was a problem hiding this comment.
Will add them in a follow-up PR, I don't want to retrigger a whole CI just for that.
gsoldevila
added a commit
that referenced
this pull request
Feb 23, 2026
Part of elastic/kibana-team#2829 ## Summary Builds on #254085 (`@kbn/cps-server-utils`) to allow callers of `ClusterClient.asScoped()` to control how the CPS `project_routing` header is injected on a per-client basis. ### `asScoped` options `ClusterClient.asScoped(request, opts?)` now accepts a typed `opts` argument. Three named specializations of `AsScopedOptions` are provided, each narrowing the `projectRouting` field: - `OriginOnlyRouting` (`projectRouting: 'origin-only'`, default) — preserves the existing behavior, injects `_alias:_origin`. - `AllProjectsRouting` (`projectRouting: 'all'`) — injects `_alias:*`, routing across all CPS-connected projects. - `SpaceNPRERouting` (`projectRouting: 'space'`) — derives the NPRE from the request URL and injects `kibana_space_${spaceId}_default`. Using `SpaceNPRERouting` requires passing a `ScopeableUrlRequest` (`KibanaRequest | UrlRequest`) as the first argument. This is enforced by an overload on `asScoped`, so passing an incompatible `FakeRequest` (without a `url`) is a compile-time error. ### New request types - `UrlRequest` — a minimal `FakeRequest` with a `url: URL` property, for use in non-HTTP contexts (e.g. background tasks) where no real `KibanaRequest` is available but space-level routing is needed. - `ScopeableUrlRequest` — `KibanaRequest | UrlRequest`, the type accepted by the `SpaceNPRERouting` overload of `asScoped`. Route handlers can pass their incoming `KibanaRequest` directly. ### `OnRequestHandlerFactory` A mandatory factory (`OnRequestHandlerFactory`) is passed to the `ClusterClient` constructor instead of a bare `OnRequestHandler`. The factory is called lazily per scope, producing the right handler based on the routing mode. The `projectRouting` field is a discriminated union: a `ScopeableUrlRequest` signals space routing, while `'origin-only'` and `'all'` cover the remaining modes. ### `getSpaceNPRE` in `@kbn/cps-server-utils` Broadened to accept `string | { url: URL }` instead of `string | KibanaRequest`, removing the coupling to `KibanaRequest`. Both `KibanaRequest` and `UrlRequest` satisfy this structural type. A defensive runtime guard is retained (with a documented `@throws`) to protect against JavaScript callers bypassing the type system. ### Known limitation `getSpaceNPRE` assumes the server base path is `/`. If Kibana were deployed with a custom `server.basePath` the space segment would not be stripped before matching, and the function would always resolve to the default space. CPS is a Serverless-only feature and Serverless deployments always run at the root path, so this is not a concern in practice today. This assumption is documented in the JSDoc. ### CLI setup client `cli_setup`'s `ClusterClient` is on-prem only; it uses `getRequestHandlerFactory(false)` (CPS disabled), which strips any `project_routing` header rather than no-oping. Made with [Cursor](https://cursor.com) --------- Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Rudolf Meijering <skaapgif@gmail.com>
bhapas
pushed a commit
to bhapas/kibana
that referenced
this pull request
Feb 25, 2026
) Part of elastic/kibana-team#2829 ## Summary Builds on elastic#254085 (`@kbn/cps-server-utils`) to allow callers of `ClusterClient.asScoped()` to control how the CPS `project_routing` header is injected on a per-client basis. ### `asScoped` options `ClusterClient.asScoped(request, opts?)` now accepts a typed `opts` argument. Three named specializations of `AsScopedOptions` are provided, each narrowing the `projectRouting` field: - `OriginOnlyRouting` (`projectRouting: 'origin-only'`, default) — preserves the existing behavior, injects `_alias:_origin`. - `AllProjectsRouting` (`projectRouting: 'all'`) — injects `_alias:*`, routing across all CPS-connected projects. - `SpaceNPRERouting` (`projectRouting: 'space'`) — derives the NPRE from the request URL and injects `kibana_space_${spaceId}_default`. Using `SpaceNPRERouting` requires passing a `ScopeableUrlRequest` (`KibanaRequest | UrlRequest`) as the first argument. This is enforced by an overload on `asScoped`, so passing an incompatible `FakeRequest` (without a `url`) is a compile-time error. ### New request types - `UrlRequest` — a minimal `FakeRequest` with a `url: URL` property, for use in non-HTTP contexts (e.g. background tasks) where no real `KibanaRequest` is available but space-level routing is needed. - `ScopeableUrlRequest` — `KibanaRequest | UrlRequest`, the type accepted by the `SpaceNPRERouting` overload of `asScoped`. Route handlers can pass their incoming `KibanaRequest` directly. ### `OnRequestHandlerFactory` A mandatory factory (`OnRequestHandlerFactory`) is passed to the `ClusterClient` constructor instead of a bare `OnRequestHandler`. The factory is called lazily per scope, producing the right handler based on the routing mode. The `projectRouting` field is a discriminated union: a `ScopeableUrlRequest` signals space routing, while `'origin-only'` and `'all'` cover the remaining modes. ### `getSpaceNPRE` in `@kbn/cps-server-utils` Broadened to accept `string | { url: URL }` instead of `string | KibanaRequest`, removing the coupling to `KibanaRequest`. Both `KibanaRequest` and `UrlRequest` satisfy this structural type. A defensive runtime guard is retained (with a documented `@throws`) to protect against JavaScript callers bypassing the type system. ### Known limitation `getSpaceNPRE` assumes the server base path is `/`. If Kibana were deployed with a custom `server.basePath` the space segment would not be stripped before matching, and the function would always resolve to the default space. CPS is a Serverless-only feature and Serverless deployments always run at the root path, so this is not a concern in practice today. This assumption is documented in the JSDoc. ### CLI setup client `cli_setup`'s `ClusterClient` is on-prem only; it uses `getRequestHandlerFactory(false)` (CPS disabled), which strips any `project_routing` header rather than no-oping. Made with [Cursor](https://cursor.com) --------- Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Rudolf Meijering <skaapgif@gmail.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.
Summary
Introduces
@kbn/cps-server-utils, a newshared-serverpackage that exposes agetSpaceNPREutility for computing the Named Project Routing Expression (NPRE) for a Kibana space.The NPRE follows the convention
kibana_space_${spaceId}_default, which identifies the default NPRE for a given space in Elasticsearch.The function accepts two forms:
getSpaceNPRE(spaceIdOrRequest: string | KibanaRequest)string: directly from a space ID (empty string falls back to"default")KibanaRequest: extracts the space from the request URL path without depending on thespacespluginMade with Cursor