Skip to content

Commit

Permalink
removed host port from policy config, added try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Nov 21, 2022
1 parent b29541b commit 6c6de57
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
66 changes: 43 additions & 23 deletions x-pack/plugins/fleet/server/collectors/agent_policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,53 @@ import type { ElasticsearchClient } from '@kbn/core/server';

import { AGENT_POLICY_INDEX } from '../../common';
import { ES_SEARCH_LIMIT } from '../../common/constants';
import { appContextService } from '../services';

interface AgentPoliciesUsage {
count: number;
output_types: string[];
}

const DEFAULT_AGENT_POLICIES_USAGE = {
count: 0,
output_types: [],
};

export const getAgentPoliciesUsage = async (
esClient: ElasticsearchClient,
abortController: AbortController
): Promise<any> => {
const res = await esClient.search(
{
index: AGENT_POLICY_INDEX,
size: ES_SEARCH_LIMIT,
track_total_hits: true,
rest_total_hits_as_int: true,
},
{ signal: abortController.signal }
);

const agentPolicies = res.hits.hits;

const outputTypes = new Set<string>();
agentPolicies.forEach((item) => {
const source = (item._source as any) ?? {};
Object.keys(source.data.outputs).forEach((output) => {
outputTypes.add(source.data.outputs[output].type);
): Promise<AgentPoliciesUsage> => {
try {
const res = await esClient.search(
{
index: AGENT_POLICY_INDEX,
size: ES_SEARCH_LIMIT,
track_total_hits: true,
rest_total_hits_as_int: true,
},
{ signal: abortController.signal }
);

const agentPolicies = res.hits.hits;

const outputTypes = new Set<string>();
agentPolicies.forEach((item) => {
const source = (item._source as any) ?? {};
Object.keys(source.data.outputs).forEach((output) => {
outputTypes.add(source.data.outputs[output].type);
});
});
});

return {
count: res.hits.total,
output_types: Array.from(outputTypes),
};
return {
count: res.hits.total as number,
output_types: Array.from(outputTypes),
};
} catch (error) {
if (error.statusCode === 404) {
appContextService.getLogger().debug('Index .fleet-policies does not exist yet.');
} else {
throw error;
}
return DEFAULT_AGENT_POLICIES_USAGE;
}
};
11 changes: 10 additions & 1 deletion x-pack/plugins/fleet/server/collectors/fleet_server_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,17 @@ export const getFleetServerConfig = async (soClient: SavedObjectsClient): Promis
perPage: SO_SEARCH_LIMIT,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name:fleet_server`,
});
const getInputConfig = (item: any) => {
let config = (item.inputs[0] ?? {}).compiled_input;
if (config.server) {
config = { ...config, server: { ...config.server } };
delete config.server.host;
delete config.server.port;
}
return config;
};
const policies = res.items.map((item) => ({
input_config: (item.inputs[0] ?? {}).compiled_input,
input_config: getInputConfig(item),
}));

return { policies };
Expand Down

0 comments on commit 6c6de57

Please sign in to comment.