Files
deskflow/scripts/lib/certificate.py
Nick Bolton bec38ab47b Re-implement packaging for GitHub workflows (Windows) (#7360)
* Add new version env vars to example

* Remove test file

* Move CMake packaging to separate module and configure OpenSSL path for Windows

* Make VS Code CMake build task default

* Generate Microsoft-friendly 4-digit version number

* Update macOS bundle .plist with build year variable

* Use correct OpenSSL path and fixed various MSI variables

* Use correct rest/dist dir for MSI

* Add version .rc file for Windows

* Use macro instead of over-complicated version query command

* Made cmd_utils more secure by defaulting to no-shell and no-print

* Add certificate management module

* Implement packaging script on Windows

* Refactor Mac packaging script to use new cmd_utils args and new cert module

* Update ChangeLog

* Change PFX env vars and add to CI

* Use import as instead of from lib to solve resolve issue

* Allow custom certificate extensions

* Check for package version when using gdrive

* Make version number required

* Add missing shell

* Add missing gdrive value in test

* Find OpenSSL dir based on openssl binary

* Only use first OpenSSL entry

* More verbose logging

* Improve logging

* Only use env var if not empty

* Fixed wrong var

* Fixed macOS GitHub artefact name

* Change filename format to match new convention
2024-06-28 09:35:18 +00:00

41 lines
1.3 KiB
Python

import os, base64
temp_path = "tmp/certificate"
class Certificate:
"""
Installs a certificate from a base64 string, and returns the path to the certificate.
Once the context is exited, the certificate is removed from the filesystem.
Example usage:
with Certificate(base64) as cert_path:
print(f"Certificate path: {cert_path}")
"""
def __init__(self, base64, file_ext):
self.base64 = base64
self.temp_filename = f"{temp_path}.{file_ext}"
def __enter__(self):
print(f"Decoding certificate to temporary path: {self.temp_filename}")
try:
cert_bytes = base64.b64decode(self.base64)
except Exception as e:
raise ValueError("Failed to decode certificate base64") from e
os.makedirs(os.path.dirname(self.temp_filename), exist_ok=True)
with open(self.temp_filename, "wb") as cert_file:
cert_file.write(cert_bytes)
return self.temp_filename
def __exit__(self, _exc_type, _exc_value, _traceback):
# not strictly necessary for ci, but when run on a dev machine, it reduces the risk
# that private keys are left on the filesystem
print(f"Removing temporary certificate file: {self.temp_filename}")
os.remove(self.temp_filename)
# propagate exceptions
return False