-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathoutputs.ts
196 lines (172 loc) · 6.63 KB
/
outputs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* 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 type { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server';
import { isEqual } from 'lodash';
import { safeDump } from 'js-yaml';
import type { PreconfiguredOutput, Output, NewOutput } from '../../../common/types';
import { normalizeHostsForAgents } from '../../../common/services';
import type { FleetConfigType } from '../../config';
import { DEFAULT_OUTPUT_ID, DEFAULT_OUTPUT } from '../../constants';
import { outputService } from '../output';
import { agentPolicyService } from '../agent_policy';
import { appContextService } from '../app_context';
import { isDifferent } from './utils';
export function getPreconfiguredOutputFromConfig(config?: FleetConfigType) {
const { outputs: outputsOrUndefined } = config;
const outputs: PreconfiguredOutput[] = (outputsOrUndefined || []).concat([
...(config?.agents.elasticsearch.hosts
? [
{
...DEFAULT_OUTPUT,
id: DEFAULT_OUTPUT_ID,
hosts: config?.agents.elasticsearch.hosts,
ca_sha256: config?.agents.elasticsearch.ca_sha256,
is_preconfigured: true,
} as PreconfiguredOutput,
]
: []),
]);
return outputs;
}
export async function ensurePreconfiguredOutputs(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
outputs: PreconfiguredOutput[]
) {
await createOrUpdatePreconfiguredOutputs(soClient, esClient, outputs);
await cleanPreconfiguredOutputs(soClient, esClient, outputs);
}
export async function createOrUpdatePreconfiguredOutputs(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
outputs: PreconfiguredOutput[]
) {
const logger = appContextService.getLogger();
if (outputs.length === 0) {
return;
}
const existingOutputs = await outputService.bulkGet(
soClient,
outputs.map(({ id }) => id),
{ ignoreNotFound: true }
);
await Promise.all(
outputs.map(async (output) => {
const existingOutput = existingOutputs.find((o) => o.id === output.id);
const { id, config, ...outputData } = output;
const configYaml = config ? safeDump(config) : undefined;
const data: NewOutput = {
...outputData,
is_preconfigured: true,
config_yaml: configYaml ?? null,
// Set value to null to update these fields on update
ca_sha256: outputData.ca_sha256 ?? null,
ca_trusted_fingerprint: outputData.ca_trusted_fingerprint ?? null,
ssl: outputData.ssl ?? null,
};
if (!data.hosts || data.hosts.length === 0) {
data.hosts = outputService.getDefaultESHosts();
}
const isCreate = !existingOutput;
// field in allow edit are not updated through preconfiguration
if (!isCreate && output.allow_edit) {
for (const key of output.allow_edit) {
// @ts-expect-error
data[key] = existingOutput[key];
}
}
const isUpdateWithNewData =
existingOutput && isPreconfiguredOutputDifferentFromCurrent(existingOutput, data);
if (isCreate) {
logger.debug(`Creating output ${output.id}`);
await outputService.create(soClient, esClient, data, { id, fromPreconfiguration: true });
} else if (isUpdateWithNewData) {
logger.debug(`Updating output ${output.id}`);
await outputService.update(soClient, esClient, id, data, { fromPreconfiguration: true });
// Bump revision of all policies using that output
if (outputData.is_default || outputData.is_default_monitoring) {
await agentPolicyService.bumpAllAgentPolicies(soClient, esClient);
} else {
await agentPolicyService.bumpAllAgentPoliciesForOutput(soClient, esClient, id);
}
}
})
);
}
export async function cleanPreconfiguredOutputs(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
outputs: PreconfiguredOutput[]
) {
const existingOutputs = await outputService.list(soClient);
const existingPreconfiguredOutput = existingOutputs.items.filter(
(o) => o.is_preconfigured === true
);
const logger = appContextService.getLogger();
for (const output of existingPreconfiguredOutput) {
const hasBeenDelete = !outputs.find(({ id }) => output.id === id);
if (!hasBeenDelete) {
continue;
}
if (output.is_default) {
logger.info(`Updating default preconfigured output ${output.id} is no longer preconfigured`);
await outputService.update(
soClient,
esClient,
output.id,
{ is_preconfigured: false },
{
fromPreconfiguration: true,
}
);
} else if (output.is_default_monitoring) {
logger.info(`Updating default preconfigured output ${output.id} is no longer preconfigured`);
await outputService.update(
soClient,
esClient,
output.id,
{ is_preconfigured: false },
{
fromPreconfiguration: true,
}
);
} else {
logger.info(`Deleting preconfigured output ${output.id}`);
await outputService.delete(soClient, output.id, { fromPreconfiguration: true });
}
}
}
function isPreconfiguredOutputDifferentFromCurrent(
existingOutput: Output,
preconfiguredOutput: Partial<Output>
): boolean {
return (
!existingOutput.is_preconfigured ||
isDifferent(existingOutput.is_default, preconfiguredOutput.is_default) ||
isDifferent(existingOutput.is_default_monitoring, preconfiguredOutput.is_default_monitoring) ||
isDifferent(existingOutput.name, preconfiguredOutput.name) ||
isDifferent(existingOutput.type, preconfiguredOutput.type) ||
(preconfiguredOutput.hosts &&
!isEqual(
existingOutput?.type === 'elasticsearch'
? existingOutput.hosts?.map(normalizeHostsForAgents)
: existingOutput.hosts,
preconfiguredOutput.type === 'elasticsearch'
? preconfiguredOutput.hosts.map(normalizeHostsForAgents)
: preconfiguredOutput.hosts
)) ||
isDifferent(preconfiguredOutput.ssl, existingOutput.ssl) ||
isDifferent(existingOutput.ca_sha256, preconfiguredOutput.ca_sha256) ||
isDifferent(
existingOutput.ca_trusted_fingerprint,
preconfiguredOutput.ca_trusted_fingerprint
) ||
isDifferent(existingOutput.config_yaml, preconfiguredOutput.config_yaml) ||
isDifferent(existingOutput.proxy_id, preconfiguredOutput.proxy_id) ||
isDifferent(existingOutput.allow_edit ?? [], preconfiguredOutput.allow_edit ?? [])
);
}