diff --git a/x-pack/plugins/fleet/common/types/rest_spec/download_sources.ts b/x-pack/plugins/fleet/common/types/rest_spec/download_sources.ts index 0313753ebae131..04e1b920c62241 100644 --- a/x-pack/plugins/fleet/common/types/rest_spec/download_sources.ts +++ b/x-pack/plugins/fleet/common/types/rest_spec/download_sources.ts @@ -10,7 +10,7 @@ import type { DownloadSourceBase, DownloadSource } from '../models'; import type { ListResult } from './common'; export interface GetOneDownloadSourceResponse { - item: DownloadSourceBase; + item: DownloadSource; } export interface DeleteDownloadSourceResponse { diff --git a/x-pack/plugins/fleet/server/services/agent_policy.test.ts b/x-pack/plugins/fleet/server/services/agent_policy.test.ts index 37bc2abe4c27c3..9f42746c9c5fac 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.test.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.test.ts @@ -952,8 +952,11 @@ describe('Agent policy', () => { }); } }); - - await agentPolicyService.removeOutputFromAll(soClient, esClient, 'output-id-123'); + mockedAppContextService.getInternalUserSOClientWithoutSpaceExtension.mockReturnValue( + soClient + ); + mockedAppContextService.getInternalUserSOClientForSpaceId.mockReturnValue(soClient); + await agentPolicyService.removeOutputFromAll(esClient, 'output-id-123'); expect(mockedAgentPolicyServiceUpdate).toHaveBeenCalledTimes(2); expect(mockedAgentPolicyServiceUpdate).toHaveBeenCalledWith( @@ -993,6 +996,10 @@ describe('Agent policy', () => { mockedDownloadSourceService.getDefaultDownloadSourceId.mockResolvedValue( 'default-download-source-id' ); + mockedAppContextService.getInternalUserSOClientWithoutSpaceExtension.mockReturnValue( + soClient + ); + mockedAppContextService.getInternalUserSOClientForSpaceId.mockReturnValue(soClient); soClient.find.mockResolvedValue({ saved_objects: [ { @@ -1010,11 +1017,7 @@ describe('Agent policy', () => { ], } as any); - await agentPolicyService.removeDefaultSourceFromAll( - soClient, - esClient, - 'default-download-source-id' - ); + await agentPolicyService.removeDefaultSourceFromAll(esClient, 'default-download-source-id'); expect(mockedAgentPolicyServiceUpdate).toHaveBeenCalledTimes(2); expect(mockedAgentPolicyServiceUpdate).toHaveBeenCalledWith( diff --git a/x-pack/plugins/fleet/server/services/agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policy.ts index 38c785520b5109..6867e8d52667b5 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.ts @@ -18,6 +18,7 @@ import type { SavedObjectsClientContract, SavedObject, SavedObjectsUpdateResponse, + SavedObjectsFindOptions, } from '@kbn/core/server'; import { SavedObjectsUtils } from '@kbn/core/server'; @@ -538,6 +539,7 @@ class AgentPolicyService { fields?: string[]; esClient?: ElasticsearchClient; withAgentCount?: boolean; + spaceId?: string; } ): Promise<{ items: AgentPolicy[]; @@ -555,9 +557,10 @@ class AgentPolicyService { kuery, withPackagePolicies = false, fields, + spaceId, } = options; - const baseFindParams = { + const baseFindParams: SavedObjectsFindOptions = { type: savedObjectType, sortField, sortOrder, @@ -565,6 +568,11 @@ class AgentPolicyService { perPage, ...(fields ? { fields } : {}), }; + + if (spaceId) { + baseFindParams.namespaces = [spaceId]; + } + const filter = kuery ? normalizeKuery(savedObjectType, kuery) : undefined; let agentPoliciesSO; try { @@ -839,24 +847,22 @@ class AgentPolicyService { /** * Remove an output from all agent policies that are using it, and replace the output by the default ones. - * @param soClient * @param esClient * @param outputId */ - public async removeOutputFromAll( - soClient: SavedObjectsClientContract, - esClient: ElasticsearchClient, - outputId: string - ) { + public async removeOutputFromAll(esClient: ElasticsearchClient, outputId: string) { const savedObjectType = await getAgentPolicySavedObjectType(); const agentPolicies = ( - await soClient.find({ - type: savedObjectType, - fields: ['revision', 'data_output_id', 'monitoring_output_id'], - searchFields: ['data_output_id', 'monitoring_output_id'], - search: escapeSearchQueryPhrase(outputId), - perPage: SO_SEARCH_LIMIT, - }) + await appContextService + .getInternalUserSOClientWithoutSpaceExtension() + .find({ + type: savedObjectType, + fields: ['revision', 'data_output_id', 'monitoring_output_id'], + searchFields: ['data_output_id', 'monitoring_output_id'], + search: escapeSearchQueryPhrase(outputId), + perPage: SO_SEARCH_LIMIT, + namespaces: ['*'], + }) ).saved_objects.map(mapAgentPolicySavedObjectToAgentPolicy); if (agentPolicies.length > 0) { @@ -869,6 +875,9 @@ class AgentPolicyService { await pMap( agentPolicies, async (agentPolicy) => { + const soClient = appContextService.getInternalUserSOClientForSpaceId( + agentPolicy.space_ids?.[0] + ); const existingAgentPolicy = await this.get(soClient, agentPolicy.id, true); if (!existingAgentPolicy) { @@ -888,10 +897,14 @@ class AgentPolicyService { ); await pMap( agentPolicies, - (agentPolicy) => - this.update(soClient, esClient, agentPolicy.id, getAgentPolicy(agentPolicy), { + (agentPolicy) => { + const soClient = appContextService.getInternalUserSOClientForSpaceId( + agentPolicy.space_ids?.[0] + ); + return this.update(soClient, esClient, agentPolicy.id, getAgentPolicy(agentPolicy), { skipValidation: true, - }), + }); + }, { concurrency: 50, } @@ -903,28 +916,35 @@ class AgentPolicyService { * Remove a Fleet Server from all agent policies that are using it, to use the default one instead. */ public async removeFleetServerHostFromAll( - soClient: SavedObjectsClientContract, esClient: ElasticsearchClient, fleetServerHostId: string ) { const savedObjectType = await getAgentPolicySavedObjectType(); const agentPolicies = ( - await soClient.find({ - type: savedObjectType, - fields: ['revision', 'fleet_server_host_id'], - searchFields: ['fleet_server_host_id'], - search: escapeSearchQueryPhrase(fleetServerHostId), - perPage: SO_SEARCH_LIMIT, - }) + await appContextService + .getInternalUserSOClientWithoutSpaceExtension() + .find({ + type: savedObjectType, + fields: ['revision', 'fleet_server_host_id'], + searchFields: ['fleet_server_host_id'], + search: escapeSearchQueryPhrase(fleetServerHostId), + perPage: SO_SEARCH_LIMIT, + namespaces: ['*'], + }) ).saved_objects.map(mapAgentPolicySavedObjectToAgentPolicy); if (agentPolicies.length > 0) { await pMap( agentPolicies, (agentPolicy) => - this.update(soClient, esClient, agentPolicy.id, { - fleet_server_host_id: null, - }), + this.update( + appContextService.getInternalUserSOClientForSpaceId(agentPolicy.space_ids?.[0]), + esClient, + agentPolicy.id, + { + fleet_server_host_id: null, + } + ), { concurrency: 50, } @@ -1445,35 +1465,36 @@ class AgentPolicyService { * @param esClient * @param downloadSourceId */ - public async removeDefaultSourceFromAll( - soClient: SavedObjectsClientContract, - esClient: ElasticsearchClient, - downloadSourceId: string - ) { + public async removeDefaultSourceFromAll(esClient: ElasticsearchClient, downloadSourceId: string) { const savedObjectType = await getAgentPolicySavedObjectType(); const agentPolicies = ( - await soClient.find({ - type: savedObjectType, - fields: ['revision', 'download_source_id'], - searchFields: ['download_source_id'], - search: escapeSearchQueryPhrase(downloadSourceId), - perPage: SO_SEARCH_LIMIT, - }) - ).saved_objects.map((so) => ({ - id: so.id, - ...so.attributes, - })); + await appContextService + .getInternalUserSOClientWithoutSpaceExtension() + .find({ + type: savedObjectType, + fields: ['revision', 'download_source_id'], + searchFields: ['download_source_id'], + search: escapeSearchQueryPhrase(downloadSourceId), + perPage: SO_SEARCH_LIMIT, + namespaces: ['*'], + }) + ).saved_objects.map(mapAgentPolicySavedObjectToAgentPolicy); if (agentPolicies.length > 0) { await pMap( agentPolicies, (agentPolicy) => - this.update(soClient, esClient, agentPolicy.id, { - download_source_id: - agentPolicy.download_source_id === downloadSourceId - ? null - : agentPolicy.download_source_id, - }), + this.update( + appContextService.getInternalUserSOClientForSpaceId(agentPolicy.space_ids?.[0]), + esClient, + agentPolicy.id, + { + download_source_id: + agentPolicy.download_source_id === downloadSourceId + ? null + : agentPolicy.download_source_id, + } + ), { concurrency: 50, } diff --git a/x-pack/plugins/fleet/server/services/download_source.ts b/x-pack/plugins/fleet/server/services/download_source.ts index e679123f7e2559..21b5ac8355ae82 100644 --- a/x-pack/plugins/fleet/server/services/download_source.ts +++ b/x-pack/plugins/fleet/server/services/download_source.ts @@ -156,7 +156,6 @@ class DownloadSourceService { throw new DownloadSourceError(`Default Download source ${id} cannot be deleted.`); } await agentPolicyService.removeDefaultSourceFromAll( - soClient, appContextService.getInternalUserESClient(), id ); diff --git a/x-pack/plugins/fleet/server/services/fleet_server_host.ts b/x-pack/plugins/fleet/server/services/fleet_server_host.ts index 41eaf2b0e86a38..c28a065cca63e5 100644 --- a/x-pack/plugins/fleet/server/services/fleet_server_host.ts +++ b/x-pack/plugins/fleet/server/services/fleet_server_host.ts @@ -142,7 +142,7 @@ export async function deleteFleetServerHost( ); } - await agentPolicyService.removeFleetServerHostFromAll(soClient, esClient, id); + await agentPolicyService.removeFleetServerHostFromAll(esClient, id); return await soClient.delete(FLEET_SERVER_HOST_SAVED_OBJECT_TYPE, id); } diff --git a/x-pack/plugins/fleet/server/services/output.ts b/x-pack/plugins/fleet/server/services/output.ts index bd5066d3ffc436..96a056c7a9603f 100644 --- a/x-pack/plugins/fleet/server/services/output.ts +++ b/x-pack/plugins/fleet/server/services/output.ts @@ -153,6 +153,7 @@ async function getAgentPoliciesPerOutput(outputId?: string, isDefault?: boolean) const directAgentPolicies = await agentPolicyService.list(internalSoClientWithoutSpaceExtension, { kuery: agentPoliciesKuery, perPage: SO_SEARCH_LIMIT, + spaceId: '*', }); const directAgentPolicyIds = directAgentPolicies?.items.map((policy) => policy.id); @@ -162,6 +163,7 @@ async function getAgentPoliciesPerOutput(outputId?: string, isDefault?: boolean) const packagePolicySOs = await packagePolicyService.list(internalSoClientWithoutSpaceExtension, { kuery: packagePoliciesKuery, perPage: SO_SEARCH_LIMIT, + spaceId: '*', }); const agentPolicyIdsFromPackagePolicies = [ ...new Set( @@ -234,6 +236,7 @@ async function findPoliciesWithFleetServerOrSynthetics(outputId?: string, isDefa internalSoClientWithoutSpaceExtension, { fields: ['policy_ids', 'package.name'], + spaceId: '*', kuery: [FLEET_APM_PACKAGE, FLEET_SYNTHETICS_PACKAGE, FLEET_SERVER_PACKAGE] .map((packageName) => `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name:${packageName}`) .join(' or '), @@ -820,20 +823,9 @@ class OutputService { throw new OutputUnauthorizedError(`Default monitoring output ${id} cannot be deleted.`); } - const internalSoClientWithoutSpaceExtension = - appContextService.getInternalUserSOClientWithoutSpaceExtension(); + await packagePolicyService.removeOutputFromAll(appContextService.getInternalUserESClient(), id); - await packagePolicyService.removeOutputFromAll( - internalSoClientWithoutSpaceExtension, - appContextService.getInternalUserESClient(), - id - ); - - await agentPolicyService.removeOutputFromAll( - internalSoClientWithoutSpaceExtension, - appContextService.getInternalUserESClient(), - id - ); + await agentPolicyService.removeOutputFromAll(appContextService.getInternalUserESClient(), id); auditLoggingService.writeCustomSoAuditLog({ action: 'delete', diff --git a/x-pack/plugins/fleet/server/services/package_policy.test.ts b/x-pack/plugins/fleet/server/services/package_policy.test.ts index fa86c4b0b9e2d8..f0558aaa8fe265 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.test.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.test.ts @@ -5157,8 +5157,14 @@ describe('Package policy service', () => { }); } }); + appContextService.start( + createAppContextStartContractMock(undefined, false, { + internal: soClient, + withoutSpaceExtensions: soClient, + }) + ); - await packagePolicyService.removeOutputFromAll(soClient, esClient, 'output-id-123'); + await packagePolicyService.removeOutputFromAll(esClient, 'output-id-123'); expect(updateSpy).toHaveBeenCalledTimes(1); expect(updateSpy).toHaveBeenCalledWith( diff --git a/x-pack/plugins/fleet/server/services/package_policy.ts b/x-pack/plugins/fleet/server/services/package_policy.ts index c1970548e98faa..3daab1c0cd7c69 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.ts @@ -2030,20 +2030,19 @@ class PackagePolicyClientImpl implements PackagePolicyClient { } } - public async removeOutputFromAll( - soClient: SavedObjectsClientContract, - esClient: ElasticsearchClient, - outputId: string - ) { + public async removeOutputFromAll(esClient: ElasticsearchClient, outputId: string) { const savedObjectType = await getPackagePolicySavedObjectType(); const packagePolicies = ( - await soClient.find({ - type: savedObjectType, - fields: ['name', 'enabled', 'policy_ids', 'inputs', 'output_id'], - searchFields: ['output_id'], - search: escapeSearchQueryPhrase(outputId), - perPage: SO_SEARCH_LIMIT, - }) + await appContextService + .getInternalUserSOClientWithoutSpaceExtension() + .find({ + type: savedObjectType, + fields: ['name', 'enabled', 'policy_ids', 'inputs', 'output_id'], + searchFields: ['output_id'], + search: escapeSearchQueryPhrase(outputId), + perPage: SO_SEARCH_LIMIT, + namespaces: ['*'], + }) ).saved_objects.map(mapPackagePolicySavedObjectToPackagePolicy); if (packagePolicies.length > 0) { @@ -2060,6 +2059,9 @@ class PackagePolicyClientImpl implements PackagePolicyClient { await pMap( packagePolicies, async (packagePolicy) => { + const soClient = appContextService.getInternalUserSOClientForSpaceId( + packagePolicy.spaceIds?.[0] + ); const existingPackagePolicy = await this.get(soClient, packagePolicy.id); if (!existingPackagePolicy) { @@ -2087,6 +2089,9 @@ class PackagePolicyClientImpl implements PackagePolicyClient { await pMap( packagePolicies, (packagePolicy) => { + const soClient = appContextService.getInternalUserSOClientForSpaceId( + packagePolicy.spaceIds?.[0] + ); return this.update( soClient, esClient, diff --git a/x-pack/plugins/fleet/server/services/package_policy_service.ts b/x-pack/plugins/fleet/server/services/package_policy_service.ts index 8b47f42380935d..46913642843cf7 100644 --- a/x-pack/plugins/fleet/server/services/package_policy_service.ts +++ b/x-pack/plugins/fleet/server/services/package_policy_service.ts @@ -225,11 +225,7 @@ export interface PackagePolicyClient { * @param esClient * @param outputId */ - removeOutputFromAll( - soClient: SavedObjectsClientContract, - esClient: ElasticsearchClient, - outputId: string - ): Promise; + removeOutputFromAll(esClient: ElasticsearchClient, outputId: string): Promise; /** * Returns an `AsyncIterable` for retrieving all integration policy IDs diff --git a/x-pack/test/fleet_api_integration/apis/space_awareness/agent_policies_side_effects.ts b/x-pack/test/fleet_api_integration/apis/space_awareness/agent_policies_side_effects.ts new file mode 100644 index 00000000000000..47f10f705dfd01 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/space_awareness/agent_policies_side_effects.ts @@ -0,0 +1,214 @@ +/* + * 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 { CreateAgentPolicyResponse } from '@kbn/fleet-plugin/common'; +import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; +import { skipIfNoDockerRegistry } from '../../helpers'; +import { SpaceTestApiClient } from './api_helper'; +import { cleanFleetIndices } from './helpers'; + +export default function (providerContext: FtrProviderContext) { + const { getService } = providerContext; + const supertest = getService('supertest'); + const esClient = getService('es'); + const kibanaServer = getService('kibanaServer'); + const spaces = getService('spaces'); + let TEST_SPACE_1: string; + + describe('agent policies side effects', function () { + skipIfNoDockerRegistry(providerContext); + const apiClient = new SpaceTestApiClient(supertest); + + let defaultSpacePolicy1: CreateAgentPolicyResponse; + let spaceTest1Policy1: CreateAgentPolicyResponse; + + before(async () => { + TEST_SPACE_1 = spaces.getDefaultTestSpace(); + await kibanaServer.savedObjects.cleanStandardList(); + await kibanaServer.savedObjects.cleanStandardList({ + space: TEST_SPACE_1, + }); + await cleanFleetIndices(esClient); + + await apiClient.postEnableSpaceAwareness(); + await spaces.createTestSpace(TEST_SPACE_1); + + const [_defaultSpacePolicy1, _spaceTest1Policy1] = await Promise.all([ + apiClient.createAgentPolicy(), + apiClient.createAgentPolicy(TEST_SPACE_1), + ]); + defaultSpacePolicy1 = _defaultSpacePolicy1; + spaceTest1Policy1 = _spaceTest1Policy1; + }); + + after(async () => { + await kibanaServer.savedObjects.cleanStandardList(); + await kibanaServer.savedObjects.cleanStandardList({ + space: TEST_SPACE_1, + }); + await cleanFleetIndices(esClient); + }); + + describe('Download source', () => { + let downloadSourceId: string; + before(async () => { + const res = await apiClient.postDownloadSource({ + name: `test ${Date.now()}`, + host: 'https://test.fr', + }); + downloadSourceId = res.item.id; + await Promise.all([ + apiClient.putAgentPolicy(defaultSpacePolicy1.item.id, { + name: defaultSpacePolicy1.item.name, + namespace: defaultSpacePolicy1.item.namespace, + description: defaultSpacePolicy1.item.description, + download_source_id: downloadSourceId, + }), + apiClient.putAgentPolicy( + spaceTest1Policy1.item.id, + { + name: spaceTest1Policy1.item.name, + namespace: spaceTest1Policy1.item.namespace, + description: spaceTest1Policy1.item.description, + download_source_id: downloadSourceId, + }, + TEST_SPACE_1 + ), + ]); + }); + it('should remove download_source_id accross spaces', async () => { + const policiesResBefore = await Promise.all([ + apiClient.getAgentPolicy(defaultSpacePolicy1.item.id), + apiClient.getAgentPolicy(spaceTest1Policy1.item.id, TEST_SPACE_1), + ]); + + for (const policyRes of policiesResBefore) { + expect(policyRes.item.download_source_id).to.be(downloadSourceId); + } + + await apiClient.deleteDownloadSource(downloadSourceId); + + const policiesResAfter = await Promise.all([ + apiClient.getAgentPolicy(defaultSpacePolicy1.item.id), + apiClient.getAgentPolicy(spaceTest1Policy1.item.id, TEST_SPACE_1), + ]); + + for (const policyRes of policiesResAfter) { + expect(policyRes.item.download_source_id).not.to.be(downloadSourceId); + } + }); + }); + + describe('Fleet server host', () => { + let fleetServerHostId: string; + before(async () => { + const res = await apiClient.postFleetServerHosts({ + name: `test ${Date.now()}`, + host_urls: ['https://test.fr'], + }); + fleetServerHostId = res.item.id; + await Promise.all([ + apiClient.putAgentPolicy(defaultSpacePolicy1.item.id, { + name: defaultSpacePolicy1.item.name, + namespace: defaultSpacePolicy1.item.namespace, + description: defaultSpacePolicy1.item.description, + fleet_server_host_id: fleetServerHostId, + }), + apiClient.putAgentPolicy( + spaceTest1Policy1.item.id, + { + name: spaceTest1Policy1.item.name, + namespace: spaceTest1Policy1.item.namespace, + description: spaceTest1Policy1.item.description, + fleet_server_host_id: fleetServerHostId, + }, + TEST_SPACE_1 + ), + ]); + }); + it('should remove fleet server host accross spaces', async () => { + const policiesResBefore = await Promise.all([ + apiClient.getAgentPolicy(defaultSpacePolicy1.item.id), + apiClient.getAgentPolicy(spaceTest1Policy1.item.id, TEST_SPACE_1), + ]); + + for (const policyRes of policiesResBefore) { + expect(policyRes.item.fleet_server_host_id).to.be(fleetServerHostId); + } + + await apiClient.deleteFleetServerHosts(fleetServerHostId); + + const policiesResAfter = await Promise.all([ + apiClient.getAgentPolicy(defaultSpacePolicy1.item.id), + apiClient.getAgentPolicy(spaceTest1Policy1.item.id, TEST_SPACE_1), + ]); + + for (const policyRes of policiesResAfter) { + expect(policyRes.item.fleet_server_host_id).not.to.be(fleetServerHostId); + } + }); + }); + + describe('Output', () => { + let outputId: string; + before(async () => { + const res = await apiClient.postOutput({ + name: `test ${Date.now()}`, + type: 'elasticsearch', + is_default: false, + is_default_monitoring: false, + hosts: ['https://test.fr'], + }); + outputId = res.item.id; + await Promise.all([ + apiClient.putAgentPolicy(defaultSpacePolicy1.item.id, { + name: defaultSpacePolicy1.item.name, + namespace: defaultSpacePolicy1.item.namespace, + description: defaultSpacePolicy1.item.description, + data_output_id: outputId, + monitoring_output_id: outputId, + }), + apiClient.putAgentPolicy( + spaceTest1Policy1.item.id, + { + name: spaceTest1Policy1.item.name, + namespace: spaceTest1Policy1.item.namespace, + description: spaceTest1Policy1.item.description, + data_output_id: outputId, + monitoring_output_id: outputId, + }, + TEST_SPACE_1 + ), + ]); + }); + it('should remove output host accross spaces', async () => { + const policiesResBefore = await Promise.all([ + apiClient.getAgentPolicy(defaultSpacePolicy1.item.id), + apiClient.getAgentPolicy(spaceTest1Policy1.item.id, TEST_SPACE_1), + ]); + + for (const policyRes of policiesResBefore) { + expect(policyRes.item.data_output_id).to.be(outputId); + expect(policyRes.item.monitoring_output_id).to.be(outputId); + } + + await apiClient.deleteOutput(outputId); + + const policiesResAfter = await Promise.all([ + apiClient.getAgentPolicy(defaultSpacePolicy1.item.id), + apiClient.getAgentPolicy(spaceTest1Policy1.item.id, TEST_SPACE_1), + ]); + + for (const policyRes of policiesResAfter) { + expect(policyRes.item.data_output_id).not.to.be(outputId); + expect(policyRes.item.monitoring_output_id).not.to.be(outputId); + } + }); + }); + }); +} diff --git a/x-pack/test/fleet_api_integration/apis/space_awareness/api_helper.ts b/x-pack/test/fleet_api_integration/apis/space_awareness/api_helper.ts index e7863e7c46d2c5..9009e2b81a73bd 100644 --- a/x-pack/test/fleet_api_integration/apis/space_awareness/api_helper.ts +++ b/x-pack/test/fleet_api_integration/apis/space_awareness/api_helper.ts @@ -33,6 +33,12 @@ import { UpdateAgentPolicyRequest, UpdatePackageResponse, UpdatePackageRequest, + PostDownloadSourceRequest, + GetOneDownloadSourceResponse, + PostFleetServerHostsRequest, + PostFleetServerHostsResponse, + PostOutputRequest, + GetOneOutputResponse, } from '@kbn/fleet-plugin/common/types'; import { GetUninstallTokenResponse, @@ -481,4 +487,67 @@ export class SpaceTestApiClient { return res; } + // Download source + async deleteDownloadSource(id: string, spaceId?: string) { + const { body: res } = await this.supertest + .delete(`${this.getBaseUrl(spaceId)}/api/fleet/agent_download_sources/${id}`) + .set('kbn-xsrf', 'xxxx') + .expect(200); + + return res; + } + async postDownloadSource( + data: PostDownloadSourceRequest['body'], + spaceId?: string + ): Promise { + const { body: res } = await this.supertest + .post(`${this.getBaseUrl(spaceId)}/api/fleet/agent_download_sources`) + .set('kbn-xsrf', 'xxxx') + .send(data) + .expect(200); + + return res; + } + // Fleet server hosts + async deleteFleetServerHosts(id: string, spaceId?: string) { + const { body: res } = await this.supertest + .delete(`${this.getBaseUrl(spaceId)}/api/fleet/fleet_server_hosts/${id}`) + .set('kbn-xsrf', 'xxxx') + .expect(200); + + return res; + } + async postFleetServerHosts( + data: PostFleetServerHostsRequest['body'], + spaceId?: string + ): Promise { + const { body: res } = await this.supertest + .post(`${this.getBaseUrl(spaceId)}/api/fleet/fleet_server_hosts`) + .set('kbn-xsrf', 'xxxx') + .send(data) + .expect(200); + + return res; + } + // Output + async deleteOutput(id: string, spaceId?: string) { + const { body: res } = await this.supertest + .delete(`${this.getBaseUrl(spaceId)}/api/fleet/outputs/${id}`) + .set('kbn-xsrf', 'xxxx') + .expect(200); + + return res; + } + async postOutput( + data: PostOutputRequest['body'], + spaceId?: string + ): Promise { + const { body: res } = await this.supertest + .post(`${this.getBaseUrl(spaceId)}/api/fleet/outputs`) + .set('kbn-xsrf', 'xxxx') + .send(data) + .expect(200); + + return res; + } } diff --git a/x-pack/test/fleet_api_integration/apis/space_awareness/index.js b/x-pack/test/fleet_api_integration/apis/space_awareness/index.js index 3162492de20365..775afcd54cdae4 100644 --- a/x-pack/test/fleet_api_integration/apis/space_awareness/index.js +++ b/x-pack/test/fleet_api_integration/apis/space_awareness/index.js @@ -10,6 +10,7 @@ export default function loadTests({ loadTestFile }) { loadTestFile(require.resolve('./enrollment_api_keys')); loadTestFile(require.resolve('./uninstall_tokens')); loadTestFile(require.resolve('./agent_policies')); + loadTestFile(require.resolve('./agent_policies_side_effects')); loadTestFile(require.resolve('./agents')); loadTestFile(require.resolve('./enrollment_settings')); loadTestFile(require.resolve('./package_install'));