Skip to content

Commit

Permalink
Merge pull request ItzyBitzySpider#12 from ItzyBitzySpider/runner_fix
Browse files Browse the repository at this point in the history
bump runner and auto init tables
  • Loading branch information
beanbeah authored Oct 26, 2022
2 parents 2799ae6 + 8a89575 commit 5dc1fb2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 39 deletions.
27 changes: 0 additions & 27 deletions docker-misc/postgres/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -205,30 +205,3 @@ psql -d "$POSTGRES_DB" -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE $RUNNER_DB;
GRANT ALL PRIVILEGES ON DATABASE runner_db TO $POSTGRES_USER;
EOSQL

#create runner_db database
echo "Init Runner DB"
psql -d "$RUNNER_DB" -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE TABLE challenges (
challenge_id varchar(64) PRIMARY KEY,
challenge_name varchar(255) NOT NULL,
port_types varchar(255) NOT NULL,
docker_compose bool NOT NULL,
port_count int NOT NULL,
internal_port varchar(255),
image_name varchar(255),
docker_cmds varchar(255),
docker_compose_file text
);
CREATE TABLE instances (
instance_id SERIAL PRIMARY KEY,
usr_id varchar(64) NOT NULL,
challenge_id varchar(64) NOT NULL,
portainer_url varchar(64) NOT NULL,
portainer_id varchar(64) NOT NULL DEFAULT '',
instance_timeout bigint NOT NULL,
ports_used varchar(255) NOT NULL
);
EOSQL
echo "DONE!!!"
34 changes: 22 additions & 12 deletions runner/python-portainer/portainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
challenges = []

# --- build docker-compose.yml and send data to runner endpoint ---
def docker_compose(challenge, chall_data, dir, env):
def docker_compose(challenge, chall_data, dir, env, service_type):
# Reject composes with local volumes
for service in chall_data['services']:
try:
Expand All @@ -51,7 +51,7 @@ def docker_compose(challenge, chall_data, dir, env):
except AttributeError:
pass
if docker.returncode != 0:
logging.warning(f'An error occured while building {challenge}. {bytes.decode(docker.stderr, "utf-8")}')
logging.warning(f'An error occured while building {challenge}. {docker.stderr}')
return
else:
logging.info(f'{challenge} built successfully')
Expand All @@ -72,7 +72,7 @@ def docker_compose(challenge, chall_data, dir, env):
}
payload = {
'challenge_name': challenge.lower(),
'port_types': ",".join(["nc"] * port_count), #TODO: Handle other permutations
'port_types': ",".join([service_type] * port_count),
'docker_compose': 'True',
'docker_compose_file': bytes.decode(base64.b64encode(new_compose)),
}
Expand All @@ -86,7 +86,7 @@ def docker_compose(challenge, chall_data, dir, env):
return

# --- build Dockerfile and send data to runner endpoint
def dockerfile(challenge, chall_data, dir, env):
def dockerfile(challenge, chall_data, dir, env, service_type):
# Grab port from Dockerfile
port = regex.search('EXPOSE ([0-9]+)', chall_data)
if port == None:
Expand All @@ -104,7 +104,7 @@ def dockerfile(challenge, chall_data, dir, env):
except AttributeError:
pass
if docker.returncode != 0:
logging.warning(f'An error occured while building {challenge}. {bytes.decode(docker.stderr, "utf-8")}')
logging.warning(f'An error occured while building {challenge}. {docker.stderr}')
return
else:
logging.info(f'{challenge} built successfully')
Expand All @@ -115,7 +115,7 @@ def dockerfile(challenge, chall_data, dir, env):
}
payload = {
'challenge_name': challenge.lower(),
'port_types': "nc", #TODO: Handle "http"
'port_types': service_type,
'docker_compose': 'False',
'internal_port': port,
'image_name': challenge.lower(),
Expand Down Expand Up @@ -146,29 +146,39 @@ def main():
with os.scandir() as scan:
for i in scan:
if i.is_dir():
challenges.append(i.name)
with os.scandir(i) as ctf:
for challenge in ctf:
if challenge.is_dir():
challenges.append(challenge.path)

# Save challenge data and build image
for challenge in challenges:
# Cache challenge directory
dir = os.path.join('.', challenge)
dir = challenge

# Check for service type (http/nc)
try:
with open(os.path.join(dir, 'SERVICE')) as file:
service_type = file.readline().strip()
except FileNotFoundError:
logging.warning(f'{challenge} has no SERVICE, defaulting to nc-type service')
service_type = 'nc'


# Check for docker-compose.yml
try:
with open(os.path.join(dir, 'docker-compose.yml')) as file:
chall_data = yaml.safe_load(file)
except FileNotFoundError:
# logging.warning(f'{challenge} has no docker-compose, skipping')
# continue
try:
with open(os.path.join(dir, 'Dockerfile')) as file:
chall_data = file.read()
except FileNotFoundError:
logging.warning(f'{challenge} has no docker-compose or Dockerfile, skipping')
else:
dockerfile(challenge, chall_data, dir, env)
dockerfile(os.path.basename(challenge), chall_data, dir, env, service_type)
else:
docker_compose(challenge, chall_data, dir, env)
docker_compose(os.path.basename(challenge), chall_data, dir, env, service_type)

if __name__ == '__main__':
main()
Binary file modified runner/runner
Binary file not shown.

0 comments on commit 5dc1fb2

Please sign in to comment.