-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Further optimize check privileges response validation #90631
Further optimize check privileges response validation #90631
Conversation
@elasticmachine merge upstream |
@elasticmachine merge upstream |
Pinging @elastic/kibana-security (Team:Security) |
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.
Author's notes
const actualActions = Object.keys(value).sort(); | ||
if ( | ||
actions.length !== actualActions.length || | ||
![...actions].sort().every((x, i) => x === actualActions[i]) |
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.
note: actions
is provided by consumers, so we shouldn't be mutating their copy of the data.
According to this benchmark, using the spread operator is the fastest way to copy an array.
{}, | ||
{ | ||
unknowns: 'allow', | ||
validate: (value) => { | ||
const actualResources = Object.keys(value).sort(); | ||
if ( | ||
resources.length !== actualResources.length || | ||
!resources.sort().every((x, i) => x === actualResources[i]) | ||
![...resources].sort().every((x, i) => x === actualResources[i]) |
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.
note: resources
is provided by consumers, so we shouldn't be mutating their copy of the data.
According to this benchmark, using the spread operator is the fastest way to copy an array.
@elasticmachine merge upstream |
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.
LGTM, one nit below
function buildValidationSchema(application: string, actions: string[], resources: string[]) { | ||
const actionValidationSchema = buildActionsValidationSchema(actions); | ||
const actionValidationSchema = schema.boolean(); | ||
const actionsValidationSchema = schema.object( | ||
{}, | ||
{ | ||
unknowns: 'allow', | ||
validate: (value) => { | ||
const actualActions = Object.keys(value).sort(); | ||
if ( | ||
actions.length !== actualActions.length || | ||
![...actions].sort().every((x, i) => x === actualActions[i]) |
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.
Nit: FWIW, these changes will cause this validation schema to behave differently (fail) if the consumer passes in any duplicate actions. We only have one consumer, which is already ensuring that actions are unique:
const allApplicationPrivileges = uniq([actions.version, actions.login, ...kibanaPrivileges]); |
May be worth noting this in a TSdoc on the validateEsPrivilegeResponse
function, though.
Edit: added in 3026eb8.
💚 Build SucceededMetrics [docs]
History
To update your PR or re-run it, just comment with: |
Summary
Followup to #90074
Performs a similar optimization to #90074, but this time for
actions
instead ofresources
.@dgieselaar would you be able to profile this to see if it shows a reasonable improvement based on your testing? Or I'd be happy to do so if you help me get your tooling setup 🙂