diff --git a/x-pack/plugins/fleet/server/routes/agent/source_uri_utils.test.ts b/x-pack/plugins/fleet/server/routes/agent/source_uri_utils.test.ts index 68308ce053c5d5..17d477d5be2182 100644 --- a/x-pack/plugins/fleet/server/routes/agent/source_uri_utils.test.ts +++ b/x-pack/plugins/fleet/server/routes/agent/source_uri_utils.test.ts @@ -106,9 +106,9 @@ describe('helpers', () => { is_protected: false, }; - expect(await getSourceUriForAgentPolicy(soClientMock, agentPolicy)).toEqual( - 'http://custom-registry-test' - ); + expect(await getSourceUriForAgentPolicy(soClientMock, agentPolicy)).toEqual({ + host: 'http://custom-registry-test', + }); }); it('should return the default source_uri if there is none set on the agent policy ', async () => { const agentPolicy: AgentPolicy = { @@ -124,9 +124,9 @@ describe('helpers', () => { is_protected: false, }; - expect(await getSourceUriForAgentPolicy(soClientMock, agentPolicy)).toEqual( - 'http://default-registry.co' - ); + expect(await getSourceUriForAgentPolicy(soClientMock, agentPolicy)).toEqual({ + host: 'http://default-registry.co', + }); }); }); }); diff --git a/x-pack/plugins/fleet/server/routes/agent/source_uri_utils.ts b/x-pack/plugins/fleet/server/routes/agent/source_uri_utils.ts index 6c6edd7b401b7a..3f571fdcb09cc5 100644 --- a/x-pack/plugins/fleet/server/routes/agent/source_uri_utils.ts +++ b/x-pack/plugins/fleet/server/routes/agent/source_uri_utils.ts @@ -24,5 +24,5 @@ export const getSourceUriForAgentPolicy = async ( if (!downloadSource) { throw new Error(`Download source host not found ${downloadSourceId}`); } - return downloadSource.host; + return { host: downloadSource.host, proxy_id: downloadSource.proxy_id }; }; diff --git a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts index d50e12541063a2..0a05d178ac7455 100644 --- a/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.ts @@ -58,8 +58,15 @@ export async function getFullAgentPolicy( return null; } - const { outputs, proxies, dataOutput, fleetServerHosts, monitoringOutput, sourceUri } = - await fetchRelatedSavedObjects(soClient, agentPolicy); + const { + outputs, + proxies, + dataOutput, + fleetServerHosts, + monitoringOutput, + downloadSourceUri, + downloadSourceProxyUri, + } = await fetchRelatedSavedObjects(soClient, agentPolicy); // Build up an in-memory object for looking up Package Info, so we don't have // call `getPackageInfo` for every single policy, which incurs performance costs @@ -118,7 +125,8 @@ export async function getFullAgentPolicy( revision: agentPolicy.revision, agent: { download: { - sourceURI: sourceUri, + sourceURI: downloadSourceUri, + ...(downloadSourceProxyUri ? { proxy_url: downloadSourceProxyUri } : {}), }, monitoring: agentPolicy.monitoring_enabled && agentPolicy.monitoring_enabled.length > 0 diff --git a/x-pack/plugins/fleet/server/services/agent_policies/related_saved_objects.ts b/x-pack/plugins/fleet/server/services/agent_policies/related_saved_objects.ts index e13f1e5b7121f2..b614b9c2dd9e4b 100644 --- a/x-pack/plugins/fleet/server/services/agent_policies/related_saved_objects.ts +++ b/x-pack/plugins/fleet/server/services/agent_policies/related_saved_objects.ts @@ -34,19 +34,20 @@ export async function fetchRelatedSavedObjects( const monitoringOutputId = agentPolicy.monitoring_output_id || defaultMonitoringOutputId || dataOutputId; - const [outputs, sourceUri, fleetServerHosts] = await Promise.all([ - outputService.bulkGet(soClient, uniq([dataOutputId, monitoringOutputId]), { - ignoreNotFound: true, - }), - getSourceUriForAgentPolicy(soClient, agentPolicy), - getFleetServerHostsForAgentPolicy(soClient, agentPolicy).catch((err) => { - appContextService - .getLogger() - ?.warn(`Unable to get fleet server hosts for policy ${agentPolicy?.id}: ${err.message}`); + const [outputs, { host: downloadSourceUri, proxy_id: downloadSourceProxyId }, fleetServerHosts] = + await Promise.all([ + outputService.bulkGet(soClient, uniq([dataOutputId, monitoringOutputId]), { + ignoreNotFound: true, + }), + getSourceUriForAgentPolicy(soClient, agentPolicy), + getFleetServerHostsForAgentPolicy(soClient, agentPolicy).catch((err) => { + appContextService + .getLogger() + ?.warn(`Unable to get fleet server hosts for policy ${agentPolicy?.id}: ${err.message}`); - return; - }), - ]); + return; + }), + ]); const dataOutput = outputs.find((output) => output.id === dataOutputId); if (!dataOutput) { @@ -62,16 +63,27 @@ export async function fetchRelatedSavedObjects( .flatMap((output) => output.proxy_id) .filter((proxyId): proxyId is string => typeof proxyId !== 'undefined' && proxyId !== null) .concat(fleetServerHosts?.proxy_id ? [fleetServerHosts.proxy_id] : []) + .concat(downloadSourceProxyId ? [downloadSourceProxyId] : []) ); const proxies = proxyIds.length ? await bulkGetFleetProxies(soClient, proxyIds) : []; + let downloadSourceProxyUri: string | null = null; + + if (downloadSourceProxyId) { + const downloadSourceProxy = proxies.find((proxy) => proxy.id === downloadSourceProxyId); + if (downloadSourceProxy) { + downloadSourceProxyUri = downloadSourceProxy.url; + } + } + return { outputs, proxies, dataOutput, monitoringOutput, - sourceUri, + downloadSourceUri, + downloadSourceProxyUri, fleetServerHosts, }; } diff --git a/x-pack/test/fleet_api_integration/apis/download_sources/crud.ts b/x-pack/test/fleet_api_integration/apis/download_sources/crud.ts index a07604da510f75..82db67c75f593d 100644 --- a/x-pack/test/fleet_api_integration/apis/download_sources/crud.ts +++ b/x-pack/test/fleet_api_integration/apis/download_sources/crud.ts @@ -200,25 +200,14 @@ export default function (providerContext: FtrProviderContext) { }); }); - describe.only('proxy_id behaviour', () => { + describe('proxy_id behaviour', () => { const PROXY_ID = 'download-source-proxy-id'; before(async () => { - const existingProxy = await supertest - .get(`/api/fleet/proxies/${PROXY_ID}`) - .catch(() => null); - - if (existingProxy) { - return; - } - await supertest - .post(`/api/fleet/proxies`) - .set('kbn-xsrf', 'xxxx') - .send({ - id: PROXY_ID, - name: 'Download source proxy test', - url: 'https://some.proxy:3232', - }) - .expect(200); + await supertest.post(`/api/fleet/proxies`).set('kbn-xsrf', 'xxxx').send({ + id: PROXY_ID, + name: 'Download source proxy test', + url: 'https://some.source.proxy:3232', + }); }); it('should allow creating a new download source host with a proxy_id ', async function () { @@ -242,6 +231,43 @@ export default function (providerContext: FtrProviderContext) { }); }); + it('should set agent.download.proxy_url on the full agent policy', async function () { + const { body: postResponse } = await supertest + .post(`/api/fleet/agent_download_sources`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'download source with valid proxy id for agent test', + host: 'http://test.fr:443', + proxy_id: PROXY_ID, + is_default: false, + }) + .expect(200); + + const { id: downloadSourceId } = postResponse.item; + + const { body: postAgentPolicyResponse } = await supertest + .post(`/api/fleet/agent_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'agent policy with download source', + namespace: 'default', + description: '', + is_default: false, + download_source_id: downloadSourceId, + }); + + const { id: agentPolicyId } = postAgentPolicyResponse.item; + + const { body: getAgentPolicyResponse } = await supertest + .get(`/api/fleet/agent_policies/${agentPolicyId}/full`) + .set('kbn-xsrf', 'xxxx') + .send(); + + expect(getAgentPolicyResponse.item.agent.download.proxy_url).to.eql( + 'https://some.source.proxy:3232' + ); + }); + it('should not allow creating a new download source host with an invalid proxy_id ', async function () { await supertest .post(`/api/fleet/agent_download_sources`)