-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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] fixed undefined error when missing policy vars in template #124215
[Fleet] fixed undefined error when missing policy vars in template #124215
Conversation
Pinging @elastic/fleet (Team:Fleet) |
@@ -143,7 +143,7 @@ export const validatePackagePolicy = ( | |||
results[name] = input.enabled | |||
? validatePackagePolicyConfig( | |||
configEntry, | |||
inputVarDefsByPolicyTemplateAndType[inputKey][name], | |||
(inputVarDefsByPolicyTemplateAndType[inputKey] ?? {})[name], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have a second level of inputVarDefs just by input type if there is no policy_template
something like this? otherwise we loose validation and I do not think we should allow to create inputs that are not valid.
inputVarDefsByPolicyTemplateAndType[inputKey]
? inputVarDefsByPolicyTemplateAndType[inputKey][name]
: (inputVarDefsByType[inputType] ?? {})[name] // here I am wondering if we should not throw if there is no input matching the input type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what do you mean to put in inputVarDefsByType
, in this scenario, there are empty vars in policy_templates.inputs.linux/metrics.vars
. The validation is already there for data_streams.streams.input.vars
.
See the package details:
{
"item": {
"policy_templates": [
{
"name": "system",
"title": "Linux kernel metrics",
"description": "Collect system metrics from Linux operating systems",
"inputs": [
{
"title": "Collect low-level system metrics from Linux instances",
"vars": [],
"type": "linux/metrics",
"description": "Collecting Linux conntrack, ksm, pageinfo metrics."
}
],
"multiple": true
}
],
"data_streams": [
{
"dataset": "linux.memory",
"package": "linux",
"path": "memory",
"streams": [
{
"input": "linux/metrics",
"title": "Linux memory metrics",
"vars": [
{
"name": "period",
"type": "text",
"title": "Period",
"multi": false,
"required": true,
"show_user": true,
"default": "10s"
}
],
"template_path": "stream.yml.hbs",
"description": "Linux paging and memory management metrics"
}
],
"title": "Linux-only memory metrics",
"release": "experimental",
"type": "metrics"
}
]
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here the validatePackagePolicyConfig
function take as the first argument the user provided value and as the second one the package provided spec to validate against if we do not provide a second argument we are not validating the input.
What I suggest is in case we are not able to map the user provided input to a package var def to try to map only using the input type.
Does it make clearer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean, though in this scenario, inputVarDefsByType
would build the same object as inputVarDefsByPolicyTemplateAndType
(none of the policy_templates have that var provided by the user, the vars are really just misplaced in the request):
hasIntegration
is false here, so the object keys will beinput.type
only:
https://github.com/elastic/kibana/blob/main/x-pack/plugins/fleet/common/services/validate_package_policy.ts#L104
the question is, what should happen if there are extra vars provided that are not there in the template, should there be a validation error coming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the question is, what should happen if there are extra vars provided that are not there in the template, should there be a validation error coming?
I personally think we should reject the variable, but it's kind of a breaking change, but otherwise this will result in non validated package and I am not sure these variables that are not are the correct level are not used in the agent stream template after.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your assessment. I'll update the pr to give a validation error instead of silencing the error in case of extra/unexpected vars. I think this change is not breaking in a sense that it only improves the error message (the request would fail with undefined error anyway).
Though the original change might cause some unexpected impact where this API was called with invalid package request, like in e2e-testing.
@juliaElastic Might this be the same issue as #110202 (comment)? |
@jen-huang No, I think this is a different one recently happening due to the improved validation. |
? inputVarDefsByPolicyTemplateAndType[inputKey] === undefined | ||
? [ | ||
i18n.translate('xpack.fleet.packagePolicyValidation.missingInputKeyMessage', { | ||
defaultMessage: '{inputKey} has no vars in policy template', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added validation error here, though I don't like it too much, it would be better to include this logic in validatePackagePolicyConfig
.
I'm thinking the best place to add the validation error is when varDef
is not defined here, meaning all unrecognized variables would be rejected with validation error. What do you think?
kibana/x-pack/plugins/fleet/common/services/validate_package_policy.ts
Lines 212 to 217 in fbe6836
if (varDef === undefined) { | |
// eslint-disable-next-line no-console | |
console.debug(`No variable definition for ${varName} found`); | |
return null; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it would be better 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
💚 Build SucceededMetrics [docs]Page load bundle
Unknown metric groupsESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing that 🚀
…lastic#124215) * fixed undefined error when missing policy vars in template * added validation error if input has no vars * fixed checks * added unit test * fixed checks * fixed checks * fixed checks * moved validation error to validatePackagePolicyConfig (cherry picked from commit 67430f9)
💔 Some backports could not be created
How to fixRe-run the backport manually:
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
…124215) (#124379) * fixed undefined error when missing policy vars in template * added validation error if input has no vars * fixed checks * added unit test * fixed checks * fixed checks * fixed checks * moved validation error to validatePackagePolicyConfig (cherry picked from commit 67430f9) Co-authored-by: Julia Bardi <90178898+juliaElastic@users.noreply.github.com>
…lastic#124215) * fixed undefined error when missing policy vars in template * added validation error if input has no vars * fixed checks * added unit test * fixed checks * fixed checks * fixed checks * moved validation error to validatePackagePolicyConfig
…124658) * [Fleet] fixed undefined error when missing policy vars in template (#124215) * fixed undefined error when missing policy vars in template * added validation error if input has no vars * fixed checks * added unit test * fixed checks * fixed checks * fixed checks * moved validation error to validatePackagePolicyConfig * fix cypress
Could you please let us know details how to validate this. Thanks |
@dikshachauhan-qasource if you check the issue description, you can verify by running the http query and verifying that the undefined error message doesn't come, instead validation errors come. |
Using the API provided in description, I attempted to validate it on self 8.0 bc1 managed environment and observed that undefined error message is showing up. API Used:
Please let us know if we are missing any other specific input. Thanks |
@dikshachauhan-qasource I think the fix is not in 8.0-BC1, it seems to include changes up to Jan 31 (7 days ago), while this fix was merged 5 days ago. |
Ok Thank you for the confirmation. @juliaElastic I'll try later again. |
…lastic#124215) * fixed undefined error when missing policy vars in template * added validation error if input has no vars * fixed checks * added unit test * fixed checks * fixed checks * fixed checks * moved validation error to validatePackagePolicyConfig (cherry picked from commit 67430f9) # Conflicts: # x-pack/plugins/fleet/common/services/validate_package_policy.ts
…late (#124215) (#136324) * [Fleet] fixed undefined error when missing policy vars in template (#124215) * fixed undefined error when missing policy vars in template * added validation error if input has no vars * fixed checks * added unit test * fixed checks * fixed checks * fixed checks * moved validation error to validatePackagePolicyConfig (cherry picked from commit 67430f9) # Conflicts: # x-pack/plugins/fleet/common/services/validate_package_policy.ts * Fix duplicate test title Co-authored-by: Julia Bardi <90178898+juliaElastic@users.noreply.github.com>
Summary
@mdelapenya reported an issue in e2e-tests that seems to be surfaced by improving package policy validation
When trying to create a package policy with this request body, there is an undefined error coming:
Response:
Stack trace:
There seems to be a nullcheck missing. After adding it, a more meaningful validation error comes: