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='', createFolder=True)
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_move_preserves_key_selection_nested_links_and_grants(self):
185 source = self.root / 'source'
186 (source / 'nested').mkdir(parents=True)
187 (source / 'kept.txt').write_text('disposable')
188 destination = self.root / 'destination'
189 self.link(count=1, files=['kept.txt'])
190 self.link(key='nested', path='clover/source/nested')
191 self.call('grant.save', id=None, subject='user:' + self.subject, path=str(source / 'nested'), permissions='rw')
192 stop, start = self.offline()
193 with stop as stopped, start as started:
194 self.update(destination, mode='move')
195 stopped.assert_called_once()
196 started.assert_called_once()
197 self.assertFalse(source.exists())
198 self.assertEqual((destination / 'kept.txt').read_text(), 'disposable')
199 state = self.call('state')
200 self.assertEqual({link['key']: link['path'] for link in state['links']}, {'public': str(destination), 'nested': str(destination / 'nested')})
201 self.assertEqual(state['grants'][0]['path'], str(destination / 'nested'))
202 self.assertEqual(next(link for link in state['links'] if link['key'] == 'public')['files'], ['kept.txt'])
203
204 def test_move_rejects_existing_destination_and_ancestor_selection(self):
205 source = self.root / 'source'
206 source.mkdir()
207 destination = self.root / 'destination'
208 destination.mkdir()
209 self.link()
210 with self.assertRaises(shares.Error):
211 self.update(destination, mode='move')
212 destination.rmdir()
213 self.link(key='ancestor', path='clover', count=0)
214 with self.assertRaises(shares.Error):
215 self.update(destination, mode='move')
216 self.assertTrue(source.is_dir())
217 self.assertFalse(destination.exists())
218
219 def test_reload_failure_restores_config_state_database_and_secret(self):
220 original = (self.cfg / 'copyparty.conf').read_bytes()
221 calls = 0
222 def reload(manager, items):
223 nonlocal calls
224 calls += 1
225 if calls == 1:
226 raise shares.Error(502, 'test rejection')
227 self.reload_mock.side_effect = reload
228 with self.assertRaises(shares.Error):
229 self.call('account.create', name='Local AI')
230 self.reload_mock.side_effect = lambda manager, items, **kwargs: self.reloads.append(copy.deepcopy(items))
231 self.assertEqual((self.cfg / 'copyparty.conf').read_bytes(), original)
232 self.assertEqual(self.call('state')['accounts'], [])
233 self.assertEqual(self.variable['Items'], {'idp_header': 'a' * 32, 'unrelated': 'retained'})
234 self.assertEqual(calls, 2)
235 destination = self.root / 'target'
236 destination.mkdir()
237 self.link()
238 self.reload_mock.side_effect = [shares.Error(502, 'test rejection'), None]
239 with self.assertRaises(shares.Error):
240 self.update(destination)
241 self.assertEqual(self.call('state')['links'][0]['path'], str(self.root / 'source'))
242
243 def test_failed_offline_restart_restores_folder_and_mappings(self):
244 source = self.root / 'source'
245 source.mkdir()
246 destination = self.root / 'destination'
247 self.link()
248 stop, start = self.offline()
249 with stop as stopped, patch.object(shares.Manager, 'start', autospec=True, side_effect=[shares.Error(502, 'test restart'), None]) as started:
250 with self.assertRaises(shares.Error):
251 self.update(destination, mode='move')
252 self.assertEqual(stopped.call_count, 2)
253 self.assertEqual(started.call_count, 2)
254 self.assertTrue(source.is_dir())
255 self.assertFalse(destination.exists())
256 self.assertEqual(self.call('state')['links'][0]['path'], str(source))
257
258 def test_new_folders_work_under_host_private_umask(self):
259 previous = os.umask(0o077)
260 try:
261 self.call('link.create', name='Clover - Restricted', path=str(self.root / 'Share/Clover - Restricted'),
262 permissions='r', expires=0, password='', createFolder=True)
263 finally:
264 os.umask(previous)
265 self.assertEqual((self.root / 'Share').stat().st_mode & 0o777, 0o775)
266 self.assertEqual((self.root / 'Share/Clover - Restricted').stat().st_mode & 0o777, 0o775)
267
268 def test_api_scope_excludes_wildcards_until_ancestor_grant(self):
269 source = Path(self.temporary.name) / 'base.conf'
270 source.write_text(Path(shares.__file__).with_name('copyparty.conf').read_text() +
271 '\n[/clover/scoped]\n /w/clover/scoped\n accs:\n r: *\n w: @acct\n')
272 (self.root / 'scoped').mkdir()
273 with patch.object(shares, 'configuration', partial(shares.configuration, source=source)):
274 account = self.call('account.create', name='Scoped')['account']
275 config = (self.cfg / 'copyparty.conf').read_text()
276 scoped = config.split('[/clover/scoped]')[1]
277 self.assertIn('r: *,-' + account['username'], scoped)
278 self.assertIn('w: @acct,-' + account['username'], scoped)
279 self.call('grant.save', id=None, subject=account['subject'], path=str(self.root), permissions='r')
280 config = (self.cfg / 'copyparty.conf').read_text()
281 scoped = config.split('[/clover/scoped]')[1]
282 self.assertIn('r: *\n', scoped)
283 self.assertIn('w: @acct,-' + account['username'], scoped)
284 self.assertIn('r: ' + account['username'], scoped)
285 logs = config.split('[/logs]')[1].split('[/clover/scoped]')[0]
286 self.assertIn('g: *,-' + account['username'], logs)
287 manager = shares.Manager(self.users)
288 manager.data = json.loads((self.cfg / 'manager.json').read_text())
289 manager.items = self.variable['Items']
290 manager.owner_permissions(account['username'], self.root / 'scoped', 'r')
291 with self.assertRaises(shares.Error):
292 manager.owner_permissions(account['username'], self.root / 'scoped', 'w')
293
294 def test_admin_can_retarget_other_owner_with_destination_permissions(self):
295 destination = self.root / 'other'
296 destination.mkdir()
297 self.call('grant.save', id=None, subject='user:' + self.subject, path=str(destination), permissions='rw')
298 self.link(owner='test-user')
299 self.update(destination)
300 self.assertEqual(self.call('state')['links'][0]['owner'], 'test-user')
301 denied = self.root / 'denied'
302 denied.mkdir()
303 with self.assertRaises(shares.Error):
304 self.update(denied)
305 self.assertEqual(self.call('state')['links'][0]['path'], str(destination))
306
307 def test_stage_rebases_grants_without_changing_native_virtual_sources(self):
308 folder = self.root / 'work'
309 folder.mkdir()
310 self.call('grant.save', id=None, subject='user:' + self.subject, path=str(folder), permissions='r')
311 self.link()
312 clone = self.root.parent / 'clover-clone'
313 clone.mkdir()
314 (clone / 'work').mkdir()
315 with patch.dict(os.environ, {'SNOWGLOBE_CLOVER_ROOT': str(clone)}):
316 state = self.call('state')
317 self.assertEqual(state['grants'][0]['path'], str(clone / 'work'))
318 self.assertEqual(state['links'][0]['path'], str(clone / 'source'))
319
320 def test_no_clobber_rename_keeps_both_folders(self):
321 source = self.root / 'source'
322 destination = self.root / 'destination'
323 source.mkdir()
324 destination.mkdir()
325 with self.assertRaises(shares.Error):
326 shares.rename(source, destination)
327 self.assertTrue(source.is_dir())
328 self.assertTrue(destination.is_dir())
329
330
331if __name__ == '__main__':
332 unittest.main()