Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read_public_key_file(path):
try:
with open(utils.full_path(path), 'rb') as f:
data = f.read()
if not data.startswith(b"ssh-rsa"):
raise ValueError(
"Bad public key data, must be an RSA key in SSH authorized "
"keys format (beginning with `ssh-rsa`)")
return data.strip()
except (ValueError, IOError, OSError) as e:
logger.error("Failed to read public key file {}: {}".format(
path, e))
return None
paths[key] = path
parameters = {
"ServerCertificateName": kwargs.get("cert_name"),
}
for key, path in paths.items():
if not path:
continue
# Allow passing of file like object for tests
try:
contents = path.read()
except AttributeError:
with open(utils.full_path(path)) as read_file:
contents = read_file.read()
if key == "certificate":
parameters["CertificateBody"] = contents
elif key == "private_key":
parameters["PrivateKey"] = contents
elif key == "chain":
parameters["CertificateChain"] = contents
return parameters
def create_key_pair_local(ec2, keypair_name, dest_dir):
dest_dir = utils.full_path(dest_dir)
if not os.path.isdir(dest_dir):
logger.error("\"%s\" is not a valid directory", dest_dir)
return None
file_name = "{0}.pem".format(keypair_name)
key_path = os.path.join(dest_dir, file_name)
if os.path.isfile(key_path):
# This mimics the old boto2 keypair.save error
logger.error("\"%s\" already exists in \"%s\" directory",
file_name, dest_dir)
return None
# Open the file before creating the key pair to catch errors early
with open(key_path, "wb") as f:
keypair = create_key_pair(ec2, keypair_name)
f.write(keypair["KeyMaterial"].encode("ascii"))