From 0548eda896fce2341da028bd94a13e2fdf2520f0 Mon Sep 17 00:00:00 2001 From: Vladimir Diaz Date: Fri, 20 Dec 2013 16:46:59 -0500 Subject: [PATCH] Address Issue #147 in libtuf.py --- tuf/libtuf.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/tuf/libtuf.py b/tuf/libtuf.py index 14dce933..6b8512be 100755 --- a/tuf/libtuf.py +++ b/tuf/libtuf.py @@ -2690,14 +2690,20 @@ def generate_and_write_rsa_keypair(filepath, bits=DEFAULT_RSA_KEY_BITS, # create it (and all its parent directories, if necessary). tuf.util.ensure_parent_dir(filepath) - with open(filepath+'.pub', 'w') as file_object: - file_object.write(public) + # Create a tempororary file, write the contents of the public key, and move + # to final destination. + file_object = tuf.util.TempFile() + file_object.write(public) + + # The temporary file is closed after the final move. + file_object.move(filepath+'.pub') # Write the private key in encrypted PEM format to ''. # Unlike the public key file, the private key does not have a file # extension. - with open(filepath, 'w') as file_object: - file_object.write(encrypted_pem) + file_object = tuf.util.TempFile() + file_object.write(encrypted_pem) + file_object.move(filepath) @@ -2879,14 +2885,20 @@ def generate_and_write_ed25519_keypair(filepath, password=None): # '.pub'. tuf.util.ensure_parent_dir(filepath) - with open(filepath+'.pub', 'w') as file_object: - file_object.write(json.dumps(ed25519key_metadata_format)) + # Create a tempororary file, write the contents of the public key, and move + # to final destination. + file_object = tuf.util.TempFile() + file_object.write(json.dumps(ed25519key_metadata_format)) + + # The temporary file is closed after the final move. + file_object.move(filepath+'.pub') # Write the encrypted key string, conformant to # 'tuf.formats.ENCRYPTEDKEY_SCHEMA', to ''. - with open(filepath, 'w') as file_object: - file_object.write(encrypted_key) - + file_object = tuf.util.TempFile() + file_object.write(encrypted_key) + file_object.move(filepath) +