Initialize session index even if the legacy template API is not available. (#164714)

## Summary

Elasticsearch [legacy template
API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates-v1.html)
isn't available in the Serverless offering (returns 410), but it
shouldn't prevent session index initialization.

## How to test
1. Run ES Serverless with `./gradlew :run`
2. Run Kibana with `yarn start --serverless
--elasticsearch.serviceAccountToken=AAEAAWVsYXN0aWMva2liYW5hL2tpYmFuYS1kZXY6VVVVVVVVTEstKiBaNA
--no-dev-credentials`
This commit is contained in:
Aleh Zasypkin 2023-08-24 17:43:31 +02:00 committed by GitHub
parent acedd23097
commit e83cfabb92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 5 deletions

View file

@ -100,6 +100,54 @@ describe('Session index', () => {
expect(mockElasticsearchClient.indices.create).not.toHaveBeenCalled();
});
it('does not delete legacy index template if the legacy template API is not available (410)', async () => {
const goneError = new errors.ResponseError(
securityMock.createApiResponse(
securityMock.createApiResponse({ body: { type: 'And it is gone!' }, statusCode: 410 })
)
);
mockElasticsearchClient.indices.existsTemplate.mockRejectedValueOnce(goneError);
mockElasticsearchClient.indices.existsIndexTemplate.mockResponse(false);
mockElasticsearchClient.indices.exists.mockResponse(false);
await sessionIndex.initialize();
assertExistenceChecksPerformed();
expect(mockElasticsearchClient.indices.deleteTemplate).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.deleteIndexTemplate).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.putAlias).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.getMapping).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.putMapping).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.create).toHaveBeenCalledWith(
getSessionIndexSettings({ indexName, aliasName })
);
});
it('does not delete legacy index template if the legacy template API is not available (404)', async () => {
const goneError = new errors.ResponseError(
securityMock.createApiResponse(
securityMock.createApiResponse({ body: { type: 'And it is gone!' }, statusCode: 404 })
)
);
mockElasticsearchClient.indices.existsTemplate.mockRejectedValueOnce(goneError);
mockElasticsearchClient.indices.existsIndexTemplate.mockResponse(false);
mockElasticsearchClient.indices.exists.mockResponse(false);
await sessionIndex.initialize();
assertExistenceChecksPerformed();
expect(mockElasticsearchClient.indices.deleteTemplate).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.deleteIndexTemplate).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.putAlias).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.getMapping).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.putMapping).not.toHaveBeenCalled();
expect(mockElasticsearchClient.indices.create).toHaveBeenCalledWith(
getSessionIndexSettings({ indexName, aliasName })
);
});
it('deletes legacy index template if needed and creates index if it does not exist', async () => {
mockElasticsearchClient.indices.existsTemplate.mockResponse(true);
mockElasticsearchClient.indices.existsIndexTemplate.mockResponse(false);

View file

@ -26,7 +26,7 @@ import type { AuditLogger } from '../audit';
import { sessionCleanupConcurrentLimitEvent, sessionCleanupEvent } from '../audit';
import { AnonymousAuthenticationProvider } from '../authentication';
import type { ConfigType } from '../config';
import { getDetailedErrorMessage } from '../errors';
import { getDetailedErrorMessage, getErrorStatusCode } from '../errors';
export interface SessionIndexOptions {
readonly elasticsearchClient: ElasticsearchClient;
@ -403,10 +403,15 @@ export class SessionIndex {
}
);
} catch (err) {
this.options.logger.error(
`Failed to check if session legacy index template exists: ${err.message}`
);
return reject(err);
// The Template API is deprecated and may become unavailable at some point (404 Not Found). It's also
// unavailable in the Serverless offering (410 Gone). In either of these cases, we should disregard the error.
const errorStatusCode = getErrorStatusCode(err);
if (errorStatusCode !== 404 && errorStatusCode !== 410) {
this.options.logger.error(
`Failed to check if session legacy index template exists: ${err.message}`
);
return reject(err);
}
}
if (legacyIndexTemplateExists) {