summaryrefslogtreecommitdiffstats
path: root/snaps/file_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'snaps/file_utils.py')
-rw-r--r--snaps/file_utils.py76
1 files changed, 75 insertions, 1 deletions
diff --git a/snaps/file_utils.py b/snaps/file_utils.py
index ff2f1b3..699d378 100644
--- a/snaps/file_utils.py
+++ b/snaps/file_utils.py
@@ -14,6 +14,9 @@
# limitations under the License.
import os
import logging
+
+from cryptography.hazmat.primitives import serialization
+
try:
import urllib.request as urllib
except ImportError:
@@ -65,7 +68,8 @@ def download(url, dest_path, name=None):
raise
try:
with open(dest, 'wb') as download_file:
- logger.debug('Saving file to - ' + os.path.abspath(download_file.name))
+ logger.debug('Saving file to - %s',
+ os.path.abspath(download_file.name))
response = __get_url_response(url)
download_file.write(response.read())
return download_file
@@ -74,6 +78,76 @@ def download(url, dest_path, name=None):
download_file.close()
+def save_keys_to_files(keys=None, pub_file_path=None, priv_file_path=None):
+ """
+ Saves the generated RSA generated keys to the filesystem
+ :param keys: the keys to save generated by cryptography
+ :param pub_file_path: the path to the public keys
+ :param priv_file_path: the path to the private keys
+ """
+ if keys:
+ if pub_file_path:
+ # To support '~'
+ pub_expand_file = os.path.expanduser(pub_file_path)
+ pub_dir = os.path.dirname(pub_expand_file)
+
+ if not os.path.isdir(pub_dir):
+ os.mkdir(pub_dir)
+
+ public_handle = None
+ try:
+ public_handle = open(pub_expand_file, 'wb')
+ public_bytes = keys.public_key().public_bytes(
+ serialization.Encoding.OpenSSH,
+ serialization.PublicFormat.OpenSSH)
+ public_handle.write(public_bytes)
+ finally:
+ if public_handle:
+ public_handle.close()
+
+ os.chmod(pub_expand_file, 0o400)
+ logger.info("Saved public key to - " + pub_expand_file)
+ if priv_file_path:
+ # To support '~'
+ priv_expand_file = os.path.expanduser(priv_file_path)
+ priv_dir = os.path.dirname(priv_expand_file)
+ if not os.path.isdir(priv_dir):
+ os.mkdir(priv_dir)
+
+ private_handle = None
+ try:
+ private_handle = open(priv_expand_file, 'wb')
+ private_handle.write(
+ keys.private_bytes(
+ encoding=serialization.Encoding.PEM,
+ format=serialization.PrivateFormat.TraditionalOpenSSL,
+ encryption_algorithm=serialization.NoEncryption()))
+ finally:
+ if private_handle:
+ private_handle.close()
+
+ os.chmod(priv_expand_file, 0o400)
+ logger.info("Saved private key to - " + priv_expand_file)
+
+
+def save_string_to_file(string, file_path, mode=None):
+ """
+ Stores
+ :param string: the string contents to store
+ :param file_path: the file path to create
+ :param mode: the file's mode
+ :return: the file object
+ """
+ save_file = open(file_path, 'w')
+ try:
+ save_file.write(string)
+ if mode:
+ os.chmod(file_path, mode)
+ return save_file
+ finally:
+ save_file.close()
+
+
def get_content_length(url):
"""
Returns the number of bytes to be downloaded from the given URL