Skip to content

Commit

Permalink
Fix bug with duplicate Fleet Server inputs
Browse files Browse the repository at this point in the history
Rework logic around looking up `originalInput` value to handle cases in
which `policy_template` is `undefined` on the policy's input object.
  • Loading branch information
kpollich committed Nov 30, 2021
1 parent 3c11fa0 commit 1146d88
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 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 Down

0 comments on commit 1146d88

Please sign in to comment.