|
| 1 | +# Copyright 2021 The Matrix.org Foundation C.I.C. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import logging |
| 16 | +from typing import List, Optional |
| 17 | + |
| 18 | +import attr |
| 19 | + |
| 20 | +from synapse.config._base import Config, ConfigError |
| 21 | + |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +@attr.s(slots=True, frozen=True, auto_attribs=True) |
| 26 | +class RetentionPurgeJob: |
| 27 | + """Object describing the configuration of the manhole""" |
| 28 | + |
| 29 | + interval: int |
| 30 | + shortest_max_lifetime: Optional[int] |
| 31 | + longest_max_lifetime: Optional[int] |
| 32 | + |
| 33 | + |
| 34 | +class RetentionConfig(Config): |
| 35 | + section = "retention" |
| 36 | + |
| 37 | + def read_config(self, config, **kwargs): |
| 38 | + retention_config = config.get("retention") |
| 39 | + if retention_config is None: |
| 40 | + retention_config = {} |
| 41 | + |
| 42 | + self.retention_enabled = retention_config.get("enabled", False) |
| 43 | + |
| 44 | + retention_default_policy = retention_config.get("default_policy") |
| 45 | + |
| 46 | + if retention_default_policy is not None: |
| 47 | + self.retention_default_min_lifetime = retention_default_policy.get( |
| 48 | + "min_lifetime" |
| 49 | + ) |
| 50 | + if self.retention_default_min_lifetime is not None: |
| 51 | + self.retention_default_min_lifetime = self.parse_duration( |
| 52 | + self.retention_default_min_lifetime |
| 53 | + ) |
| 54 | + |
| 55 | + self.retention_default_max_lifetime = retention_default_policy.get( |
| 56 | + "max_lifetime" |
| 57 | + ) |
| 58 | + if self.retention_default_max_lifetime is not None: |
| 59 | + self.retention_default_max_lifetime = self.parse_duration( |
| 60 | + self.retention_default_max_lifetime |
| 61 | + ) |
| 62 | + |
| 63 | + if ( |
| 64 | + self.retention_default_min_lifetime is not None |
| 65 | + and self.retention_default_max_lifetime is not None |
| 66 | + and ( |
| 67 | + self.retention_default_min_lifetime |
| 68 | + > self.retention_default_max_lifetime |
| 69 | + ) |
| 70 | + ): |
| 71 | + raise ConfigError( |
| 72 | + "The default retention policy's 'min_lifetime' can not be greater" |
| 73 | + " than its 'max_lifetime'" |
| 74 | + ) |
| 75 | + else: |
| 76 | + self.retention_default_min_lifetime = None |
| 77 | + self.retention_default_max_lifetime = None |
| 78 | + |
| 79 | + if self.retention_enabled: |
| 80 | + logger.info( |
| 81 | + "Message retention policies support enabled with the following default" |
| 82 | + " policy: min_lifetime = %s ; max_lifetime = %s", |
| 83 | + self.retention_default_min_lifetime, |
| 84 | + self.retention_default_max_lifetime, |
| 85 | + ) |
| 86 | + |
| 87 | + self.retention_allowed_lifetime_min = retention_config.get( |
| 88 | + "allowed_lifetime_min" |
| 89 | + ) |
| 90 | + if self.retention_allowed_lifetime_min is not None: |
| 91 | + self.retention_allowed_lifetime_min = self.parse_duration( |
| 92 | + self.retention_allowed_lifetime_min |
| 93 | + ) |
| 94 | + |
| 95 | + self.retention_allowed_lifetime_max = retention_config.get( |
| 96 | + "allowed_lifetime_max" |
| 97 | + ) |
| 98 | + if self.retention_allowed_lifetime_max is not None: |
| 99 | + self.retention_allowed_lifetime_max = self.parse_duration( |
| 100 | + self.retention_allowed_lifetime_max |
| 101 | + ) |
| 102 | + |
| 103 | + if ( |
| 104 | + self.retention_allowed_lifetime_min is not None |
| 105 | + and self.retention_allowed_lifetime_max is not None |
| 106 | + and self.retention_allowed_lifetime_min |
| 107 | + > self.retention_allowed_lifetime_max |
| 108 | + ): |
| 109 | + raise ConfigError( |
| 110 | + "Invalid retention policy limits: 'allowed_lifetime_min' can not be" |
| 111 | + " greater than 'allowed_lifetime_max'" |
| 112 | + ) |
| 113 | + |
| 114 | + self.retention_purge_jobs: List[RetentionPurgeJob] = [] |
| 115 | + for purge_job_config in retention_config.get("purge_jobs", []): |
| 116 | + interval_config = purge_job_config.get("interval") |
| 117 | + |
| 118 | + if interval_config is None: |
| 119 | + raise ConfigError( |
| 120 | + "A retention policy's purge jobs configuration must have the" |
| 121 | + " 'interval' key set." |
| 122 | + ) |
| 123 | + |
| 124 | + interval = self.parse_duration(interval_config) |
| 125 | + |
| 126 | + shortest_max_lifetime = purge_job_config.get("shortest_max_lifetime") |
| 127 | + |
| 128 | + if shortest_max_lifetime is not None: |
| 129 | + shortest_max_lifetime = self.parse_duration(shortest_max_lifetime) |
| 130 | + |
| 131 | + longest_max_lifetime = purge_job_config.get("longest_max_lifetime") |
| 132 | + |
| 133 | + if longest_max_lifetime is not None: |
| 134 | + longest_max_lifetime = self.parse_duration(longest_max_lifetime) |
| 135 | + |
| 136 | + if ( |
| 137 | + shortest_max_lifetime is not None |
| 138 | + and longest_max_lifetime is not None |
| 139 | + and shortest_max_lifetime > longest_max_lifetime |
| 140 | + ): |
| 141 | + raise ConfigError( |
| 142 | + "A retention policy's purge jobs configuration's" |
| 143 | + " 'shortest_max_lifetime' value can not be greater than its" |
| 144 | + " 'longest_max_lifetime' value." |
| 145 | + ) |
| 146 | + |
| 147 | + self.retention_purge_jobs.append( |
| 148 | + RetentionPurgeJob(interval, shortest_max_lifetime, longest_max_lifetime) |
| 149 | + ) |
| 150 | + |
| 151 | + if not self.retention_purge_jobs: |
| 152 | + self.retention_purge_jobs = [ |
| 153 | + RetentionPurgeJob(self.parse_duration("1d"), None, None) |
| 154 | + ] |
| 155 | + |
| 156 | + def generate_config_section(self, config_dir_path, server_name, **kwargs): |
| 157 | + return """\ |
| 158 | + # Message retention policy at the server level. |
| 159 | + # |
| 160 | + # Room admins and mods can define a retention period for their rooms using the |
| 161 | + # 'm.room.retention' state event, and server admins can cap this period by setting |
| 162 | + # the 'allowed_lifetime_min' and 'allowed_lifetime_max' config options. |
| 163 | + # |
| 164 | + # If this feature is enabled, Synapse will regularly look for and purge events |
| 165 | + # which are older than the room's maximum retention period. Synapse will also |
| 166 | + # filter events received over federation so that events that should have been |
| 167 | + # purged are ignored and not stored again. |
| 168 | + # |
| 169 | + retention: |
| 170 | + # The message retention policies feature is disabled by default. Uncomment the |
| 171 | + # following line to enable it. |
| 172 | + # |
| 173 | + #enabled: true |
| 174 | +
|
| 175 | + # Default retention policy. If set, Synapse will apply it to rooms that lack the |
| 176 | + # 'm.room.retention' state event. Currently, the value of 'min_lifetime' doesn't |
| 177 | + # matter much because Synapse doesn't take it into account yet. |
| 178 | + # |
| 179 | + #default_policy: |
| 180 | + # min_lifetime: 1d |
| 181 | + # max_lifetime: 1y |
| 182 | +
|
| 183 | + # Retention policy limits. If set, and the state of a room contains a |
| 184 | + # 'm.room.retention' event in its state which contains a 'min_lifetime' or a |
| 185 | + # 'max_lifetime' that's out of these bounds, Synapse will cap the room's policy |
| 186 | + # to these limits when running purge jobs. |
| 187 | + # |
| 188 | + #allowed_lifetime_min: 1d |
| 189 | + #allowed_lifetime_max: 1y |
| 190 | +
|
| 191 | + # Server admins can define the settings of the background jobs purging the |
| 192 | + # events which lifetime has expired under the 'purge_jobs' section. |
| 193 | + # |
| 194 | + # If no configuration is provided, a single job will be set up to delete expired |
| 195 | + # events in every room daily. |
| 196 | + # |
| 197 | + # Each job's configuration defines which range of message lifetimes the job |
| 198 | + # takes care of. For example, if 'shortest_max_lifetime' is '2d' and |
| 199 | + # 'longest_max_lifetime' is '3d', the job will handle purging expired events in |
| 200 | + # rooms whose state defines a 'max_lifetime' that's both higher than 2 days, and |
| 201 | + # lower than or equal to 3 days. Both the minimum and the maximum value of a |
| 202 | + # range are optional, e.g. a job with no 'shortest_max_lifetime' and a |
| 203 | + # 'longest_max_lifetime' of '3d' will handle every room with a retention policy |
| 204 | + # which 'max_lifetime' is lower than or equal to three days. |
| 205 | + # |
| 206 | + # The rationale for this per-job configuration is that some rooms might have a |
| 207 | + # retention policy with a low 'max_lifetime', where history needs to be purged |
| 208 | + # of outdated messages on a more frequent basis than for the rest of the rooms |
| 209 | + # (e.g. every 12h), but not want that purge to be performed by a job that's |
| 210 | + # iterating over every room it knows, which could be heavy on the server. |
| 211 | + # |
| 212 | + # If any purge job is configured, it is strongly recommended to have at least |
| 213 | + # a single job with neither 'shortest_max_lifetime' nor 'longest_max_lifetime' |
| 214 | + # set, or one job without 'shortest_max_lifetime' and one job without |
| 215 | + # 'longest_max_lifetime' set. Otherwise some rooms might be ignored, even if |
| 216 | + # 'allowed_lifetime_min' and 'allowed_lifetime_max' are set, because capping a |
| 217 | + # room's policy to these values is done after the policies are retrieved from |
| 218 | + # Synapse's database (which is done using the range specified in a purge job's |
| 219 | + # configuration). |
| 220 | + # |
| 221 | + #purge_jobs: |
| 222 | + # - longest_max_lifetime: 3d |
| 223 | + # interval: 12h |
| 224 | + # - shortest_max_lifetime: 3d |
| 225 | + # interval: 1d |
| 226 | + """ |
0 commit comments