diff --git a/x-pack/plugins/fleet/common/constants/output.ts b/x-pack/plugins/fleet/common/constants/output.ts index ffa626fbe2ba7..fdf2075c541a3 100644 --- a/x-pack/plugins/fleet/common/constants/output.ts +++ b/x-pack/plugins/fleet/common/constants/output.ts @@ -13,7 +13,7 @@ export const outputType = { Elasticsearch: 'elasticsearch', Logstash: 'logstash', Kafka: 'kafka', - RemoteElasticsearch: 'remote-elasticsearch', + RemoteElasticsearch: 'remote_elasticsearch', } as const; export const DEFAULT_OUTPUT_ID = 'fleet-default-output'; diff --git a/x-pack/plugins/fleet/public/applications/fleet/hooks/use_check_permissions.ts b/x-pack/plugins/fleet/public/applications/fleet/hooks/use_check_permissions.ts index c5968048e7a85..9bb7b59bb93d0 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/hooks/use_check_permissions.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/hooks/use_check_permissions.ts @@ -4,34 +4,29 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - -import { useEffect, useState } from 'react'; +import { useQuery } from '@tanstack/react-query'; import { sendGetPermissionsCheck } from '../../../hooks'; -export const useCheckPermissions = () => { - const [isPermissionsLoading, setIsPermissionsLoading] = useState(false); - const [permissionsError, setPermissionsError] = useState(); - - useEffect(() => { - async function checkPermissions() { - setIsPermissionsLoading(false); - setPermissionsError(undefined); +async function checkPermissions() { + let permissionsError; - try { - setIsPermissionsLoading(true); - const permissionsResponse = await sendGetPermissionsCheck(true); + try { + const permissionsResponse = await sendGetPermissionsCheck(true); - setIsPermissionsLoading(false); - if (!permissionsResponse.data?.success) { - setPermissionsError(permissionsResponse.data?.error || 'REQUEST_ERROR'); - } - } catch (err) { - setPermissionsError('REQUEST_ERROR'); - } + if (!permissionsResponse.data?.success) { + permissionsError = permissionsResponse.data?.error || 'REQUEST_ERROR'; } - checkPermissions(); - }, []); + } catch (err) { + permissionsError = 'REQUEST_ERROR'; + } + return permissionsError; +} - return { isPermissionsLoading, permissionsError }; +export const useCheckPermissions = () => { + const { data: permissionsError, status } = useQuery([ + 'fetch-check-permissions', + checkPermissions, + ]); + return { isPermissionsLoading: status === 'loading', permissionsError }; }; diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/hooks.test.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/hooks.test.tsx index ed70778522b38..e027317bf621a 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/hooks.test.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/hooks.test.tsx @@ -107,7 +107,7 @@ const mockApiCallsWithRemoteESOutputs = (http: MockedFleetStartServices['http']) { id: 'remote1', name: 'Remote1', - type: 'remote-elasticsearch', + type: 'remote_elasticsearch', is_default: false, is_default_monitoring: false, }, diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/index.test.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/index.test.tsx index f2304f1f26100..ddd68d8380419 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/index.test.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/index.test.tsx @@ -166,7 +166,7 @@ describe('EditOutputFlyout', () => { it('should render the flyout if the output provided is a remote ES output', async () => { jest.spyOn(ExperimentalFeaturesService, 'get').mockReturnValue({ remoteESOutput: true }); const { utils } = renderFlyout({ - type: 'remote-elasticsearch', + type: 'remote_elasticsearch', name: 'remote es output', id: 'outputR', is_default: false, diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_remote_es.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_remote_es.tsx index 9c26d9b6f2a02..9b4c8f085af9a 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_remote_es.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_output_flyout/output_form_remote_es.tsx @@ -70,7 +70,10 @@ export const OutputFormRemoteEsSection: React.FunctionComponent = (props) data-test-subj="serviceTokenCallout" > - {`POST kbn:/api/fleet/service_tokens?remote=true`} + {`POST kbn:/api/fleet/service_tokens +{ + "remote": true +}`} diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/outputs_table/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/outputs_table/index.tsx index 97c5700727dc8..fe05e00f795d7 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/outputs_table/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/outputs_table/index.tsx @@ -36,7 +36,7 @@ function displayOutputType(type: string) { return i18n.translate('xpack.fleet.settings.outputsTable.elasticsearchTypeLabel', { defaultMessage: 'Elasticsearch', }); - case 'remote-elasticsearch': + case 'remote_elasticsearch': return i18n.translate('xpack.fleet.settings.outputsTable.remoteElasticsearchTypeLabel', { defaultMessage: 'Remote Elasticsearch', }); diff --git a/x-pack/plugins/fleet/server/routes/app/index.ts b/x-pack/plugins/fleet/server/routes/app/index.ts index 2ae12a2ba44cb..32ae2caaf8717 100644 --- a/x-pack/plugins/fleet/server/routes/app/index.ts +++ b/x-pack/plugins/fleet/server/routes/app/index.ts @@ -63,12 +63,12 @@ export const getCheckPermissionsHandler: FleetRequestHandler< export const generateServiceTokenHandler: RequestHandler< null, - TypeOf, - null + null, + TypeOf > = async (context, request, response) => { // Generate the fleet server service token as the current user as the internal user do not have the correct permissions const esClient = (await context.core).elasticsearch.client.asCurrentUser; - const serviceAccount = request.query.remote ? 'fleet-server-remote' : 'fleet-server'; + const serviceAccount = request.body.remote ? 'fleet-server-remote' : 'fleet-server'; appContextService .getLogger() .debug(`Creating service token for account elastic/${serviceAccount}`); diff --git a/x-pack/plugins/fleet/server/services/output.test.ts b/x-pack/plugins/fleet/server/services/output.test.ts index 40518d5025c37..03d43b130c4ea 100644 --- a/x-pack/plugins/fleet/server/services/output.test.ts +++ b/x-pack/plugins/fleet/server/services/output.test.ts @@ -716,7 +716,7 @@ describe('Output Service', () => { is_default: true, is_default_monitoring: false, name: 'Test', - type: 'remote-elasticsearch', + type: 'remote_elasticsearch', }, { id: 'output-1' } ) @@ -1534,7 +1534,7 @@ describe('Output Service', () => { is_default: true, is_default_monitoring: false, name: 'Test', - type: 'remote-elasticsearch', + type: 'remote_elasticsearch', }) ).rejects.toThrow( `Remote elasticsearch output cannot be set as default output for integration data. Please set "is_default" to false.` diff --git a/x-pack/plugins/fleet/server/types/rest_spec/app.ts b/x-pack/plugins/fleet/server/types/rest_spec/app.ts index 0d9d935a1b312..e79f8dda0d2ee 100644 --- a/x-pack/plugins/fleet/server/types/rest_spec/app.ts +++ b/x-pack/plugins/fleet/server/types/rest_spec/app.ts @@ -8,7 +8,7 @@ import { schema } from '@kbn/config-schema'; export const GenerateServiceTokenRequestSchema = { - query: schema.object({ + body: schema.object({ remote: schema.boolean({ defaultValue: false }), }), }; diff --git a/x-pack/test/fleet_api_integration/apis/service_tokens.ts b/x-pack/test/fleet_api_integration/apis/service_tokens.ts index 7cd57e774bf59..2ee14af55efe4 100644 --- a/x-pack/test/fleet_api_integration/apis/service_tokens.ts +++ b/x-pack/test/fleet_api_integration/apis/service_tokens.ts @@ -56,8 +56,9 @@ export default function (providerContext: FtrProviderContext) { it('should create a valid remote service account token', async () => { const { body: apiResponse } = await supertest - .post(`/api/fleet/service_tokens?remote=true`) + .post(`/api/fleet/service_tokens`) .set('kbn-xsrf', 'xxxx') + .send(`{"remote":true}`) .expect(200); expect(apiResponse).have.property('name');