Skip to content

Commit

Permalink
[Enterprise Search] Import correct exception identifier (#161935)
Browse files Browse the repository at this point in the history
## Summary

The access control deletion function was importing the wrong
`isIndexNotFoundException` to identify when an index doesn't exist. This
would cause index deletion to fail if a search index is missing an
access control index.
  • Loading branch information
navarone-feekery authored Jul 14, 2023
1 parent 091643c commit 3077919
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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 { IScopedClusterClient } from '@kbn/core/server';

import { ElasticsearchResponseError } from '../../utils/identify_exceptions';

import { deleteAccessControlIndex } from './delete_access_control_index';

describe('deleteAccessControlIndex lib function', () => {
const mockClient = {
asCurrentUser: {
indices: {
delete: jest.fn(),
},
},
asInternalUser: {},
};

beforeEach(() => {
jest.clearAllMocks();
});

describe('when ACL index exists', () => {
it('should delete index', async () => {
mockClient.asCurrentUser.indices.delete.mockImplementation(() => true);
await expect(
deleteAccessControlIndex(mockClient as unknown as IScopedClusterClient, 'indexName')
).resolves.toEqual(true);
expect(mockClient.asCurrentUser.indices.delete).toHaveBeenCalledWith({
index: 'indexName',
});
});
});

describe('when ACL index is missing', () => {
const mockErrorRejection: ElasticsearchResponseError = {
meta: {
body: {
error: {
type: 'index_not_found_exception',
},
},
statusCode: 404,
},
name: 'ResponseError',
};

it('exits gracefully', async () => {
mockClient.asCurrentUser.indices.delete.mockImplementation(() => {
return Promise.reject(mockErrorRejection);
});
await expect(
deleteAccessControlIndex(mockClient as unknown as IScopedClusterClient, 'indexName')
).resolves.not.toThrowError();
expect(mockClient.asCurrentUser.indices.delete).toHaveBeenCalledWith({
index: 'indexName',
});
});
});

describe('when index exists but another error is thrown', () => {
const mockErrorRejection: ElasticsearchResponseError = {
meta: {
body: {
error: {
type: 'uncaught_exception',
},
},
statusCode: 500,
},
name: 'ResponseError',
};

it('throws the error', async () => {
mockClient.asCurrentUser.indices.delete.mockImplementation(() => {
return Promise.reject(mockErrorRejection);
});
await expect(
deleteAccessControlIndex(mockClient as unknown as IScopedClusterClient, 'indexName')
).rejects.toEqual(mockErrorRejection);
expect(mockClient.asCurrentUser.indices.delete).toHaveBeenCalledWith({
index: 'indexName',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* 2.0.
*/

import { isIndexNotFoundException } from '@kbn/core-saved-objects-migration-server-internal';
import { IScopedClusterClient } from '@kbn/core/server';

import { CONNECTORS_ACCESS_CONTROL_INDEX_PREFIX } from '../../../common/constants';
import { isIndexNotFoundException } from '../../utils/identify_exceptions';

export const deleteAccessControlIndex = async (client: IScopedClusterClient, index: string) => {
try {
await client.asCurrentUser.indices.delete({
return await client.asCurrentUser.indices.delete({
index: index.replace('search-', CONNECTORS_ACCESS_CONTROL_INDEX_PREFIX),
});
} catch (e) {
Expand Down

0 comments on commit 3077919

Please sign in to comment.