def _load_from_directory(self, fpath, filetype): ####################################################################### """Helper function to read in (keys|certs) and store them correctly""" ####################################################################### try: print("Loading certs into database") for fname in tqdm(listdir(fpath)): if fname.endswith(tuple(self.extensions[filetype])): uid = fname.split(".")[0] filepath = path.join(fpath, fname) if filetype == "certificate": self.load_db( uid, certlist=[x.as_text() for x in parse_file(filepath)] ) elif filetype == "key": session = sessionmaker(bind=self.args["db"]["engine"])() create_or_update( session, Recipient, unique_identifiers=["name"], **{ "name": uid, "key": filepath, }, ) session.commit() except OSError as error: raise FileOpenError(fpath, str(error.strerror)) from error
def _run_command_setup(self, parsedargs): ################################################################## """ Passes the argparse Namespace object of parsed arguments """ ################################################################## # Build a dict out of the argparse args Namespace object and a dict from any # configuration files and merge the two with cli taking priority cli_args = vars(parsedargs) config_args = self._get_config_args(cli_args['config'], cli_args) self.args.update(config_args) fles = ['cabundle', 'pwstore'] for key, value in iteritems(cli_args): if value is not None or key not in self.args: self.args[key] = value if key in fles and not os.path.exists(self.args[key]): raise FileOpenError(self.args[key], "No such file or directory") # json args connectmap = self._parse_json_arguments('connect') # self.args['color'] = self.args['color'].upper() == 'TRUE' if 'color' in self.args and self.args['color'] is not None else True self.args['color'] = self._handle_boolean_args('color') self._convert_strings_to_list('groups') self._convert_strings_to_list('users') self._convert_strings_to_list('escrow_users') self._validate_combinatorial_args() self._validate_args() verify_on_load = self.args['subparser_name'] in ['listrecipients', 'import', 'interpreter'] # Build the list of recipients that this command will act on self._build_recipient_list() # If there are defined repositories of keys and certificates, load them if not self.iddbcached: self.identities.cabundle = self.args['cabundle'] self.identities.load_certs_from_directory( self.args['certpath'], verify_on_load=verify_on_load, connectmap=connectmap) self.identities.load_keys_from_directory(self.args['keypath']) self._validate_identities() self.args['card_slot'] = self.args['card_slot'] if self.args['card_slot'] else 0 if 'nopassphrase' in self.selected_args and not self.args['nopassphrase']: if self.args['verbosity'] != -1: print_card_info(self.args['card_slot'], self.identities.iddb[self.args['identity']], self.args['verbosity'], self.args['color'], self.args['theme_map']) self.passphrase = getpass.getpass("Enter Pin/Passphrase: ")
def _load_from_directory(self, fpath, filetype): """ Helper function to read in (keys|certs) and store them correctly """ ####################################################################### try: for fname in listdir(fpath): if fname.endswith(tuple(self.extensions[filetype])): uid = fname.split('.')[0] filepath = path.join(fpath, fname) try: self.iddb[uid]["%s_path" % filetype] = filepath except KeyError as error: identity = {'uid': fname.split('.')[0], "%s_path" % filetype: filepath} self.iddb[identity['uid']] = identity except OSError as error: raise FileOpenError(fpath, str(error.strerror))
def collect_args(parsedargs): ################################################################## """Build a dict out of the argparse args Namespace object and a dict from any configuration files and merge the two with cli taking priority""" ################################################################## args = { "ignore_decrypt": False, "identity": getuser(), "cabundle": "./certs/ca-bundle", "keypath": "./private", "pwstore": "./passwords", "time": 10, "card_slot": None, "certpath": None, "escrow_users": None, "min_escrow": None, "no_cache": False, "noverify": None, "noescrow": False, "overwrite": False, "recovery": False, "rules": "default", "stdin": False, "theme_map": None, "color": True, "verbosity": 0, } cli_args = parsedargs if isinstance(parsedargs, dict) else vars(parsedargs) config_args = get_config_args(cli_args["config"], cli_args) args.update(config_args) args["connect"] = parse_json_arguments(args, "connect") args = handle_filepath_args(args) fles = ["cabundle", "pwstore"] for key, value in cli_args.items(): if value is not None or key not in args: args[key] = value if key in fles and not path.exists(args[key]): raise FileOpenError(args[key], "No such file or directory") # json args args["color"] = handle_boolean_args(args, "color") args["groups"] = convert_strings_to_list(args, "groups") args["users"] = convert_strings_to_list(args, "users") args["escrow_users"] = convert_strings_to_list(args, "escrow_users") return setup_db(args)
def collect_args(parsedargs): ################################################################## # Build a dict out of the argparse args Namespace object and a dict from any # configuration files and merge the two with cli taking priority args = { 'ignore_decrypt': False, 'identity': getuser(), 'cabundle': './certs/ca-bundle', 'keypath': './private', 'pwstore': './passwords', 'time': 10, 'card_slot': None, 'certpath': None, 'escrow_users': None, 'min_escrow': None, 'no_cache': False, 'noverify': None, 'noescrow': False, 'overwrite': False, 'recovery': False, 'rules': 'default', 'stdin': False, 'theme_map': None, 'color': True, 'verbosity': 0, } cli_args = parsedargs if isinstance(parsedargs, dict) else vars(parsedargs) config_args = get_config_args(cli_args['config'], cli_args) args.update(config_args) args['connect'] = parse_json_arguments(args, 'connect') args = handle_filepath_args(args) fles = ['cabundle', 'pwstore'] for key, value in cli_args.items(): if value is not None or key not in args: args[key] = value if key in fles and not path.exists(args[key]): raise FileOpenError(args[key], "No such file or directory") # json args args['color'] = handle_boolean_args(args, 'color') args['groups'] = convert_strings_to_list(args, 'groups') args['users'] = convert_strings_to_list(args, 'users') args['escrow_users'] = convert_strings_to_list(args, 'escrow_users') return args
def _run_command_execution(self): #################################################################### """Run function for class.""" #################################################################### try: contents = "" with open(self.args["pwfile"], "r", encoding="ASCII") as fcontents: contents = fcontents.read().strip() if self.args["nocrypto"]: self._file_handler(contents) else: passwd = getpass.getpass("Please enter the password for the file: ") passwords = contents.split("\n") for password in tqdm(passwords): self._file_handler(sk_decrypt_string(password, passwd)) except IOError as err: raise FileOpenError( self.args["pwfile"], "No such file or directory" ) from err
def _run_command_execution(self): #################################################################### """ Run function for class. """ #################################################################### try: contents = "" with open(self.args['pwfile'], 'r') as fcontents: contents = fcontents.read().strip() if self.args['nocrypto']: self._file_handler(contents) else: passwd = getpass.getpass( "Please enter the password for the file: ") passwords = [] for password in contents.split("\n"): passwords.append(crypto.sk_decrypt_string( password, passwd)) self._file_handler("\n".join(passwords)) except IOError: raise FileOpenError(self.args['pwfile'], "No such file or directory")
def _run_command_execution(self): """ Run function for class. """ #################################################################### try: contents = "" with open(self.args['pwfile'], 'r') as fcontents: contents = fcontents.read().strip() if self.args['nocrypto']: self._file_handler(contents) else: passwd = getpass.getpass( "Please enter the password for the file: ") i = 1 passwords = contents.split("\n") db_len = len(passwords) for password in passwords: self._file_handler( crypto.sk_decrypt_string(password, passwd)) self.progress_bar(i, db_len) i += 1 print("") except IOError: raise FileOpenError(self.args['pwfile'], "No such file or directory")