21
21
22
22
import base64
23
23
import binascii
24
+ import io
24
25
import os
26
+ import typing as ty
25
27
26
28
from cryptography .hazmat import backends
27
29
from cryptography .hazmat .primitives .asymmetric import padding
31
33
from oslo_concurrency import processutils
32
34
from oslo_log import log as logging
33
35
import paramiko
34
- import six
35
36
36
37
import nova .conf
37
38
from nova import exception
44
45
CONF = nova .conf .CONF
45
46
46
47
47
- def generate_fingerprint (public_key ) :
48
+ def generate_fingerprint (public_key : str ) -> str :
48
49
try :
49
50
pub_bytes = public_key .encode ('utf-8' )
50
51
# Test that the given public_key string is a proper ssh key. The
@@ -56,58 +57,59 @@ def generate_fingerprint(public_key):
56
57
digest = hashes .Hash (hashes .MD5 (), backends .default_backend ())
57
58
digest .update (pub_data )
58
59
md5hash = digest .finalize ()
59
- raw_fp = binascii .hexlify (md5hash )
60
- if six .PY3 :
61
- raw_fp = raw_fp .decode ('ascii' )
60
+ raw_fp = binascii .hexlify (md5hash ).decode ('ascii' )
62
61
return ':' .join (a + b for a , b in zip (raw_fp [::2 ], raw_fp [1 ::2 ]))
63
62
except Exception :
64
63
raise exception .InvalidKeypair (
65
64
reason = _ ('failed to generate fingerprint' ))
66
65
67
66
68
- def generate_x509_fingerprint (pem_key ) :
67
+ def generate_x509_fingerprint (pem_key : ty . Union [ bytes , str ]) -> str :
69
68
try :
70
- if isinstance (pem_key , six . text_type ):
69
+ if isinstance (pem_key , str ):
71
70
pem_key = pem_key .encode ('utf-8' )
72
71
cert = x509 .load_pem_x509_certificate (
73
72
pem_key , backends .default_backend ())
74
- raw_fp = binascii .hexlify (cert . fingerprint ( hashes . SHA1 ()))
75
- if six . PY3 :
76
- raw_fp = raw_fp .decode ('ascii' )
73
+ raw_fp = binascii .hexlify (
74
+ cert . fingerprint ( hashes . SHA1 ())
75
+ ) .decode ('ascii' )
77
76
return ':' .join (a + b for a , b in zip (raw_fp [::2 ], raw_fp [1 ::2 ]))
78
77
except (ValueError , TypeError , binascii .Error ) as ex :
79
78
raise exception .InvalidKeypair (
80
79
reason = _ ('failed to generate X509 fingerprint. '
81
80
'Error message: %s' ) % ex )
82
81
83
82
84
- def generate_key_pair (bits = 2048 ):
83
+ def generate_key_pair (bits : int = 2048 ) -> ty . Tuple [ str , str , str ] :
85
84
key = paramiko .RSAKey .generate (bits )
86
- keyout = six .StringIO ()
85
+ keyout = io .StringIO ()
87
86
key .write_private_key (keyout )
88
87
private_key = keyout .getvalue ()
89
88
public_key = '%s %s Generated-by-Nova' % (key .get_name (), key .get_base64 ())
90
89
fingerprint = generate_fingerprint (public_key )
91
90
return (private_key , public_key , fingerprint )
92
91
93
92
94
- def ssh_encrypt_text (ssh_public_key , text ) :
93
+ def ssh_encrypt_text (ssh_public_key : str , text : ty . Union [ str , bytes ]) -> bytes :
95
94
"""Encrypt text with an ssh public key.
96
95
97
96
If text is a Unicode string, encode it to UTF-8.
98
97
"""
99
- if isinstance (text , six . text_type ):
98
+ if isinstance (text , str ):
100
99
text = text .encode ('utf-8' )
101
100
try :
102
101
pub_bytes = ssh_public_key .encode ('utf-8' )
103
102
pub_key = serialization .load_ssh_public_key (
104
103
pub_bytes , backends .default_backend ())
105
104
return pub_key .encrypt (text , padding .PKCS1v15 ())
106
105
except Exception as exc :
107
- raise exception .EncryptionFailure (reason = six . text_type (exc ))
106
+ raise exception .EncryptionFailure (reason = str (exc ))
108
107
109
108
110
- def generate_winrm_x509_cert (user_id , bits = 2048 ):
109
+ def generate_winrm_x509_cert (
110
+ user_id : str ,
111
+ bits : int = 2048
112
+ ) -> ty .Tuple [str , str , str ]:
111
113
"""Generate a cert for passwordless auth for user in project."""
112
114
subject = '/CN=%s' % user_id
113
115
upn = '%s@localhost' % user_id
@@ -118,28 +120,26 @@ def generate_winrm_x509_cert(user_id, bits=2048):
118
120
119
121
_create_x509_openssl_config (conffile , upn )
120
122
121
- ( certificate , _err ) = processutils .execute (
122
- 'openssl' , 'req' , '-x509' , '-nodes' , '-days' , '3650' ,
123
- '-config' , conffile , '-newkey' , 'rsa:%s' % bits ,
124
- '-outform' , 'PEM' , '-keyout' , keyfile , '-subj' , subject ,
125
- '-extensions' , 'v3_req_client' ,
126
- binary = True )
123
+ out , _ = processutils .execute (
124
+ 'openssl' , 'req' , '-x509' , '-nodes' , '-days' , '3650' ,
125
+ '-config' , conffile , '-newkey' , 'rsa:%s' % bits ,
126
+ '-outform' , 'PEM' , '-keyout' , keyfile , '-subj' , subject ,
127
+ '-extensions' , 'v3_req_client' ,
128
+ binary = True )
127
129
128
- (out , _err ) = processutils .execute ('openssl' , 'pkcs12' , '-export' ,
129
- '-inkey' , keyfile , '-password' , 'pass:' ,
130
- process_input = certificate ,
131
- binary = True )
130
+ certificate = out .decode ('utf-8' )
132
131
133
- private_key = base64 .b64encode (out )
132
+ out , _ = processutils .execute (
133
+ 'openssl' , 'pkcs12' , '-export' , '-inkey' , keyfile , '-password' ,
134
+ 'pass:' , process_input = out , binary = True )
135
+
136
+ private_key = base64 .b64encode (out ).decode ('ascii' )
134
137
fingerprint = generate_x509_fingerprint (certificate )
135
- if six .PY3 :
136
- private_key = private_key .decode ('ascii' )
137
- certificate = certificate .decode ('utf-8' )
138
138
139
139
return (private_key , certificate , fingerprint )
140
140
141
141
142
- def _create_x509_openssl_config (conffile , upn ):
142
+ def _create_x509_openssl_config (conffile : str , upn : str ):
143
143
content = ("distinguished_name = req_distinguished_name\n "
144
144
"[req_distinguished_name]\n "
145
145
"[v3_req_client]\n "
0 commit comments