forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] Replace Console API with internal API (elastic#186899)
## Summary Closes elastic#160194 Parts of the [Fleet debugger](https://github.com/elastic/kibana/blob/main/x-pack/plugins/fleet/dev_docs/fleet_debugger.md) currently use the Console API to query Fleet indices and Ingest Saved Objects. This PR replaces those uses with internal API endpoints that use Elasticsearch and Saved Objects clients. Affected parts: - Fleet Index Debugger: - This component directly queries some Fleet indices. - The new service uses an Elasticsearch client. - There should be no difference with respect to the previous behaviour. - Saved Object Debugger: - This component directly queries the `.kibana-ingest` index to retrieve saved objects of a given type. - Based on the type, the existing saved object names are retrieved: this is now done using a Saved Objects client and should work the same. - When a name is selected, the actual saved object is fetched: this is now done using a Saved Objects client and there are small differences in the result (no information is lost). The following example shows the difference between the saved object response before and after this change: <details> <summary>Before</summary> ```json { "total": { "value": 1, "relation": "eq" }, "max_score": 0.9808291, "hits": [ { "_index": ".kibana_ingest_8.15.0_001", "_id": "ingest-agent-policies:174cc817-06d7-4e2c-88b3-44d7cb5322b6", "_score": 0.9808291, "_source": { "ingest-agent-policies": { "name": "Agent policy 1", "description": "", "namespace": "default", "monitoring_enabled": [ "logs", "metrics" ], "inactivity_timeout": 1209600, "is_protected": false, "status": "active", "is_managed": false, "revision": 1, "updated_at": "2024-06-24T08:15:37.813Z", "updated_by": "elastic", "schema_version": "1.1.1" }, "type": "ingest-agent-policies", "references": [], "managed": false, "coreMigrationVersion": "8.8.0", "typeMigrationVersion": "10.3.0", "updated_at": "2024-06-24T08:15:37.813Z", "created_at": "2024-06-24T08:15:37.813Z" } } ] } ``` </details> <details> <summary>After</summary> ```json [ { "type": "ingest-agent-policies", "id": "174cc817-06d7-4e2c-88b3-44d7cb5322b6", "attributes": { "name": "Agent policy 1", "description": "", "namespace": "default", "monitoring_enabled": [ "logs", "metrics" ], "inactivity_timeout": 1209600, "is_protected": false, "status": "active", "is_managed": false, "revision": 1, "updated_at": "2024-06-24T08:15:37.813Z", "updated_by": "elastic", "schema_version": "1.1.1" }, "references": [], "managed": false, "updated_at": "2024-06-24T08:15:37.813Z", "created_at": "2024-06-24T08:15:37.813Z", "version": "WzQ2OCwxXQ==", "coreMigrationVersion": "8.8.0", "typeMigrationVersion": "10.3.0", "score": 0.9808291 } ] ``` </details> ### Testing Go to `app/fleet/_debug` and check the various parts described above. ### Screenshots <img width="1918" alt="Screenshot 2024-06-26 at 15 28 07" src="https://github.com/elastic/kibana/assets/23701614/8541e4e6-1f02-4a31-936b-e5e24dd1994c"> <img width="1918" alt="Screenshot 2024-06-26 at 15 27 52" src="https://github.com/elastic/kibana/assets/23701614/80d0e041-115f-42b5-95ab-cf2a601b74c8"> ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- Loading branch information
1 parent
31e132b
commit 028994c
Showing
11 changed files
with
228 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { TypeOf } from '@kbn/config-schema'; | ||
|
||
import type { FleetRequestHandler } from '../../types'; | ||
import { fetchIndex, fetchSavedObjectNames, fetchSavedObjects } from '../../services/debug'; | ||
import type { | ||
FetchIndexRequestSchema, | ||
FetchSavedObjectNamesRequestSchema, | ||
FetchSavedObjectsRequestSchema, | ||
} from '../../types/rest_spec/debug'; | ||
|
||
export const fetchIndexHandler: FleetRequestHandler< | ||
undefined, | ||
undefined, | ||
TypeOf<typeof FetchIndexRequestSchema.body> | ||
> = async (context, request, response) => { | ||
const coreContext = await context.core; | ||
const esClient = coreContext.elasticsearch.client.asInternalUser; | ||
const res = await fetchIndex(esClient, request.body.index); | ||
return response.ok({ body: res }); | ||
}; | ||
|
||
export const fetchSavedObjectsHandler: FleetRequestHandler< | ||
undefined, | ||
undefined, | ||
TypeOf<typeof FetchSavedObjectsRequestSchema.body> | ||
> = async (context, request, response) => { | ||
const soClient = (await context.fleet).internalSoClient; | ||
const res = await fetchSavedObjects(soClient, request.body.type, request.body.name); | ||
return response.ok({ body: res }); | ||
}; | ||
|
||
export const fetchSavedObjectNamesHandler: FleetRequestHandler< | ||
undefined, | ||
undefined, | ||
TypeOf<typeof FetchSavedObjectNamesRequestSchema.body> | ||
> = async (context, request, response) => { | ||
const soClient = (await context.fleet).internalSoClient; | ||
const res = await fetchSavedObjectNames(soClient, request.body.type); | ||
return response.ok({ body: res }); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { FleetAuthzRouter } from '../../services/security'; | ||
|
||
import { FLEET_DEBUG_ROUTES } from '../../constants'; | ||
import { API_VERSIONS } from '../../../common/constants'; | ||
|
||
import { | ||
FetchIndexRequestSchema, | ||
FetchSavedObjectNamesRequestSchema, | ||
FetchSavedObjectsRequestSchema, | ||
} from '../../types/rest_spec/debug'; | ||
|
||
import { | ||
fetchIndexHandler, | ||
fetchSavedObjectNamesHandler, | ||
fetchSavedObjectsHandler, | ||
} from './handler'; | ||
|
||
export const registerRoutes = (router: FleetAuthzRouter) => { | ||
router.versioned | ||
.post({ | ||
path: FLEET_DEBUG_ROUTES.INDEX_PATTERN, | ||
access: 'internal', | ||
fleetAuthz: { | ||
fleet: { all: true }, | ||
}, | ||
}) | ||
.addVersion( | ||
{ | ||
version: API_VERSIONS.internal.v1, | ||
validate: { request: FetchIndexRequestSchema }, | ||
}, | ||
fetchIndexHandler | ||
); | ||
|
||
router.versioned | ||
.post({ | ||
path: FLEET_DEBUG_ROUTES.SAVED_OBJECTS_PATTERN, | ||
access: 'internal', | ||
fleetAuthz: { | ||
fleet: { all: true }, | ||
}, | ||
}) | ||
.addVersion( | ||
{ | ||
version: API_VERSIONS.internal.v1, | ||
validate: { request: FetchSavedObjectsRequestSchema }, | ||
}, | ||
fetchSavedObjectsHandler | ||
); | ||
|
||
router.versioned | ||
.post({ | ||
path: FLEET_DEBUG_ROUTES.SAVED_OBJECT_NAMES_PATTERN, | ||
access: 'internal', | ||
fleetAuthz: { | ||
fleet: { all: true }, | ||
}, | ||
}) | ||
.addVersion( | ||
{ | ||
version: API_VERSIONS.internal.v1, | ||
validate: { request: FetchSavedObjectNamesRequestSchema }, | ||
}, | ||
fetchSavedObjectNamesHandler | ||
); | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server'; | ||
|
||
export async function fetchIndex(esClient: ElasticsearchClient, index: string) { | ||
return esClient.search({ index }); | ||
} | ||
|
||
export async function fetchSavedObjects( | ||
soClient: SavedObjectsClientContract, | ||
type: string, | ||
name: string | ||
) { | ||
return soClient.find({ | ||
type, | ||
search: `\"${name}\"`, // Search for phrase | ||
searchFields: ['name'], // SO type automatically inferred | ||
}); | ||
} | ||
|
||
export async function fetchSavedObjectNames(soClient: SavedObjectsClientContract, type: string) { | ||
return soClient.find({ | ||
type, | ||
aggs: { | ||
names: { | ||
terms: { field: `${type}.attributes.name` }, // cf. SavedObjectsFindOptions definition in packages/core/saved-objects/core-saved-objects-api-server/src/apis/find.ts | ||
}, | ||
}, | ||
}); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
|
||
export const FetchIndexRequestSchema = { | ||
body: schema.object({ | ||
index: schema.string(), | ||
}), | ||
}; | ||
|
||
export const FetchSavedObjectsRequestSchema = { | ||
body: schema.object({ | ||
type: schema.string(), | ||
name: schema.string(), | ||
}), | ||
}; | ||
|
||
export const FetchSavedObjectNamesRequestSchema = { | ||
body: schema.object({ | ||
type: schema.string(), | ||
}), | ||
}; |