From 630a95bc10ace25c4d2db09928e6dacb77267109 Mon Sep 17 00:00:00 2001 From: Tomasz Ciecierski Date: Wed, 6 Sep 2023 15:47:47 +0200 Subject: [PATCH] [Defend Workflows] Adjust headers in fleet api (#165823) --- .../pipelines/pull_request/pipeline.ts | 1 + x-pack/plugins/fleet/common/index.ts | 2 ++ .../details_page/hooks/use_agent_status.tsx | 3 +++ .../use_package_policies_with_agent_policy.ts | 3 +++ .../public/hooks/use_request/use_request.ts | 1 + .../scripts/create_agents/create_agents.ts | 2 ++ .../osquery/cypress/tasks/api_fixtures.ts | 6 ++++++ .../osquery/cypress/tasks/integrations.ts | 5 +++-- .../data_loaders/index_fleet_agent.ts | 3 ++- .../index_fleet_endpoint_policy.ts | 18 +++++++++++++++- .../data_loaders/setup_fleet_for_endpoint.ts | 14 ++++++++++++- .../common/endpoint/utils/package.ts | 3 ++- .../cypress/tasks/endpoint_policy.ts | 16 ++++++++++++-- .../public/management/cypress/tasks/fleet.ts | 13 ++++++++++++ .../policy/use_fetch_endpoint_policy.test.ts | 2 ++ .../hooks/policy/use_fetch_endpoint_policy.ts | 5 +++-- ...etch_endpoint_policy_agent_summary.test.ts | 3 ++- ...use_fetch_endpoint_policy_agent_summary.ts | 3 ++- .../policy/use_update_endpoint_policy.test.ts | 3 ++- .../policy/use_update_endpoint_policy.ts | 3 ++- .../policy_settings_layout.test.tsx | 3 ++- .../services/policies/ingest.test.ts | 11 +++++++--- .../management/services/policies/ingest.ts | 14 ++++++++++--- .../services/policies/policies.test.ts | 4 +++- .../management/services/policies/policies.ts | 3 ++- .../scripts/endpoint/common/fleet_services.ts | 17 +++++++++++++++ .../endpoint_agent_runner/fleet_server.ts | 21 ++++++++++++++++++- x-pack/test/osquery_cypress/utils.ts | 16 ++++++++++++-- 28 files changed, 172 insertions(+), 26 deletions(-) diff --git a/.buildkite/scripts/pipelines/pull_request/pipeline.ts b/.buildkite/scripts/pipelines/pull_request/pipeline.ts index 6fe1bb1b251e1..80d1312af6e64 100644 --- a/.buildkite/scripts/pipelines/pull_request/pipeline.ts +++ b/.buildkite/scripts/pipelines/pull_request/pipeline.ts @@ -141,6 +141,7 @@ const uploadPipeline = (pipelineContent: string | object) => { ) { pipeline.push(getPipeline('.buildkite/pipelines/pull_request/fleet_cypress.yml')); pipeline.push(getPipeline('.buildkite/pipelines/pull_request/defend_workflows.yml')); + pipeline.push(getPipeline('.buildkite/pipelines/pull_request/osquery_cypress.yml')); } if ( diff --git a/x-pack/plugins/fleet/common/index.ts b/x-pack/plugins/fleet/common/index.ts index df9ecae76bea5..9ac36d753c4e8 100644 --- a/x-pack/plugins/fleet/common/index.ts +++ b/x-pack/plugins/fleet/common/index.ts @@ -59,6 +59,8 @@ export { // dashboards ids DASHBOARD_LOCATORS_IDS, FLEET_ENROLLMENT_API_PREFIX, + API_VERSIONS, + APP_API_ROUTES, } from './constants'; export { // Route services diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/details_page/hooks/use_agent_status.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/details_page/hooks/use_agent_status.tsx index 4a92e44436fcd..c31140aca3a96 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/details_page/hooks/use_agent_status.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/details_page/hooks/use_agent_status.tsx @@ -7,6 +7,8 @@ import React from 'react'; +import { API_VERSIONS } from '../../../../../../../common/constants'; + import type { UseRequestConfig } from '../../../../hooks'; import { useRequest } from '../../../../hooks'; import type { GetAgentStatusResponse } from '../../../../types'; @@ -21,6 +23,7 @@ export function useGetAgentStatus(policyId?: string, options?: RequestOptions) { policyId, }, method: 'get', + version: API_VERSIONS.public.v1, ...options, }); diff --git a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/use_package_policies_with_agent_policy.ts b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/use_package_policies_with_agent_policy.ts index 399016ebf2042..a7ecbf95d25c1 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/use_package_policies_with_agent_policy.ts +++ b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/use_package_policies_with_agent_policy.ts @@ -7,6 +7,8 @@ import { useEffect, useMemo, useState } from 'react'; +import { API_VERSIONS } from '../../../../../../../../common/constants'; + import type { PackagePolicy, GetAgentPoliciesResponse, @@ -75,6 +77,7 @@ export const usePackagePoliciesWithAgentPolicy = ( full: true, ignoreMissing: true, }, + version: API_VERSIONS.public.v1, shouldSendRequest: agentPoliciesIds.length > 0, } as SendConditionalRequestConfig); diff --git a/x-pack/plugins/fleet/public/hooks/use_request/use_request.ts b/x-pack/plugins/fleet/public/hooks/use_request/use_request.ts index 9bd7d482e7e4a..69bf82061d89c 100644 --- a/x-pack/plugins/fleet/public/hooks/use_request/use_request.ts +++ b/x-pack/plugins/fleet/public/hooks/use_request/use_request.ts @@ -109,6 +109,7 @@ export const useConditionalRequest = ( path: config.path, query: config.query, body: config.body, + version: config.version, }); if (res.error) { throw res.error; diff --git a/x-pack/plugins/fleet/scripts/create_agents/create_agents.ts b/x-pack/plugins/fleet/scripts/create_agents/create_agents.ts index 206d30cabbb79..52723d0450351 100644 --- a/x-pack/plugins/fleet/scripts/create_agents/create_agents.ts +++ b/x-pack/plugins/fleet/scripts/create_agents/create_agents.ts @@ -201,6 +201,7 @@ async function getAgentPolicy(id: string) { 'Content-Type': 'application/json', 'kbn-xsrf': 'kibana', 'x-elastic-product-origin': 'fleet', + 'Elastic-Api-Version': PUBLIC_VERSION_V1, }, }); const data = await res.json(); @@ -346,6 +347,7 @@ async function bumpAgentPolicyRevision(id: string, policy: any) { 'Content-Type': 'application/json', 'kbn-xsrf': 'kibana', 'x-elastic-product-origin': 'fleet', + 'Elastic-Api-Version': PUBLIC_VERSION_V1, }, }); diff --git a/x-pack/plugins/osquery/cypress/tasks/api_fixtures.ts b/x-pack/plugins/osquery/cypress/tasks/api_fixtures.ts index 9a139eadf9adf..d7b9f7d43ce43 100644 --- a/x-pack/plugins/osquery/cypress/tasks/api_fixtures.ts +++ b/x-pack/plugins/osquery/cypress/tasks/api_fixtures.ts @@ -294,6 +294,9 @@ export const loadAgentPolicy = () => monitoring_enabled: ['logs', 'metrics'], inactivity_timeout: 1209600, }, + headers: { + 'Elastic-Api-Version': API_VERSIONS.public.v1, + }, url: '/api/fleet/agent_policies', }).then((response) => response.body.item); @@ -301,5 +304,8 @@ export const cleanupAgentPolicy = (agentPolicyId: string) => request({ method: 'POST', body: { agentPolicyId }, + headers: { + 'Elastic-Api-Version': API_VERSIONS.public.v1, + }, url: '/api/fleet/agent_policies/delete', }); diff --git a/x-pack/plugins/osquery/cypress/tasks/integrations.ts b/x-pack/plugins/osquery/cypress/tasks/integrations.ts index 09948eca70538..c8bb688dac0f5 100644 --- a/x-pack/plugins/osquery/cypress/tasks/integrations.ts +++ b/x-pack/plugins/osquery/cypress/tasks/integrations.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { API_VERSIONS } from '../../common/constants'; import { DEFAULT_POLICY } from '../screens/fleet'; import { ADD_POLICY_BTN, @@ -125,7 +126,7 @@ export const deleteIntegrations = async (integrationName: string) => { .then(() => { cy.request({ url: `/api/fleet/package_policies/delete`, - headers: { 'kbn-xsrf': 'cypress' }, + headers: { 'kbn-xsrf': 'cypress', 'Elastic-Api-Version': API_VERSIONS.public.v1 }, body: `{ "packagePolicyIds": ${JSON.stringify(ids)} }`, method: 'POST', }); @@ -135,7 +136,7 @@ export const deleteIntegrations = async (integrationName: string) => { export const installPackageWithVersion = (integration: string, version: string) => { cy.request({ url: `/api/fleet/epm/packages/${integration}-${version}`, - headers: { 'kbn-xsrf': 'cypress' }, + headers: { 'kbn-xsrf': 'cypress', 'Elastic-Api-Version': API_VERSIONS.public.v1 }, body: '{ "force": true }', method: 'POST', }); diff --git a/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_fleet_agent.ts b/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_fleet_agent.ts index 0b988d831c923..781d6243384e9 100644 --- a/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_fleet_agent.ts +++ b/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_fleet_agent.ts @@ -10,7 +10,7 @@ import type { AxiosResponse } from 'axios'; import type { DeleteByQueryResponse } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import type { KbnClient } from '@kbn/test'; import type { Agent, FleetServerAgent, GetOneAgentResponse } from '@kbn/fleet-plugin/common'; -import { AGENT_API_ROUTES } from '@kbn/fleet-plugin/common'; +import { AGENT_API_ROUTES, API_VERSIONS } from '@kbn/fleet-plugin/common'; import type { HostMetadata } from '../types'; import { FleetAgentGenerator } from '../data_generators/fleet_agent_generator'; import { wrapErrorAndRejectPromise } from './utils'; @@ -90,6 +90,7 @@ const fetchFleetAgent = async (kbnClient: KbnClient, agentId: string): Promise ).data.item; diff --git a/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_fleet_endpoint_policy.ts b/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_fleet_endpoint_policy.ts index 0730eba99306c..fda862d247bae 100644 --- a/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_fleet_endpoint_policy.ts +++ b/x-pack/plugins/security_solution/common/endpoint/data_loaders/index_fleet_endpoint_policy.ts @@ -16,7 +16,11 @@ import type { DeleteAgentPolicyResponse, PostDeletePackagePoliciesResponse, } from '@kbn/fleet-plugin/common'; -import { AGENT_POLICY_API_ROUTES, PACKAGE_POLICY_API_ROUTES } from '@kbn/fleet-plugin/common'; +import { + AGENT_POLICY_API_ROUTES, + PACKAGE_POLICY_API_ROUTES, + API_VERSIONS, +} from '@kbn/fleet-plugin/common'; import { memoize } from 'lodash'; import { getEndpointPackageInfo } from '../utils/package'; import type { PolicyData } from '../types'; @@ -61,6 +65,9 @@ export const indexFleetEndpointPolicy = async ( agentPolicy = (await kbnClient .request({ path: AGENT_POLICY_API_ROUTES.CREATE_PATTERN, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, method: 'POST', body: newAgentPolicyData, }) @@ -101,6 +108,9 @@ export const indexFleetEndpointPolicy = async ( path: PACKAGE_POLICY_API_ROUTES.CREATE_PATTERN, method: 'POST', body: newPackagePolicyData, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }) .catch(wrapErrorAndRejectPromise)) as AxiosResponse; @@ -135,6 +145,9 @@ export const deleteIndexedFleetEndpointPolicies = async ( (await kbnClient .request({ path: PACKAGE_POLICY_API_ROUTES.DELETE_PATTERN, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, method: 'POST', body: { packagePolicyIds: indexData.integrationPolicies.map((policy) => policy.id), @@ -153,6 +166,9 @@ export const deleteIndexedFleetEndpointPolicies = async ( (await kbnClient .request({ path: AGENT_POLICY_API_ROUTES.DELETE_PATTERN, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, method: 'POST', body: { agentPolicyId: agentPolicy.id, diff --git a/x-pack/plugins/security_solution/common/endpoint/data_loaders/setup_fleet_for_endpoint.ts b/x-pack/plugins/security_solution/common/endpoint/data_loaders/setup_fleet_for_endpoint.ts index 177dccce73714..6c84cd6e5738f 100644 --- a/x-pack/plugins/security_solution/common/endpoint/data_loaders/setup_fleet_for_endpoint.ts +++ b/x-pack/plugins/security_solution/common/endpoint/data_loaders/setup_fleet_for_endpoint.ts @@ -13,7 +13,12 @@ import type { IBulkInstallPackageHTTPError, PostFleetSetupResponse, } from '@kbn/fleet-plugin/common'; -import { AGENTS_SETUP_API_ROUTES, EPM_API_ROUTES, SETUP_API_ROUTE } from '@kbn/fleet-plugin/common'; +import { + AGENTS_SETUP_API_ROUTES, + EPM_API_ROUTES, + SETUP_API_ROUTE, + API_VERSIONS, +} from '@kbn/fleet-plugin/common'; import { ToolingLog } from '@kbn/tooling-log'; import { UsageTracker } from './usage_tracker'; import { EndpointDataLoadingError, retryOnError, wrapErrorAndRejectPromise } from './utils'; @@ -43,6 +48,7 @@ export const setupFleetForEndpoint = async ( const setupResponse = (await kbnClient .request({ path: SETUP_API_ROUTE, + headers: { 'Elastic-Api-Version': API_VERSIONS.public.v1 }, method: 'POST', }) .catch(wrapErrorAndRejectPromise)) as AxiosResponse; @@ -64,6 +70,9 @@ export const setupFleetForEndpoint = async ( .request({ path: AGENTS_SETUP_API_ROUTES.CREATE_PATTERN, method: 'POST', + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }) .catch(wrapErrorAndRejectPromise)) as AxiosResponse; @@ -118,6 +127,9 @@ export const installOrUpgradeEndpointFleetPackage = async ( query: { prerelease: true, }, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }) .catch(wrapErrorAndRejectPromise)) as AxiosResponse; diff --git a/x-pack/plugins/security_solution/common/endpoint/utils/package.ts b/x-pack/plugins/security_solution/common/endpoint/utils/package.ts index c45c44fa9c561..599fcabe24c9c 100644 --- a/x-pack/plugins/security_solution/common/endpoint/utils/package.ts +++ b/x-pack/plugins/security_solution/common/endpoint/utils/package.ts @@ -9,7 +9,7 @@ import type { AxiosResponse } from 'axios'; import type { KbnClient } from '@kbn/test'; import type { GetInfoResponse } from '@kbn/fleet-plugin/common'; -import { epmRouteService } from '@kbn/fleet-plugin/common'; +import { API_VERSIONS, epmRouteService } from '@kbn/fleet-plugin/common'; export const getEndpointPackageInfo = async ( kbnClient: KbnClient @@ -18,6 +18,7 @@ export const getEndpointPackageInfo = async ( const endpointPackage = ( (await kbnClient.request({ path, + headers: { 'Elastic-Api-Version': API_VERSIONS.public.v1 }, method: 'GET', })) as AxiosResponse ).data.item; diff --git a/x-pack/plugins/security_solution/public/management/cypress/tasks/endpoint_policy.ts b/x-pack/plugins/security_solution/public/management/cypress/tasks/endpoint_policy.ts index 9353cdf9ed822..a1b5972758742 100644 --- a/x-pack/plugins/security_solution/public/management/cypress/tasks/endpoint_policy.ts +++ b/x-pack/plugins/security_solution/public/management/cypress/tasks/endpoint_policy.ts @@ -10,7 +10,7 @@ import type { UpdatePackagePolicy, UpdatePackagePolicyResponse, } from '@kbn/fleet-plugin/common'; -import { packagePolicyRouteService } from '@kbn/fleet-plugin/common'; +import { packagePolicyRouteService, API_VERSIONS } from '@kbn/fleet-plugin/common'; import { request } from './common'; import { ProtectionModes } from '../../../../common/endpoint/types'; @@ -24,6 +24,9 @@ export const enableAllPolicyProtections = ( return request({ method: 'GET', url: packagePolicyRouteService.getInfoPath(endpointPolicyId), + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }).then(({ body: { item: endpointPolicy } }) => { const { created_by: _createdBy, @@ -58,6 +61,9 @@ export const enableAllPolicyProtections = ( method: 'PUT', url: packagePolicyRouteService.getUpdatePath(endpointPolicyId), body: updatedEndpointPolicy, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }); }); }; @@ -69,6 +75,9 @@ export const setCustomProtectionUpdatesManifestVersion = ( return request({ method: 'GET', url: packagePolicyRouteService.getInfoPath(endpointPolicyId), + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }).then(({ body: { item: endpointPolicy } }) => { const { created_by: _createdBy, @@ -91,6 +100,9 @@ export const setCustomProtectionUpdatesManifestVersion = ( method: 'PUT', url: packagePolicyRouteService.getUpdatePath(endpointPolicyId), body: updatedEndpointPolicy, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }); }); }; @@ -103,6 +115,6 @@ export const setCustomProtectionUpdatesNote = ( method: 'POST', url: `/api/endpoint/protection_updates_note/${endpointPolicyId}`, body: { note }, - headers: { 'Elastic-Api-Version': '2023-10-31' }, + headers: { 'Elastic-Api-Version': API_VERSIONS.public.v1 }, }); }; diff --git a/x-pack/plugins/security_solution/public/management/cypress/tasks/fleet.ts b/x-pack/plugins/security_solution/public/management/cypress/tasks/fleet.ts index 775baa7409f0a..bd6edbea158ce 100644 --- a/x-pack/plugins/security_solution/public/management/cypress/tasks/fleet.ts +++ b/x-pack/plugins/security_solution/public/management/cypress/tasks/fleet.ts @@ -15,6 +15,7 @@ import { agentRouteService, epmRouteService, packagePolicyRouteService, + API_VERSIONS, } from '@kbn/fleet-plugin/common'; import type { PutAgentReassignResponse } from '@kbn/fleet-plugin/common/types'; import type { IndexedFleetEndpointPolicyResponse } from '../../../../common/endpoint/data_loaders/index_fleet_endpoint_policy'; @@ -24,6 +25,9 @@ export const getEndpointIntegrationVersion = (): Cypress.Chainable => request({ url: epmRouteService.getInfoPath('endpoint'), method: 'GET', + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }).then((response) => response.body.item.version); export const getAgentByHostName = (hostname: string): Cypress.Chainable => @@ -33,6 +37,9 @@ export const getAgentByHostName = (hostname: string): Cypress.Chainable = qs: { kuery: `local_metadata.host.hostname: "${hostname}"`, }, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }).then((response) => response.body.items[0]); export const reassignAgentPolicy = ( @@ -45,6 +52,9 @@ export const reassignAgentPolicy = ( body: { policy_id: agentPolicyId, }, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }); export const yieldEndpointPolicyRevision = (): Cypress.Chainable => @@ -54,6 +64,9 @@ export const yieldEndpointPolicyRevision = (): Cypress.Chainable => qs: { kuery: 'ingest-package-policies.package.name: endpoint', }, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }).then(({ body }) => { return body.items?.[0]?.revision ?? -1; }); diff --git a/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy.test.ts b/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy.test.ts index 78cb1282f94a8..85647202a755c 100644 --- a/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy.test.ts +++ b/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy.test.ts @@ -17,6 +17,7 @@ import { DefaultPolicyRuleNotificationMessage, } from '../../../../common/endpoint/models/policy_config'; import { set } from 'lodash'; +import { API_VERSIONS } from '@kbn/fleet-plugin/common'; const useQueryMock = _useQuery as jest.Mock; @@ -60,6 +61,7 @@ describe('When using the `useGetFileInfo()` hook', () => { expect(apiMocks.responseProvider.endpointPackagePolicy).toHaveBeenCalledWith({ path: `/api/fleet/package_policies/${policy.id}`, + version: API_VERSIONS.public.v1, }); }); diff --git a/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy.ts b/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy.ts index 35d8a0f2ea255..bb8033ae012bb 100644 --- a/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy.ts +++ b/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy.ts @@ -8,7 +8,7 @@ import type { UseQueryOptions, UseQueryResult } from '@tanstack/react-query'; import type { IHttpFetchError } from '@kbn/core-http-browser'; import { useQuery } from '@tanstack/react-query'; -import { packagePolicyRouteService } from '@kbn/fleet-plugin/common'; +import { packagePolicyRouteService, API_VERSIONS } from '@kbn/fleet-plugin/common'; import { DefaultPolicyNotificationMessage, DefaultPolicyRuleNotificationMessage, @@ -45,7 +45,8 @@ export const useFetchEndpointPolicy = ( ...options, queryFn: async () => { const apiResponse = await http.get( - packagePolicyRouteService.getInfoPath(policyId) + packagePolicyRouteService.getInfoPath(policyId), + { version: API_VERSIONS.public.v1 } ); applyDefaultsToPolicyIfNeeded(apiResponse.item); diff --git a/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy_agent_summary.test.ts b/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy_agent_summary.test.ts index 0513fee664dc1..80fd6d41dfd6e 100644 --- a/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy_agent_summary.test.ts +++ b/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy_agent_summary.test.ts @@ -12,7 +12,7 @@ import type { PolicyData } from '../../../../common/endpoint/types'; import { allFleetHttpMocks } from '../../mocks'; import { FleetPackagePolicyGenerator } from '../../../../common/endpoint/data_generators/fleet_package_policy_generator'; import { useFetchAgentByAgentPolicySummary } from './use_fetch_endpoint_policy_agent_summary'; -import { agentRouteService } from '@kbn/fleet-plugin/common'; +import { agentRouteService, API_VERSIONS } from '@kbn/fleet-plugin/common'; const useQueryMock = _useQuery as jest.Mock; @@ -57,6 +57,7 @@ describe('When using the `useFetchEndpointPolicyAgentSummary()` hook', () => { expect(apiMocks.responseProvider.agentStatus).toHaveBeenCalledWith({ path: agentRouteService.getStatusPath(), query: { policyId: policy.policy_id }, + version: API_VERSIONS.public.v1, }); expect(data).toEqual({ total: 50, diff --git a/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy_agent_summary.ts b/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy_agent_summary.ts index cbe69a321d1f5..82cebb6263f96 100644 --- a/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy_agent_summary.ts +++ b/x-pack/plugins/security_solution/public/management/hooks/policy/use_fetch_endpoint_policy_agent_summary.ts @@ -9,7 +9,7 @@ import type { UseQueryOptions, UseQueryResult } from '@tanstack/react-query'; import type { IHttpFetchError } from '@kbn/core-http-browser'; import type { GetAgentStatusResponse } from '@kbn/fleet-plugin/common'; import { useQuery } from '@tanstack/react-query'; -import { agentRouteService } from '@kbn/fleet-plugin/common'; +import { agentRouteService, API_VERSIONS } from '@kbn/fleet-plugin/common'; import { useHttp } from '../../../common/lib/kibana'; type EndpointPolicyAgentSummary = GetAgentStatusResponse['results']; @@ -30,6 +30,7 @@ export const useFetchAgentByAgentPolicySummary = ( return ( await http.get(agentRouteService.getStatusPath(), { query: { policyId: agentPolicyId }, + version: API_VERSIONS.public.v1, }) ).results; }, diff --git a/x-pack/plugins/security_solution/public/management/hooks/policy/use_update_endpoint_policy.test.ts b/x-pack/plugins/security_solution/public/management/hooks/policy/use_update_endpoint_policy.test.ts index 1f9dc8e300f05..8b6be08b41b46 100644 --- a/x-pack/plugins/security_solution/public/management/hooks/policy/use_update_endpoint_policy.test.ts +++ b/x-pack/plugins/security_solution/public/management/hooks/policy/use_update_endpoint_policy.test.ts @@ -17,7 +17,7 @@ import type { RenderHookResult } from '@testing-library/react-hooks/src/types'; import { useUpdateEndpointPolicy } from './use_update_endpoint_policy'; import type { PolicyData } from '../../../../common/endpoint/types'; import { FleetPackagePolicyGenerator } from '../../../../common/endpoint/data_generators/fleet_package_policy_generator'; -import { packagePolicyRouteService } from '@kbn/fleet-plugin/common'; +import { API_VERSIONS, packagePolicyRouteService } from '@kbn/fleet-plugin/common'; import { getPolicyDataForUpdate } from '../../../../common/endpoint/service/policy'; const useMutationMock = _useMutation as jest.Mock; @@ -64,6 +64,7 @@ describe('When using the `useFetchEndpointPolicyAgentSummary()` hook', () => { expect(apiMocks.responseProvider.updateEndpointPolicy).toHaveBeenCalledWith({ path: packagePolicyRouteService.getUpdatePath(policy.id), body: JSON.stringify(getPolicyDataForUpdate(policy)), + version: API_VERSIONS.public.v1, }); expect(result).toEqual({ item: expect.any(Object) }); diff --git a/x-pack/plugins/security_solution/public/management/hooks/policy/use_update_endpoint_policy.ts b/x-pack/plugins/security_solution/public/management/hooks/policy/use_update_endpoint_policy.ts index 2e7f8b83ac19b..5e078ad563481 100644 --- a/x-pack/plugins/security_solution/public/management/hooks/policy/use_update_endpoint_policy.ts +++ b/x-pack/plugins/security_solution/public/management/hooks/policy/use_update_endpoint_policy.ts @@ -8,7 +8,7 @@ import type { UseMutationOptions, UseMutationResult } from '@tanstack/react-query'; import type { IHttpFetchError } from '@kbn/core-http-browser'; import { useMutation } from '@tanstack/react-query'; -import { packagePolicyRouteService } from '@kbn/fleet-plugin/common'; +import { packagePolicyRouteService, API_VERSIONS } from '@kbn/fleet-plugin/common'; import { getPolicyDataForUpdate } from '../../../../common/endpoint/service/policy'; import { useHttp } from '../../../common/lib/kibana'; import type { PolicyData } from '../../../../common/endpoint/types'; @@ -39,6 +39,7 @@ export const useUpdateEndpointPolicy = ( return http.put(packagePolicyRouteService.getUpdatePath(policy.id), { body: JSON.stringify(update), + version: API_VERSIONS.public.v1, }); }, options); }; diff --git a/x-pack/plugins/security_solution/public/management/pages/policy/view/policy_settings_layout/policy_settings_layout.test.tsx b/x-pack/plugins/security_solution/public/management/pages/policy/view/policy_settings_layout/policy_settings_layout.test.tsx index 64188ce296c6e..0c5a98281eec6 100644 --- a/x-pack/plugins/security_solution/public/management/pages/policy/view/policy_settings_layout/policy_settings_layout.test.tsx +++ b/x-pack/plugins/security_solution/public/management/pages/policy/view/policy_settings_layout/policy_settings_layout.test.tsx @@ -20,7 +20,7 @@ import { expectIsViewOnly, getPolicySettingsFormTestSubjects } from '../policy_s import { cloneDeep, set } from 'lodash'; import { ProtectionModes } from '../../../../../../common/endpoint/types'; import { waitFor, cleanup } from '@testing-library/react'; -import { packagePolicyRouteService } from '@kbn/fleet-plugin/common'; +import { packagePolicyRouteService, API_VERSIONS } from '@kbn/fleet-plugin/common'; import { getPolicyDataForUpdate } from '../../../../../../common/endpoint/service/policy'; import { getDeferred } from '../../../../mocks/utils'; @@ -133,6 +133,7 @@ describe('When rendering PolicySettingsLayout', () => { expect(apiMocks.responseProvider.updateEndpointPolicy).toHaveBeenCalledWith({ path: packagePolicyRouteService.getUpdatePath(policyData.id), body: JSON.stringify(getPolicyDataForUpdate(expectedUpdatedPolicy)), + version: API_VERSIONS.public.v1, }); }); diff --git a/x-pack/plugins/security_solution/public/management/services/policies/ingest.test.ts b/x-pack/plugins/security_solution/public/management/services/policies/ingest.test.ts index a3644ce691478..d498f32bb1cab 100644 --- a/x-pack/plugins/security_solution/public/management/services/policies/ingest.test.ts +++ b/x-pack/plugins/security_solution/public/management/services/policies/ingest.test.ts @@ -7,7 +7,7 @@ import { sendGetPackagePolicy, sendGetEndpointSecurityPackage } from './ingest'; import { httpServiceMock } from '@kbn/core/public/mocks'; -import { PACKAGE_POLICY_API_ROOT, epmRouteService } from '@kbn/fleet-plugin/common'; +import { PACKAGE_POLICY_API_ROOT, epmRouteService, API_VERSIONS } from '@kbn/fleet-plugin/common'; import { policyListApiPathHandlers } from '../../pages/policy/store/test_mock_utils'; describe('ingest service', () => { @@ -20,7 +20,9 @@ describe('ingest service', () => { describe('sendGetPackagePolicy()', () => { it('builds correct API path', async () => { await sendGetPackagePolicy(http, '123'); - expect(http.get).toHaveBeenCalledWith(`${PACKAGE_POLICY_API_ROOT}/123`, undefined); + expect(http.get).toHaveBeenCalledWith(`${PACKAGE_POLICY_API_ROOT}/123`, { + version: API_VERSIONS.public.v1, + }); }); it('supports http options', async () => { await sendGetPackagePolicy(http, '123', { query: { page: 1 } }); @@ -28,6 +30,7 @@ describe('ingest service', () => { query: { page: 1, }, + version: API_VERSIONS.public.v1, }); }); }); @@ -37,7 +40,9 @@ describe('ingest service', () => { const path = epmRouteService.getInfoPath('endpoint'); http.get.mockReturnValue(Promise.resolve(policyListApiPathHandlers()[path]())); await sendGetEndpointSecurityPackage(http); - expect(http.get).toHaveBeenCalledWith(path); + expect(http.get).toHaveBeenCalledWith(path, { + version: API_VERSIONS.public.v1, + }); }); it('should throw if package is not found', async () => { diff --git a/x-pack/plugins/security_solution/public/management/services/policies/ingest.ts b/x-pack/plugins/security_solution/public/management/services/policies/ingest.ts index e5347449d4dbe..4a2690a112e24 100644 --- a/x-pack/plugins/security_solution/public/management/services/policies/ingest.ts +++ b/x-pack/plugins/security_solution/public/management/services/policies/ingest.ts @@ -11,7 +11,7 @@ import type { GetPackagePoliciesResponse, GetInfoResponse, } from '@kbn/fleet-plugin/common'; -import { epmRouteService } from '@kbn/fleet-plugin/common'; +import { epmRouteService, API_VERSIONS } from '@kbn/fleet-plugin/common'; import type { NewPolicyData } from '../../../../common/endpoint/types'; import type { GetPolicyResponse, UpdatePolicyResponse } from '../../pages/policy/types'; @@ -34,7 +34,10 @@ export const sendGetPackagePolicy = ( packagePolicyId: string, options?: HttpFetchOptions ) => { - return http.get(`${INGEST_API_PACKAGE_POLICIES}/${packagePolicyId}`, options); + return http.get(`${INGEST_API_PACKAGE_POLICIES}/${packagePolicyId}`, { + ...options, + version: API_VERSIONS.public.v1, + }); }; /** @@ -50,6 +53,7 @@ export const sendBulkGetPackagePolicies = ( ) => { return http.post(`${INGEST_API_PACKAGE_POLICIES}/_bulk_get`, { ...options, + version: API_VERSIONS.public.v1, body: JSON.stringify({ ids: packagePolicyIds, ignoreMissing: true, @@ -73,6 +77,7 @@ export const sendPutPackagePolicy = ( ): Promise => { return http.put(`${INGEST_API_PACKAGE_POLICIES}/${packagePolicyId}`, { ...options, + version: API_VERSIONS.public.v1, body: JSON.stringify(packagePolicy), }); }; @@ -92,6 +97,7 @@ export const sendGetFleetAgentStatusForPolicy = ( ): Promise => { return http.get(INGEST_API_FLEET_AGENT_STATUS, { ...options, + version: API_VERSIONS.public.v1, query: { policyId, }, @@ -105,7 +111,9 @@ export const sendGetEndpointSecurityPackage = async ( http: HttpStart ): Promise => { const path = epmRouteService.getInfoPath('endpoint'); - const endpointPackageResponse = await http.get(path); + const endpointPackageResponse = await http.get(path, { + version: API_VERSIONS.public.v1, + }); const endpointPackageInfo = endpointPackageResponse.item; if (!endpointPackageInfo) { throw new Error('Endpoint package was not found.'); diff --git a/x-pack/plugins/security_solution/public/management/services/policies/policies.test.ts b/x-pack/plugins/security_solution/public/management/services/policies/policies.test.ts index 86715ebff3f75..a523f94d489c5 100644 --- a/x-pack/plugins/security_solution/public/management/services/policies/policies.test.ts +++ b/x-pack/plugins/security_solution/public/management/services/policies/policies.test.ts @@ -7,7 +7,7 @@ import { httpServiceMock } from '@kbn/core/public/mocks'; import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common'; -import { PACKAGE_POLICY_API_ROUTES } from '@kbn/fleet-plugin/common/constants/routes'; +import { API_VERSIONS, PACKAGE_POLICY_API_ROUTES } from '@kbn/fleet-plugin/common/constants'; import { sendGetEndpointSpecificPackagePolicies } from './policies'; describe('ingest service', () => { @@ -24,6 +24,7 @@ describe('ingest service', () => { query: { kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name: endpoint`, }, + version: API_VERSIONS.public.v1, }); }); it('supports additional KQL to be defined on input for query params', async () => { @@ -36,6 +37,7 @@ describe('ingest service', () => { perPage: 10, page: 1, }, + version: API_VERSIONS.public.v1, }); }); }); diff --git a/x-pack/plugins/security_solution/public/management/services/policies/policies.ts b/x-pack/plugins/security_solution/public/management/services/policies/policies.ts index de700e20e8c40..a7a45f832b56d 100644 --- a/x-pack/plugins/security_solution/public/management/services/policies/policies.ts +++ b/x-pack/plugins/security_solution/public/management/services/policies/policies.ts @@ -7,7 +7,7 @@ import type { HttpFetchOptions, HttpStart } from '@kbn/core/public'; import type { GetPackagePoliciesRequest } from '@kbn/fleet-plugin/common'; -import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common'; +import { PACKAGE_POLICY_SAVED_OBJECT_TYPE, API_VERSIONS } from '@kbn/fleet-plugin/common'; import type { GetPolicyListResponse } from '../../pages/policy/types'; import { INGEST_API_PACKAGE_POLICIES } from './ingest'; @@ -29,5 +29,6 @@ export const sendGetEndpointSpecificPackagePolicies = ( options?.query?.kuery ? `${options.query.kuery} and ` : '' }${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name: endpoint`, }, + version: API_VERSIONS.public.v1, }); }; diff --git a/x-pack/plugins/security_solution/scripts/endpoint/common/fleet_services.ts b/x-pack/plugins/security_solution/scripts/endpoint/common/fleet_services.ts index 0eca2cbf03501..95f000fd00355 100644 --- a/x-pack/plugins/security_solution/scripts/endpoint/common/fleet_services.ts +++ b/x-pack/plugins/security_solution/scripts/endpoint/common/fleet_services.ts @@ -19,6 +19,7 @@ import { agentPolicyRouteService, agentRouteService, AGENTS_INDEX, + API_VERSIONS, } from '@kbn/fleet-plugin/common'; import { ToolingLog } from '@kbn/tooling-log'; import type { KbnClient } from '@kbn/test'; @@ -106,6 +107,9 @@ export const fetchFleetAgents = async ( .request({ method: 'GET', path: AGENT_API_ROUTES.LIST_PATTERN, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, query: options, }) .catch(catchAxiosErrorFormatAndThrow) @@ -160,6 +164,9 @@ export const fetchFleetServerUrl = async (kbnClient: KbnClient): Promise({ method: 'GET', path: fleetServerHostsRoutesService.getListPath(), + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, query: { perPage: 100, }, @@ -197,6 +204,9 @@ export const fetchAgentPolicyEnrollmentKey = async ( .request({ method: 'GET', path: enrollmentAPIKeyRouteService.getListPath(), + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, query: { kuery: `policy_id: "${agentPolicyId}"` }, }) .catch(catchAxiosErrorFormatAndThrow) @@ -222,6 +232,9 @@ export const fetchAgentPolicyList = async ( .request({ method: 'GET', path: agentPolicyRouteService.getListPath(), + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, query: options, }) .catch(catchAxiosErrorFormatAndThrow) @@ -323,6 +336,7 @@ export const getAgentDownloadUrl = async ( * Given a stack version number, function will return the closest Agent download version available * for download. THis could be the actual version passed in or lower. * @param version + * @param log */ export const getLatestAgentDownloadVersion = async ( version: string, @@ -386,6 +400,9 @@ export const unEnrollFleetAgent = async ( method: 'POST', path: agentRouteService.getUnenrollPath(agentId), body: { revoke: force }, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, }) .catch(catchAxiosErrorFormatAndThrow); diff --git a/x-pack/plugins/security_solution/scripts/endpoint/endpoint_agent_runner/fleet_server.ts b/x-pack/plugins/security_solution/scripts/endpoint/endpoint_agent_runner/fleet_server.ts index f9d88382d81c7..60471c5c46864 100644 --- a/x-pack/plugins/security_solution/scripts/endpoint/endpoint_agent_runner/fleet_server.ts +++ b/x-pack/plugins/security_solution/scripts/endpoint/endpoint_agent_runner/fleet_server.ts @@ -17,8 +17,9 @@ import { FLEET_SERVER_PACKAGE, PACKAGE_POLICY_API_ROUTES, PACKAGE_POLICY_SAVED_OBJECT_TYPE, + API_VERSIONS, + APP_API_ROUTES, } from '@kbn/fleet-plugin/common'; -import { APP_API_ROUTES } from '@kbn/fleet-plugin/common/constants'; import type { FleetServerHost, GenerateServiceTokenResponse, @@ -87,6 +88,9 @@ const getFleetServerPackagePolicy = async (): Promise .request({ method: 'GET', path: PACKAGE_POLICY_API_ROUTES.LIST_PATTERN, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, query: { perPage: 1, kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name: "${FLEET_SERVER_PACKAGE}"`, @@ -121,6 +125,9 @@ const getOrCreateFleetServerAgentPolicyId = async (): Promise => { .request({ method: 'POST', path: AGENT_POLICY_API_ROUTES.CREATE_PATTERN, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, body: { name: `Fleet Server policy (${Math.random().toString(32).substring(2)})`, description: `Created by CLI Tool via: ${__filename}`, @@ -150,6 +157,9 @@ const generateFleetServiceToken = async (): Promise => { .request({ method: 'POST', path: APP_API_ROUTES.GENERATE_SERVICE_TOKEN_PATTERN, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, body: {}, }) .then((response) => response.data.value); @@ -279,6 +289,9 @@ const configureFleetIfNeeded = async () => { const fleetOutputs = await kbnClient .request({ method: 'GET', + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, path: outputRoutesService.getListPath(), }) .then((response) => response.data); @@ -318,6 +331,9 @@ const configureFleetIfNeeded = async () => { await kbnClient .request({ method: 'PUT', + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, path: outputRoutesService.getUpdatePath(id), body: update, }) @@ -356,6 +372,9 @@ const addFleetServerHostToFleetSettings = async ( .request({ method: 'POST', path: fleetServerHostsRoutesService.getCreatePath(), + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, body: newFleetHostEntry, }) .catch(catchAxiosErrorFormatAndThrow) diff --git a/x-pack/test/osquery_cypress/utils.ts b/x-pack/test/osquery_cypress/utils.ts index 572170e0ec301..a852cfd8bed99 100644 --- a/x-pack/test/osquery_cypress/utils.ts +++ b/x-pack/test/osquery_cypress/utils.ts @@ -8,7 +8,7 @@ import axios from 'axios'; import semver from 'semver'; import { map } from 'lodash'; -import { PackagePolicy, CreatePackagePolicyResponse } from '@kbn/fleet-plugin/common'; +import { PackagePolicy, CreatePackagePolicyResponse, API_VERSIONS } from '@kbn/fleet-plugin/common'; import { KbnClient } from '@kbn/test'; import { GetEnrollmentAPIKeysResponse, @@ -26,7 +26,10 @@ export const getInstalledIntegration = async (kbnClient: KbnClient, integrationN } = await kbnClient.request<{ item: PackagePolicy }>({ method: 'GET', path: `/api/fleet/epm/packages/${integrationName}`, - headers: DEFAULT_HEADERS, + headers: { + ...DEFAULT_HEADERS, + 'elastic-api-version': API_VERSIONS.public.v1, + }, }); return item; @@ -46,6 +49,9 @@ export const createAgentPolicy = async ( } = await kbnClient.request({ method: 'POST', path: `/api/fleet/agent_policies?sys_monitoring=true`, + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, body: { name: agentPolicyName, description: '', @@ -62,6 +68,9 @@ export const createAgentPolicy = async ( log.info('Getting agent enrollment key'); const { data: apiKeys } = await kbnClient.request({ method: 'GET', + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, path: '/api/fleet/enrollment_api_keys', }); @@ -79,6 +88,9 @@ export const addIntegrationToAgentPolicy = async ( return kbnClient.request({ method: 'POST', path: '/api/fleet/package_policies', + headers: { + 'elastic-api-version': API_VERSIONS.public.v1, + }, body: { policy_id: agentPolicyId, package: {