Skip to content

Commit

Permalink
Add extra useful methods and one bugfix (#51)
Browse files Browse the repository at this point in the history
* add method to determine if TLS is enabled

* fix bug with cer relation name. add len(units) hack. ca_method

* fix linting
  • Loading branch information
dstathis committed Jul 13, 2023
1 parent 57107c0 commit 63672f9
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/charms/observability_libs/v0/cert_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

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


class CertChanged(EventBase):
Expand Down Expand Up @@ -139,7 +139,7 @@ def __init__(
self._on_all_certificates_invalidated,
)
self.framework.observe(
self.charm.on.certificates_relation_broken, # pyright: ignore
self.charm.on[self.certificates_relation_name].relation_broken, # pyright: ignore
self._on_certificates_relation_broken,
)

Expand All @@ -148,6 +148,16 @@ def __init__(
self.charm.on[self.peer_relation_name].relation_created, self._on_peer_relation_created
)

@property
def enabled(self) -> bool:
"""Boolean indicating whether the charm has a tls_certificates relation."""
# We need to check for units as a temporary workaround because of https://bugs.launchpad.net/juju/+bug/2024583
# This could in theory not work correctly on scale down to 0 but it is necessary for the moment.
return (
len(self.charm.model.relations[self.certificates_relation_name]) > 0
and len(self.charm.model.get_relation(self.certificates_relation_name).units) > 0 # type: ignore
)

@property
def _peer_relation(self) -> Optional[Relation]:
"""Return the peer relation."""
Expand Down Expand Up @@ -302,6 +312,11 @@ def cert(self):
"""Return the server cert."""
return self._server_cert

@property
def ca(self):
"""Return the CA cert."""
return self._ca_cert

@property
def _server_cert(self) -> Optional[str]:
if self._peer_relation:
Expand Down

0 comments on commit 63672f9

Please sign in to comment.