diff --git a/osc/obs_api/__init__.py b/osc/obs_api/__init__.py index 37c7a52b..0084bff9 100644 --- a/osc/obs_api/__init__.py +++ b/osc/obs_api/__init__.py @@ -1,3 +1,4 @@ +from .keyinfo import Keyinfo from .package import Package from .package_sources import PackageSources from .person import Person diff --git a/osc/obs_api/keyinfo.py b/osc/obs_api/keyinfo.py new file mode 100644 index 00000000..7a7e9eed --- /dev/null +++ b/osc/obs_api/keyinfo.py @@ -0,0 +1,33 @@ +import textwrap + +from ..util.models import * # pylint: disable=wildcard-import,unused-wildcard-import +from .keyinfo_pubkey import KeyinfoPubkey +from .keyinfo_sslcert import KeyinfoSslcert + + +class Keyinfo(XmlModel): + XML_TAG = "keyinfo" + + project: str = Field( + xml_attribute=True, + description=textwrap.dedent( + """ + The name of the project. + """ + ), + ) + + pubkey_list: Optional[List[KeyinfoPubkey]] = Field( + xml_name="pubkey", + ) + + sslcert_list: Optional[List[KeyinfoSslcert]] = Field( + xml_name="sslcert", + ) + + @classmethod + def from_api(cls, apiurl: str, project: str) -> "Keyinfo": + url_path = ["source", project, "_keyinfo"] + url_query = {} + response = cls.xml_request("GET", apiurl, url_path, url_query) + return cls.from_file(response, apiurl=apiurl) diff --git a/osc/obs_api/keyinfo_pubkey.py b/osc/obs_api/keyinfo_pubkey.py new file mode 100644 index 00000000..a3c99515 --- /dev/null +++ b/osc/obs_api/keyinfo_pubkey.py @@ -0,0 +1,51 @@ +from ..util.models import * # pylint: disable=wildcard-import,unused-wildcard-import + + +class KeyinfoPubkey(XmlModel): + XML_TAG = "pubkey" + + keyid: str = Field( + xml_attribute=True, + ) + + userid: str = Field( + xml_attribute=True, + ) + + algo: str = Field( + xml_attribute=True, + ) + + keysize: str = Field( + xml_attribute=True, + ) + + expires: int = Field( + xml_attribute=True, + ) + + fingerprint: str = Field( + xml_attribute=True, + ) + + value: str = Field( + xml_set_text=True, + ) + + def get_expires_str(self) -> str: + import datetime + return datetime.datetime.fromtimestamp(self.expires).strftime("%Y-%m-%d %H:%M:%S") + + def to_human_readable_string(self) -> str: + """ + Render the object as a human readable string. + """ + from ..output import KeyValueTable + table = KeyValueTable() + table.add("Type", "GPG public key") + table.add("User ID", self.userid, color="bold") + table.add("Algorithm", self.algo) + table.add("Key size", self.keysize) + table.add("Expires", self.get_expires_str()) + table.add("Fingerprint", self.fingerprint) + return f"{table}\n{self.value}" diff --git a/osc/obs_api/keyinfo_sslcert.py b/osc/obs_api/keyinfo_sslcert.py new file mode 100644 index 00000000..8101056c --- /dev/null +++ b/osc/obs_api/keyinfo_sslcert.py @@ -0,0 +1,71 @@ +from ..util.models import * # pylint: disable=wildcard-import,unused-wildcard-import + + +class KeyinfoSslcert(XmlModel): + XML_TAG = "sslcert" + + keyid: str = Field( + xml_attribute=True, + ) + + serial: str = Field( + xml_attribute=True, + ) + + issuer: Optional[str] = Field( + xml_attribute=True, + ) + + subject: str = Field( + xml_attribute=True, + ) + + algo: str = Field( + xml_attribute=True, + ) + + keysize: str = Field( + xml_attribute=True, + ) + + begins: int = Field( + xml_attribute=True, + ) + + expires: int = Field( + xml_attribute=True, + ) + + fingerprint: str = Field( + xml_attribute=True, + ) + + value: str = Field( + xml_set_text=True, + ) + + def get_begins_str(self) -> str: + import datetime + return datetime.datetime.fromtimestamp(self.begins).strftime("%Y-%m-%d %H:%M:%S") + + def get_expires_str(self) -> str: + import datetime + return datetime.datetime.fromtimestamp(self.expires).strftime("%Y-%m-%d %H:%M:%S") + + def to_human_readable_string(self) -> str: + """ + Render the object as a human readable string. + """ + from ..output import KeyValueTable + table = KeyValueTable() + table.add("Type", "SSL certificate") + table.add("Subject", self.subject, color="bold") + table.add("Key ID", self.keyid) + table.add("Serial", self.serial) + table.add("Issuer", self.issuer) + table.add("Algorithm", self.algo) + table.add("Key size", self.keysize) + table.add("Begins", self.get_begins_str()) + table.add("Expires", self.get_expires_str()) + table.add("Fingerprint", self.fingerprint) + return f"{table}\n{self.value}"