Skip to content

Commit

Permalink
Merge branch 'main' into fix-openapi20-ws
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmithv11 authored Jul 12, 2023
2 parents 6659275 + 00422d0 commit 7b0cbde
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ jobs:
exit $?
unit-tests:
timeout-minutes: 30
runs-on: [self-hosted, public, linux, x64]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3
- name: Set up Python 3.7
Expand Down
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# CHANGELOG

## [Unreleased](https://github.com/bridgecrewio/checkov/compare/2.3.316...HEAD)
## [Unreleased](https://github.com/bridgecrewio/checkov/compare/2.3.318...HEAD)

## [2.3.318](https://github.com/bridgecrewio/checkov/compare/2.3.316...2.3.318) - 2023-07-10

### Feature

- **general:** support UTF-16 and other encodings in multiple frameworks - [#5308](https://github.com/bridgecrewio/checkov/pull/5308)
- **kustomize:** add back reverted kustomize annotations and update build github action to use github runners - [#5316](https://github.com/bridgecrewio/checkov/pull/5316)
- **kustomize:** Add origin annotations to calculate bases of kustomize checks - [#5298](https://github.com/bridgecrewio/checkov/pull/5298)

## [2.3.316](https://github.com/bridgecrewio/checkov/compare/2.3.314...2.3.316) - 2023-07-09

Expand Down
1 change: 1 addition & 0 deletions checkov/common/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ class _EntityContext(TypedDict, total=False):
policy: str
code_lines: list[tuple[int, str]]
skipped_checks: list[_SkippedCheck]
origin_relative_path: str
67 changes: 42 additions & 25 deletions checkov/kubernetes/kubernetes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,34 +138,25 @@ def build_definitions_context(
resource_id = get_resource_id(resource)
if not resource_id:
continue
start_line = resource[START_LINE]
end_line = min(resource[END_LINE], len(definitions_raw[file_path]))
first_line_index = 0
# skip empty lines
while not str.strip(definitions_raw[file_path][first_line_index][1]):
first_line_index += 1
# check if the file is a json file
if str.strip(definitions_raw[file_path][first_line_index][1])[0] == "{":
start_line += 1
end_line += 1
else:
# add resource comments to definition lines
current_line = str.strip(definitions_raw[file_path][start_line - 1][1])
while not current_line or current_line[0] == YAML_COMMENT_MARK:
start_line -= 1
current_line = str.strip(definitions_raw[file_path][start_line - 1][1])

# remove next resource comments from definition lines
current_line = str.strip(definitions_raw[file_path][end_line - 1][1])
while not current_line or current_line[0] == YAML_COMMENT_MARK:
end_line -= 1
current_line = str.strip(definitions_raw[file_path][end_line - 1][1])

code_lines = definitions_raw[file_path][start_line - 1: end_line]

relative_resource_path = None
if 'metadata' in resource:
metadata = resource['metadata']
if 'annotations' in metadata and metadata['annotations'] is not None\
and 'config.kubernetes.io/origin' in metadata['annotations']:
metadata_path = metadata['annotations']['config.kubernetes.io/origin']
if 'path:' in metadata_path:
relative_resource_path = metadata_path.split('path:')[1].strip()

resource_start_line = resource[START_LINE]
resource_end_line = min(resource[END_LINE], len(definitions_raw[file_path]))
raw_code = definitions_raw[file_path]
code_lines, start_line, end_line = calculate_code_lines(raw_code, resource_start_line, resource_end_line)
dpath.new(
definitions_context,
[file_path, resource_id],
{"start_line": start_line, "end_line": end_line, "code_lines": code_lines},
{"start_line": start_line, "end_line": end_line, "code_lines": code_lines,
"origin_relative_path": relative_resource_path},
)

skipped_checks = get_skipped_checks(resource)
Expand All @@ -177,6 +168,32 @@ def build_definitions_context(
return definitions_context


def calculate_code_lines(raw_code: list[tuple[int, str]], start_line: int, end_line: int) \
-> tuple[list[tuple[int, str]], int, int]:
first_line_index = 0
# skip empty lines
while not str.strip(raw_code[first_line_index][1]):
first_line_index += 1
# check if the file is a json file
if str.strip(raw_code[first_line_index][1])[0] == "{":
start_line += 1
end_line += 1
else:
# add resource comments to definition lines
current_line = str.strip(raw_code[start_line - 1][1])
while not current_line or current_line[0] == YAML_COMMENT_MARK:
start_line -= 1
current_line = str.strip(raw_code[start_line - 1][1])

# remove next resource comments from definition lines
current_line = str.strip(raw_code[end_line - 1][1])
while not current_line or current_line[0] == YAML_COMMENT_MARK:
end_line -= 1
current_line = str.strip(raw_code[end_line - 1][1])
code_lines = raw_code[start_line - 1: end_line]
return code_lines, start_line, end_line


def is_invalid_k8_definition(definition: Dict[str, Any]) -> bool:
return (
not isinstance(definition, dict)
Expand Down
4 changes: 3 additions & 1 deletion checkov/kubernetes/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ def check_definitions(
# TODO? - Variable Eval Message!
variable_evaluations: "dict[str, Any]" = {}

report = self.mutate_kubernetes_results(results, report, k8_file, k8_file_path, file_abs_path, entity_conf, variable_evaluations)
report = self.mutate_kubernetes_results(results, report, k8_file, k8_file_path, file_abs_path,
entity_conf, variable_evaluations, root_folder)
self.pbar.update()
self.pbar.close()
return report
Expand All @@ -194,6 +195,7 @@ def mutate_kubernetes_results(
file_abs_path: str,
entity_conf: dict[str, Any],
variable_evaluations: dict[str, Any],
root_folder: str | None = None
) -> Report:
# Moves report generation logic out of run() method in Runner class.
# Allows function overriding of a much smaller function than run() for other "child" frameworks such as Kustomize, Helm
Expand Down
Loading

0 comments on commit 7b0cbde

Please sign in to comment.