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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
"""Tests for state: absent functionality."""
import os
testinfra_hosts = [
"borg-client-single-delete",
"borg-client-multi-delete",
"borg-client-per-repo-delete",
"borg-client-nonroot-delete",
"borg-client-never-delete",
"borg-server-delete",
]
def _scenario_file(filename):
scenario_dir = os.environ.get("MOLECULE_SCENARIO_DIRECTORY")
if scenario_dir:
return os.path.join(scenario_dir, filename)
test_dir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(test_dir, "..", filename)
def _read_local_file(filename):
with open(_scenario_file(filename), "r", encoding="utf-8") as file_handle:
return file_handle.read()
class TestSingleRepoDelete:
def test_systemd_units_removed(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-single-delete":
return
timer = host.file("/etc/systemd/system/borg_backup@single-backup.timer")
service = host.file("/etc/systemd/system/borg_backup@single-backup.service")
assert not timer.exists
assert not service.exists
def test_backup_scripts_removed(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-single-delete":
return
repo_script = host.file("/usr/local/bin/run_borg_backup@single-backup")
base_script = host.file("/usr/local/bin/run_borg_backup")
assert not repo_script.exists
assert not base_script.exists
def test_shared_ssh_key_kept(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-single-delete":
return
key = host.file("/root/.ssh/id_rsa")
assert key.exists
def test_repository_data_deleted(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-server-delete":
return
repo = host.file("/opt/borg/single-backup")
assert not repo.exists
def test_authorized_keys_entry_removed(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-server-delete":
return
auth_keys = host.file("/opt/borg/.ssh/authorized_keys")
assert auth_keys.exists
assert "root@borg-client-single-delete" not in auth_keys.content_string
class TestMultiInstanceDelete:
def test_removed_repo_units_removed(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-multi-delete":
return
timer = host.file("/etc/systemd/system/borg_backup@multi-repo-a.timer")
service = host.file("/etc/systemd/system/borg_backup@multi-repo-a.service")
script = host.file("/usr/local/bin/run_borg_backup@multi-repo-a")
assert not timer.exists
assert not service.exists
assert not script.exists
def test_remaining_repo_artifacts_exist(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-multi-delete":
return
timer = host.file("/etc/systemd/system/borg_backup@multi-repo-b.timer")
service = host.file("/etc/systemd/system/borg_backup@multi-repo-b.service")
script = host.file("/usr/local/bin/run_borg_backup@multi-repo-b")
assert timer.exists
assert service.exists
assert script.exists
def test_base_script_keeps_only_remaining_block(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-multi-delete":
return
base_script = host.file("/usr/local/bin/run_borg_backup")
assert base_script.exists
content = base_script.content_string
assert "/opt/borg/multi-repo-b" in content
assert "/opt/borg/multi-repo-a" not in content
assert content.count("borg create") == 1
def test_shared_ssh_key_kept(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-multi-delete":
return
key = host.file("/root/.ssh/id_rsa")
assert key.exists
def test_repository_data_kept(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-server-delete":
return
removed_repo = host.file("/opt/borg/multi-repo-a")
remaining_repo = host.file("/opt/borg/multi-repo-b")
assert removed_repo.exists
assert removed_repo.is_directory
assert remaining_repo.exists
assert remaining_repo.is_directory
def test_authorized_keys_keeps_only_remaining_repo(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-server-delete":
return
auth_keys = host.file("/opt/borg/.ssh/authorized_keys")
content = auth_keys.content_string
host_entries = [
line
for line in content.splitlines()
if "root@borg-client-multi-delete" in line
]
assert len(host_entries) == 1
assert "--restrict-to-repository /opt/borg/multi-repo-b" in host_entries[0]
assert "--restrict-to-repository /opt/borg/multi-repo-a" not in host_entries[0]
class TestPerRepoKeyDelete:
def test_removed_repo_artifacts_removed(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-per-repo-delete":
return
timer = host.file("/etc/systemd/system/borg_backup@per-repo-a.timer")
service = host.file("/etc/systemd/system/borg_backup@per-repo-a.service")
script = host.file("/usr/local/bin/run_borg_backup@per-repo-a")
assert not timer.exists
assert not service.exists
assert not script.exists
def test_remaining_repo_untouched(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-per-repo-delete":
return
timer = host.file("/etc/systemd/system/borg_backup@per-repo-b.timer")
service = host.file("/etc/systemd/system/borg_backup@per-repo-b.service")
script = host.file("/usr/local/bin/run_borg_backup@per-repo-b")
base_script = host.file("/usr/local/bin/run_borg_backup")
assert timer.exists
assert service.exists
assert script.exists
assert base_script.exists
assert "/opt/borg/per-repo-b" in base_script.content_string
assert "/opt/borg/per-repo-a" not in base_script.content_string
def test_per_repo_ssh_key_removed(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-per-repo-delete":
return
key = host.file(
"/root/.ssh/id_ed25519_borgbackup_borg_server_delete_per_repo_a"
)
key_pub = host.file(
"/root/.ssh/id_ed25519_borgbackup_borg_server_delete_per_repo_a.pub"
)
assert not key.exists
assert not key_pub.exists
def test_other_repo_ssh_key_kept(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-per-repo-delete":
return
key = host.file(
"/root/.ssh/id_ed25519_borgbackup_borg_server_delete_per_repo_b"
)
key_pub = host.file(
"/root/.ssh/id_ed25519_borgbackup_borg_server_delete_per_repo_b.pub"
)
assert key.exists
assert key_pub.exists
def test_repository_data_deleted_and_remaining_kept(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-server-delete":
return
removed_repo = host.file("/opt/borg/per-repo-a")
remaining_repo = host.file("/opt/borg/per-repo-b")
assert not removed_repo.exists
assert remaining_repo.exists
def test_authorized_keys_per_repo_entry_removed(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-server-delete":
return
auth_keys = host.file("/opt/borg/.ssh/authorized_keys")
content = auth_keys.content_string
assert "--restrict-to-repository /opt/borg/per-repo-a" not in content
assert "--restrict-to-repository /opt/borg/per-repo-b" in content
class TestNonRootDelete:
def test_removed_repo_artifacts_removed(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-nonroot-delete":
return
timer = host.file("/etc/systemd/system/borg_backup@nonroot-a.timer")
service = host.file("/etc/systemd/system/borg_backup@nonroot-a.service")
script = host.file("/usr/local/bin/run_borg_backup@nonroot-a")
assert not timer.exists
assert not service.exists
assert not script.exists
def test_remaining_repo_and_shared_key_kept(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-nonroot-delete":
return
timer = host.file("/etc/systemd/system/borg_backup@nonroot-b.timer")
service = host.file("/etc/systemd/system/borg_backup@nonroot-b.service")
script = host.file("/usr/local/bin/run_borg_backup@nonroot-b")
key = host.file("/home/backupuser/.ssh/id_rsa")
assert timer.exists
assert service.exists
assert script.exists
assert key.exists
def test_authorized_keys_preserves_nonroot_comment(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-server-delete":
return
auth_keys = host.file("/opt/borg/.ssh/authorized_keys")
content = auth_keys.content_string
host_entries = [
line
for line in content.splitlines()
if "backupuser@borg-client-nonroot-delete" in line
]
assert len(host_entries) == 1
assert "root@borg-client-nonroot-delete" not in host_entries[0]
assert "--restrict-to-repository /opt/borg/nonroot-b" in host_entries[0]
assert "--restrict-to-repository /opt/borg/nonroot-a" not in host_entries[0]
class TestDecryptionKeyCleanup:
def test_deleted_entries_removed_and_remaining_kept(self):
content = _read_local_file("decryption_keys_delete.yml")
assert "borg-client-single-delete_single-backup:" not in content
assert "borg-client-multi-delete_multi-repo-a:" not in content
assert "borg-client-per-repo-delete_per-repo-a:" not in content
assert "borg-client-nonroot-delete_nonroot-a:" not in content
assert "borg-client-multi-delete_multi-repo-b:" in content
assert "borg-client-per-repo-delete_per-repo-b:" in content
assert "borg-client-nonroot-delete_nonroot-b:" in content
def test_empty_decryption_keys_file_stays_valid(self):
content = _read_local_file("decryption_keys_empty_delete.yml")
assert content.strip() == "{}"
class TestNeverConfiguredDelete:
def test_no_artifacts_created(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-client-never-delete":
return
timer = host.file("/etc/systemd/system/borg_backup@never-configured.timer")
service = host.file("/etc/systemd/system/borg_backup@never-configured.service")
script = host.file("/usr/local/bin/run_borg_backup@never-configured")
base_script = host.file("/usr/local/bin/run_borg_backup")
assert not timer.exists
assert not service.exists
assert not script.exists
assert not base_script.exists
def test_server_repo_not_created(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-server-delete":
return
repo = host.file("/opt/borg/never-configured")
assert not repo.exists
def test_no_authorized_keys_entry_created(self, host):
hostname = host.backend.get_hostname()
if hostname != "borg-server-delete":
return
auth_keys = host.file("/opt/borg/.ssh/authorized_keys")
assert "borg-client-never-delete" not in auth_keys.content_string
|