canonical / observability-libs

A collection of charm libraries curated by the Observability team.
https://charmhub.io/observability-libs
Apache License 2.0
3 stars 8 forks source link

fix race condition in privkey generation #76

Closed lucabello closed 6 months ago

lucabello commented 6 months ago

Issue

When trying to use this library in Alertmanager, I got the following error:

  File "/var/lib/juju/agents/unit-am-0/charm/lib/charms/observability_libs/v1/cert_handler.py", line 214, in _on_config_changed
    self._generate_csr(renew=True)
  File "/var/lib/juju/agents/unit-am-0/charm/lib/charms/observability_libs/v1/cert_handler.py", line 233, in _generate_csr
    raise RuntimeError(
RuntimeError: private key unset. call _generate_privkey() before you call this method.

In fact, _on_config_changed() doesn't call _generate_privkey().

The issue is that a race condition probably causes config-changed to arrive before certificates-relation-joined, which doesn't generate the private key beforehand.

Please note that calling the function multiple times isn't a problem as the function will just skip :)

Solution

Add a call to _generate_privkey() in the _on_config_changed() handler.

Abuelodelanada commented 6 months ago

This PR solves: https://github.com/canonical/observability-libs/issues/75 and https://github.com/canonical/observability-libs/issues/79