@@ -226,36 +226,59 @@ def dump_token_to_file(self) -> None:
226226 """Dump token to file."""
227227 token_filepath = os .path .join (WORKDIR_LOCATION , "token" )
228228 os .makedirs (os .path .dirname (token_filepath ), exist_ok = True )
229- with open (token_filepath , "w" ) as token_file :
230- token_file .write (self .token )
231229
232230 if sys .platform == "win32" :
233- import ntsecuritycon as con # pylint: disable=import-error
234- import win32api # pylint: disable=import-error
235- import win32security # pylint: disable=import-error
236-
237- user , _ , _ = win32security .LookupAccountName ("" , win32api .GetUserName ())
238- security_descriptor = win32security .GetFileSecurity (
239- token_filepath ,
240- win32security .DACL_SECURITY_INFORMATION ,
241- )
242- dacl = win32security .ACL ()
243- dacl .AddAccessAllowedAce (
244- win32security .ACL_REVISION ,
245- con .FILE_GENERIC_READ | con .FILE_GENERIC_WRITE ,
246- user ,
247- )
248- security_descriptor .SetSecurityDescriptorDacl (1 , dacl , 0 )
249- win32security .SetFileSecurity (
250- token_filepath ,
251- win32security .DACL_SECURITY_INFORMATION ,
252- security_descriptor ,
253- )
254- else :
255- os .chown (token_filepath , uid = os .geteuid (), gid = os .getgid ())
256- os .chmod (token_filepath , 0o600 )
231+ self .create_secured_token_file_win (token_filepath )
232+
233+ try :
234+ token_file = os .open (token_filepath , flags = os .O_WRONLY | os .O_CREAT , mode = 0o600 )
235+ os .write (token_file , self .token .encode ())
236+ except Exception as err :
237+ raise err
238+ finally :
239+ os .close (token_file )
240+
257241 log .debug (f"Token has been dumped to { token_filepath } ." )
258242
243+ @staticmethod
244+ def create_secured_token_file_win (token_filepath : str ):
245+ """Create secured file on Windows OS."""
246+ import ntsecuritycon as con # pylint: disable=import-error
247+ import win32api # pylint: disable=import-error
248+ import win32file # pylint: disable=import-error
249+ import win32security # pylint: disable=import-error
250+
251+ username = win32api .GetUserName ()
252+ os .makedirs (os .path .dirname (token_filepath ), exist_ok = True )
253+
254+ if os .path .exists (token_filepath ):
255+ os .remove (token_filepath )
256+
257+ security_descriptor = win32security .SECURITY_DESCRIPTOR ()
258+ user_sid , _ , _ = win32security .LookupAccountName ("" , username )
259+
260+ access_rights = con .FILE_ALL_ACCESS
261+
262+ dacl = win32security .ACL ()
263+ dacl .AddAccessAllowedAce (win32security .ACL_REVISION , access_rights , user_sid )
264+
265+ security_descriptor .SetSecurityDescriptorDacl (1 , dacl , 0 )
266+
267+ security_attributes = win32security .SECURITY_ATTRIBUTES ()
268+ security_attributes .SECURITY_DESCRIPTOR = security_descriptor
269+
270+ handle = win32file .CreateFile (
271+ token_filepath ,
272+ win32file .GENERIC_WRITE ,
273+ win32file .FILE_SHARE_READ ,
274+ security_attributes ,
275+ win32file .CREATE_NEW ,
276+ win32file .FILE_ATTRIBUTE_NORMAL ,
277+ None ,
278+ )
279+
280+ win32file .CloseHandle (handle )
281+
259282 def _ensure_valid_port (self , port : int ) -> None :
260283 """Validate if proposed port number is allowed by TCP/IP."""
261284 if port < 1 :
0 commit comments