Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Fix bug with duplicate Fleet Server inputs in Cloud deployments #119925

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1066,11 +1066,26 @@ export function updatePackageInputs(
];

for (const update of inputsUpdated) {
// If update have an undefined policy template
// we only match on `type` .
let originalInput = update.policy_template
? inputs.find((i) => i.type === update.type && i.policy_template === update.policy_template)
: inputs.find((i) => i.type === update.type);
let originalInput: NewPackagePolicyInput | undefined;

if (update.policy_template) {
// If the updated value defines a policy template, try to find an original input
// with the same policy template value
const matchingInput = inputs.find(
(i) => i.type === update.type && i.policy_template === update.policy_template
);

// If we didn't find an input with the same policy template, try to look for one
// with the same type, but with an undefined policy template. This ensures we catch
// cases where we're upgrading an older policy from before policy template was
// reliably define on package policy inputs.
originalInput =
matchingInput || inputs.find((i) => i.type === update.type && !i.policy_template);
} else {
// For inputs that don't specify a policy template, just grab the first input
// that matches its `type`
originalInput = inputs.find((i) => i.type === update.type);
}

// If there's no corresponding input on the original package policy, just
// take the override value from the new package as-is. This case typically
Expand All @@ -1091,6 +1106,12 @@ export function updatePackageInputs(
originalInput.keep_enabled = update.keep_enabled;
}

// `policy_template` should always be defined, so if we have an older policy here we need
// to ensure we set it
if (originalInput.policy_template === undefined && update.policy_template !== undefined) {
originalInput.policy_template = update.policy_template;
}

if (update.vars) {
const indexOfInput = inputs.indexOf(originalInput);
inputs[indexOfInput] = deepMergeVars(originalInput, update, true) as NewPackagePolicyInput;
Expand Down