11
11
from datetime import UTC , datetime , timedelta
12
12
import logging
13
13
from os import makedirs
14
+ from pathlib import Path
14
15
import re
15
16
16
17
from aiohttp import ClientError , ClientSession
17
18
from cryptography import x509
18
19
from cryptography .hazmat .primitives import serialization
19
20
20
- from matter_server .server .const import PAA_ROOT_CERTS_DIR
21
-
22
21
# Git repo details
23
22
OWNER = "project-chip"
24
23
REPO = "connectedhomeip"
33
32
LAST_CERT_IDS : set [str ] = set ()
34
33
35
34
36
- async def write_paa_root_cert (certificate : str , subject : str ) -> None :
35
+ async def write_paa_root_cert (
36
+ paa_root_cert_dir : Path , certificate : str , subject : str
37
+ ) -> None :
37
38
"""Write certificate from string to file."""
38
39
39
40
def _write () -> None :
40
41
filename_base = "dcld_mirror_" + re .sub (
41
42
"[^a-zA-Z0-9_-]" , "" , re .sub ("[=, ]" , "_" , subject )
42
43
)
43
- filepath_base = PAA_ROOT_CERTS_DIR .joinpath (filename_base )
44
+ filepath_base = paa_root_cert_dir .joinpath (filename_base )
44
45
# handle PEM certificate file
45
46
file_path_pem = f"{ filepath_base } .pem"
46
47
LOGGER .debug ("Writing certificate %s" , file_path_pem )
@@ -58,6 +59,7 @@ def _write() -> None:
58
59
59
60
60
61
async def fetch_dcl_certificates (
62
+ paa_root_cert_dir : Path ,
61
63
fetch_test_certificates : bool = True ,
62
64
fetch_production_certificates : bool = True ,
63
65
) -> int :
@@ -99,6 +101,7 @@ async def fetch_dcl_certificates(
99
101
certificate = certificate .rstrip ("\n " )
100
102
101
103
await write_paa_root_cert (
104
+ paa_root_cert_dir ,
102
105
certificate ,
103
106
subject ,
104
107
)
@@ -119,7 +122,7 @@ async def fetch_dcl_certificates(
119
122
# are correctly captured
120
123
121
124
122
- async def fetch_git_certificates () -> int :
125
+ async def fetch_git_certificates (paa_root_cert_dir : Path ) -> int :
123
126
"""Fetch Git PAA Certificates."""
124
127
fetch_count = 0
125
128
LOGGER .info ("Fetching the latest PAA root certificates from Git." )
@@ -137,7 +140,7 @@ async def fetch_git_certificates() -> int:
137
140
continue
138
141
async with http_session .get (f"{ GIT_URL } /{ cert } .pem" ) as response :
139
142
certificate = await response .text ()
140
- await write_paa_root_cert (certificate , cert )
143
+ await write_paa_root_cert (paa_root_cert_dir , certificate , cert )
141
144
LAST_CERT_IDS .add (cert )
142
145
fetch_count += 1
143
146
except (ClientError , TimeoutError ) as err :
@@ -150,24 +153,18 @@ async def fetch_git_certificates() -> int:
150
153
return fetch_count
151
154
152
155
153
- async def _get_certificate_age () -> datetime :
154
- """Get last time PAA Certificates have been fetched."""
155
- loop = asyncio .get_running_loop ()
156
- stat = await loop .run_in_executor (None , PAA_ROOT_CERTS_DIR .stat )
157
- return datetime .fromtimestamp (stat .st_mtime , tz = UTC )
158
-
159
-
160
156
async def fetch_certificates (
157
+ paa_root_cert_dir : Path ,
161
158
fetch_test_certificates : bool = True ,
162
159
fetch_production_certificates : bool = True ,
163
160
) -> int :
164
161
"""Fetch PAA Certificates."""
165
162
loop = asyncio .get_running_loop ()
166
163
167
- if not PAA_ROOT_CERTS_DIR .is_dir ():
168
- await loop .run_in_executor (None , makedirs , PAA_ROOT_CERTS_DIR )
164
+ if not paa_root_cert_dir .is_dir ():
165
+ await loop .run_in_executor (None , makedirs , paa_root_cert_dir )
169
166
else :
170
- stat = await loop .run_in_executor (None , PAA_ROOT_CERTS_DIR .stat )
167
+ stat = await loop .run_in_executor (None , paa_root_cert_dir .stat )
171
168
last_fetch = datetime .fromtimestamp (stat .st_mtime , tz = UTC )
172
169
if last_fetch > datetime .now (tz = UTC ) - timedelta (days = 1 ):
173
170
LOGGER .info (
@@ -176,13 +173,14 @@ async def fetch_certificates(
176
173
return 0
177
174
178
175
fetch_count = await fetch_dcl_certificates (
176
+ paa_root_cert_dir = paa_root_cert_dir ,
179
177
fetch_test_certificates = fetch_test_certificates ,
180
178
fetch_production_certificates = fetch_production_certificates ,
181
179
)
182
180
183
181
if fetch_test_certificates :
184
- fetch_count += await fetch_git_certificates ()
182
+ fetch_count += await fetch_git_certificates (paa_root_cert_dir )
185
183
186
- await loop .run_in_executor (None , PAA_ROOT_CERTS_DIR .touch )
184
+ await loop .run_in_executor (None , paa_root_cert_dir .touch )
187
185
188
186
return fetch_count
0 commit comments