diff --git a/src/charm.py b/src/charm.py index 1cd86db..5376d9b 100755 --- a/src/charm.py +++ b/src/charm.py @@ -74,6 +74,7 @@ RABBITMQ_SERVICE = "rabbitmq" RABBITMQ_USER = "rabbitmq" RABBITMQ_GROUP = "rabbitmq" +RABBITMQ_DATA_DIR = "/var/lib/rabbitmq" RABBITMQ_COOKIE_PATH = "/var/lib/rabbitmq/.erlang.cookie" SELECTOR_ALL = "all" @@ -201,6 +202,9 @@ def _on_config_changed(self, event: EventBase) -> None: event.defer() return + # Change ownership of /var/lib/rabbitmq + self._set_ownership_on_data_dir() + # Render and push configuration files self._render_and_push_config_files() @@ -711,6 +715,34 @@ def _initialize_operator_user(self) -> None: logging.warning("Deleting the guest user.") api.delete_user("guest") + def _set_ownership_on_data_dir(self) -> None: + """Set ownership on /var/lib/rabbitmq.""" + container = self.unit.get_container(RABBITMQ_CONTAINER) + paths = container.list_files(RABBITMQ_DATA_DIR, itself=True) + if len(paths) == 0: + return + + logger.debug( + f"rabbitmq lib directory ownership: {paths[0].user}:{paths[0].group}" + ) + if paths[0].user != RABBITMQ_USER or paths[0].group != RABBITMQ_GROUP: + logger.debug( + f"Changing ownership to {RABBITMQ_USER}:{RABBITMQ_GROUP}" + ) + try: + container.exec( + [ + "chown", + "-R", + f"{RABBITMQ_USER}:{RABBITMQ_GROUP}", + RABBITMQ_DATA_DIR, + ] + ) + except ExecError as e: + logger.error( + f"Exited with code {e.exit_code}. Stderr:\n{e.stderr}" + ) + def _render_and_push_config_files(self) -> None: """Render and push configuration files. diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 066b0fe..dc48975 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -66,6 +66,7 @@ def test_rabbitmq_pebble_ready(self): """Test pebble handler.""" # self.harness.charm._render_and_push_config_files = Mock() # self.harness.charm._render_and_push_plugins = Mock() + self.harness.charm._set_ownership_on_data_dir = Mock() # Check the initial Pebble plan is empty self.harness.set_can_connect("rabbitmq", True) initial_plan = self.harness.get_container_pebble_plan("rabbitmq")