Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update acme_client library to fix blocking bug #2

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ jobs:
uses: canonical/charming-actions/[email protected]
id: channel
- name: Upload charm to Charmhub
uses: canonical/charming-actions/[email protected]
ghislainbourgeois marked this conversation as resolved.
Show resolved Hide resolved
with:
credentials: "${{ secrets.CHARMHUB_TOKEN }}"
github-token: "${{ secrets.GITHUB_TOKEN }}"
channel: "${{ steps.channel.outputs.name }}"
env:
CHARMCRAFT_AUTH: "${{ secrets.CHARMHUB_TOKEN }}"
run: charmcraft upload ./httpreq-acme-operator_ubuntu-22.04-amd64.charm --release ${{ steps.channel.outputs.name }}
44 changes: 40 additions & 4 deletions lib/charms/acme_client_operator/v0/acme_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,23 @@ def _on_config_changed(self, _):

@property
def _plugin_config(self):
return None
return {}
```

Charms using this library are expected to:
- Inherit from AcmeClient
- Call `super().__init__(*args, plugin="")` with the lego plugin name
- Observe `ConfigChanged` to a method called `_on_config_changed`
- `_on_config_changed` must follow those requirements:
- Validate its specific configuration, blocking if invalid
- Validate generic configuration, by calling `self.validate_generic_acme_config()`,
returning immediately when it returns `False`
- Sets the status to Active
- Accept any kind of events
- Implement the `_plugin_config` property, returning a dictionary of its specific
configuration. Keys must be capitalized and follow the plugins documentation from
lego.

Charms that leverage this library also need to specify a `provides` relation in their
`metadata.yaml` file. For example:
```yaml
Expand All @@ -58,6 +73,7 @@ def _plugin_config(self):
from cryptography import x509
from cryptography.x509.oid import NameOID
from ops.charm import CharmBase
from ops.framework import EventBase
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus
from ops.pebble import ExecError

Expand All @@ -69,7 +85,7 @@ def _plugin_config(self):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 2
LIBPATCH = 3

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -111,6 +127,26 @@ def validate_generic_acme_config(self) -> bool:
return False
return True

@abstractmethod
def _on_config_changed(self, event: EventBase) -> None:
"""Validate configuration and sets status accordingly.

Implementations need to follow the following steps:

1. Validate their specific configuration, setting the status
to `Blocked` if invalid and returning immediately.
2. Validate generic configuration by calling
`self.validate_generic_acme_config()`, returning immediately
if it returns `False`.
3. Set the status to `Active` and return.

Args:
event (EventBase): Any Juju event

Returns:
None
"""

@staticmethod
def _get_subject_from_csr(certificate_signing_request: str) -> str:
"""Returns subject from a provided CSR."""
Expand Down Expand Up @@ -154,8 +190,8 @@ def _on_certificate_creation_request(self, event: CertificateCreationRequestEven
- Pulls certificates from workload
- Sends certificates to requesting charm
"""
if not self.validate_generic_acme_config():
self.unit.status = BlockedStatus("Invalid ACME configuration")
self._on_config_changed(event)
if not isinstance(self.unit.status, ActiveStatus):
event.defer()
return
if not self.unit.is_leader():
Expand Down