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

Reworked player-cli's parse_dockerfile_cmd-Function #7

Merged
merged 4 commits into from
Sep 16, 2023
Merged
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
75 changes: 47 additions & 28 deletions ataka/player-cli/player_cli/util.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import os
import re
import time
import typer
from datetime import datetime

import player_cli
import requests
import typer
from requests import JSONDecodeError
from rich import print
from rich.text import Text

import player_cli
from rich import print

from datetime import datetime
CHECK_FOR_CMD = re.compile(r'CMD\s*\[\s*(.+)\s*\]')


def colorfy(msg, color):
Expand Down Expand Up @@ -108,27 +111,43 @@ def highlight_flags(s, func):
return player_cli.ctfconfig_wrapper.FLAG_FINDER.sub(repl, s)


def parse_dockerfile_cmd(content):
exec_args = None

for line in content.splitlines():
line = line.strip()

if not line.startswith('CMD '):
continue
line = line[4:].strip()

if line[0] != '[' or line[-1] != ']':
continue
line = line[1:-1].strip()

args = []
for arg in line.split(','):
arg = arg.strip()
if arg[0] != '"' or arg[-1] != '"':
break
args.append(arg[1:-1])
else:
exec_args = args

return exec_args
def parse_dockerfile_cmd(content: str) -> list[str] | None:
""" Extractes the CMD-Block out of a dockerfile and parses it

Args:
content (str): The content of the dockerfile

Returns:
list[str] | None: Either a list containing the programm and
the arguments, in a special uecase an empty list
(see examples) or None

Usage examples:
>>> parse_dockerfile_cmd('CMD [ "prog"]')
['prog']
>>> parse_dockerfile_cmd("CMD [ 'prog','arg1']")
['prog', 'arg1']
>>> parse_dockerfile_cmd('CMD [ "prog", \\'arg1\\']')
['prog', 'arg1']
>>> parse_dockerfile_cmd("CMD [ \\"prog\\", 'arg1']")
['prog', 'arg1']
>>> parse_dockerfile_cmd('CMD []') is None
True
>>> parse_dockerfile_cmd('CMD [ ]') # In this case, a empty list is returned
[]
"""
matches = CHECK_FOR_CMD.findall(content)
if matches:
ret_arguments = []
for argument in matches[-1].split(","):
argument = argument.strip()
# If the length is zero, don't add an empty string
if len(argument) == 0:
continue

ret_arguments.append(
argument[1:-1]
)

return ret_arguments
return None