From 1a80b3698f9918d8c92ab027c88c7ab1fa45a0ee Mon Sep 17 00:00:00 2001 From: bdzim Date: Wed, 19 Aug 2020 08:42:46 -0600 Subject: [PATCH 1/2] add a script to delete users by email --- bin/purge_users_by_email.py | 96 ++++++++++++++++++++++++++++++ netkes/account_mgr/accounts_api.py | 2 +- 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 bin/purge_users_by_email.py diff --git a/bin/purge_users_by_email.py b/bin/purge_users_by_email.py new file mode 100644 index 0000000..a0810dd --- /dev/null +++ b/bin/purge_users_by_email.py @@ -0,0 +1,96 @@ +import logging +from optparse import OptionParser, OptionGroup + +from account_mgr.accounts_api import Api +from common import read_config_file, merge_config, set_config, validate_config, NetKesConfigError + + +def _initialize_logging(verbose=False): + handler = logging.StreamHandler() + + formatter = logging.Formatter( + '%(asctime)s %(levelname)-8s %(name)-20s: %(message)s') + handler.setFormatter(formatter) + logging.root.addHandler(handler) + logging.root.setLevel(logging.INFO) + + +def parse_cmdline(): + parser = OptionParser() + + config_group = OptionGroup(parser, "General Configuration Options", + "These control the configuration of the overall SpiderOak Blue system.") + config_group.add_option("--config", dest="config_file", + help="The location of the JSON configuration file.", + metavar="FILE") + config_group.add_option("--emails", dest="email_file", + help="The location of the file containing the emails of users to purge.", + metavar="FILE") + config_group.add_option("--dry-run", dest="dry_run", action="store_true", default=False, + help="Only display actions to be taken - do not actually perform purging.") + + parser.add_option_group(config_group) + + options, _ = parser.parse_args() + if not options.config_file: + parser.error("Missing required argument --config") + if not options.email_file: + parser.error("Missing required argument --emails") + + # Prune it up a bit and return it as a dict. + optdict = vars(options) + for key in optdict.keys(): + if optdict[key] is None: + del optdict[key] + + return optdict + + +def process_config(): + cmdline_opts = parse_cmdline() + + config = read_config_file(cmdline_opts.get('config_file', None)) + + try: + validate_config(config) + except NetKesConfigError, e: + raise e + + with open(cmdline_opts['email_file']) as f: + emails = f.readlines() + + return config, emails, cmdline_opts['dry_run'] + + +def main(): + config, emails, dry_run = process_config() + _initialize_logging() + log = logging.getLogger('purge_users_by_email') + + api = Api.create( + config['api_root'], + config['api_user'], + config['api_password'], + ) + + for email in emails: + email = email.strip() + try: + user = api.get_user(email) + except Api.NotFound: + log.error('Unable to find user: "%s"', email) + continue + if user['purgehold_active']: + log.info('Skipping user "%s" because purgehold is active', email) + else: + log.info('Purging user "%s"', email) + if not dry_run: + try: + api.delete_user(email) + except: + log.error('Error purging user "%s"', email) + log.info('User "%s" has been purged', email) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/netkes/account_mgr/accounts_api.py b/netkes/account_mgr/accounts_api.py index d567520..12ba2b0 100644 --- a/netkes/account_mgr/accounts_api.py +++ b/netkes/account_mgr/accounts_api.py @@ -29,7 +29,7 @@ def wrapper(*args, **kwargs): return func(*args, **kwargs) except Error: log = logging.getLogger('accounts_api') - log.exception('%s - %s - %s' % (func.__name__, args, kwargs)) + log.debug('%s - %s - %s' % (func.__name__, args, kwargs)) raise wrapper.__name__ = func.__name__ return wrapper From e9c1914dd0e412004d027d5635bf17da3914a0df Mon Sep 17 00:00:00 2001 From: bdzim Date: Wed, 19 Aug 2020 11:09:09 -0600 Subject: [PATCH 2/2] improve purge email script logging --- bin/purge_users_by_email.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/purge_users_by_email.py b/bin/purge_users_by_email.py index a0810dd..c490fcf 100644 --- a/bin/purge_users_by_email.py +++ b/bin/purge_users_by_email.py @@ -83,13 +83,14 @@ def main(): if user['purgehold_active']: log.info('Skipping user "%s" because purgehold is active', email) else: - log.info('Purging user "%s"', email) - if not dry_run: + if dry_run: + log.info('Dry run - simulating purging user "%s"', email) + else: try: api.delete_user(email) + log.info('User "%s" has been purged', email) except: log.error('Error purging user "%s"', email) - log.info('User "%s" has been purged', email) if __name__ == "__main__":