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

add date validation for the date constraint rule #644

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Internal server error during cluster setup with Ruby 3.2
- Set `Content-Security-Policy: frame-ancestors 'self'; default-src 'self'`
HTTP header for HTTP 404 responses as well ([rhbz#2160664])
- Validate dates in location constraint rules ([ghpull#644])

### Changed
- Resource/stonith agent self-validation of instance attributes is now
Expand All @@ -28,6 +29,7 @@

[ghissue#604]: https://github.com/ClusterLabs/pcs/issues/604
[ghpull#559]: https://github.com/ClusterLabs/pcs/pull/559
[ghpull#644]: https://github.com/ClusterLabs/pcs/pull/644
[rhbz#2151164]: https://bugzilla.redhat.com/show_bug.cgi?id=2151164
[rhbz#2151524]: https://bugzilla.redhat.com/show_bug.cgi?id=2151524
[rhbz#2158790]: https://bugzilla.redhat.com/show_bug.cgi?id=2158790
Expand Down
10 changes: 10 additions & 0 deletions pcs/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,11 @@ def build_date_expression(self, dom_element, syntactic_tree):
"date_expression",
dom_element.getAttribute("id") + "-expr",
)
if not utils.is_iso8601_date(syntactic_tree.children[1].value):
raise SyntaxError(
"'%s' is not an ISO 8601 date"
% syntactic_tree.children[1].value
)
dom_expression.setAttribute("operation", syntactic_tree.symbol_id)
if syntactic_tree.symbol_id == "gt":
dom_expression.setAttribute(
Expand All @@ -998,6 +1003,11 @@ def build_date_expression(self, dom_element, syntactic_tree):
for key, value in duration.parts.items():
dom_duration.setAttribute(key, value)
else:
if not utils.is_iso8601_date(syntactic_tree.children[2].value):
raise SyntaxError(
"'%s' is not an ISO 8601 date"
% syntactic_tree.children[2].value
)
dom_expression.setAttribute(
"end", syntactic_tree.children[2].value
)
Expand Down
46 changes: 46 additions & 0 deletions pcs_test/tier1/legacy/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2199,6 +2199,14 @@ def test_success(self):
).split(),
)

self.assert_pcs_success(
(
"constraint location dummy1 rule (#uname eq node3) and "
"(date gt 2022-01-01 or date lt 2023-01-01 "
"or date in_range 2023-02-01 to 2023-02-28)"
).split(),
)

self.assert_pcs_success(
"constraint location config --full".split(),
dedent(
Expand All @@ -2221,6 +2229,13 @@ def test_success(self):
Date Spec: hours=12-23 weekdays=1-5 (id:complexRule-rule-1-expr-datespec)
Expression: date in_range 2014-07-26 to duration (id:complexRule-rule-1-expr-1)
Duration: months=1 (id:complexRule-rule-1-expr-1-duration)
Constraint: location-dummy1-3
Rule: boolean-op=and score=INFINITY (id:location-dummy1-3-rule)
Expression: #uname eq node3 (id:location-dummy1-3-rule-expr)
Rule: boolean-op=or score=0 (id:location-dummy1-3-rule-rule)
Expression: date gt 2022-01-01 (id:location-dummy1-3-rule-rule-expr)
Expression: date lt 2023-01-01 (id:location-dummy1-3-rule-rule-expr-1)
Expression: date in_range 2023-02-01 to 2023-02-28 (id:location-dummy1-3-rule-rule-expr-2)
""",
),
)
Expand All @@ -2247,6 +2262,13 @@ def test_success(self):
Date Spec: hours=12-23 weekdays=1-5
Expression: date in_range 2014-07-26 to duration
Duration: months=1
Constraint: location-dummy1-3
Rule: boolean-op=and score=INFINITY
Expression: #uname eq node3
Rule: boolean-op=or score=0
Expression: date gt 2022-01-01
Expression: date lt 2023-01-01
Expression: date in_range 2023-02-01 to 2023-02-28
""",
),
)
Expand Down Expand Up @@ -2340,3 +2362,27 @@ def test_ivalid_options(self):
"constraint location dummy1 rule id=MyRule #uname eq node1".split(),
"Error: id 'MyRule' is already in use, please specify another one\n",
)

@skip_unless_crm_rule()
def test_invalid_date(self):
self.assert_pcs_fail(
"constraint location dummy1 rule date gt abcd".split(),
(
"Error: 'date gt abcd' is not a valid rule expression: 'abcd' "
"is not an ISO 8601 date\n"
),
)
self.assert_pcs_fail(
"constraint location dummy1 rule date in_range abcd to 2023-01-01".split(),
(
"Error: 'date in_range abcd to 2023-01-01' is not a valid rule "
"expression: invalid date 'abcd' in 'in_range ... to'\n"
),
)
self.assert_pcs_fail(
"constraint location dummy1 rule date in_range 2023-01-01 to abcd".split(),
(
"Error: 'date in_range 2023-01-01 to abcd' is not a valid rule "
"expression: invalid date 'abcd' in 'in_range ... to'\n"
),
)