Coverage for downtify/trash_worker.py: 88%

82 statements  

« prev     ^ index     » next       coverage.py v7.17.4, created at 2026-11-21 22:18 +0000

1"""Bounded filesystem worker; run separately with only allowlisted mounts. 

2 

3For a configuration with ``"worker": "external"``: the server then only 

4records what to do, and this does it. No HTTP listener, network access, 

5Docker socket or application startup. The auth database is opened 

6read-only to recheck administrator rights. 

7 

8SQLite (default): ``--database`` and ``--auth-database`` are required 

9files. The auth database is opened with a read-only SQLite URI. 

10 

11PostgreSQL: when the ``DATABASE_URL`` environment variable is set, 

12``--database`` and ``--auth-database`` are optional and ignored; the 

13library and auth stores come from ``--data-dir`` (default ``DATABASE_DIR`` 

14or ``/data``) via ``downtify.db.runtime``. The admin recheck runs in a 

15read-only transaction. 

16""" 

17 

18from __future__ import annotations 

19 

20import argparse 

21import json 

22import os 

23import sqlite3 

24import sys 

25from pathlib import Path 

26from typing import Any, Callable 

27 

28from downtify.db import runtime 

29from downtify.db.config import Dialect 

30from downtify.db.core import Database 

31 

32from .library_trash import trash_from_config 

33 

34 

35def make_authorizer( 

36 auth_target: Path | str | Database, 

37) -> Callable[[Any], bool]: 

38 """Return ``authorized(user_id) -> bool`` for the admin recheck. 

39 

40 A file path (or a SQLite ``Database``) is read through a read-only 

41 SQLite URI. A PostgreSQL ``Database`` is read inside a transaction 

42 set to READ ONLY. The connection must not already be inside a 

43 transaction on this thread (``SET TRANSACTION`` runs first). 

44 """ 

45 if isinstance(auth_target, Database): 

46 if auth_target.dialect is Dialect.POSTGRES: 

47 return _postgres_authorizer(auth_target) 

48 auth_path = auth_target.path 

49 assert auth_path is not None 

50 else: 

51 auth_path = Path(auth_target) 

52 return _sqlite_authorizer(auth_path) 

53 

54 

55def _sqlite_authorizer(auth_path: Path) -> Callable[[Any], bool]: 

56 def authorized(user_id): 

57 uri = auth_path.resolve().as_uri() + '?mode=ro' 

58 conn = sqlite3.connect(uri, uri=True) 

59 try: 

60 row = conn.execute( 

61 'SELECT role FROM users WHERE id=?', (user_id,) 

62 ).fetchone() 

63 return bool(row and row[0] == 'admin') 

64 finally: 

65 conn.close() 

66 

67 return authorized 

68 

69 

70def _postgres_authorizer(db: Database) -> Callable[[Any], bool]: 

71 def authorized(user_id): 

72 with db.connect() as conn: 

73 conn.execute('SET TRANSACTION READ ONLY') 

74 row = conn.execute( 

75 'SELECT role FROM users WHERE id=?', (user_id,) 

76 ).fetchone() 

77 return bool(row and row[0] == 'admin') 

78 

79 return authorized 

80 

81 

82def _database_url_set() -> bool: 

83 return bool((os.environ.get('DATABASE_URL') or '').strip()) 

84 

85 

86def main(): 

87 parser = argparse.ArgumentParser( 

88 epilog=( 

89 'With DATABASE_URL set (PostgreSQL), --database and ' 

90 '--auth-database are optional and ignored; --data-dir is only ' 

91 'used for the database group locations.' 

92 ) 

93 ) 

94 parser.add_argument('--config', type=Path, required=True) 

95 parser.add_argument( 

96 '--database', 

97 type=Path, 

98 help='SQLite library file (ignored when DATABASE_URL is set)', 

99 ) 

100 parser.add_argument( 

101 '--auth-database', 

102 type=Path, 

103 help='SQLite auth file, opened read-only (ignored when ' 

104 'DATABASE_URL is set)', 

105 ) 

106 parser.add_argument( 

107 '--data-dir', 

108 type=Path, 

109 default=None, 

110 help='directory for database groups; default DATABASE_DIR or /data', 

111 ) 

112 parser.add_argument('--limit', type=int, default=5) 

113 parser.add_argument('--skip-id', action='append', default=[]) 

114 options = parser.parse_args() 

115 if not _database_url_set() and ( 

116 options.database is None or options.auth_database is None 

117 ): 

118 parser.error( 

119 'the following arguments are required: --database, --auth-database' 

120 ) 

121 try: 

122 if _database_url_set(): 

123 result = _run_postgres(options) 

124 else: 

125 result = _run_sqlite(options) 

126 finally: 

127 runtime.shutdown() 

128 print(json.dumps(result)) 

129 

130 

131def _run_sqlite(options) -> dict: 

132 if not options.auth_database.is_file() or not options.database.is_file(): 

133 raise SystemExit('required_database_unavailable') 

134 store = trash_from_config(options.config, options.database) 

135 _note_if_embedded(store) 

136 authorized = make_authorizer(options.auth_database) 

137 rows = store.process( 

138 limit=_limit(options.limit), 

139 authorized=authorized, 

140 skip_ids=frozenset(options.skip_id), 

141 ) 

142 return _summary(rows) 

143 

144 

145def _run_postgres(options) -> dict: 

146 data_dir = options.data_dir 

147 if data_dir is None: 

148 data_dir = Path(os.environ.get('DATABASE_DIR') or '/data') 

149 library = runtime.db_target('library', data_dir) 

150 auth = runtime.db_target('auth', data_dir) 

151 store = trash_from_config(options.config, library) 

152 _note_if_embedded(store) 

153 rows = store.process( 

154 limit=_limit(options.limit), 

155 authorized=make_authorizer(auth), 

156 skip_ids=frozenset(options.skip_id), 

157 ) 

158 return _summary(rows) 

159 

160 

161def _limit(value: int) -> int: 

162 return max(1, min(value, 20)) 

163 

164 

165def _summary(result: list[dict]) -> dict: 

166 return { 

167 'processed': len(result), 

168 'succeeded': sum(row['ok'] for row in result), 

169 'failed': sum(not row['ok'] for row in result), 

170 } 

171 

172 

173def _note_if_embedded(store) -> None: 

174 if store.worker != 'external': 

175 # Harmless (one worker at a time), but the server already does it. 

176 print( 

177 'note: this configuration runs the worker inside the server; ' 

178 'set "worker": "external" to run it here instead', 

179 file=sys.stderr, 

180 ) 

181 

182 

183if __name__ == '__main__': 

184 main()