Skip to content

Refactor: Use ipaddress module for CHALLENGES_ROOT_SUBNET handling and subnet calculations #24

@cyberd3ath

Description

@cyberd3ath

Summary

Refactor the handling of CHALLENGES_ROOT_SUBNET and related variables in the codebase to use the Python ipaddress module, improving clarity and correctness.

Details

Replace this code in launch_challenge.py, stop_challenge.py, and teardown_challenge.py:

CHALLENGES_ROOT_SUBNET = os.getenv("CHALLENGES_ROOT_SUBNET", "10.128.0.0")
CHALLENGES_ROOT_SUBNET_MASK = os.getenv("CHALLENGES_ROOT_SUBNET_MASK", "255.128.0.0")
CHALLENGES_ROOT_SUBNET_MASK_INT = sum(bin(int(x)).count('1') for x in CHALLENGES_ROOT_SUBNET_MASK.split('.'))
CHALLENGES_ROOT_SUBNET_CIDR = f"{CHALLENGES_ROOT_SUBNET}/{CHALLENGES_ROOT_SUBNET_MASK_INT}"

with an approach, leveraging the Python ipaddress module.

Also, update subnet_calculations.py to take advantage of ipaddress for subnet and IP calculations, if possible, to simplify and improve reliability.

Rationale

  • More robust and readable handling of subnets using a well-maintained standard library.
  • Simplifies conversion between mask formats and avoids manual calculations.

Additional Context

  • Ensure import of ipaddress is added where needed.
  • Refactored code should maintain backward compatibility with current environment variables in .env.

Metadata

Metadata

Assignees

No one assigned

    Labels

    dependenciesPull requests that update a dependency fileenhancementNew feature or request

    Type

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions