Skip to content

Commit

Permalink
Api changes (#19)
Browse files Browse the repository at this point in the history
* bumped requirements

* Changed API
  • Loading branch information
Savallator authored Jun 4, 2020
1 parent 6c0d1da commit 87aa26a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
32 changes: 21 additions & 11 deletions enochecker_async/enochecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,33 @@ def __init__(self, service_name: str, checker_port: int, flags_per_round: int, n
class ELKFormatter(logging.Formatter):
def format(self, record: logging.LogRecord) -> str:
record.msg = record.msg % record.args
return LOGGING_PREFIX + jsons.dumps(self.create_message(record))
return LOGGING_PREFIX + jsons.dumps(self.create_message(record), key_transformer=jsons.KEY_TRANSFORMER_CAMELCASE)

def to_level(self, levelname: str) -> int:
if levelname=="CRITICAL": return 4
if levelname=="ERROR": return 3
if levelname=="WARNING": return 2
if levelname=="INFO": return 1
if levelname=="DEBUG": return 0
return 0

def create_message(self, record: logging.LogRecord):
return EnoLogMessage(BaseChecker.name,
"infrastructure",
record.levelname,
self.to_level(record.levelname),
datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
record.checker.name if hasattr(record, "checker") else None,
record.module,
record.funcName,
record.checker_task.flag if hasattr(record, "checker_task") else None,
record.checker_task.flagIndex if hasattr(record, "checker_task") else None,
record.checker_task.runId if hasattr(record, "checker_task") else None,
record.checker_task.round if hasattr(record, "checker_task") else None,
record.checker_task.flag_index if hasattr(record, "checker_task") else None,
record.checker_task.run_id if hasattr(record, "checker_task") else None,
record.checker_task.round_id if hasattr(record, "checker_task") else None,
record.checker_task.related_round_id if hasattr(record, "checker_task") else None,
record.msg,
record.checker_task.teamId if hasattr(record, "checker_task") else None,
record.checker_task.team if hasattr(record, "checker_task") else None,
record.checker_task.serviceId if hasattr(record, "checker_task") else None,
record.checker_task.team_name if hasattr(record, "checker_task") else None,
record.checker_task.team_id if hasattr(record, "checker_task") else None,
#record.checker_task.serviceId if hasattr(record, "checker_task") else None, #Missing in EniLogMessage, TODO: maybe add everywhere
record.checker.service_name if hasattr(record, "checker") else None,
record.checker_task.method if hasattr(record, "checker_task") else None)

Expand All @@ -59,12 +69,12 @@ async def post(self):
scoped_logger = self.settings['logger']
try:
collection: MotorCollection = self.settings['mongo']['checker_storage']
checker_task = jsons.loads(self.request.body, CheckerTaskMessage)
checker_task = jsons.loads(self.request.body, CheckerTaskMessage, key_transformer=jsons.KEY_TRANSFORMER_SNAKECASE)

# create LoggerAdapter
extra = { 'checker_task': checker_task, 'checker': checker }
scoped_logger = logging.LoggerAdapter(scoped_logger, extra=extra)
scoped_logger.info("Received task (id={}, teamid={}, method={}, index={})".format(checker_task.runId, checker_task.teamId, checker_task.method, checker_task.flagIndex))
scoped_logger.info("Received task (id={}, teamid={}, method={}, index={})".format(checker_task.run_id, checker_task.team_id, checker_task.method, checker_task.flag_index))

# call method
if checker_task.method == CheckerTaskType.CHECKER_TASK_TYPE_PUTFLAG.value:
Expand All @@ -79,7 +89,7 @@ async def post(self):
await checker.havoc(scoped_logger, checker_task, collection)
else:
raise Exception("Unknown rpc method {}".format(checker_task.method))
scoped_logger.info("Task finished OK (id={}, teamid={}, method={}, index={})".format(checker_task.runId, checker_task.teamId, checker_task.method, checker_task.flagIndex))
scoped_logger.info("Task finished OK (id={}, teamid={}, method={}, index={})".format(checker_task.run_id, checker_task.team_id, checker_task.method, checker_task.flag_index))
self.write(jsons.dumps(CheckerResultMessage(CheckerTaskResult.CHECKER_TASK_RESULT_OK.value)))
except OfflineException as ex:
stacktrace = ''.join(traceback.format_exception(None, ex, ex.__traceback__))
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enochecker_core>=0.3
enochecker_core>=0.4
tornado>=6.0
motor>=2.1
jsons>=1.1
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="enochecker_async",
version="0.3.4",
version="0.4.0",
author="Trolldemorted",
author_email="benediktradtke@gmail.com",
description="Library to build async checker scripts for the EnoEngine A/D CTF Framework in Python",
Expand All @@ -22,7 +22,7 @@
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
'Development Status :: 3 - Alpha',
'Development Status :: 4 - Beta',

# Indicate who your project is intended for
# 'Intended Audience :: Developers',
Expand Down

0 comments on commit 87aa26a

Please sign in to comment.