1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
"""shadow multihost host."""
from __future__ import annotations
from pathlib import PurePosixPath
from typing import Any
from pytest_mh.conn import ProcessLogLevel
from .base import BaseHost, BaseLinuxHost
__all__ = [
"ShadowHost",
]
class ShadowHost(BaseHost, BaseLinuxHost):
"""
shadow host object.
This is the host where the tests are run.
.. note::
Full backup and restore of shadow state is supported.
"""
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._features: dict[str, bool] | None = None
"""Features dictionary."""
self._backup_path: PurePosixPath | None = None
"""Path to backup files."""
self._verify_files: list[dict[str, str]] = [
{"origin": "/etc/passwd", "backup": "passwd"},
{"origin": "/etc/shadow", "backup": "shadow"},
{"origin": "/etc/group", "backup": "group"},
{"origin": "/etc/gshadow", "backup": "gshadow"},
]
"""Files to verify for mismatch."""
def pytest_setup(self) -> None:
super().pytest_setup()
def start(self) -> None:
"""
Not supported.
:raises NotImplementedError: _description_
"""
raise NotImplementedError("Starting shadow service is not implemented.")
def stop(self) -> None:
"""
Not supported.
:raises NotImplementedError: _description_
"""
raise NotImplementedError("Stopping shadow service is not implemented.")
@property
def features(self) -> dict[str, bool]:
"""
Features supported by the host.
"""
if self._features is not None:
return self._features
self.logger.info(f"Detecting shadow features on {self.hostname}")
result = self.conn.run(
"""
set -ex
getent gshadow > /dev/null 2>&1 && echo "gshadow" || :
""",
log_level=ProcessLogLevel.Error,
)
# Set default values
self._features = {
"gshadow": False,
}
self._features.update({k: True for k in result.stdout_lines})
self.logger.info("Detected features:", extra={"data": {"Features": self._features}})
return self._features
def backup(self) -> Any:
"""
Backup all shadow data.
:return: Backup data.
:rtype: Any
"""
self.logger.info("Creating backup of shadow host")
result = self.conn.run(
"""
set -ex
function backup {
if [ -d "$1" ] || [ -f "$1" ]; then
cp --force --archive "$1" "$2"
fi
}
path=`mktemp -d`
backup /etc/login.defs "$path/login.defs"
backup /etc/default/useradd "$path/useradd"
backup /etc/passwd "$path/passwd"
backup /etc/shadow "$path/shadow"
backup /etc/group "$path/group"
backup /etc/gshadow "$path/gshadow"
backup /etc/subuid "$path/subuid"
backup /etc/subgid "$path/subgid"
backup /home "$path/home"
backup /var/log/secure "$path/secure"
echo $path
""",
log_level=ProcessLogLevel.Error,
)
self._backup_path = PurePosixPath(result.stdout_lines[-1].strip())
return PurePosixPath(result.stdout_lines[-1].strip())
def restore(self, backup_data: Any | None) -> None:
"""
Restore all shadow data.
:return: Backup data.
:rtype: Any
"""
if backup_data is None:
return
if not isinstance(backup_data, PurePosixPath):
raise TypeError(f"Expected PurePosixPath, got {type(backup_data)}")
backup_path = str(backup_data)
self.logger.info(f"Restoring shadow data from {backup_path}")
self.conn.run(
f"""
set -ex
function restore {{
rm --force --recursive "$2"
if [ -d "$1" ] || [ -f "$1" ]; then
cp --force --archive "$1" "$2"
fi
}}
rm --force --recursive /var/log/secure
restore "{backup_path}/login.defs" /etc/login.defs
restore "{backup_path}/useradd" /etc/default/useradd
restore "{backup_path}/passwd" /etc/passwd
restore "{backup_path}/shadow" /etc/shadow
restore "{backup_path}/group" /etc/group
restore "{backup_path}/gshadow" /etc/gshadow
restore "{backup_path}/subuid" /etc/subuid
restore "{backup_path}/subgid" /etc/subgid
restore "{backup_path}/home" /home
restore "{backup_path}/secure" /var/log/secure
""",
log_level=ProcessLogLevel.Error,
)
def detect_file_mismatches(self) -> None:
"""
Shadow binaries modify a number of files, but usually do not modify all of them. This is why we add an
additional check at the end of the test to verify that the files that should not have been modified are still
intact.
"""
self.logger.info(f"Detecting mismatches in shadow files {self._backup_path}")
for x in self._verify_files:
result = self.conn.run(
f"""
set -ex
cmp {x['origin']} {self._backup_path}/{x['backup']}
""",
log_level=ProcessLogLevel.Error,
raise_on_error=False,
)
if result.rc != 0:
self.logger.error(f"File mismatch in '{x['origin']}' and '{self._backup_path}/{x['backup']}'")
result.throw()
def discard_file(self, origin: str) -> None:
"""
Discard modified files from the files that should be verified.
"""
for x in self._verify_files:
if x["origin"] == origin:
self._verify_files.remove(x)
break
|