Skip to content

Commit fc658f7

Browse files
committed
refactor: new _install_system_certificates sub-function
Signed-off-by: Oleksander Piskun <oleksandr2088@icloud.com>
1 parent 77fc464 commit fc658f7

1 file changed

Lines changed: 73 additions & 63 deletions

File tree

haproxy_agent.py

Lines changed: 73 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,69 +1239,9 @@ async def docker_exapp_install_certificates(request: web.Request):
12391239
LOGGER.info("OS Info for container '%s':\n%s", container_name, os_info_content.strip())
12401240

12411241
if payload.system_certs_bundle:
1242-
target_cert_dir = _get_target_cert_dir(os_info_content)
1243-
if target_cert_dir:
1244-
LOGGER.info("Target system cert directory for container '%s': %s", container_name, target_cert_dir)
1245-
exit_code, raw_output = await _execute_command_in_container_simplified(
1246-
session, docker_engine_port, container_name, ["mkdir", "-p", target_cert_dir]
1247-
)
1248-
if exit_code != 0:
1249-
LOGGER.error(
1250-
"Failed to create cert dir '%s' in container '%s'. Exit: %s, Raw Output: %s",
1251-
target_cert_dir,
1252-
container_name,
1253-
exit_code,
1254-
raw_output,
1255-
)
1256-
raise web.HTTPInternalServerError(
1257-
text=f"Failed to create cert directory. Exit: {exit_code}. Output: {raw_output[:200]}"
1258-
)
1259-
1260-
certs_to_install = {}
1261-
parsed_certs = _parse_certs_from_bundle(payload.system_certs_bundle)
1262-
for i, cert_content in enumerate(parsed_certs):
1263-
cert_filename = f"custom_ca_cert_{i}.crt"
1264-
certs_to_install[os.path.join(target_cert_dir.lstrip("/"), cert_filename)] = cert_content
1265-
1266-
if certs_to_install:
1267-
tar_bytes = _create_tar_archive_in_memory(certs_to_install)
1268-
await _put_archive_to_container(session, docker_engine_port, container_name, "/", tar_bytes)
1269-
LOGGER.info(
1270-
"Installed %d system CA certificates into '%s' in container '%s'.",
1271-
len(parsed_certs),
1272-
target_cert_dir,
1273-
container_name,
1274-
)
1275-
1276-
update_cmd_list = _get_certificate_update_command(os_info_content)
1277-
if update_cmd_list:
1278-
LOGGER.info("Running certificate update command: %s", " ".join(update_cmd_list))
1279-
exit_code, raw_output = await _execute_command_in_container_simplified(
1280-
session, docker_engine_port, container_name, update_cmd_list
1281-
)
1282-
if exit_code != 0:
1283-
LOGGER.error(
1284-
"Certificate update command failed in container '%s'. Exit: %s, Raw Output: %s",
1285-
container_name,
1286-
exit_code,
1287-
raw_output,
1288-
)
1289-
else:
1290-
LOGGER.info("Certificate update command successful. Raw Output: %s", raw_output.strip())
1291-
else:
1292-
LOGGER.warning(
1293-
"No certificate update command found for OS in container '%s'.", container_name
1294-
)
1295-
else:
1296-
LOGGER.info(
1297-
"No individual certificates parsed from system_certs_bundle for container '%s'.",
1298-
container_name,
1299-
)
1300-
else:
1301-
LOGGER.warning(
1302-
"OS in container '%s' not supported for sys cert installation, or bundle empty. Skipping.",
1303-
container_name,
1304-
)
1242+
await _install_system_certificates(
1243+
session, docker_engine_port, container_name, payload.system_certs_bundle, os_info_content
1244+
)
13051245
else:
13061246
LOGGER.info(
13071247
"No system_certs_bundle provided for container '%s'. Skipping system cert installation.",
@@ -1347,6 +1287,76 @@ async def docker_exapp_install_certificates(request: web.Request):
13471287
LOGGER.error("Error stopping container '%s' in finally block: %s", container_name, e_stop)
13481288

13491289

1290+
async def _install_system_certificates(
1291+
session: aiohttp.ClientSession,
1292+
docker_engine_port: int,
1293+
container_name: str,
1294+
system_certs_bundle: str,
1295+
os_info_content: str,
1296+
) -> None:
1297+
target_cert_dir = _get_target_cert_dir(os_info_content)
1298+
if target_cert_dir:
1299+
LOGGER.info("Target system cert directory for container '%s': %s", container_name, target_cert_dir)
1300+
exit_code, raw_output = await _execute_command_in_container_simplified(
1301+
session, docker_engine_port, container_name, ["mkdir", "-p", target_cert_dir]
1302+
)
1303+
if exit_code != 0:
1304+
LOGGER.error(
1305+
"Failed to create cert dir '%s' in container '%s'. Exit: %s, Raw Output: %s",
1306+
target_cert_dir,
1307+
container_name,
1308+
exit_code,
1309+
raw_output,
1310+
)
1311+
raise web.HTTPInternalServerError(
1312+
text=f"Failed to create cert directory. Exit: {exit_code}. Output: {raw_output[:200]}"
1313+
)
1314+
1315+
certs_to_install = {}
1316+
parsed_certs = _parse_certs_from_bundle(system_certs_bundle)
1317+
for i, cert_content in enumerate(parsed_certs):
1318+
cert_filename = f"custom_ca_cert_{i}.crt"
1319+
certs_to_install[os.path.join(target_cert_dir.lstrip("/"), cert_filename)] = cert_content
1320+
1321+
if certs_to_install:
1322+
tar_bytes = _create_tar_archive_in_memory(certs_to_install)
1323+
await _put_archive_to_container(session, docker_engine_port, container_name, "/", tar_bytes)
1324+
LOGGER.info(
1325+
"Installed %d system CA certificates into '%s' in container '%s'.",
1326+
len(parsed_certs),
1327+
target_cert_dir,
1328+
container_name,
1329+
)
1330+
1331+
update_cmd_list = _get_certificate_update_command(os_info_content)
1332+
if update_cmd_list:
1333+
LOGGER.info("Running certificate update command: %s", " ".join(update_cmd_list))
1334+
exit_code, raw_output = await _execute_command_in_container_simplified(
1335+
session, docker_engine_port, container_name, update_cmd_list
1336+
)
1337+
if exit_code != 0:
1338+
LOGGER.error(
1339+
"Certificate update command failed in container '%s'. Exit: %s, Raw Output: %s",
1340+
container_name,
1341+
exit_code,
1342+
raw_output,
1343+
)
1344+
else:
1345+
LOGGER.info("Certificate update command successful. Raw Output: %s", raw_output.strip())
1346+
else:
1347+
LOGGER.warning("No certificate update command found for OS in container '%s'.", container_name)
1348+
else:
1349+
LOGGER.info(
1350+
"No individual certificates parsed from system_certs_bundle for container '%s'.",
1351+
container_name,
1352+
)
1353+
else:
1354+
LOGGER.warning(
1355+
"OS in container '%s' not supported for sys cert installation, or bundle empty. Skipping.",
1356+
container_name,
1357+
)
1358+
1359+
13501360
async def _install_frp_certificates(
13511361
session: aiohttp.ClientSession, docker_engine_port: int, container_name: str
13521362
) -> None:

0 commit comments

Comments
 (0)