Skip to content

Commit

Permalink
agent policy full test working
Browse files Browse the repository at this point in the history
  • Loading branch information
hop-dev committed Aug 15, 2023
1 parent 89f28e3 commit 741e336
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 40 deletions.
Expand Up @@ -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 = {
Expand All @@ -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',
});
});
});
});
Expand Up @@ -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 };
};
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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) {
Expand All @@ -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,
};
}
60 changes: 43 additions & 17 deletions x-pack/test/fleet_api_integration/apis/download_sources/crud.ts
Expand Up @@ -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 () {
Expand All @@ -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`)
Expand Down

0 comments on commit 741e336

Please sign in to comment.