-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(graph): fix attributes checks logic #7264
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
Closed
Closed
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5474558
Fix logic
inbalavital 8571299
Fix logic and tests
inbalavital 7d71b6b
Fix lint & mypy
inbalavital 9dd4ecf
Fix logic
inbalavital a189d3b
Add comments
inbalavital 3445e6b
Add test
inbalavital 5cb3af6
Fix lint
inbalavital 1b16efb
Support not found case in and and or solvers
inbalavital f68d585
Merge branch 'main' into bugfix/fix-attributes-solver-checks
inbalavital fd618bb
Use const
inbalavital cd540e6
Change policy
inbalavital File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from typing import List, Any, Dict, Optional | ||
from typing import List, Any, Dict, Optional, Union | ||
|
||
from checkov.common.graph.checks_infra.enums import Operators | ||
from checkov.common.graph.checks_infra.solvers.base_solver import BaseSolver | ||
|
@@ -16,12 +16,26 @@ def __init__(self, solvers: List[BaseSolver], resource_types: List[str]) -> None | |
def _get_operation(self, *args: Any, **kwargs: Any) -> Any: | ||
return reduce(and_, args) | ||
|
||
def get_operation(self, vertex: Dict[str, Any]) -> Optional[bool]: | ||
def get_operation(self, vertex: Dict[str, Any]) -> Union[Optional[bool], str]: | ||
has_unrendered_attribute = False | ||
# found flag indicates if at least one solver tested the vertex | ||
found = False | ||
for solver in self.solvers: | ||
resource_types = solver.get_resource_types() | ||
# In case that current solver's resource types aren't compatible with vertex, this operation should be | ||
# skipped | ||
if resource_types and not self.resource_type_pred(vertex, resource_types): | ||
continue | ||
found = True | ||
operation = solver.get_operation(vertex) | ||
if operation is None: | ||
has_unrendered_attribute = True | ||
elif operation == "not found": | ||
continue | ||
elif not operation: | ||
return False | ||
return None if has_unrendered_attribute else True | ||
if not found: | ||
return "not found" | ||
|
||
elif has_unrendered_attribute: | ||
return None | ||
return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
tests/terraform/runner/extra_yaml_checks/aws_tagging_different_resource_types.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
metadata: | ||
id: "CUSTOM_AWS_TAGGING_POLICY" | ||
name: "HF_all_Policy" | ||
guidelines: "Tagging needs to be done correctly." | ||
category: "General" | ||
severity: "critical" | ||
scope: | ||
provider: "aws" | ||
definition: | ||
and: | ||
- cond_type: attribute | ||
resource_types: | ||
- aws_organizations_account | ||
- aws_elasticache_cluster | ||
- aws_db_instance | ||
attribute: "tags.AccountType" | ||
operator: regex_match | ||
value: "^Lab$|^Sandbox$|^NonProd$|^Prod$|^PublicProd$|^PublicNonProd$" | ||
- cond_type: attribute | ||
resource_types: | ||
- aws_db_instance # | ||
- aws_organizations_account # | ||
- aws_vpc | ||
- aws_route53_zone | ||
- aws_elasticache_cluster | ||
- aws_lambda_function | ||
- aws_ecs_cluster | ||
- aws_instance | ||
- aws_dynamodb_table | ||
- aws_ebs_volume | ||
- aws_efs_file_system | ||
- aws_eks_cluster | ||
- aws_fsx_windows_file_system | ||
- aws_docdb_cluster | ||
- aws_s3_bucket | ||
attribute: "tags.Billing" | ||
operator: regex_match | ||
value: "APP-\\d{4}$|d{4}$" # Accepts APP-1234 or 4-digit dept codes |
20 changes: 20 additions & 0 deletions
20
tests/terraform/runner/resources/and_with_different_resource_types/dynamo_db_table.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
provider "aws" { | ||
region = "us-east-1" | ||
} | ||
resource "aws_dynamodb_table" "example" { | ||
name = "example-table" | ||
billing_mode = "PAY_PER_REQUEST" # or "PROVISIONED" | ||
hash_key = "id" attribute { | ||
name = "id" | ||
type = "S" # S = String, N = Number, B = Binary | ||
} | ||
tags = { | ||
AppId = "APP-1234" | ||
Billing = "APP-1234" | ||
DynatraceMonitoring = true | ||
EnvironmentType = "PROD" | ||
DataClassification = "HighlyRestricted" | ||
yor_name = "example" | ||
yor_trace = "d9afaa27-0ffc-49c4-90fa-9c3a7649637f" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't like that the return type changed like that, specifically if we want to return
not found
we should useLiteral["not found"]
to be more specific.In addition - is it really needed? Can't we just return the value which will not change the check?
For example in
and-solver
we can returnTrue
and inor-solver
returnFalse
, to make sure this is simplerThere 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.
this was my last resort, it's essential because otherwise it affects checks results, which is not what we want.