Skip to content

Commit

Permalink
[Defend Workflows] Adjust headers in fleet api (#165823)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsonpl authored Sep 6, 2023
1 parent 8c39735 commit 630a95b
Show file tree
Hide file tree
Showing 28 changed files with 172 additions and 26 deletions.
1 change: 1 addition & 0 deletions .buildkite/scripts/pipelines/pull_request/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export {
// dashboards ids
DASHBOARD_LOCATORS_IDS,
FLEET_ENROLLMENT_API_PREFIX,
API_VERSIONS,
APP_API_ROUTES,
} from './constants';
export {
// Route services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,6 +23,7 @@ export function useGetAgentStatus(policyId?: string, options?: RequestOptions) {
policyId,
},
method: 'get',
version: API_VERSIONS.public.v1,
...options,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import { useEffect, useMemo, useState } from 'react';

import { API_VERSIONS } from '../../../../../../../../common/constants';

import type {
PackagePolicy,
GetAgentPoliciesResponse,
Expand Down Expand Up @@ -75,6 +77,7 @@ export const usePackagePoliciesWithAgentPolicy = (
full: true,
ignoreMissing: true,
},
version: API_VERSIONS.public.v1,
shouldSendRequest: agentPoliciesIds.length > 0,
} as SendConditionalRequestConfig);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const useConditionalRequest = <D = any, E = RequestError>(
path: config.path,
query: config.query,
body: config.body,
version: config.version,
});
if (res.error) {
throw res.error;
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/scripts/create_agents/create_agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
},
});

Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/osquery/cypress/tasks/api_fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,18 @@ 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);

export const cleanupAgentPolicy = (agentPolicyId: string) =>
request({
method: 'POST',
body: { agentPolicyId },
headers: {
'Elastic-Api-Version': API_VERSIONS.public.v1,
},
url: '/api/fleet/agent_policies/delete',
});
5 changes: 3 additions & 2 deletions x-pack/plugins/osquery/cypress/tasks/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { API_VERSIONS } from '../../common/constants';
import { DEFAULT_POLICY } from '../screens/fleet';
import {
ADD_POLICY_BTN,
Expand Down Expand Up @@ -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',
});
Expand All @@ -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',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -90,6 +90,7 @@ const fetchFleetAgent = async (kbnClient: KbnClient, agentId: string): Promise<A
.request({
path: AGENT_API_ROUTES.INFO_PATTERN.replace('{agentId}', agentId),
method: 'GET',
headers: { 'elastic-api-version': API_VERSIONS.public.v1 },
})
.catch(wrapErrorAndRejectPromise)) as AxiosResponse<GetOneAgentResponse>
).data.item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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<CreatePackagePolicyResponse>;

Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<PostFleetSetupResponse>;
Expand All @@ -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<PostFleetSetupResponse>;

Expand Down Expand Up @@ -118,6 +127,9 @@ export const installOrUpgradeEndpointFleetPackage = async (
query: {
prerelease: true,
},
headers: {
'elastic-api-version': API_VERSIONS.public.v1,
},
})
.catch(wrapErrorAndRejectPromise)) as AxiosResponse<BulkInstallPackagesResponse>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<GetInfoResponse>
).data.item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -24,6 +24,9 @@ export const enableAllPolicyProtections = (
return request<GetOnePackagePolicyResponse>({
method: 'GET',
url: packagePolicyRouteService.getInfoPath(endpointPolicyId),
headers: {
'elastic-api-version': API_VERSIONS.public.v1,
},
}).then(({ body: { item: endpointPolicy } }) => {
const {
created_by: _createdBy,
Expand Down Expand Up @@ -58,6 +61,9 @@ export const enableAllPolicyProtections = (
method: 'PUT',
url: packagePolicyRouteService.getUpdatePath(endpointPolicyId),
body: updatedEndpointPolicy,
headers: {
'elastic-api-version': API_VERSIONS.public.v1,
},
});
});
};
Expand All @@ -69,6 +75,9 @@ export const setCustomProtectionUpdatesManifestVersion = (
return request<GetOnePackagePolicyResponse>({
method: 'GET',
url: packagePolicyRouteService.getInfoPath(endpointPolicyId),
headers: {
'elastic-api-version': API_VERSIONS.public.v1,
},
}).then(({ body: { item: endpointPolicy } }) => {
const {
created_by: _createdBy,
Expand All @@ -91,6 +100,9 @@ export const setCustomProtectionUpdatesManifestVersion = (
method: 'PUT',
url: packagePolicyRouteService.getUpdatePath(endpointPolicyId),
body: updatedEndpointPolicy,
headers: {
'elastic-api-version': API_VERSIONS.public.v1,
},
});
});
};
Expand All @@ -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 },
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,6 +25,9 @@ export const getEndpointIntegrationVersion = (): Cypress.Chainable<string> =>
request<GetInfoResponse>({
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<Agent> =>
Expand All @@ -33,6 +37,9 @@ export const getAgentByHostName = (hostname: string): Cypress.Chainable<Agent> =
qs: {
kuery: `local_metadata.host.hostname: "${hostname}"`,
},
headers: {
'elastic-api-version': API_VERSIONS.public.v1,
},
}).then((response) => response.body.items[0]);

export const reassignAgentPolicy = (
Expand All @@ -45,6 +52,9 @@ export const reassignAgentPolicy = (
body: {
policy_id: agentPolicyId,
},
headers: {
'elastic-api-version': API_VERSIONS.public.v1,
},
});

export const yieldEndpointPolicyRevision = (): Cypress.Chainable<number> =>
Expand All @@ -54,6 +64,9 @@ export const yieldEndpointPolicyRevision = (): Cypress.Chainable<number> =>
qs: {
kuery: 'ingest-package-policies.package.name: endpoint',
},
headers: {
'elastic-api-version': API_VERSIONS.public.v1,
},
}).then(({ body }) => {
return body.items?.[0]?.revision ?? -1;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -45,7 +45,8 @@ export const useFetchEndpointPolicy = (
...options,
queryFn: async () => {
const apiResponse = await http.get<GetPolicyResponse>(
packagePolicyRouteService.getInfoPath(policyId)
packagePolicyRouteService.getInfoPath(policyId),
{ version: API_VERSIONS.public.v1 }
);

applyDefaultsToPolicyIfNeeded(apiResponse.item);
Expand Down
Loading

0 comments on commit 630a95b

Please sign in to comment.