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

Stop scan crashing when casting none to int #848

Merged
merged 5 commits into from
Dec 17, 2024

Conversation

oussamaessaji
Copy link
Member

In the stop scan, we list all the networks and attempt to retrieve ostorlab.universe from each network, casting it to an integer. However, this approach fails because there are networks that are not related to the scan universe and do not contain ostorlab.universe. This pull request refactors the code to first check if a network has ostorlab.universe before proceeding with the casting.

@oussamaessaji oussamaessaji requested a review from a team as a code owner December 17, 2024 10:37
mohsinenar
mohsinenar previously approved these changes Dec 17, 2024
Copy link

codecov bot commented Dec 17, 2024

Codecov Report

Attention: Patch coverage is 27.27273% with 24 lines in your changes missing coverage. Please review.

Project coverage is 62.65%. Comparing base (3328dc7) to head (1e542db).
Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
tests/runtimes/local/runtime_test.py 12.50% 21 Missing ⚠️
src/ostorlab/runtimes/local/runtime.py 66.66% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #848      +/-   ##
==========================================
- Coverage   62.69%   62.65%   -0.05%     
==========================================
  Files         342      342              
  Lines       14491    14520      +29     
==========================================
+ Hits         9085     9097      +12     
- Misses       5406     5423      +17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pytest.mark.docker
def testRuntimeScanStop_whenUnrelatedNetworks_RemovesScanServiceWithoutCrash(
mocker, db_engine_path
):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
):
) -> None:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and args typing pls

PiranhaSa
PiranhaSa previously approved these changes Dec 17, 2024
Copy link

@ostorlab-ai-pr-review ostorlab-ai-pr-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Bot Review comment

src/ostorlab/runtimes/local/runtime.py Show resolved Hide resolved
src/ostorlab/runtimes/local/runtime.py Show resolved Hide resolved
@@ -309,9 +309,13 @@
networks = self._docker_client.networks.list()
for network in networks:
network_labels = network.attrs["Labels"]
if network_labels is None:
continue
universe = network_labels.get("ostorlab.universe")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider verifying that 'network_labels' is indeed a dictionary or mapping type to prevent unexpected attribute access errors.

Suggested change
universe = network_labels.get("ostorlab.universe")
if isinstance(network_labels, dict):
universe = network_labels.get('ostorlab.universe')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oussamaessaji also not a bad comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken into consideration.

if (
network_labels is not None
and int(network_labels.get("ostorlab.universe")) == scan_id
universe is not None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, ensure 'universe is not None' is part of an explicit check in an 'if' statement or logical condition rather than being implicitly assumed.

Suggested change
universe is not None
if universe is not None:

network_labels is not None
and int(network_labels.get("ostorlab.universe")) == scan_id
universe is not None
and network_labels is not None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition should be part of a larger logical statement. Use an explicit 'if' with combined conditions instead to enhance readability and prevent future logic errors.

Suggested change
and network_labels is not None
if universe is not None and isinstance(network_labels, dict) and 'ostorlab.universe' in network_labels:

and int(network_labels.get("ostorlab.universe")) == scan_id
universe is not None
and network_labels is not None
and int(universe) == scan_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using 'int()' conversion can lead to exceptions if 'universe' is not a convertible type. Consider implementing a safer conversion or validation before this comparison.

Suggested change
and int(universe) == scan_id
if universe.isdigit() and int(universe) == scan_id:

src/ostorlab/runtimes/local/runtime.py Show resolved Hide resolved
@@ -309,9 +309,13 @@
networks = self._docker_client.networks.list()
for network in networks:
network_labels = network.attrs["Labels"]
if network_labels is None:
continue

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After skipping an iteration, ensure the loop continues as expected, and consider logging the reason for the skip to aid in debugging.

Suggested change
continue
logger.debug('Skipping network with no labels'); continue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bad comment @oussamaessaji .

@@ -309,9 +309,13 @@
networks = self._docker_client.networks.list()
for network in networks:
network_labels = network.attrs["Labels"]
if network_labels is None:
continue
universe = network_labels.get("ostorlab.universe")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line assumes that 'network_labels' structure is always the same. Consider validating the expected structure before accessing it or using a safer method to prevent potential KeyError.

Suggested change
universe = network_labels.get("ostorlab.universe")
universe = network_labels.get("ostorlab.universe") if isinstance(network_labels, dict) else None

and int(network_labels.get("ostorlab.universe")) == scan_id
universe is not None
and network_labels is not None
and int(universe) == scan_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the condition more readable by breaking it into multiple lines or using helper variables. Explicitly check that 'universe' is a string before converting it to integer to avoid ValueError.

Suggested change
and int(universe) == scan_id
if universe and isinstance(universe, str) and int(universe) == scan_id:

@oussamaessaji oussamaessaji dismissed stale reviews from PiranhaSa and mohsinenar via 9ef1082 December 17, 2024 10:54
mocker.patch.object(models, "ENGINE_URL", db_engine_path)
create_scan_db = models.Scan.create("test")

def docker_services():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing return type.


return [services_model.Service(attrs=service) for service in services]

def docker_networks():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type

network_labels is not None
and int(network_labels.get("ostorlab.universe")) == scan_id
universe is not None
and network_labels is not None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont need that, since you already have a check before

Suggested change
and network_labels is not None

ostorlab added 2 commits December 17, 2024 14:10
…ing_none_to_int' into fix/stop_scan_crashing_when_casting_none_to_int
adnaneserrar
adnaneserrar previously approved these changes Dec 17, 2024
mocker.patch.object(models, "ENGINE_URL", db_engine_path)
create_scan_db = models.Scan.create("test")

def docker_services() -> list[services_model.Service]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be useful as a fixture?

src/ostorlab/runtimes/local/runtime.py Show resolved Hide resolved
@@ -309,9 +309,13 @@
networks = self._docker_client.networks.list()
for network in networks:
network_labels = network.attrs["Labels"]
if network_labels is None:
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bad comment @oussamaessaji .

@@ -309,9 +309,13 @@
networks = self._docker_client.networks.list()
for network in networks:
network_labels = network.attrs["Labels"]
if network_labels is None:
continue
universe = network_labels.get("ostorlab.universe")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oussamaessaji also not a bad comment.

@3asm 3asm merged commit 4d10186 into main Dec 17, 2024
10 of 12 checks passed
@3asm 3asm deleted the fix/stop_scan_crashing_when_casting_none_to_int branch December 17, 2024 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.