Skip to content

Commit a7aaea9

Browse files
authored
[DPE-7685] setrpiv shim (#1027)
* Don't switch _daemon_ directly * Revert timeout * Wait for cluster to init before starting the observer * Bring back create dirs * Reorder peer tls hook * Fake cert location * Run as root * Use revisions from edge
1 parent 90834b0 commit a7aaea9

File tree

7 files changed

+8
-78
lines changed

7 files changed

+8
-78
lines changed

refresh_versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ name = "charmed-postgresql"
66

77
[snap.revisions]
88
# amd64
9-
x86_64 = "197"
9+
x86_64 = "201"
1010
# arm64
11-
aarch64 = "198"
11+
aarch64 = "202"

scripts/cluster_topology_observer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ def check_for_database_changes(run_cmd, unit, charm_dir, previous_databases):
9797
password = conf_file_contents["postgresql"]["authentication"]["superuser"]["password"]
9898
env = environ.copy()
9999
env["PGPASSWORD"] = password
100+
# Fake cert location for patronictl
101+
env["PGSSLCERT"] = "/var/snap/charmed-postgresql/current/etc/patroni/nonexistent_cert.pem"
100102
command = [
101103
"sudo",
102104
"-E",
103105
"-H",
104-
"-u",
105-
"_daemon_",
106106
"charmed-postgresql.patronictl",
107107
"-c",
108108
conf_file_path,

src/backups.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import json
77
import logging
88
import os
9-
import pwd
109
import re
1110
import shutil
1211
import tempfile
@@ -342,22 +341,11 @@ def _execute_command(
342341
timeout: int | None = None,
343342
) -> tuple[int, str, str]:
344343
"""Execute a command in the workload container."""
345-
346-
def demote():
347-
pw_record = pwd.getpwnam("_daemon_")
348-
349-
def result():
350-
os.setgid(pw_record.pw_gid)
351-
os.setuid(pw_record.pw_uid)
352-
353-
return result
354-
355344
# Input is generated by the charm
356345
process = run( # noqa: S603
357346
command,
358347
input=command_input,
359348
capture_output=True,
360-
preexec_fn=demote(),
361349
timeout=timeout,
362350
)
363351
return process.returncode, process.stdout.decode(), process.stderr.decode()

src/charm.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,16 +1324,6 @@ def _on_install(self, event: InstallEvent) -> None:
13241324
except snap.SnapError:
13251325
logger.warning("Unable to create psql alias")
13261326

1327-
# Create the user home directory for the _daemon_ user.
1328-
# This is needed due to https://bugs.launchpad.net/snapd/+bug/2011581.
1329-
try:
1330-
# Input is hardcoded
1331-
subprocess.check_call(["mkdir", "-p", "/home/_daemon_"]) # noqa: S607
1332-
subprocess.check_call(["chown", "_daemon_:_daemon_", "/home/_daemon_"]) # noqa: S607
1333-
subprocess.check_call(["usermod", "-d", "/home/_daemon_", "_daemon_"]) # noqa: S607
1334-
except subprocess.CalledProcessError:
1335-
logger.exception("Unable to create _daemon_ home dir")
1336-
13371327
self.set_unit_status(WaitingStatus("waiting to start PostgreSQL"))
13381328

13391329
def _on_leader_elected(self, event: LeaderElectedEvent) -> None: # noqa: C901

tests/integration/test_tls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ async def test_tls_enabled(ops_test: OpsTest) -> None:
113113
await run_command_on_unit(
114114
ops_test,
115115
replica,
116-
"sudo -u _daemon_ charmed-postgresql.pg-ctl -D /var/snap/charmed-postgresql/common/var/lib/postgresql/ promote",
116+
"sudo charmed-postgresql.pg-ctl -D /var/snap/charmed-postgresql/common/var/lib/postgresql/ promote",
117117
)
118118

119119
# Check that the replica was promoted.

tests/unit/test_backups.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# See LICENSE file for licensing details.
33
from os import cpu_count
44
from subprocess import CompletedProcess, TimeoutExpired
5-
from unittest.mock import ANY, MagicMock, PropertyMock, call, mock_open, patch
5+
from unittest.mock import MagicMock, PropertyMock, call, mock_open, patch
66

77
import botocore as botocore
88
import pytest
@@ -483,29 +483,21 @@ def test_change_connectivity_to_database(harness):
483483
def test_execute_command(harness):
484484
with (
485485
patch("backups.run") as _run,
486-
patch("pwd.getpwnam") as _getpwnam,
487486
):
488487
# Test when the command fails.
489488
command = ["rm", "-r", "/var/snap/charmed-postgresql/common/data/db"]
490489
_run.return_value = CompletedProcess(command, 1, b"", b"fake stderr")
491490
assert harness.charm.backup._execute_command(command) == (1, "", "fake stderr")
492-
_run.assert_called_once_with(
493-
command, input=None, capture_output=True, preexec_fn=ANY, timeout=None
494-
)
495-
_getpwnam.assert_called_once_with("_daemon_")
491+
_run.assert_called_once_with(command, input=None, capture_output=True, timeout=None)
496492

497493
# Test when the command runs successfully.
498494
_run.reset_mock()
499-
_getpwnam.reset_mock()
500495
_run.side_effect = None
501496
_run.return_value = CompletedProcess(command, 0, b"fake stdout", b"")
502497
assert harness.charm.backup._execute_command(
503498
command, command_input=b"fake input", timeout=5
504499
) == (0, "fake stdout", "")
505-
_run.assert_called_once_with(
506-
command, input=b"fake input", capture_output=True, preexec_fn=ANY, timeout=5
507-
)
508-
_getpwnam.assert_called_once_with("_daemon_")
500+
_run.assert_called_once_with(command, input=b"fake input", capture_output=True, timeout=5)
509501

510502

511503
def test_format_backup_list(harness):

tests/unit/test_charm.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def harness():
6464

6565
def test_on_install(harness):
6666
with (
67-
patch("charm.subprocess.check_call") as _check_call,
6867
patch("charm.snap.SnapCache") as _snap_cache,
6968
patch("charm.PostgresqlOperatorCharm._install_snap_package") as _install_snap_package,
7069
patch(
@@ -88,45 +87,6 @@ def test_on_install(harness):
8887
pg_snap.alias.assert_any_call("psql")
8988
pg_snap.alias.assert_any_call("patronictl")
9089

91-
assert _check_call.call_count == 3
92-
_check_call.assert_any_call(["mkdir", "-p", "/home/_daemon_"])
93-
_check_call.assert_any_call(["chown", "_daemon_:_daemon_", "/home/_daemon_"])
94-
_check_call.assert_any_call(["usermod", "-d", "/home/_daemon_", "_daemon_"])
95-
96-
# Assert the status set by the event handler.
97-
assert isinstance(harness.model.unit.status, WaitingStatus)
98-
99-
100-
def test_on_install_failed_to_create_home(harness):
101-
with (
102-
patch("charm.subprocess.check_call") as _check_call,
103-
patch("charm.snap.SnapCache") as _snap_cache,
104-
patch("charm.PostgresqlOperatorCharm._install_snap_package") as _install_snap_package,
105-
patch(
106-
"charm.PostgresqlOperatorCharm._reboot_on_detached_storage"
107-
) as _reboot_on_detached_storage,
108-
patch(
109-
"charm.PostgresqlOperatorCharm._is_storage_attached",
110-
side_effect=[False, True, True],
111-
) as _is_storage_attached,
112-
patch("charm.logger.exception") as _logger_exception,
113-
):
114-
# Test without storage.
115-
harness.charm.on.install.emit()
116-
_reboot_on_detached_storage.assert_called_once()
117-
pg_snap = _snap_cache.return_value[charm_refresh.snap_name()]
118-
_check_call.side_effect = [subprocess.CalledProcessError(-1, ["test"])]
119-
120-
# Test without adding Patroni resource.
121-
harness.charm.on.install.emit()
122-
# Assert that the needed calls were made.
123-
_install_snap_package.assert_called_once_with(revision=None)
124-
assert pg_snap.alias.call_count == 2
125-
pg_snap.alias.assert_any_call("psql")
126-
pg_snap.alias.assert_any_call("patronictl")
127-
128-
_logger_exception.assert_called_once_with("Unable to create _daemon_ home dir")
129-
13090
# Assert the status set by the event handler.
13191
assert isinstance(harness.model.unit.status, WaitingStatus)
13292

0 commit comments

Comments
 (0)