|
| 1 | +# Copyright 2023 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 | +from OpenSSL.SSL import Context |
| 15 | +from twisted.internet import ssl |
| 16 | + |
| 17 | +from synapse.config.redis import RedisConfig |
| 18 | + |
| 19 | + |
| 20 | +class ClientContextFactory(ssl.ClientContextFactory): |
| 21 | + def __init__(self, redis_config: RedisConfig): |
| 22 | + self.redis_config = redis_config |
| 23 | + |
| 24 | + def getContext(self) -> Context: |
| 25 | + ctx = super().getContext() |
| 26 | + if self.redis_config.redis_certificate: |
| 27 | + ctx.use_certificate_file(self.redis_config.redis_certificate) |
| 28 | + if self.redis_config.redis_private_key: |
| 29 | + ctx.use_privatekey_file(self.redis_config.redis_private_key) |
| 30 | + if self.redis_config.redis_ca_file: |
| 31 | + ctx.load_verify_locations(cafile=self.redis_config.redis_ca_file) |
| 32 | + elif self.redis_config.redis_ca_path: |
| 33 | + ctx.load_verify_locations(capath=self.redis_config.redis_ca_path) |
| 34 | + return ctx |
0 commit comments