def write_password_data( self, filename, overwrite=False, export=False, encrypted_export=False, password=None, ): #################################################################### """Write password data to a password file""" #################################################################### self.validate() # check if the file is in the base password directory incwd = path.basename(filename) == filename # if not ensure path exists try: if not incwd and not path.isdir(path.dirname(filename)): makedirs(path.dirname(filename)) passdata = {key: value for key, value in self.todict().items() if value} open_method = "a" if export else "w" with open(filename, open_method, encoding="ASCII") as fname: if encrypted_export: encrypted = sk_encrypt_string( yaml.safe_dump(passdata, default_flow_style=False), password ) fname.write(encrypted.decode() + "\n") else: fname.write(yaml.safe_dump(passdata, default_flow_style=False)) except (OSError, IOError) as error: raise PasswordIOError(f"Error creating '{filename}'") from error
def write_password_data(self, filename, overwrite=False, encrypted_export=False, password=None): """Write password data to a password file """ ############################################################################################ self.validate() # check if the file is in the base password directory incwd = os.path.basename(filename) == filename # if not ensure path exists try: if not incwd and not os.path.isdir(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) passdata = { key: value for key, value in self.todict().items() if value } with open(filename, 'w') as fname: if encrypted_export: encrypted = crypto.sk_encrypt_string( yaml.safe_dump(passdata, default_flow_style=False), password) fname.write(encrypted + "\n") else: fname.write( yaml.safe_dump(passdata, default_flow_style=False)) except (OSError, IOError): raise PasswordIOError("Error creating '%s'" % filename)
def delete_pass(self): """This deletes a password that the user has created, useful for testing""" ################################################################## filepath = path.join(self.args['pwstore'], self.args['pwname']) try: remove(filepath) except OSError: raise PasswordIOError("Password '%s' not found" % self.args['pwname'])
def delete_pass(self): ################################################################## """This deletes a password that the user has created, useful for testing""" ################################################################## filepath = path.join(self.args["pwstore"], self.args["pwname"]) try: remove(filepath) except OSError as err: raise PasswordIOError( f"Password '{self.args['pwname']}' not found" ) from err
def rename_pass(self): """This renames a password that the user has created""" ################################################################## oldpath = path.join(self.args['pwstore'], self.args['pwname']) newpath = path.join(self.args['pwstore'], self.args['rename']) try: rename(oldpath, newpath) password = PasswordEntry() password.read_password_data(newpath) password['metadata']['name'] = self.args['rename'] password.write_password_data(newpath) except OSError: raise PasswordIOError("Password '%s' not found" % self.args['pwname'])
def _show_wrapper(self, password, myidentity): """Wrapper for show to allow for on behalf of""" #################################################################### if self.args['all']: try: self._walk_dir(self.args['pwstore'], password, myidentity, self.args['ignore_decrypt']) except DecryptionError as err: raise err elif self.args['pwname'] is None: raise PasswordIOError("No password supplied") else: self._decrypt_wrapper(self.args['pwstore'], password, myidentity, self.args['pwname'])
def read_password_data(self, filename): """ Open a password file, load passwords and read metadata """ ########################################################################## try: with open(filename, 'r') as fname: password_data = yaml.safe_load(fname) self.metadata = password_data['metadata'] self.recipients = password_data['recipients'] if 'escrow' in password_data: self.escrow = password_data['escrow'] self.validate() except (OSError, IOError) as error: raise PasswordIOError("Error Opening %s due to %s" % (filename, error.strerror)) except (yaml.scanner.ScannerError, yaml.parser.ParserError) as error: raise YamlFormatError(str(error.problem_mark), error.problem)
def _show_wrapper(self, password): #################################################################### """Wrapper for show to allow for on behalf of""" #################################################################### if self.args["all"]: try: yield from self._walk_dir( self.args["pwstore"], password, self.args["ignore_decrypt"] ) except DecryptionError as err: raise err elif self.args["pwname"] is None: raise PasswordIOError("No password supplied") else: yield from self._decrypt_wrapper( self.args["pwstore"], password, self.args["pwname"] )
def write_password_data_to_file(self, password_data, filename, overwrite=False): """ Write password data and metadata to the appropriate password file """ ############################################################################## try: if not os.path.isdir(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) with open(filename, 'w+') as fname: passdata = { key: value for key, value in password_data.todict().items() if value } fname.write(yaml.dump(passdata, default_flow_style=False)) except (OSError, IOError): raise PasswordIOError("Error creating '%s'" % filename)
def read_password_data_from_file(self, filename): """ Open a password file, load passwords and read metadata """ ############################################################################## try: with open(filename, 'r') as fname: password_data = yaml.safe_load(fname) password_entry = PasswordEntry() password_entry.metadata = password_data['metadata'] password_entry.recipients = password_data['recipients'] if 'escrow' in password_data: password_entry.escrow = password_data['escrow'] password_entry.validate() return password_entry except (OSError, IOError, TypeError): raise PasswordIOError( "Error reading '%s' perhaps a path error for the db, or malformed file" % filename)
def _run_command_execution(self): """ Run function for class. """ #################################################################### password = PasswordEntry() myidentity = self.identities.iddb[self.args['identity']] if self.args['all']: try: self._walk_dir(self.args['pwstore'], password, myidentity, self.args['ignore_decrypt']) except DecryptionError as err: raise err elif self.args['pwname'] is None: raise PasswordIOError("No password supplied") else: self._decrypt_wrapper(self._resolve_directory_path(), password, myidentity, self.args['pwname'])
def rename_pass(self): ################################################################## """This renames a password that the user has created""" ################################################################## oldpath = path.join(self.args["pwstore"], self.args["pwname"]) newpath = path.join(self.args["pwstore"], self.args["rename"]) try: rename(oldpath, newpath) password = PasswordEntry() password.read_password_data(newpath) password["metadata"]["name"] = self.args["rename"] password.write_password_data(newpath) except OSError as err: raise PasswordIOError( f"Password '{self.args['pwname']}' not found" ) from err
def read_password_data(self, filename): #################################################################### """Open a password file, load passwords and read metadata""" #################################################################### try: with open(filename, "r", encoding="ASCII") as fname: password_data = yaml.safe_load(fname) self.metadata = password_data["metadata"] self.recipients = password_data["recipients"] if "escrow" in password_data: self.escrow = password_data["escrow"] self.validate() except (OSError, IOError) as error: raise PasswordIOError( f"Error Opening {filename} due to {error.strerror}" ) from error except (yaml.scanner.ScannerError, yaml.parser.ParserError) as error: raise YamlFormatError(str(error.problem_mark), error.problem) from error
def write_password_data_to_file(self, password_data, filename, overwrite=False): #################################################################### """Write password data and metadata to the appropriate password file""" #################################################################### try: if not path.isdir(path.dirname(filename)): makedirs(path.dirname(filename)) with open(filename, "w+", encoding="ASCII") as fname: passdata = { key: value for key, value in password_data.todict().items() if value } fname.write(dump(passdata, default_flow_style=False)) except (OSError, IOError) as err: raise PasswordIOError(f"Error creating '{filename}'") from err
def read_password_data_from_file(self, filename): #################################################################### """Open a password file, load passwords and read metadata""" #################################################################### try: with open(filename, "r", encoding="ASCII") as fname: password_data = safe_load(fname) password_entry = PasswordEntry() password_entry.metadata = password_data["metadata"] password_entry.recipients = password_data["recipients"] if "escrow" in password_data: password_entry.escrow = password_data["escrow"] password_entry.validate() return password_entry except (OSError, IOError, TypeError) as err: raise PasswordIOError( f"Error reading '{filename}' perhaps a path error for the db, or malformed file" ) from err