def __init__(self, file_in, file_out, debug=False): self.data = { 'file': file_in, 'secret': file_out, 'pem': path_expand('~/.ssh/id_rsa.pub.pem'), 'key': path_expand(' ~/.ssh/id_rsa') } self.debug = debug
def encrypt(self): # encrypt the file into secret.txt print(self.data) command = path_expand( "openssl rsautl -encrypt -pubin -inkey {pem} -in {file} -out {secret}" .format(**self.data)) self._execute(command)
def decrypt(self, filename=None): if filename is not None: self.data['secret'] = filename command = path_expand( "openssl rsautl -decrypt -inkey {key} -in {secret}".format( **self.data)) self._execute(command)
def unzip(source_filename, dest_dir): """ unzips a file into the destination directory :param source_filename: :param dest_dir: the destination directory :return: """ with zipfile.ZipFile(source_filename) as zf: for member in zf.infolist(): # Path traversal defense copied from # http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789 words = member.filename.split('/') path = path_expand(dest_dir) for word in words[:-1]: drive, word = os.path.splitdrive(word) head, word = os.path.split(word) if word in (os.curdir, os.pardir, ''): continue path = os.path.join(path, word) zf.extract(member, path)
def mkdir(cls, directory): """ creates a directory with all its parents in ots name :param directory: the path of the directory :return: """ directory = path_expand(directory) try: os.makedirs(directory) except OSError as e: # EEXIST (errno 17) occurs under two conditions when the path exists: # - it is a file # - it is a directory # # if it is a file, this is a valid error, otherwise, all # is fine. if e.errno == errno.EEXIST and os.path.isdir(directory): pass else: raise
def pem_cat(self): command = path_expand("cat {pem}".format(**self.data)) self._execute(command)
def pem_create(self): command = path_expand( "openssl rsa -in {key} -pubout > {pem}".format(**self.data)) self._execute(command)