mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[@kbn/securitysolution-es-utils] remove transport API in favour of typed public API (#113717)
* remove transport API in favour of typed public API * put elasticsearch_client back * fix create index call * fix setpolicy * fix unit tests in SecuritySolution
This commit is contained in:
parent
d8b4f4bdcb
commit
1f60a1662f
16 changed files with 28 additions and 50 deletions
|
@ -6,7 +6,7 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
// See the reference(s) below on explanations about why -000001 was chosen and
|
||||
// why the is_write_index is true as well as the bootstrapping step which is needed.
|
||||
|
@ -16,9 +16,8 @@ export const createBootstrapIndex = async (
|
|||
index: string
|
||||
): Promise<unknown> => {
|
||||
return (
|
||||
await esClient.transport.request({
|
||||
path: `/${index}-000001`,
|
||||
method: 'PUT',
|
||||
await esClient.indices.create({
|
||||
index: `${index}-000001`,
|
||||
body: {
|
||||
aliases: {
|
||||
[index]: {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const deleteAllIndex = async (
|
||||
esClient: ElasticsearchClient,
|
||||
|
|
|
@ -6,16 +6,14 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const deletePolicy = async (
|
||||
esClient: ElasticsearchClient,
|
||||
policy: string
|
||||
): Promise<unknown> => {
|
||||
return (
|
||||
await esClient.transport.request({
|
||||
path: `/_ilm/policy/${policy}`,
|
||||
method: 'DELETE',
|
||||
})
|
||||
).body;
|
||||
// @ts-expect-error policy_id is required by mistake. fixed in the v8.0
|
||||
(await esClient.ilm.deleteLifecycle({ policy })).body
|
||||
);
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const deleteTemplate = async (
|
||||
esClient: ElasticsearchClient,
|
||||
|
|
|
@ -10,12 +10,6 @@
|
|||
// as these types aren't part of any package yet. Once they are, remove this completely
|
||||
|
||||
import type { KibanaClient } from '@elastic/elasticsearch/api/kibana';
|
||||
import type {
|
||||
ApiResponse,
|
||||
TransportRequestOptions,
|
||||
TransportRequestParams,
|
||||
TransportRequestPromise,
|
||||
} from '@elastic/elasticsearch/lib/Transport';
|
||||
|
||||
/**
|
||||
* Client used to query the elasticsearch cluster.
|
||||
|
@ -25,11 +19,4 @@ import type {
|
|||
export type ElasticsearchClient = Omit<
|
||||
KibanaClient,
|
||||
'connectionPool' | 'transport' | 'serializer' | 'extend' | 'child' | 'close'
|
||||
> & {
|
||||
transport: {
|
||||
request(
|
||||
params: TransportRequestParams,
|
||||
options?: TransportRequestOptions
|
||||
): TransportRequestPromise<ApiResponse>;
|
||||
};
|
||||
};
|
||||
>;
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
interface AliasesResponse {
|
||||
[indexName: string]: {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
/**
|
||||
* Retrieves the count of documents in a given index
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const getIndexExists = async (
|
||||
esClient: ElasticsearchClient,
|
||||
|
|
|
@ -5,17 +5,15 @@
|
|||
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
||||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const getPolicyExists = async (
|
||||
esClient: ElasticsearchClient,
|
||||
policy: string
|
||||
): Promise<boolean> => {
|
||||
try {
|
||||
await esClient.transport.request({
|
||||
path: `/_ilm/policy/${policy}`,
|
||||
method: 'GET',
|
||||
await esClient.ilm.getLifecycle({
|
||||
policy,
|
||||
});
|
||||
// Return true that there exists a policy which is not 404 or some error
|
||||
// Since there is not a policy exists API, this is how we create one by calling
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const getTemplateExists = async (
|
||||
esClient: ElasticsearchClient,
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const readIndex = async (esClient: ElasticsearchClient, index: string): Promise<unknown> => {
|
||||
return esClient.indices.get({
|
||||
|
|
|
@ -6,16 +6,14 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const readPrivileges = async (
|
||||
esClient: ElasticsearchClient,
|
||||
index: string
|
||||
): Promise<unknown> => {
|
||||
return (
|
||||
await esClient.transport.request({
|
||||
path: '/_security/user/_has_privileges',
|
||||
method: 'POST',
|
||||
await esClient.security.hasPrivileges({
|
||||
body: {
|
||||
cluster: [
|
||||
'all',
|
||||
|
|
|
@ -5,8 +5,7 @@
|
|||
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
||||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const setPolicy = async (
|
||||
esClient: ElasticsearchClient,
|
||||
|
@ -14,9 +13,8 @@ export const setPolicy = async (
|
|||
body: Record<string, unknown>
|
||||
): Promise<unknown> => {
|
||||
return (
|
||||
await esClient.transport.request({
|
||||
path: `/_ilm/policy/${policy}`,
|
||||
method: 'PUT',
|
||||
await esClient.ilm.putLifecycle({
|
||||
policy,
|
||||
body,
|
||||
})
|
||||
).body;
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
import { ElasticsearchClient } from '../elasticsearch_client';
|
||||
import type { ElasticsearchClient } from '../elasticsearch_client';
|
||||
|
||||
export const setTemplate = async (
|
||||
esClient: ElasticsearchClient,
|
||||
|
|
|
@ -35,8 +35,8 @@ type SecuritySolutionRequestHandlerContextMock = SecuritySolutionRequestHandlerC
|
|||
asCurrentUser: {
|
||||
updateByQuery: jest.Mock;
|
||||
search: jest.Mock;
|
||||
transport: {
|
||||
request: jest.Mock;
|
||||
security: {
|
||||
hasPrivileges: jest.Mock;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
|
|
@ -19,7 +19,7 @@ describe('read_privileges route', () => {
|
|||
server = serverMock.create();
|
||||
({ context } = requestContextMock.createTools());
|
||||
|
||||
context.core.elasticsearch.client.asCurrentUser.transport.request.mockResolvedValue({
|
||||
context.core.elasticsearch.client.asCurrentUser.security.hasPrivileges.mockResolvedValue({
|
||||
body: getMockPrivilegesResult(),
|
||||
});
|
||||
|
||||
|
@ -65,7 +65,7 @@ describe('read_privileges route', () => {
|
|||
});
|
||||
|
||||
test('returns 500 when bad response from cluster', async () => {
|
||||
context.core.elasticsearch.client.asCurrentUser.transport.request.mockResolvedValue(
|
||||
context.core.elasticsearch.client.asCurrentUser.security.hasPrivileges.mockResolvedValue(
|
||||
elasticsearchClientMock.createErrorTransportRequestPromise(new Error('Test error'))
|
||||
);
|
||||
const response = await server.inject(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue