-
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
[ResponseOps][Rules] Validate timezone in rule routes #201508
Conversation
|
||
export function validateTimezone(timezone: string) { | ||
if (moment.tz.names().includes(timezone)) { | ||
if (moment.tz.zone(timezone) != 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.
moment.tz.zone
is faster than moment.tz.names().includes
.
@@ -478,6 +478,38 @@ export default function createAlertTests({ getService }: FtrProviderContext) { | |||
}); | |||
}); | |||
|
|||
it('should return 400 if the timezone of an action is not valid', async () => { |
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.
Validation already existed. I added a test to avoid bugs in the future.
@@ -182,6 +182,51 @@ export default function createUpdateTests({ getService }: FtrProviderContext) { | |||
}); | |||
}); | |||
|
|||
it('should return 400 if the timezone of an action is not valid', async () => { |
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.
Validation already existed. I added a test to avoid bugs in the future.
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 validations are used only in internal routes.
@@ -10,7 +10,7 @@ import { RuleSnoozeSchedule } from '../../types'; | |||
|
|||
const MAX_TIMESTAMP = 8640000000000000; | |||
|
|||
export function isSnoozeActive(snooze: RuleSnoozeSchedule) { | |||
export function getActiveSnoozeIfExist(snooze: RuleSnoozeSchedule) { |
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 function returns an active snooze or null
. I found the isSnoozeActive
misleading and I renamed the function to something more intuitive. I am open to suggestions 🙂.
/ci |
396cd29
to
d63506b
Compare
@adcoelho @js-jankisalvi I pushed some commits based on your feedback and fixed the bug. Could you please review it again? Also, @Zacqary, I made some changes in the |
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.
New approach looks good 🎉
Verified locally, bug is not reproducible anymore 👍
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.
Verified the bug, it is now fixed 👍
try { | ||
validateOptions(options); | ||
|
||
return true; |
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'd prefer to find a way to surface the error message that validateOptions
produces instead of just turning it into a boolean. Not a blocker for this PR because I see we're just using it to display a notification badge. If there's a way to surface the error message without a huge rewrite I'd say give it a shot, otherwise feel free to merge.
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.
Hey Zacq! In my use case, I do not want to surface the message but to know which rules have an invalid snooze schedule (very rare after the guardrails we put) and show an "Invalid Snooze" notification badge which the user can click and remove. Wdyt?
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.
Sounds all right, we've got the bones in place for surfacing the error message in future use cases
💚 Build Succeeded
Metrics [docs]Module Count
Public APIs missing comments
Async chunks
Public APIs missing exports
Page load bundle
History
cc @cnasikas |
Starting backport for target branches: 8.16, 8.17, 8.x |
## Summary This PR adds validation only for internal routes that use the `rRule` schema. ## Testing 1. Create a rule in main. 2. Snooze the rule by using the API as ``` POST /internal/alerting/rule/<ruleId>/_snooze { "snooze_schedule": { "id": "e58e2340-dba6-454c-8308-b2ca66a7cf7b", "duration": 86400000, "rRule": { "dtstart": "2024-09-04T09:27:37.011Z", "tzid": "invalid", "freq": 2, "interval": 1, "byweekday": [ "invalid" ] } } } ``` 4. Go to the rules page and verify that the rules are not loaded. 5. Switch to my PR. 6. Go to the rules page and verify that the rules load. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: kibanamachine <[email protected]> (cherry picked from commit 9a3fc89)
💔 Some backports could not be created
Note: Successful backport PRs will be merged automatically after passing CI. Manual backportTo create the backport manually run:
Questions ?Please refer to the Backport tool documentation |
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
## Summary This PR adds validation only for internal routes that use the `rRule` schema. ## Testing 1. Create a rule in main. 2. Snooze the rule by using the API as ``` POST /internal/alerting/rule/<ruleId>/_snooze { "snooze_schedule": { "id": "e58e2340-dba6-454c-8308-b2ca66a7cf7b", "duration": 86400000, "rRule": { "dtstart": "2024-09-04T09:27:37.011Z", "tzid": "invalid", "freq": 2, "interval": 1, "byweekday": [ "invalid" ] } } } ``` 4. Go to the rules page and verify that the rules are not loaded. 5. Switch to my PR. 6. Go to the rules page and verify that the rules load. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: kibanamachine <[email protected]> (cherry picked from commit 9a3fc89) # Conflicts: # packages/kbn-rrule/validate.test.ts # packages/kbn-rrule/validate.ts # x-pack/packages/kbn-cloud-security-posture/common/types/graph/latest.ts # x-pack/platform/plugins/shared/alerting/server/lib/snooze/is_snooze_active.test.ts # x-pack/platform/plugins/shared/alerting/server/lib/snooze/is_snooze_active.ts # x-pack/plugins/alerting/common/routes/r_rule/request/schemas/v1.ts # x-pack/plugins/alerting/common/routes/r_rule/validation/index.ts # x-pack/plugins/alerting/common/routes/r_rule/validation/validate_recurrence_by/v1.ts # x-pack/plugins/alerting/server/application/rule/methods/snooze/snooze_rule.test.ts # x-pack/plugins/alerting/server/lib/snooze/get_active_snooze_if_exist.test.ts # x-pack/plugins/alerting/server/lib/snooze/get_active_snooze_if_exist.ts # x-pack/plugins/alerting/server/lib/snooze/is_snooze_active.test.ts # x-pack/plugins/alerting/server/lib/snooze/is_snooze_active.ts # x-pack/plugins/triggers_actions_ui/tsconfig.json
…#208252) # Backport This will backport the following commits from `main` to `8.x`: - [[ResponseOps][Rules] Validate timezone in rule routes (#201508)](#201508) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Christos Nasikas","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-24T17:46:24Z","message":"[ResponseOps][Rules] Validate timezone in rule routes (#201508)\n\n## Summary\r\n\r\nThis PR adds validation only for internal routes that use the `rRule`\r\nschema.\r\n\r\n## Testing\r\n\r\n1. Create a rule in main.\r\n2. Snooze the rule by using the API as\r\n\r\n```\r\nPOST /internal/alerting/rule/<ruleId>/_snooze\r\n{\r\n \"snooze_schedule\": {\r\n \"id\": \"e58e2340-dba6-454c-8308-b2ca66a7cf7b\",\r\n \"duration\": 86400000,\r\n \"rRule\": {\r\n \"dtstart\": \"2024-09-04T09:27:37.011Z\",\r\n \"tzid\": \"invalid\",\r\n \"freq\": 2,\r\n \"interval\": 1,\r\n \"byweekday\": [\r\n \"invalid\"\r\n ]\r\n }\r\n }\r\n}\r\n```\r\n\r\n4. Go to the rules page and verify that the rules are not loaded.\r\n5. Switch to my PR.\r\n6. Go to the rules page and verify that the rules load.\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"9a3fc89629e1a6cec2f5200bb75099fcab866701","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:ResponseOps","v9.0.0","Feature:Alerting/RulesFramework","backport:prev-major","v8.18.0","v8.16.3","v8.17.1"],"title":"[ResponseOps][Rules] Validate timezone in rule routes","number":201508,"url":"https://github.com/elastic/kibana/pull/201508","mergeCommit":{"message":"[ResponseOps][Rules] Validate timezone in rule routes (#201508)\n\n## Summary\r\n\r\nThis PR adds validation only for internal routes that use the `rRule`\r\nschema.\r\n\r\n## Testing\r\n\r\n1. Create a rule in main.\r\n2. Snooze the rule by using the API as\r\n\r\n```\r\nPOST /internal/alerting/rule/<ruleId>/_snooze\r\n{\r\n \"snooze_schedule\": {\r\n \"id\": \"e58e2340-dba6-454c-8308-b2ca66a7cf7b\",\r\n \"duration\": 86400000,\r\n \"rRule\": {\r\n \"dtstart\": \"2024-09-04T09:27:37.011Z\",\r\n \"tzid\": \"invalid\",\r\n \"freq\": 2,\r\n \"interval\": 1,\r\n \"byweekday\": [\r\n \"invalid\"\r\n ]\r\n }\r\n }\r\n}\r\n```\r\n\r\n4. Go to the rules page and verify that the rules are not loaded.\r\n5. Switch to my PR.\r\n6. Go to the rules page and verify that the rules load.\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"9a3fc89629e1a6cec2f5200bb75099fcab866701"}},"sourceBranch":"main","suggestedTargetBranches":["8.x","8.16","8.17"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/201508","number":201508,"mergeCommit":{"message":"[ResponseOps][Rules] Validate timezone in rule routes (#201508)\n\n## Summary\r\n\r\nThis PR adds validation only for internal routes that use the `rRule`\r\nschema.\r\n\r\n## Testing\r\n\r\n1. Create a rule in main.\r\n2. Snooze the rule by using the API as\r\n\r\n```\r\nPOST /internal/alerting/rule/<ruleId>/_snooze\r\n{\r\n \"snooze_schedule\": {\r\n \"id\": \"e58e2340-dba6-454c-8308-b2ca66a7cf7b\",\r\n \"duration\": 86400000,\r\n \"rRule\": {\r\n \"dtstart\": \"2024-09-04T09:27:37.011Z\",\r\n \"tzid\": \"invalid\",\r\n \"freq\": 2,\r\n \"interval\": 1,\r\n \"byweekday\": [\r\n \"invalid\"\r\n ]\r\n }\r\n }\r\n}\r\n```\r\n\r\n4. Go to the rules page and verify that the rules are not loaded.\r\n5. Switch to my PR.\r\n6. Go to the rules page and verify that the rules load.\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"9a3fc89629e1a6cec2f5200bb75099fcab866701"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.16","label":"v8.16.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.17","label":"v8.17.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> --------- Co-authored-by: Christos Nasikas <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
Summary
This PR adds validation only for internal routes that use the
rRule
schema.Testing
Checklist