Skip to content

Commit

Permalink
Add some useful utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Abuelodelanada committed Jan 16, 2024
1 parent 54db962 commit d206c78
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/charms/observability_libs/v1/cert_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

LIBID = "b5cd5cd580f3428fa5f59a8876dcbe6a"
LIBAPI = 1
LIBPATCH = 0
LIBPATCH = 1


def is_ip_address(value: str) -> bool:
Expand Down Expand Up @@ -297,6 +297,16 @@ def _on_certificate_available(self, event: CertificateAvailableEvent) -> None:
relation.data[self.charm.unit]["secret-id"] = secret.id # pyright: ignore
self.on.cert_changed.emit() # pyright: ignore


def _retrieve_secret_id(self, secret_id_name: str) -> Optional[str]:
if not (relation := self.charm.model.get_relation(self.certificates_relation_name)):
return None

if not (secret_id := relation.data[self.charm.unit].get(secret_id_name)):
return None

return secret_id

def _retrieve_from_secret(self, value: str, secret_id_name: str) -> Optional[str]:
if not (relation := self.charm.model.get_relation(self.certificates_relation_name)):
return None
Expand All @@ -315,6 +325,11 @@ def private_key(self) -> Optional[str]:
"""Private key."""
return self._retrieve_from_secret("private-key", "private-key-secret-id")

@property
def private_key_secret_id(self) -> Optional[str]:
"""Private key secret id."""
return self._retrieve_secret_id("private-key-secret-id")

@property
def _csr(self) -> Optional[str]:
return self._retrieve_from_secret("csr", "csr-secret-id")
Expand All @@ -338,6 +353,11 @@ def ca_cert(self) -> Optional[str]:
"""CA Certificate."""
return self._retrieve_from_secret("ca-cert", "secret-id")

@property
def ca_server_cert_secret_id(self) -> Optional[str]:
"""CA, servert cert secret id."""
return self._retrieve_secret_id("secret-id")

@property
def server_cert(self) -> Optional[str]:
"""Server Certificate."""
Expand Down Expand Up @@ -376,13 +396,13 @@ def _on_certificate_invalidated(self, event: CertificateInvalidatedEvent) -> Non
self._generate_csr(overwrite=True, clear_cert=True)
self.on.cert_changed.emit() # pyright: ignore

def _on_all_certificates_invalidated(self, event: AllCertificatesInvalidatedEvent) -> None:
def _on_all_certificates_invalidated(self, _: AllCertificatesInvalidatedEvent) -> None:
# Do what you want with this information, probably remove all certificates
# Note: assuming "limit: 1" in metadata
self._generate_csr(overwrite=True, clear_cert=True)
self.on.cert_changed.emit() # pyright: ignore

def _on_certificates_relation_broken(self, event: RelationBrokenEvent) -> None:
def _on_certificates_relation_broken(self, _: RelationBrokenEvent) -> None:
"""Clear the certificates data when removing the relation."""
try:
secret = self.model.get_secret(label="csr-secret-id")
Expand Down

0 comments on commit d206c78

Please sign in to comment.