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

Add c2cciutils-security-md to check SECURITY.md files #1702

Merged
merged 1 commit into from
May 31, 2024
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
56 changes: 55 additions & 1 deletion c2cciutils/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
import markdown
from markdown.extensions.tables import TableExtension

HEADER_VERSION = "Version"
HEADER_ALTERNATE_TAG = "Alternate Tag"
HEADER_SUPPORT_UNTIL = "Supported Until"
SUPPORT_TO_BE_DEFINED = "To be defined"
SUPPORT_BEST_EFFORT = "Best effort"
SUPPORT_UNSUPPORTED = "Unsupported"


class Security:
"""
Expand All @@ -18,12 +25,13 @@ class Security:
data: list[list[str]]
_row: Optional[list[str]] = None

def __init__(self, status: str):
def __init__(self, status: str, check: bool = True):
"""
Initialize.

Arguments:
status: the content of the SECURITY.md file.
check: Set to `False` to skip the check.
"""

self.headers = []
Expand All @@ -40,6 +48,52 @@ def __init__(self, status: str):
for row in self.data:
row.append("")

self.version_index = self.headers.index(HEADER_VERSION) if HEADER_VERSION in self.headers else -1
self.alternate_tag_index = (
self.headers.index(HEADER_ALTERNATE_TAG) if HEADER_ALTERNATE_TAG in self.headers else -1
)
self.support_until_index = (
self.headers.index(HEADER_SUPPORT_UNTIL) if HEADER_SUPPORT_UNTIL in self.headers else -1
)

if check:
if not self.check(verbose=0):
raise ValueError("SECURITY.md file is not valid.")

def check(self, verbose: int = -1) -> bool:
"""
Check the content.

Arguments:
verbose: the verbosity level, `-1` for no output, `0` for errors only, `1` for all.

Return:
`True` if the content is valid, `False` otherwise.
"""

success = True
if self.version_index == -1:
if verbose >= 0:
print("`Version` column not found.")
success = False
elif verbose >= 1:
print(f"`Version` column found at index {self.version_index}.")

if self.alternate_tag_index == -1:
if verbose >= 1:
print("Optional `Alternate Tag` column not found.")
elif verbose >= 1:
print(f"`Alternate Tag` column found at index {self.alternate_tag_index}.")

if self.support_until_index == -1:
if verbose >= 0:
print("`Support Until` column not found.")
success = False
elif verbose >= 1:
print(f"`Support Until` column found at index {self.support_until_index}.")

return success

def _pe(self, elem: xml.etree.ElementTree.Element) -> None:
"""
Parse the HTML table.
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ exclude = ["c2cciutils/node_modules/**/test"]
[tool.poetry.scripts]
c2cciutils = "c2cciutils.scripts.main:main"
c2cciutils-env = "c2cciutils.scripts.env:main"
c2cciutils-checks = "c2cciutils.scripts.env:main"
c2cciutils-pull-request-checks = "c2cciutils.scripts.pr_checks:main"
c2cciutils-audit = "c2cciutils.scripts.audit:main"
c2cciutils-publish = "c2cciutils.scripts.publish:main"
c2cciutils-version = "c2cciutils.scripts.version:main"
c2cciutils-clean = "c2cciutils.scripts.clean:main"
c2cciutils-security-md = "c2cciutils.scripts.security_md:main"
c2cciutils-checks = "c2cciutils.scripts.env:main"
c2cciutils-pull-request-checks = "c2cciutils.scripts.pr_checks:main"
c2cciutils-audit = "c2cciutils.scripts.audit:main"
c2cciutils-google-calendar = "c2cciutils.publish:main_calendar"
c2cciutils-k8s-install = "c2cciutils.scripts.k8s.install:main"
c2cciutils-k8s-db = "c2cciutils.scripts.k8s.db:main"
Expand Down