Skip to content

Commit 757ebc5

Browse files
authored
Fetch certificates only once a day (#611)
1 parent dfd6e0e commit 757ebc5

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

matter_server/server/device_controller.py

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ async def initialize(self) -> None:
126126
# (re)fetch all PAA certificates once at startup
127127
# NOTE: this must be done before initializing the controller
128128
await fetch_certificates()
129+
129130
# Instantiate the underlying ChipDeviceController instance on the Fabric
130131
self.chip_controller = self.server.stack.fabric_admin.NewController(
131132
paaTrustStorePath=str(PAA_ROOT_CERTS_DIR)

matter_server/server/helpers/paa_certificates.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99

1010
import asyncio
11+
from datetime import UTC, datetime, timedelta
1112
import logging
1213
from os import makedirs
1314
import re
@@ -62,9 +63,6 @@ async def fetch_dcl_certificates(
6263
) -> int:
6364
"""Fetch DCL PAA Certificates."""
6465
LOGGER.info("Fetching the latest PAA root certificates from DCL.")
65-
if not PAA_ROOT_CERTS_DIR.is_dir():
66-
loop = asyncio.get_running_loop()
67-
await loop.run_in_executor(None, makedirs, PAA_ROOT_CERTS_DIR)
6866
fetch_count: int = 0
6967
base_urls = set()
7068
# determine which url's need to be queried.
@@ -152,11 +150,30 @@ async def fetch_git_certificates() -> int:
152150
return fetch_count
153151

154152

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+
155160
async def fetch_certificates(
156161
fetch_test_certificates: bool = True,
157162
fetch_production_certificates: bool = True,
158163
) -> int:
159164
"""Fetch PAA Certificates."""
165+
loop = asyncio.get_running_loop()
166+
167+
if not PAA_ROOT_CERTS_DIR.is_dir():
168+
await loop.run_in_executor(None, makedirs, PAA_ROOT_CERTS_DIR)
169+
else:
170+
stat = await loop.run_in_executor(None, PAA_ROOT_CERTS_DIR.stat)
171+
last_fetch = datetime.fromtimestamp(stat.st_mtime, tz=UTC)
172+
if last_fetch > datetime.now(tz=UTC) - timedelta(days=1):
173+
LOGGER.info(
174+
"Skip fetching certificates (already fetched within the last 24h)."
175+
)
176+
return 0
160177

161178
fetch_count = await fetch_dcl_certificates(
162179
fetch_test_certificates=fetch_test_certificates,
@@ -166,4 +183,6 @@ async def fetch_certificates(
166183
if fetch_test_certificates:
167184
fetch_count += await fetch_git_certificates()
168185

186+
await loop.run_in_executor(None, PAA_ROOT_CERTS_DIR.touch)
187+
169188
return fetch_count

0 commit comments

Comments
 (0)