17
17
DEFAULT_KEY_STRENGTH = 2048 # 2048-bit encryption
18
18
DEFAULT_HASH_ALG = "sha256"
19
19
20
- def write_certificate (options , cert , destination ):
21
- """
22
- Write out the certificate to a temporary file first, then atomically copy
23
- it to the destination path. This will avoid race-condition bugs with
24
- checking for file presence and then writing to it. Note: this will clobber
25
- the destination path.
26
- """
27
-
28
- # Create the temporary file in the same directory as the destination
29
- # This ensures that we can atomically move it to the final name.
30
-
31
- try :
32
- (fd , fpath ) = tempfile .mkstemp (dir = os .path .dirname (destination ))
33
- if options .debug :
34
- print (_ ("Creating temporary certificate file at {}" ).format (fpath ))
35
- f = os .fdopen (fd , "w" )
36
-
37
- f .write (crypto .dump_certificate (options .cert_format , cert ).decode ("UTF-8" ))
38
- f .close ()
39
- except IOError :
40
- # Something went wrong. Remove the temporary file before failing.
41
- print (_ ("Could not write to {0}. Error: {1}" ).format (
42
- fpath , sys .exc_info ()[1 ]),
43
- file = sys .stderr )
44
- os .unlink (fpath )
45
- raise
46
20
47
- # Now atomically move the temporary file into place.
48
- # We use os.rename because this is guaranteed to be atomic if it succeeds
49
- # This operation can fail on some flavors of UNIX if the source and
50
- # destination are on different filesystems, but this should not be the case.
51
- try :
52
- if options .debug :
53
- print (_ ("Renaming {} to {}" .format (fpath , destination )))
54
- os .rename (fpath , destination )
55
- except IOError :
56
- # Something went wrong. Remove the temporary file before failing.
57
- print (_ ("Could not rename to {0}. Error: {1}" ).format (
58
- destination , sys .exc_info ()[1 ]))
59
- os .unlink (fpath )
60
- raise
61
-
62
-
63
- def write_certificate_key (options , key , destination , cipher = None , passphrase = None ):
21
+ def write_secure_file (options , destination , data ):
64
22
"""
65
- Write out the certificate key to a temporary file first, then atomically
23
+ Write out the certificate or key to a temporary file first, then atomically
66
24
copy it to the destination path. This will avoid race-condition bugs with
67
25
checking for file presence and then writing to it. Note: this will clobber
68
26
the destination path.
@@ -71,34 +29,26 @@ def write_certificate_key(options, key, destination, cipher=None, passphrase=Non
71
29
# Create the temporary file in the same directory as the destination
72
30
# This ensures that we can atomically move it to the final name.
73
31
32
+ f = tempfile .NamedTemporaryFile (dir = os .path .dirname (destination ),
33
+ delete = False )
74
34
try :
75
- (fd , fpath ) = tempfile .mkstemp (dir = os .path .dirname (destination ))
76
- if options .debug :
77
- print (_ ("Creating temporary keyfile at {}" ).format (fpath ))
78
-
79
- f = os .fdopen (fd , "w" )
80
-
81
- f .write (crypto .dump_privatekey (options .cert_format , key , cipher , passphrase ).decode ("UTF-8" ))
35
+ f .write (data )
36
+ f .flush ()
37
+ except IOError as e :
82
38
f .close ()
83
- except IOError :
84
- # Something went wrong. Remove the temporary file before failing.
85
- print (_ ("Could not write to {0}. Error: {1}" ).format (
86
- fpath , sys .exc_info ()[1 ]),
87
- file = sys .stderr )
88
- os .unlink (fpath )
89
- raise
39
+ os .unlink (f .name )
40
+ raise Exception (_ ("Could not write to {0}. Error: {1}" ).format (f .name , e ))
90
41
91
42
# Now atomically move the temporary file into place.
92
43
# We use os.rename because this is guaranteed to be atomic if it succeeds
93
44
# This operation can fail on some flavors of UNIX if the source and
94
45
# destination are on different filesystems, but this should not be the case.
46
+ if options .debug :
47
+ print (_ ("Renaming {} to {}" ).format (f .name , destination ))
48
+
49
+ f .close ()
95
50
try :
96
- if options .debug :
97
- print (_ ("Renaming {} to {}" ).format (fpath , destination ))
98
- os .rename (fpath , destination )
99
- except IOError :
100
- # Something went wrong. Remove the temporary file before failing.
101
- print (_ ("Could not rename to {0}. Error: {1}" ).format (
102
- destination , sys .exc_info ()[1 ]))
103
- os .unlink (fpath )
104
- raise
51
+ os .rename (f .name , destination )
52
+ except IOError as e :
53
+ os .unlink (f .name )
54
+ raise Exception (_ ("Could not rename to {0}. Error: {1}" ).format (destination , e ))
0 commit comments