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

[ResponseOps][Rules] Validate timezone in rule routes #201508

Merged
merged 30 commits into from
Jan 24, 2025

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented Nov 24, 2024

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"
            ]
        }
    }
}
  1. Go to the rules page and verify that the rules are not loaded.
  2. Switch to my PR.
  3. Go to the rules page and verify that the rules load.

Checklist

@cnasikas cnasikas added Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v9.0.0 Feature:Alerting/RulesFramework Issues related to the Alerting Rules Framework backport:prev-major Backport to (8.x, 8.17, 8.16) the previous major branch and other branches in development v8.17.0 v8.18.0 labels Nov 24, 2024
@cnasikas cnasikas self-assigned this Nov 24, 2024
@cnasikas cnasikas added the backport:skip This commit does not require backporting label Nov 24, 2024

export function validateTimezone(timezone: string) {
if (moment.tz.names().includes(timezone)) {
if (moment.tz.zone(timezone) != null) {
Copy link
Member Author

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 () => {
Copy link
Member Author

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 () => {
Copy link
Member Author

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.

Copy link
Member Author

@cnasikas cnasikas Nov 24, 2024

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) {
Copy link
Member Author

@cnasikas cnasikas Nov 30, 2024

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 🙂.

@cnasikas
Copy link
Member Author

cnasikas commented Dec 9, 2024

/ci

@cnasikas cnasikas marked this pull request as ready for review December 12, 2024 12:56
@cnasikas cnasikas requested a review from a team as a code owner December 12, 2024 12:56
@cnasikas cnasikas requested review from Zacqary and adcoelho January 20, 2025 15:16
@cnasikas
Copy link
Member Author

@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 rule package. Could you please take a look?

Copy link
Contributor

@js-jankisalvi js-jankisalvi left a 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 👍

Copy link
Contributor

@adcoelho adcoelho left a 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;
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

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

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
alerting 233 234 +1
triggersActionsUi 939 944 +5
total +6

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/rrule 16 18 +2

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
triggersActionsUi 1.7MB 1.7MB -18.5KB

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
@kbn/rrule 1 2 +1

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
triggersActionsUi 130.2KB 130.2KB +22.0B
Unknown metric groups

API count

id before after diff
@kbn/rrule 16 18 +2

History

cc @cnasikas

@cnasikas cnasikas merged commit 9a3fc89 into elastic:main Jan 24, 2025
9 checks passed
@cnasikas cnasikas deleted the fix_snooze_bug branch January 24, 2025 17:46
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.16, 8.17, 8.x

https://github.com/elastic/kibana/actions/runs/12954783212

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Jan 24, 2025
## 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)
@kibanamachine
Copy link
Contributor

💔 Some backports could not be created

Status Branch Result
8.16 Backport failed because of merge conflicts
8.17 Backport failed because of merge conflicts
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Manual backport

To create the backport manually run:

node scripts/backport --pr 201508

Questions ?

Please refer to the Backport tool documentation

@cnasikas
Copy link
Member Author

💚 All backports created successfully

Status Branch Result
8.17
8.16

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

cnasikas added a commit to cnasikas/kibana that referenced this pull request Jan 25, 2025
## 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
kibanamachine added a commit that referenced this pull request Jan 25, 2025
…#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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-major Backport to (8.x, 8.17, 8.16) the previous major branch and other branches in development Feature:Alerting/RulesFramework Issues related to the Alerting Rules Framework release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v8.16.4 v8.17.2 v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants