Skip to content

Commit

Permalink
Merge branch 'testcase' into 'main'
Browse files Browse the repository at this point in the history
Add testcases and coverage to the codebase

See merge request gridhead/firmitas!10
  • Loading branch information
gridhead committed Apr 24, 2024
2 parents 11b26f7 + a77caab commit 368b2b6
Show file tree
Hide file tree
Showing 31 changed files with 4,408 additions and 352 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Configuration files
firmitas/conf/myconfig.py


# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
23 changes: 23 additions & 0 deletions firmitas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""


import os.path
import sys
from importlib import metadata
Expand Down
21 changes: 21 additions & 0 deletions firmitas/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""
78 changes: 49 additions & 29 deletions firmitas/base/maintool.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""


import os
import sys
from datetime import datetime
Expand All @@ -22,27 +45,27 @@ def readcert(certobjc):
logrdata.logrobjc.debug(f"[{commname}] Serial number {serialno}")
logrdata.logrobjc.debug(
f"[{commname}] Valid from {strtdate} ({abs(daystobt)} days "
+ "{'passed since beginning' if cstarted else 'left before beginning'})"
+ f"{'passed since beginning' if cstarted else 'left before beginning'})"
)
logrdata.logrobjc.debug(
f"[{commname}] Valid until {stopdate} ({abs(daystodd)} days "
+ "{'passed since expiring' if cstopped else 'left before expiring'})"
+ f"{'passed since expiring' if cstopped else 'left before expiring'})"
)
return strtdate, stopdate, cstarted, cstopped, daystobt, daystodd, issuauth, serialno


def probedir():
logrdata.logrobjc.info("Probing into the configured directory")
doneqant, failqant, totlqant = 0, 0, 0
if os.path.exists(standard.hostloca):
standard.certdict = yaml.safe_load(Path(standard.hostloca).read_text())
logrdata.logrobjc.info(
f"Validating {len(standard.certdict)} X.509-standard TLS certificates"
)
for nameindx in standard.certdict:
totlqant += 1
certpath = Path(standard.certloca, standard.certdict[nameindx]["path"])
if os.path.exists(certpath):
standard.certdict = yaml.safe_load(Path(standard.hostloca).read_text())
logrdata.logrobjc.info(
f"Validating {len(standard.certdict)} X.509-standard TLS certificates"
)
for nameindx in standard.certdict:
totlqant += 1
certpath = Path(standard.certloca, standard.certdict[nameindx]["path"])
if os.path.exists(certpath):
try:
certobjc = x509.load_pem_x509_certificate(certpath.read_bytes(), default_backend())
(
standard.certdict[nameindx]["certstat"]["strtdate"],
Expand All @@ -59,23 +82,23 @@ def probedir():
f"[{nameindx}] The specified X.509-standard TLS certificate was read "
+ "successfully"
)
else:
except ValueError:
failqant += 1
logrdata.logrobjc.warning(
f"[{nameindx}] The specified X.509-standard TLS certificate could not "
+ "be located"
logrdata.logrobjc.error(
f"[{nameindx}] The specified X.509-standard TLS certificate could not be read"
)
logrdata.logrobjc.info(
f"Of {totlqant} TLS certificates, {doneqant} TLS certificate(s) were read successfully "
+ f"while {failqant} TLS certificate(s) could not be read"
)
with open(standard.hostloca, "w") as yamlfile:
yaml.safe_dump(standard.certdict, yamlfile)
else:
logrdata.logrobjc.error(
"Please set the directory containing the service hostname map properly"
)
sys.exit(1)
else:
failqant += 1
logrdata.logrobjc.warning(
f"[{nameindx}] The specified X.509-standard TLS certificate could not "
+ "be located"
)
logrdata.logrobjc.info(
f"Of {totlqant} TLS certificates, {doneqant} TLS certificate(s) were read successfully "
+ f"while {failqant} TLS certificate(s) could not be read"
)
with open(standard.hostloca, "w") as yamlfile:
yaml.safe_dump(standard.certdict, yamlfile)


def gonotify():
Expand Down Expand Up @@ -137,6 +160,3 @@ def gonotify():
elif standard.gitforge == "github":
logrdata.logrobjc.error("The notification has not yet been implemented on GitHub")
sys.exit(1)
else:
logrdata.logrobjc.error("The specified ticketing repository forge is not yet supported")
sys.exit(1)
21 changes: 21 additions & 0 deletions firmitas/conf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""
23 changes: 23 additions & 0 deletions firmitas/conf/logrdata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""


import logging

logging.basicConfig(
Expand Down
29 changes: 27 additions & 2 deletions firmitas/conf/standard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""

"""
Variables related to notifications
"""

# The limit for how long a single request must be attempted for
rqsttime = 30

# The source code forge on which the issue tickets need to be created
gitforge = "pagure"

Expand Down Expand Up @@ -31,10 +56,10 @@
"""

# The location of the X.509 standard TLS certificates
certloca = "/var/tmp/firmitas/certhere"
certloca = "/var/tmp/firmitas/certhere" # noqa : S108

# The location of the service hostnames and maintainers map
hostloca = "/var/tmp/firmitas/certlist.yml"
hostloca = "/var/tmp/firmitas/certlist.yml" # noqa: S108

"""
Variables related to logging
Expand Down
27 changes: 25 additions & 2 deletions firmitas/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""


import click

from firmitas import __vers__, readconf
Expand All @@ -17,8 +40,8 @@
def main(conffile=None):
if conffile:
confdict = {}
with open(conffile, "r") as confobjc:
exec(compile(confobjc.read(), conffile, "exec"), confdict)
with open(conffile) as confobjc:
exec(compile(confobjc.read(), conffile, "exec"), confdict) # noqa : S102
readconf(confdict)
probedir()
gonotify()
23 changes: 23 additions & 0 deletions firmitas/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""


issuhead = "[FMTS] TLS certificate for {servname} service is about to expire in {daysqant} days"

issubody = """
Expand Down
21 changes: 21 additions & 0 deletions firmitas/unit/gogithub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""
21 changes: 21 additions & 0 deletions firmitas/unit/gogitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Firmitas
Copyright (C) 2023-2024 Akashdeep Dhar
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
Any Red Hat trademarks that are incorporated in the source code or
documentation are not subject to the GNU General Public License and may only
be used or replicated with the express permission of Red Hat, Inc.
"""
Loading

0 comments on commit 368b2b6

Please sign in to comment.