kibana/test/server_integration/http/platform/status.ts
Christiane (Tina) Heiligers 3a68f8b3ae
[http] api_integration tests handle internal route restriction (#192407)
fix https://github.com/elastic/kibana/issues/192052
## Summary

Internal APIs will be
[restricted](https://github.com/elastic/kibana/issues/163654) from
public access as of 9.0.0. In non-serverless environments, this breaking
change will result in a 400 error if an external request is made to an
internal Kibana API (route `access` option as `"internal"` or
`"public"`).
This PR allows API owners of non-xpack plugins to run their `ftr` API
integration tests against the restriction and adds examples of how to
handle it.

### Checklist
- [x] [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


Note to reviewers: The header needed to allow access to internal apis
shouldn't change your test output, with or without the restriction
enabled.

### How to test the changes work:
#### Non x-pack:
1. Set `server.restrictInternalApis: true` in `test/common/config.js`
2. Ensure your tests pass

#### x-pack:
1. Set `server.restrictInternalApis: true` in
`x-pack/test/api_integration/apis/security/config.ts`
2. Ensure the spaces tests pass

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
2024-09-12 09:23:10 +02:00

77 lines
3.2 KiB
TypeScript

/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import expect from '@kbn/expect';
import type { ServiceStatus, ServiceStatusLevels } from '@kbn/core/server';
import { X_ELASTIC_INTERNAL_ORIGIN_REQUEST } from '@kbn/core-http-common';
import { FtrProviderContext } from '../../services/types';
type ServiceStatusSerialized = Omit<ServiceStatus, 'level'> & { level: string };
export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const retry = getService('retry');
const getStatus = async (pluginName: string): Promise<ServiceStatusSerialized> => {
const resp = await supertest.get('/api/status');
return resp.body.status.plugins[pluginName];
};
// max debounce of the status observable + 1
const statusPropagation = () => new Promise((resolve) => setTimeout(resolve, 501));
const setStatus = async <T extends keyof typeof ServiceStatusLevels>(level: T) =>
supertest
.post(`/internal/status_plugin_a/status/set?level=${level}`)
.set('kbn-xsrf', 'xxx')
.set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana')
.expect(200);
describe('status service', function () {
this.tags('skipFIPS');
// This test must come first because the timeout only applies to the initial emission
it("returns a timeout for status check that doesn't emit after 30s", async () => {
let aStatus = await getStatus('statusPluginA');
expect(aStatus === undefined || aStatus.level === 'unavailable').to.eql(true);
// Status will remain in unavailable until the custom status check times out
// Keep polling until that condition ends, up to a timeout
await retry.waitForWithTimeout(`Status check to timeout`, 40_000, async () => {
aStatus = await getStatus('statusPluginA');
return aStatus?.summary === 'Status check timed out after 30s';
});
expect(aStatus.level).to.eql('unavailable');
expect(aStatus.summary).to.eql('Status check timed out after 30s');
});
it('propagates status issues to dependencies', async () => {
await setStatus('degraded');
await retry.waitForWithTimeout(
`statusPluginA status to update`,
5_000,
async () => (await getStatus('statusPluginA'))?.level === 'degraded'
);
await statusPropagation();
expect((await getStatus('statusPluginA')).level).to.eql('degraded');
expect((await getStatus('statusPluginB')).level).to.eql('degraded');
await setStatus('available');
await retry.waitForWithTimeout(
`statusPluginA status to update`,
5_000,
async () => (await getStatus('statusPluginA')).level === 'available'
);
await statusPropagation();
expect((await getStatus('statusPluginA')).level).to.eql('available');
expect((await getStatus('statusPluginB')).level).to.eql('available');
});
});
}