From 1f9422198939829d3e566b9563ca5f05bf3ab300 Mon Sep 17 00:00:00 2001 From: alesstimec Date: Wed, 5 Jul 2023 10:02:10 +0200 Subject: [PATCH] Update openfga library for both charms. And a minor fix for required settings that will now display a sensible message to the user. --- .../lib/charms/openfga_k8s/v0/openfga.py | 16 +++++--- charms/jimm-k8s/src/charm.py | 37 ++++++++++--------- charms/jimm/charmcraft.yaml | 19 ++++++---- .../jimm/lib/charms/openfga_k8s/v0/openfga.py | 6 ++- charms/jimm/src/charm.py | 11 ++++-- 5 files changed, 53 insertions(+), 36 deletions(-) diff --git a/charms/jimm-k8s/lib/charms/openfga_k8s/v0/openfga.py b/charms/jimm-k8s/lib/charms/openfga_k8s/v0/openfga.py index 8160715ce..4cd3e8a31 100644 --- a/charms/jimm-k8s/lib/charms/openfga_k8s/v0/openfga.py +++ b/charms/jimm-k8s/lib/charms/openfga_k8s/v0/openfga.py @@ -76,7 +76,7 @@ def _on_openfga_store_created(self, event: OpenFGAStoreCreateEvent): # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 3 +LIBPATCH = 4 logger = logging.getLogger(__name__) @@ -88,23 +88,27 @@ class OpenFGAEvent(RelationEvent): @property def store_id(self): - return self.relation.data[self.relation.app].get("store_id") + return self.relation.data[self.relation.app].get("store_id", "") @property def token_secret_id(self): - return self.relation.data[self.relation.app].get("token_secret_id") + return self.relation.data[self.relation.app].get("token_secret_id", "") + + @property + def token(self): + return self.relation.data[self.relation.app].get("token", "") @property def address(self): - return self.relation.data[self.relation.app].get("address") + return self.relation.data[self.relation.app].get("address", "") @property def scheme(self): - return self.relation.data[self.relation.app].get("scheme") + return self.relation.data[self.relation.app].get("scheme", "") @property def port(self): - return self.relation.data[self.relation.app].get("port") + return self.relation.data[self.relation.app].get("port", "") class OpenFGAStoreCreateEvent(OpenFGAEvent): diff --git a/charms/jimm-k8s/src/charm.py b/charms/jimm-k8s/src/charm.py index 847b4640a..1037c80cb 100755 --- a/charms/jimm-k8s/src/charm.py +++ b/charms/jimm-k8s/src/charm.py @@ -62,17 +62,17 @@ WORKLOAD_CONTAINER = "jimm" -REQUIRED_SETTINGS = [ - "JIMM_UUID", - "JIMM_DSN", - "CANDID_URL", - "OPENFGA_STORE", - "OPENFGA_AUTH_MODEL", - "OPENFGA_HOST", - "OPENFGA_SCHEME", - "OPENFGA_TOKEN", - "OPENFGA_PORT", -] +REQUIRED_SETTINGS = { + "JIMM_UUID": "missing uuid configuration", + "JIMM_DSN": "missing postgresql relation", + "CANDID_URL": "missing candid-url configuration", + "OPENFGA_STORE": "missing openfga relation", + "OPENFGA_AUTH_MODEL": "run create-authorization-model action", + "OPENFGA_HOST": "missing openfga relation", + "OPENFGA_SCHEME": "missing openfga relation", + "OPENFGA_TOKEN": "missing openfga relation", + "OPENFGA_PORT": "missing openfga relation", +} DATABASE_NAME = "jimm" OPENFGA_STORE_NAME = "jimm" @@ -335,7 +335,7 @@ def _on_stop(self, _): """Stop JIMM.""" container = self.unit.get_container(WORKLOAD_CONTAINER) if container.can_connect(): - container.stop() + container.stop("jimm") self._ready() def _on_update_status(self, _): @@ -395,10 +395,10 @@ def _ready(self): env_vars = plan.services.get("jimm").environment - for setting in REQUIRED_SETTINGS: + for setting, message in REQUIRED_SETTINGS.items(): if not env_vars.get(setting, ""): self.unit.status = BlockedStatus( - "{} configuration value not set".format(setting), + "{} configuration value not set: {}".format(setting, message), ) return False @@ -586,11 +586,14 @@ def _on_openfga_store_created(self, event: OpenFGAStoreCreateEvent): if not event.store_id: return - # secret = self.model.get_secret(id=event.token_secret_id) - # secret_content = secret.get_content() + token = event.token + if event.token_secret_id: + secret = self.model.get_secret(id=event.token_secret_id) + secret_content = secret.get_content() + token = secret_content["token"] self._state.openfga_store_id = event.store_id - self._state.openfga_token = event.token # secret_content["token"] + self._state.openfga_token = token self._state.openfga_address = event.address self._state.openfga_port = event.port self._state.openfga_scheme = event.scheme diff --git a/charms/jimm/charmcraft.yaml b/charms/jimm/charmcraft.yaml index 61ae36388..9d6375eb3 100644 --- a/charms/jimm/charmcraft.yaml +++ b/charms/jimm/charmcraft.yaml @@ -4,16 +4,19 @@ type: "charm" parts: charm: prime: - - ./templates - - ./files - - README.md - charm-python-packages: [setuptools] + - ./templates + - ./files + - README.md + charm-python-packages: + - setuptools + - cosl + - pydantic==1.10 bases: # Ensure run-on is the same or newer than build-on # since jimm-server is a Go binary using CGO dependencies - build-on: - - name: "ubuntu" - channel: "20.04" + - name: "ubuntu" + channel: "20.04" run-on: - - name: "ubuntu" - channel: "20.04" + - name: "ubuntu" + channel: "20.04" diff --git a/charms/jimm/lib/charms/openfga_k8s/v0/openfga.py b/charms/jimm/lib/charms/openfga_k8s/v0/openfga.py index 8160715ce..09d86f20e 100644 --- a/charms/jimm/lib/charms/openfga_k8s/v0/openfga.py +++ b/charms/jimm/lib/charms/openfga_k8s/v0/openfga.py @@ -76,7 +76,7 @@ def _on_openfga_store_created(self, event: OpenFGAStoreCreateEvent): # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 3 +LIBPATCH = 4 logger = logging.getLogger(__name__) @@ -93,6 +93,10 @@ def store_id(self): @property def token_secret_id(self): return self.relation.data[self.relation.app].get("token_secret_id") + + @property + def token(self): + return self.relation.data[self.relation.app].get("token") @property def address(self): diff --git a/charms/jimm/src/charm.py b/charms/jimm/src/charm.py index 09d11d981..1ed190546 100755 --- a/charms/jimm/src/charm.py +++ b/charms/jimm/src/charm.py @@ -409,16 +409,19 @@ def _on_openfga_store_created(self, event: OpenFGAStoreCreateEvent): if not event.store_id: return - logger.error("token secret {}".format(event.token_secret_id)) - secret = self.model.get_secret(id=event.token_secret_id) - secret_content = secret.get_content() + token = event.token + if event.token_secret_id: + logger.error("token secret {}".format(event.token_secret_id)) + secret = self.model.get_secret(id=event.token_secret_id) + secret_content = secret.get_content() + token = secret_content["token"] args = { "openfga_host": event.address, "openfga_port": event.port, "openfga_scheme": event.scheme, "openfga_store": event.store_id, - "openfga_token": secret_content["token"], + "openfga_token": token, } with open(self._env_filename("openfga"), "wt") as f: