|
3 | 3 | SPDX-License-Identifier: MIT-0
|
4 | 4 | """
|
5 | 5 |
|
| 6 | +import regex as re |
| 7 | + |
6 | 8 | # pylint: disable=cyclic-import
|
7 | 9 | import cfnlint.rules
|
8 | 10 |
|
9 | 11 | OPERATOR = [
|
10 | 12 | "EQUALS",
|
11 | 13 | "NOT_EQUALS",
|
| 14 | + "REGEX_MATCH", |
12 | 15 | "==",
|
13 | 16 | "!=",
|
14 | 17 | "IN",
|
15 | 18 | "NOT_IN",
|
| 19 | + ">", |
| 20 | + "<", |
16 | 21 | ">=",
|
17 | 22 | "<=",
|
18 | 23 | "IS DEFINED",
|
@@ -298,7 +303,29 @@ def rule_func(value, expected_values, path):
|
298 | 303 | )
|
299 | 304 |
|
300 | 305 |
|
301 |
| -def CreateGreaterRule(rule_id, resourceType, prop, value, error_message): |
| 306 | +def CreateRegexMatchRule(rule_id, resourceType, prop, value, error_message): |
| 307 | + def rule_func(value, expected_values, path): |
| 308 | + matches = [] |
| 309 | + if not re.match(expected_values.strip(), str(value).strip()): |
| 310 | + matches.append( |
| 311 | + cfnlint.rules.RuleMatch(path, error_message or "Regex does not match") |
| 312 | + ) |
| 313 | + |
| 314 | + return matches |
| 315 | + |
| 316 | + return CreateCustomRule( |
| 317 | + rule_id, |
| 318 | + resourceType, |
| 319 | + prop, |
| 320 | + value, |
| 321 | + error_message, |
| 322 | + shortdesc="Custom rule to check for regex match", |
| 323 | + description="Created from the custom rules parameter. This rule will check if a property value match the provided regex pattern.", |
| 324 | + rule_func=rule_func, |
| 325 | + ) |
| 326 | + |
| 327 | + |
| 328 | +def CreateGreaterEqualRule(rule_id, resourceType, prop, value, error_message): |
302 | 329 | def rule_func(value, expected_value, path):
|
303 | 330 | matches = []
|
304 | 331 | if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()):
|
@@ -329,7 +356,69 @@ def rule_func(value, expected_value, path):
|
329 | 356 | )
|
330 | 357 |
|
331 | 358 |
|
| 359 | +def CreateGreaterRule(rule_id, resourceType, prop, value, error_message): |
| 360 | + def rule_func(value, expected_value, path): |
| 361 | + matches = [] |
| 362 | + if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()): |
| 363 | + if int(str(value).strip()) <= int(str(expected_value).strip()): |
| 364 | + matches.append( |
| 365 | + cfnlint.rules.RuleMatch( |
| 366 | + path, error_message or "Greater than check failed" |
| 367 | + ) |
| 368 | + ) |
| 369 | + else: |
| 370 | + matches.append( |
| 371 | + cfnlint.rules.RuleMatch( |
| 372 | + path, error_message or "Given values are not numeric" |
| 373 | + ) |
| 374 | + ) |
| 375 | + |
| 376 | + return matches |
| 377 | + |
| 378 | + return CreateCustomRule( |
| 379 | + rule_id, |
| 380 | + resourceType, |
| 381 | + prop, |
| 382 | + value, |
| 383 | + error_message, |
| 384 | + shortdesc="Custom rule to check for if a value is greater than the specified value", |
| 385 | + description="Created from the custom rules parameter. This rule will check if a property value is greater than the specified value.", |
| 386 | + rule_func=rule_func, |
| 387 | + ) |
| 388 | + |
| 389 | + |
332 | 390 | def CreateLesserRule(rule_id, resourceType, prop, value, error_message):
|
| 391 | + def rule_func(value, expected_value, path): |
| 392 | + matches = [] |
| 393 | + if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()): |
| 394 | + if int(str(value).strip()) >= int(str(expected_value).strip()): |
| 395 | + matches.append( |
| 396 | + cfnlint.rules.RuleMatch( |
| 397 | + path, error_message or "Lesser than check failed" |
| 398 | + ) |
| 399 | + ) |
| 400 | + else: |
| 401 | + matches.append( |
| 402 | + cfnlint.rules.RuleMatch( |
| 403 | + path, error_message or "Given values are not numeric" |
| 404 | + ) |
| 405 | + ) |
| 406 | + |
| 407 | + return matches |
| 408 | + |
| 409 | + return CreateCustomRule( |
| 410 | + rule_id, |
| 411 | + resourceType, |
| 412 | + prop, |
| 413 | + value, |
| 414 | + error_message, |
| 415 | + shortdesc="Custom rule to check for if a value is lesser than the specified value", |
| 416 | + description="Created from the custom rules parameter. This rule will check if a property value is lesser than the specified value.", |
| 417 | + rule_func=rule_func, |
| 418 | + ) |
| 419 | + |
| 420 | + |
| 421 | +def CreateLesserEqualRule(rule_id, resourceType, prop, value, error_message): |
333 | 422 | def rule_func(value, expected_value, path):
|
334 | 423 | matches = []
|
335 | 424 | if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()):
|
|
0 commit comments