1#!/usr/bin/env python3
2import copy
3from functools import partial
4import importlib.util
5import json
6import os
7from pathlib import Path
8import sqlite3
9import tempfile
10import unittest
11from unittest.mock import patch
12import uuid
13
14spec = importlib.util.spec_from_file_location('shares', Path(__file__).resolve().parent.parent / 'service/copyparty/manage.py')
15shares = importlib.util.module_from_spec(spec)
16spec.loader.exec_module(shares)
17
18
19class Shares(unittest.TestCase):
20 def setUp(self):
21 self.temporary = tempfile.TemporaryDirectory()
22 self.addCleanup(self.temporary.cleanup)
23 self.root = Path(self.temporary.name) / 'clover'
24 self.cfg = Path(self.temporary.name) / 'cfg'
25 self.root.mkdir()
26 self.root = self.root.resolve()
27 (self.cfg / 'copyparty').mkdir(parents=True)
28 (self.cfg / 'copyparty.conf').write_text('original configuration\n')
29 self.database = self.cfg / 'copyparty/shares.db'
30 with sqlite3.connect(self.database) as db:
31 db.executescript("CREATE TABLE kv(k text,v int); INSERT INTO kv VALUES('sver',2); CREATE TABLE sh(k text,pw text,vp text,pr text,st int,un text,t0 int,t1 int); CREATE TABLE sf(k text,vp text);")
32 self.subject = str(uuid.uuid4())
33 self.users = [{'id': self.subject, 'username': 'test-user', 'enabled': True, 'kind': None, 'requiredActions': []}]
34 self.variable = {'ModifyIndex': 1, 'Items': {'idp_header': 'a' * 32, 'unrelated': 'retained'}}
35 self.environment = patch.dict(os.environ, {'SNOWGLOBE_CLOVER_ROOT': str(self.root), 'SNOWGLOBE_COPYPARTY_CFG': str(self.cfg)})
36 self.environment.start()
37 self.addCleanup(self.environment.stop)
38 self.nomad = patch.object(shares.runs, 'nomad', side_effect=self.request)
39 self.nomad.start()
40 self.addCleanup(self.nomad.stop)
41 self.reloads = []
42 self.reload = patch.object(shares.Manager, 'reload', autospec=True, side_effect=lambda manager, items, **kwargs: self.reloads.append(copy.deepcopy(items)))
43 self.reload_mock = self.reload.start()
44 self.addCleanup(self.reload.stop)
45 self.hash = patch.object(shares.Manager, 'hash', side_effect=lambda password: '+' + 'a' * 32 if password else '')
46 self.hash.start()
47 self.container = patch.object(shares.Manager, 'container', return_value='disposable')
48 self.container.start()
49 self.addCleanup(self.container.stop)
50 self.addCleanup(self.hash.stop)
51
52 def request(self, path, method='GET', body=None, timeout=10):
53 self.assertTrue(path.startswith('var/nomad/jobs/copyparty'))
54 if method == 'GET':
55 return copy.deepcopy(self.variable)
56 self.assertEqual(method, 'PUT')
57 self.assertEqual(path.split('?cas=')[1], str(self.variable['ModifyIndex']))
58 self.variable = {'Items': copy.deepcopy(body['Items']), 'ModifyIndex': self.variable['ModifyIndex'] + 1}
59 return copy.deepcopy(self.variable)
60
61 def call(self, action, **payload):
62 return shares.handle({'action': action, 'payload': payload, 'users': self.users})
63
64 def link(self, key='public', path='clover/source', count=0, files=(), owner='snow'):
65 with sqlite3.connect(self.database) as db:
66 db.execute('INSERT INTO sh VALUES(?,?,?,?,?,?,?,?)', (key, '', path, 'rw', count, owner, 100, 0))
67 db.executemany('INSERT INTO sf VALUES(?,?)', [(key, file) for file in files])
68
69 def update(self, path, mode='retarget', key='public'):
70 return self.call('link.update', key=key, name='Clover - Example', path=str(path), permissions='rw', expires=0, password=None, mode=mode)
71
72 def offline(self):
73 def stop(manager):
74 manager.job = {'ID': 'copyparty'}
75 manager.offline_at = shares.time.monotonic()
76 manager.stopped = True
77 def start(manager):
78 manager.stopped = False
79 return patch.object(shares.Manager, 'stop', autospec=True, side_effect=stop), patch.object(shares.Manager, 'start', autospec=True, side_effect=start)
80
81 def test_account_password_is_once_only_and_nomad_preserves_other_secrets(self):
82 created = self.call('account.create', name='Local AI')
83 self.assertTrue(created['password'])
84 identifier = created['account']['id']
85 key = 'file_api_' + identifier.replace('-', '')
86 self.assertEqual(self.variable['Items'][key], '+' + 'a' * 32)
87 self.assertEqual(self.variable['Items']['unrelated'], 'retained')
88 state = self.call('state')
89 self.assertNotIn('password', state)
90 self.assertNotIn(created['password'], (self.cfg / 'manager.json').read_text())
91 self.assertNotIn(created['password'], (self.cfg / 'copyparty.conf').read_text())
92 self.assertIn('g: *,-api-' + identifier, (self.cfg / 'copyparty.conf').read_text())
93 self.call('account.delete', id=identifier)
94 self.assertNotIn(key, self.variable['Items'])
95
96 def test_rotation_revokes_existing_copyparty_session(self):
97 created = self.call('account.create', name='Local AI')
98 identifier = created['account']['id']
99 session_path = self.cfg / 'copyparty/sessions.db'
100 with sqlite3.connect(session_path) as db:
101 db.executescript('CREATE TABLE us(un text,si text,t0 int);')
102 db.execute('INSERT INTO us VALUES(?,?,?)', ('api-' + identifier, 'old-cookie', 100))
103 db.execute('INSERT INTO us VALUES(?,?,?)', ('snow', 'retained-cookie', 100))
104 self.call('account.rotate', id=identifier)
105 with sqlite3.connect(session_path) as db:
106 self.assertEqual(db.execute('SELECT un,si FROM us').fetchall(), [('snow', 'retained-cookie')])
107
108 def test_human_grant_uses_uuid_group_and_preserves_ancestor_access(self):
109 folder = self.root / 'work'
110 child = folder / 'child'
111 child.mkdir(parents=True)
112 first = self.call('grant.save', id=None, subject='user:' + self.subject, path=str(folder), permissions='rw')
113 self.call('grant.save', id=None, subject='user:' + self.subject, path=str(child), permissions='r')
114 config = (self.cfg / 'copyparty.conf').read_text()
115 self.assertIn('rw: @snow-user-' + self.subject, config)
116 self.assertNotIn('test-user', config)
117 self.assertIn('xvol', config)
118 child_config = config.split('[/clover/work/child]')[1]
119 self.assertIn('A: snow', child_config)
120 self.assertIn('rw: @snow-user-' + self.subject, child_config)
121 self.call('grant.delete', id=first['grants'][0]['id'])
122 config = (self.cfg / 'copyparty.conf').read_text()
123 self.assertNotIn('[/clover/work]\n', config)
124 self.assertNotIn('rw: @snow-user-' + self.subject, config)
125
126 def test_exact_base_mount_is_rendered_once(self):
127 self.call('grant.save', id=None, subject='user:' + self.subject, path=str(self.root), permissions='d')
128 config = (self.cfg / 'copyparty.conf').read_text()
129 self.assertEqual(config.count('[/clover]'), 1)
130 clover = config.split('[/clover]')[1].split('[/media]')[0]
131 self.assertIn('A: snow', clover)
132 self.assertIn('d: @snow-user-' + self.subject, clover)
133
134 def test_untrusted_boundary_and_symlink_escape_reject_before_nomad(self):
135 folder = self.root / 'work'
136 folder.mkdir()
137 (self.root / 'escape').symlink_to(Path(self.temporary.name))
138 cases = [dict(id=None, subject='user:' + self.subject, path=str(self.root / 'escape'), permissions='r'),
139 dict(id=None, subject='user:' + self.subject, path=str(self.root / '../outside'), permissions='r'),
140 dict(id=None, subject='user:' + self.subject, path=str(folder), permissions='x'),
141 dict(id=None, subject='user:' + str(uuid.uuid4()), path=str(folder), permissions='r')]
142 for payload in cases:
143 with self.subTest(payload=payload), self.assertRaises(shares.Error):
144 self.call('grant.save', **payload)
145 with self.assertRaises(shares.Error):
146 self.call('state', extra=True)
147 for user in [dict(self.users[0], enabled=False), dict(self.users[0], kind='ai'), dict(self.users[0], kind='guest')]:
148 self.users = [user]
149 with self.assertRaises(shares.Error):
150 self.call('grant.save', id=None, subject='user:' + self.subject, path=str(folder), permissions='r')
151
152 def test_pending_invited_user_can_receive_permissions_before_setup(self):
153 folder = self.root / 'invited-work'
154 folder.mkdir()
155 self.users[0]['requiredActions'] = ['SETUP', 'UPDATE_PASSWORD']
156 state = self.call('grant.save', id=None, subject='user:' + self.subject, path=str(folder), permissions='rw')
157 self.assertEqual(state['grants'][0]['subject'], 'user:' + self.subject)
158 self.assertEqual(state['grants'][0]['path'], str(folder))
159 self.assertIn('rw: @snow-user-' + self.subject, (self.cfg / 'copyparty.conf').read_text())
160
161 def test_retarget_preserves_selected_files_and_rejects_missing_selection(self):
162 destination = self.root / 'destination'
163 destination.mkdir()
164 (destination / 'kept.txt').write_text('disposable')
165 self.link(count=1, files=['kept.txt'])
166 self.update(destination)
167 state = self.call('state')
168 self.assertEqual(state['links'][0]['files'], ['kept.txt'])
169 self.assertEqual(state['links'][0]['path'], str(destination))
170 missing = self.root / 'missing'
171 missing.mkdir()
172 with self.assertRaises(shares.Error):
173 self.update(missing)
174 self.assertEqual(self.call('state')['links'][0]['path'], str(destination))
175
176 def test_default_folder_create_and_link_delete_keep_data(self):
177 result = self.call('link.create', name='Clover - Example', path='', permissions='rw', expires=0, password='')
178 path = self.root / 'Share/Clover - Example'
179 self.assertTrue(path.is_dir())
180 self.call('link.delete', key=result['links'][0]['key'])
181 self.assertTrue(path.is_dir())
182 self.assertEqual(self.call('state')['links'], [])
183
184 def test_link_create_reuses_existing_folder(self):
185 path = self.root / 'Share/Clover - Existing'
186 path.mkdir(parents=True)
187 (path / 'kept.txt').write_text('disposable')
188 self.call('link.create', name='Clover - Existing', path='', permissions='r', expires=0, password='')
189 self.assertEqual(self.call('state')['links'][0]['path'], str(path))
190 self.assertTrue((path / 'kept.txt').is_file())
191
192 def test_move_preserves_key_selection_nested_links_and_grants(self):
193 source = self.root / 'source'
194 (source / 'nested').mkdir(parents=True)
195 (source / 'kept.txt').write_text('disposable')
196 destination = self.root / 'destination'
197 self.link(count=1, files=['kept.txt'])
198 self.link(key='nested', path='clover/source/nested')
199 self.call('grant.save', id=None, subject='user:' + self.subject, path=str(source / 'nested'), permissions='rw')
200 stop, start = self.offline()
201 with stop as stopped, start as started:
202 self.update(destination, mode='move')
203 stopped.assert_called_once()
204 started.assert_called_once()
205 self.assertFalse(source.exists())
206 self.assertEqual((destination / 'kept.txt').read_text(), 'disposable')
207 state = self.call('state')
208 self.assertEqual({link['key']: link['path'] for link in state['links']}, {'public': str(destination), 'nested': str(destination / 'nested')})
209 self.assertEqual(state['grants'][0]['path'], str(destination / 'nested'))
210 self.assertEqual(next(link for link in state['links'] if link['key'] == 'public')['files'], ['kept.txt'])
211
212 def test_move_rejects_existing_destination_and_ancestor_selection(self):
213 source = self.root / 'source'
214 source.mkdir()
215 destination = self.root / 'destination'
216 destination.mkdir()
217 self.link()
218 with self.assertRaises(shares.Error):
219 self.update(destination, mode='move')
220 destination.rmdir()
221 self.link(key='ancestor', path='clover', count=0)
222 with self.assertRaises(shares.Error):
223 self.update(destination, mode='move')
224 self.assertTrue(source.is_dir())
225 self.assertFalse(destination.exists())
226
227 def test_reload_failure_restores_config_state_database_and_secret(self):
228 original = (self.cfg / 'copyparty.conf').read_bytes()
229 calls = 0
230 def reload(manager, items):
231 nonlocal calls
232 calls += 1
233 if calls == 1:
234 raise shares.Error(502, 'test rejection')
235 self.reload_mock.side_effect = reload
236 with self.assertRaises(shares.Error):
237 self.call('account.create', name='Local AI')
238 self.reload_mock.side_effect = lambda manager, items, **kwargs: self.reloads.append(copy.deepcopy(items))
239 self.assertEqual((self.cfg / 'copyparty.conf').read_bytes(), original)
240 self.assertEqual(self.call('state')['accounts'], [])
241 self.assertEqual(self.variable['Items'], {'idp_header': 'a' * 32, 'unrelated': 'retained'})
242 self.assertEqual(calls, 2)
243 destination = self.root / 'target'
244 destination.mkdir()
245 self.link()
246 self.reload_mock.side_effect = [shares.Error(502, 'test rejection'), None]
247 with self.assertRaises(shares.Error):
248 self.update(destination)
249 self.assertEqual(self.call('state')['links'][0]['path'], str(self.root / 'source'))
250
251 def test_failed_offline_restart_restores_folder_and_mappings(self):
252 source = self.root / 'source'
253 source.mkdir()
254 destination = self.root / 'destination'
255 self.link()
256 stop, start = self.offline()
257 with stop as stopped, patch.object(shares.Manager, 'start', autospec=True, side_effect=[shares.Error(502, 'test restart'), None]) as started:
258 with self.assertRaises(shares.Error):
259 self.update(destination, mode='move')
260 self.assertEqual(stopped.call_count, 2)
261 self.assertEqual(started.call_count, 2)
262 self.assertTrue(source.is_dir())
263 self.assertFalse(destination.exists())
264 self.assertEqual(self.call('state')['links'][0]['path'], str(source))
265
266 def test_new_folders_work_under_host_private_umask(self):
267 previous = os.umask(0o077)
268 try:
269 self.call('link.create', name='Clover - Restricted', path=str(self.root / 'Share/Clover - Restricted'),
270 permissions='r', expires=0, password='')
271 finally:
272 os.umask(previous)
273 self.assertEqual((self.root / 'Share').stat().st_mode & 0o777, 0o775)
274 self.assertEqual((self.root / 'Share/Clover - Restricted').stat().st_mode & 0o777, 0o775)
275
276 def test_api_scope_excludes_wildcards_until_ancestor_grant(self):
277 source = Path(self.temporary.name) / 'base.conf'
278 source.write_text(Path(shares.__file__).with_name('copyparty.conf').read_text() +
279 '\n[/clover/scoped]\n /w/clover/scoped\n accs:\n r: *\n w: @acct\n')
280 (self.root / 'scoped').mkdir()
281 with patch.object(shares, 'configuration', partial(shares.configuration, source=source)):
282 account = self.call('account.create', name='Scoped')['account']
283 config = (self.cfg / 'copyparty.conf').read_text()
284 scoped = config.split('[/clover/scoped]')[1]
285 self.assertIn('r: *,-' + account['username'], scoped)
286 self.assertIn('w: @acct,-' + account['username'], scoped)
287 self.call('grant.save', id=None, subject=account['subject'], path=str(self.root), permissions='r')
288 config = (self.cfg / 'copyparty.conf').read_text()
289 scoped = config.split('[/clover/scoped]')[1]
290 self.assertIn('r: *\n', scoped)
291 self.assertIn('w: @acct,-' + account['username'], scoped)
292 self.assertIn('r: ' + account['username'], scoped)
293 logs = config.split('[/logs]')[1].split('[/clover/scoped]')[0]
294 self.assertIn('g: *,-' + account['username'], logs)
295 manager = shares.Manager(self.users)
296 manager.data = json.loads((self.cfg / 'manager.json').read_text())
297 manager.items = self.variable['Items']
298 manager.owner_permissions(account['username'], self.root / 'scoped', 'r')
299 with self.assertRaises(shares.Error):
300 manager.owner_permissions(account['username'], self.root / 'scoped', 'w')
301
302 def test_admin_can_retarget_other_owner_with_destination_permissions(self):
303 destination = self.root / 'other'
304 destination.mkdir()
305 self.call('grant.save', id=None, subject='user:' + self.subject, path=str(destination), permissions='rw')
306 self.link(owner='test-user')
307 self.update(destination)
308 self.assertEqual(self.call('state')['links'][0]['owner'], 'test-user')
309 denied = self.root / 'denied'
310 denied.mkdir()
311 with self.assertRaises(shares.Error):
312 self.update(denied)
313 self.assertEqual(self.call('state')['links'][0]['path'], str(destination))
314
315 def test_stage_rebases_grants_without_changing_native_virtual_sources(self):
316 folder = self.root / 'work'
317 folder.mkdir()
318 self.call('grant.save', id=None, subject='user:' + self.subject, path=str(folder), permissions='r')
319 self.link()
320 clone = self.root.parent / 'clover-clone'
321 clone.mkdir()
322 (clone / 'work').mkdir()
323 with patch.dict(os.environ, {'SNOWGLOBE_CLOVER_ROOT': str(clone)}):
324 state = self.call('state')
325 self.assertEqual(state['grants'][0]['path'], str(clone / 'work'))
326 self.assertEqual(state['links'][0]['path'], str(clone / 'source'))
327
328 def test_no_clobber_rename_keeps_both_folders(self):
329 source = self.root / 'source'
330 destination = self.root / 'destination'
331 source.mkdir()
332 destination.mkdir()
333 with self.assertRaises(shares.Error):
334 shares.rename(source, destination)
335 self.assertTrue(source.is_dir())
336 self.assertTrue(destination.is_dir())
337
338
339if __name__ == '__main__':
340 unittest.main()