1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-01-25 22:36:13 +01:00

Add Keyinfo XML model

This commit is contained in:
Daniel Mach 2024-03-14 08:40:33 +01:00
parent 1bf2264427
commit cf4a0d06a2
4 changed files with 156 additions and 0 deletions

View File

@ -1,3 +1,4 @@
from .keyinfo import Keyinfo
from .package import Package
from .package_sources import PackageSources
from .person import Person

33
osc/obs_api/keyinfo.py Normal file
View File

@ -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)

View File

@ -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}"

View File

@ -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}"