1+ import io
12import logging
2- import tempfile
3+ import tarfile
34import time
45from http import HTTPStatus
5- from pathlib import Path
66
77import docker
88import requests
@@ -26,7 +26,6 @@ def __init__(
2626 self ._mapped_port : int | None = None
2727 self ._host = "localhost"
2828 self ._signing_key : ed25519 .Ed25519PrivateKey | None = None
29- self ._temp_key_file : Path | None = None
3029
3130 def _cleanup_existing_containers (self ) -> None :
3231 try :
@@ -60,33 +59,39 @@ def _create_container(self) -> None:
6059 "--https-enabled=false" ,
6160 ]
6261
63- volumes = None
64-
6562 if self ._signing_key is not None :
66- signing_key_bytes = self ._signing_key .private_bytes (
67- encoding = serialization .Encoding .PEM ,
68- format = serialization .PrivateFormat .PKCS8 ,
69- encryption_algorithm = serialization .NoEncryption ()
70- )
71-
72- # Create temporary file for signing key
73- temp_file = tempfile .NamedTemporaryFile (mode = 'wb' , delete = False , suffix = '.pem' )
74- temp_file .write (signing_key_bytes )
75- temp_file .close ()
76- self ._temp_key_file = Path (temp_file .name )
77-
7863 target_path = "/etc/esdb/signing-key.pem"
79- volumes = {str (self ._temp_key_file ): {'bind' : target_path , 'mode' : 'ro' }}
8064 command .extend (["--signing-key-file" , target_path ])
8165
8266 self ._container = self ._docker_client .containers .run (
8367 f"{ self ._image_name } :{ self ._image_tag } " ,
8468 command = command ,
8569 ports = port_bindings , # type: ignore
8670 detach = True ,
87- volumes = volumes , # type: ignore
8871 ) # type: ignore
8972
73+ # Copy signing key into container if needed
74+ if self ._signing_key is not None :
75+ signing_key_bytes = self ._signing_key .private_bytes (
76+ encoding = serialization .Encoding .PEM ,
77+ format = serialization .PrivateFormat .PKCS8 ,
78+ encryption_algorithm = serialization .NoEncryption ()
79+ )
80+
81+ # Create tar archive with the key file
82+ tar_stream = io .BytesIO ()
83+ tar = tarfile .TarFile (fileobj = tar_stream , mode = 'w' )
84+
85+ tarinfo = tarfile .TarInfo (name = 'signing-key.pem' )
86+ tarinfo .size = len (signing_key_bytes )
87+ tarinfo .mode = 0o644
88+
89+ tar .addfile (tarinfo , io .BytesIO (signing_key_bytes ))
90+ tar .close ()
91+
92+ tar_stream .seek (0 )
93+ self ._container .put_archive ('/etc/esdb' , tar_stream )
94+
9095 def _extract_port_from_container_info (self , container_info ) -> int | None :
9196 port = None
9297 valid_mapping = True
@@ -213,14 +218,6 @@ def _stop_and_remove_container(self) -> None:
213218 self ._container = None
214219 self ._mapped_port = None
215220
216- # Clean up temporary key file if it exists
217- if self ._temp_key_file is not None and self ._temp_key_file .exists ():
218- try :
219- self ._temp_key_file .unlink ()
220- except Exception as e :
221- logging .warning ("Warning: Error removing temporary key file: %s" , e )
222- self ._temp_key_file = None
223-
224221 def _try_get_port_from_container (self ) -> int | None :
225222 if not self ._container :
226223 return None
0 commit comments