Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ jobs:
echo "Pulled files:"
{ git lfs ls-files | grep -E '[a-f0-9]{10}\s\*'; } || true

- name: Install python dependencies
run: |
pip install -e .

- name: ctf init
run: |
ctf init test-ctf

- name: Copy CTF files
run: |
mkdir -p test-ctf/challenges
cp -r ./challenges test-ctf/
ls -al test-ctf/
ls -al test-ctf/challenges

- name: ctf version
working-directory: test-ctf
run: |
ctf version

- name: CTF stats
# Run this in the test-ctf directory
working-directory: test-ctf
run: |
ctf stats

- name: CTF list
# Run this in the test-ctf directory
working-directory: test-ctf
run: |
ctf list

- name: Remove docker
run: |
sudo apt-get autopurge -y moby-containerd docker uidmap
Expand Down Expand Up @@ -104,38 +136,11 @@ jobs:
./install-opentofu.sh --install-method deb
rm -f install-opentofu.sh

- name: Install python dependencies
run: |
pip install -e .

- name: ctf init
run: |
ctf init test-ctf

- name: Copy CTF files
run: |
mkdir -p test-ctf/challenges
cp -r ./challenges test-ctf/
ls -al test-ctf/
ls -al test-ctf/challenges

- name: Validate CTF structure
# Run this in the test-ctf directory
working-directory: test-ctf
run: |
ctf validate

- name: CTF stats
# Run this in the test-ctf directory
working-directory: test-ctf
run: |
ctf stats

- name: CTF list
# Run this in the test-ctf directory
working-directory: test-ctf
run: |
ctf list

- name: Deployment check
working-directory: test-ctf
Expand Down
54 changes: 18 additions & 36 deletions ctf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
#!/usr/bin/env python3
import importlib.metadata
import json
import logging
import os
import sys
import urllib.request

import coloredlogs
from ctf.logger import LOG
from ctf.utils import (
find_ctf_root_directory,
get_ctf_script_schemas_directory,
get_ctf_script_templates_directory,
)

VERSION = importlib.metadata.version("ctf-script")

if len(sys.argv) > 1 and sys.argv[1] == "version":
print(VERSION)
exit(code=0)


ENV = {}
for k, v in os.environ.items():
ENV[k] = v

LOG = logging.getLogger()
LOG.setLevel(level=logging.DEBUG)
coloredlogs.install(level="DEBUG", logger=LOG)
match sys.argv[1] if len(sys.argv) > 1 else "":
case "init":
CTF_ROOT_DIRECTORY = os.path.join(os.getcwd(), ".")
case "version":
CTF_ROOT_DIRECTORY = ""
case _:
CTF_ROOT_DIRECTORY = find_ctf_root_directory()

TEMPLATES_ROOT_DIRECTORY = get_ctf_script_templates_directory()
SCHEMAS_ROOT_DIRECTORY = get_ctf_script_schemas_directory()


def check_tool_version() -> None:
Expand Down Expand Up @@ -56,40 +68,10 @@ def check_tool_version() -> None:
LOG.debug("Script is up to date.")
case -1:
LOG.warning(
"Script is outdated. Please update to the latest release before continuing."
f"Script is outdated (current: {VERSION}, upstream: {latest_version}). Please update to the latest release before continuing."
)
if (input("Do you want to continue? [y/N] ").lower() or "n") == "n":
exit(code=0)


check_tool_version()


def find_ctf_root_directory() -> str:
path = os.path.join(os.getcwd(), ".")

while path != (path := os.path.dirname(p=path)):
dir = os.listdir(path=path)

if ".deploy" not in dir:
continue
if "challenges" not in dir:
continue
break

if path == "/":
if "CTF_ROOT_DIR" not in os.environ:
LOG.critical(
msg='Could not automatically find the root directory nor the "CTF_ROOT_DIR" environment variable. To initialize a new root directory, use `ctf init [path]`'
)
exit(1)
return os.environ.get("CTF_ROOT_DIR", default=".")

LOG.debug(msg=f"Found root directory: {path}")
return path


if len(sys.argv) > 1 and sys.argv[1] == "init":
CTF_ROOT_DIRECTORY = os.path.join(os.getcwd(), ".")
else:
CTF_ROOT_DIRECTORY = find_ctf_root_directory()
Loading