Skip to content

Commit f0d32c6

Browse files
authored
Merge pull request #12 from nsec/updating
Updated from the CTF
2 parents 1f57fd1 + 20a9d02 commit f0d32c6

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

ctf/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323
coloredlogs.install(level="DEBUG", logger=LOG)
2424

2525

26-
def check_tool_version():
26+
def check_tool_version() -> None:
2727
with urllib.request.urlopen(
2828
url="https://api.github.com/repos/nsec/ctf-script/releases/latest"
2929
) as r:
3030
if r.getcode() != 200:
3131
LOG.debug(r.read().decode())
3232
LOG.error("Could not verify the latest release.")
33+
return
3334
else:
3435
try:
3536
latest_version = json.loads(s=r.read().decode())["tag_name"]
3637
except Exception as e:
3738
LOG.debug(e)
3839
LOG.error("Could not verify the latest release.")
40+
return
3941

4042
compare = 0
4143
for current_part, latest_part in zip(

ctf/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def remove_tracks_from_terraform_modules(
142142
)
143143

144144

145-
def get_all_file_paths_recursively(path: str) -> Generator[None, None, str]:
145+
def get_all_file_paths_recursively(path: str) -> Generator[str, None, None]:
146146
if os.path.isfile(path=path):
147147
yield remove_ctf_script_root_directory_from_path(path=path)
148148
else:

ctf/validators.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,23 @@ def finalize(self) -> list[ValidationError]:
166166

167167

168168
class DiscoursePostsAskGodTagValidator(Validator):
169-
"""Validate that the triggers used in discourse posts are correctly defined in the discourse tag of each flag in track.yaml. Also validate that each discourse tag is unique. Also validates that the topic matches an existing file name in the posts directory."""
169+
"""
170+
Validate that the triggers used in discourse posts are correctly defined in the discourse tag of each flag in track.yaml. It checks for triggers in ALL tracks to allow a flow like: "When a flag from track A is triggered, show a post in track B".
171+
Also validate that each discourse tag is unique.
172+
Also validates that the topic matches an existing file name in the posts directory.
173+
"""
170174

171175
def __init__(self):
172176
self.discourse_tags_mapping = {}
177+
self.discourse_triggers = []
178+
self.discourse_posts = []
173179

174180
def validate(self, track_name: str) -> list[ValidationError]:
175181
track_yaml = parse_track_yaml(track_name=track_name)
176-
discourse_triggers = []
177182
for flag in track_yaml["flags"]:
178183
discourse_trigger = flag.get("tags", {}).get("discourse")
179184
if discourse_trigger:
180-
discourse_triggers.append(discourse_trigger)
185+
self.discourse_triggers.append(discourse_trigger)
181186
if discourse_trigger not in self.discourse_tags_mapping:
182187
self.discourse_tags_mapping[discourse_trigger] = []
183188
self.discourse_tags_mapping[discourse_trigger].append(track_yaml)
@@ -186,6 +191,7 @@ def validate(self, track_name: str) -> list[ValidationError]:
186191
discourse_posts = parse_post_yamls(track_name=track_name)
187192
for discourse_post in discourse_posts:
188193
if discourse_post.get("trigger", {}).get("type", "") == "flag":
194+
self.discourse_posts.append((track_name, discourse_post))
189195
if not os.path.exists(
190196
os.path.join(
191197
CTF_ROOT_DIRECTORY,
@@ -211,18 +217,6 @@ def validate(self, track_name: str) -> list[ValidationError]:
211217
},
212218
)
213219
)
214-
if discourse_post["trigger"]["tag"] not in discourse_triggers:
215-
errors.append(
216-
ValidationError(
217-
error_name="Invalid trigger in discourse post",
218-
error_description="A discourse post has a flag trigger that references a discourse tag not defined in track.yaml.",
219-
track_name=track_name,
220-
details={
221-
"Invalid tag": discourse_post["trigger"]["tag"],
222-
"Discourse tags in track.yaml": str(discourse_triggers),
223-
},
224-
)
225-
)
226220

227221
return errors
228222

@@ -238,6 +232,19 @@ def finalize(self) -> list[ValidationError]:
238232
details={'"discourse" tag': discourse_tag},
239233
)
240234
)
235+
236+
for track_name, discourse_post in self.discourse_posts:
237+
if discourse_post["trigger"]["tag"] not in self.discourse_triggers:
238+
errors.append(
239+
ValidationError(
240+
error_name="Invalid trigger in discourse post",
241+
error_description="A discourse post has a flag trigger that references a discourse tag not defined in track.yaml.",
242+
track_name=track_name,
243+
details={
244+
"Invalid tag": discourse_post["trigger"]["tag"],
245+
},
246+
)
247+
)
241248
return errors
242249

243250

@@ -378,8 +385,8 @@ def validate(self, track_name: str) -> list[ValidationError]:
378385
errors: list[ValidationError] = []
379386
services = set()
380387
for service in track_yaml["services"]:
381-
service_name = service["name"]
382-
instance_name = service["instance"]
388+
service_name = service.get("name")
389+
instance_name = service.get("instance")
383390
service = f"{instance_name}/{service_name}"
384391

385392
if service in services:

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ dependencies = [
1818
"black",
1919
"tabulate==0.9.0",
2020
]
21-
version = "1.1.0"
21+
version = "1.1.1"
2222
classifiers = [
2323
"Programming Language :: Python :: 3",
2424
"Operating System :: OS Independent",
2525
]
2626

2727
[project.optional-dependencies]
28-
coderunner = ["pybadges", "matplotlib", "standard-imghdr ; python_version >= \"3.13\""]
28+
workflow = ["pybadges", "matplotlib", "standard-imghdr ; python_version >= \"3.13\""]
2929

3030
[project.scripts]
3131
ctf = "ctf.__main__:main"

0 commit comments

Comments
 (0)