Commit 265185fc authored by lucas.tieman's avatar lucas.tieman
Browse files

initial commit, encryption working need to add readme.md

parents
##this is a sample data set, this is not used in production
MAPP_SFTP_KEY_LOCAL_NAME="o2fTo7DAtKQWtSUc858dVxnYI1i5xPd8xw_wpR1y_sE="
MAPP_SFTP_FILE_PATTERN_LOCAL_NAME="Phil"
MAPP_SFTP_EMAIL_LOCAL_NAME="lucas.tieman@seiu.org"
\ No newline at end of file
from dotenv import load_dotenv
from sys import argv
load_dotenv('.env')
import os
from cryptography.fernet import Fernet
#gets credentials and related items from environment variables
keys = {k[14:]: v for k,v in os.environ.items() if 'MAPP_SFTP_KEY_' == k[0:14]}
file_patterns = {k[23:]: v for k, v in os.environ.items() if 'MAPP_SFTP_FILE_PATTERN_' == k[0:23]}
emails = {k[15:] for k, v in os.environ.items() if 'MAPP_SFTP_EMAIL_' == k[0:16]}
def encrypt_file(key, in_filename, out_filename=None):
""" Encrypts a file using AES128 (CBC mode) with the
given key.
key:
The encryption key generated by Fernet
in_filename:
Name of the input file
out_filename:
If None, '<in_filename>.enc' will be used.
"""
if not out_filename:
out_filename = in_filename + '.enc'
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
for row in infile:
encryptor=Fernet(key.encode('utf-8'))
row = encryptor.encrypt(row)
outfile.write(row)
outfile.write(b'\n')
def decrypt_file(key, in_filename, out_filename=None, chunksize=16):
""" Decrypts a file using AES128 (CBC mode) with the
given key. Parameters are similar to encrypt_file,
with one difference: out_filename, if not supplied
will be in_filename without its last extension
(i.e. if in_filename is 'aaa.zip.enc' then
out_filename will be 'aaa.zip')
"""
if not out_filename:
out_filename = os.path.splitext(in_filename)[0]
encryptor = Fernet(key.encode('utf-8'))
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
for row in infile:
row = encryptor.decrypt(row)
outfile.write(row)
def encrypt(file_path,new_path=None):
"""
:param file_in: path (either relative or absolute) to the file that needs to be encrypted
:return: saved file to local path with name matching file pattern expected by MAPP
"""
#opens the file passed in when the script is initialized
if not new_path:
new_path = file_path
for file in os.listdir(file_path):
if file == '.keep':
continue
try:
check = [
k for k, v in file_patterns.items() if v in file
]
l = len(check)
if l == 1:
pass
elif l > 1:
raise EnvironmentError('Matches More Than One File Pattern')
elif l == 0:
raise ValueError('File Does Not Match A File Pattern')
encrypt_file(keys[check[0]],"{}/{}".format(file_path,file),"{}/{}".format(new_path,file))
except:
print('Encryption Failed')
os.remove("{}/{}".format(file_path,file))
def decrypt(file_path, new_path=None):
"""
:param file_in: path (either relative or absolute) to the file that needs to be decrypted
:return: saved file to local path with name matching file pattern expected by MAPP
"""
if not new_path:
new_path = file_path
for file in os.listdir(file_path):
if file == '.keep':
continue
try:
check = [
k for k, v in file_patterns.items() if v in file
]
l = len(check)
if l == 1:
pass
elif l > 1:
raise EnvironmentError('Matches More Than One File Pattern')
elif l == 0:
raise ValueError('File Does Not Match A File Pattern')
decrypt_file(keys[check[0]],"{}/{}".format(file_path,file),"{}/{}".format(new_path,file))
except:
print('Decryption Failed, In Production This Would Generate Emails')
os.remove("{}/{}".format(file_path,file))
def validator():
#print(argv)
if argv[1].lower() not in ['encrypt','decrypt']:
print("Mode not Recognized, use [encrypt,decrypt] [existing path (default in) ] [new path (default out)]")
exit(0)
#put logic for checking file paths here
if __name__ == '__main__':
validator()
if len(argv) == 2:
old_path = 'in'
new_path = 'out'
else:
try:
old_path = argv[2]
new_path = argv[3]
except:
old_path = argv[2]
new_path = argv[2]
if argv[1].lower() == 'encrypt':
encrypt(old_path, new_path)
elif argv[1].lower() == 'decrypt':
decrypt(old_path, new_path)
cryptography
python-dotenv
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment