diff --git a/.buildkite/ftr_configs.yml b/.buildkite/ftr_configs.yml index 438e601a6d947..554abf0d98bb9 100644 --- a/.buildkite/ftr_configs.yml +++ b/.buildkite/ftr_configs.yml @@ -196,6 +196,7 @@ enabled: - x-pack/test/api_integration/apis/security/config.ts - x-pack/test/api_integration/apis/security_solution/config.ts - x-pack/test/api_integration/apis/spaces/config.ts + - x-pack/test/api_integration/apis/stats/config.ts - x-pack/test/api_integration/apis/status/config.ts - x-pack/test/api_integration/apis/synthetics/config.ts - x-pack/test/api_integration/apis/telemetry/config.ts diff --git a/src/plugins/usage_collection/server/routes/stats/stats.ts b/src/plugins/usage_collection/server/routes/stats/stats.ts index d98457d498244..0c9d28d81109f 100644 --- a/src/plugins/usage_collection/server/routes/stats/stats.ts +++ b/src/plugins/usage_collection/server/routes/stats/stats.ts @@ -72,13 +72,13 @@ export function registerStatsRoute({ const isExtended = requestQuery.extended === '' || requestQuery.extended; const isLegacy = requestQuery.legacy === '' || requestQuery.legacy; - let extended; + let extended = {}; if (isExtended) { const core = await context.core; - const { asCurrentUser } = core.elasticsearch.client; + const { asInternalUser } = core.elasticsearch.client; // as of https://github.com/elastic/kibana/pull/151082, usage will always be an empty object. - const clusterUuid = await getClusterUuid(asCurrentUser); + const clusterUuid = await getClusterUuid(asInternalUser); const extendedClusterUuid = isLegacy ? { clusterUuid } : { cluster_uuid: clusterUuid }; extended = { usage: {}, diff --git a/x-pack/test/api_integration/apis/kibana/stats/stats.js b/x-pack/test/api_integration/apis/kibana/stats/stats.js index e1f02979ca7ca..dd6432bcd0a61 100644 --- a/x-pack/test/api_integration/apis/kibana/stats/stats.js +++ b/x-pack/test/api_integration/apis/kibana/stats/stats.js @@ -30,8 +30,9 @@ export default function ({ getService }) { expect(isUUID(body.kibana.uuid)).to.be.ok(); }); - it('should return 401 for extended', async () => { - await supertestNoAuth.get('/api/stats?extended').auth(null, null).expect(401); + it('should return 200 for extended', async () => { + const { body } = await supertestNoAuth.get('/api/stats').expect(200); + expect(isUUID(body.kibana.uuid)).to.be.ok(); }); }); diff --git a/x-pack/test/api_integration/apis/stats/config.ts b/x-pack/test/api_integration/apis/stats/config.ts new file mode 100644 index 0000000000000..0b0f4b106a0e4 --- /dev/null +++ b/x-pack/test/api_integration/apis/stats/config.ts @@ -0,0 +1,24 @@ +/* + * 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 { FtrConfigProviderContext } from '@kbn/test'; + +export default async function ({ readConfigFile }: FtrConfigProviderContext) { + const baseIntegrationTestsConfig = await readConfigFile(require.resolve('../../config.ts')); + + return { + ...baseIntegrationTestsConfig.getAll(), + kbnTestServer: { + ...baseIntegrationTestsConfig.get('kbnTestServer'), + serverArgs: [ + ...baseIntegrationTestsConfig.get('kbnTestServer.serverArgs'), + '--status.allowAnonymous=true', + ], + }, + testFiles: [require.resolve('.')], + }; +} diff --git a/x-pack/test/api_integration/apis/stats/index.ts b/x-pack/test/api_integration/apis/stats/index.ts new file mode 100644 index 0000000000000..b786b51255a3f --- /dev/null +++ b/x-pack/test/api_integration/apis/stats/index.ts @@ -0,0 +1,14 @@ +/* + * 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 { FtrProviderContext } from '../../ftr_provider_context'; + +export default function ({ loadTestFile }: FtrProviderContext) { + describe('Stats API', () => { + loadTestFile(require.resolve('./stats')); + }); +} diff --git a/x-pack/test/api_integration/apis/stats/stats.ts b/x-pack/test/api_integration/apis/stats/stats.ts new file mode 100644 index 0000000000000..846943e54160e --- /dev/null +++ b/x-pack/test/api_integration/apis/stats/stats.ts @@ -0,0 +1,40 @@ +/* + * 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 expect from '@kbn/expect'; + +import { FtrProviderContext } from '../../ftr_provider_context'; + +export default function ({ getService }: FtrProviderContext) { + const supertest = getService('supertest'); + const supertestWithoutAuth = getService('supertestWithoutAuth'); + + describe('GET /api/stats', () => { + describe('When status.allowAnonymous is true', () => { + describe('when requesting extended stats', () => { + it('returns extended stats payload for authenticated requests', async () => { + const { body } = await supertest + .get('/api/stats?extended=true') + .set('kbn-xsrf', 'kibana') + .expect(200); + + expect(body.cluster_uuid).to.be.a('string'); + expect(body.usage).to.be.an('object'); + }); + it('returns extended stats payload for unauthenticated requests', async () => { + const { body } = await supertestWithoutAuth + .get('/api/stats?extended=true') + .set('kbn-xsrf', 'kibana') + .expect(200); + + expect(body.cluster_uuid).to.be.a('string'); + expect(body.usage).to.be.an('object'); + }); + }); + }); + }); +}