in libcloud/common/google.py [0:0]
def __init__(self, user_id, key, *args, **kwargs):
"""
Check to see if cryptography is available, and convert PEM key file
into a key string, or extract the key from JSON object, string or
file.
:param user_id: Email address to be used for Service Account
authentication.
:type user_id: ``str``
:param key: The path to a PEM/JSON file containing the private RSA
key, or a str/dict containing the PEM/JSON.
:type key: ``str`` or ``dict``
"""
if SHA256 is None:
raise GoogleAuthError(
"cryptography library required for " "Service Account Authentication."
)
if isinstance(key, dict):
# if it's a dict, assume it's containing parsed JSON
key_content = key
key = None
else:
key_path = os.path.expanduser(key)
if os.path.exists(key_path) and os.path.isfile(key_path):
# Assume it's a file and read it
try:
with open(key_path) as f:
key_content = f.read()
except OSError:
raise GoogleAuthError("Missing (or unreadable) key " "file: '%s'" % key)
else:
# assume it's a PEM str or serialized JSON str
key_content = key
key = None
try:
key_content = json.loads(key_content)
# check if serialized JSON given
except TypeError:
# already a dict
pass
except ValueError:
# it's been a PEM string all along
pass
finally:
if "private_key" in key_content:
key = key_content["private_key"]
else:
key = key_content
try:
# check if the key is actually a PEM encoded private key
serialization.load_pem_private_key(b(key), password=None, backend=default_backend())
except ValueError as e:
raise GoogleAuthError("Unable to decode provided PEM key: %s" % e)
except TypeError as e:
raise GoogleAuthError("Unable to decode provided PEM key: %s" % e)
except exceptions.UnsupportedAlgorithm as e:
raise GoogleAuthError("Unable to decode provided PEM key: %s" % e)
super().__init__(user_id, key, *args, **kwargs)