def __init__(self): self.DEBUG = False self.DEVELOPMENT = False self.TESTING = False # Mail server config self.MAIL_SERVER = 'smtp.gmail.com' self.MAIL_PORT = 465 self.MAIL_USE_TLS = False self.MAIL_USE_SSL = True # Mail credentials and recipient try: kr = CryptFileKeyring() kr.file_path = os.path.join(os.getcwd(), 'keyring') self.MAIL_USERNAME = kr.get_password("scheduled_mail", "MAIL_USER") self.MAIL_PASSWORD = kr.get_password("scheduled_mail", "MAIL_PASSWORD") self.RECIPIENT = kr.get_password("scheduled_mail", "RECIPIENT") except ValueError as e: raise KeyringError("Error while accessing the keyring.") from e # Scheduler settings self.SCHEDULER_INTERVAL = 1 # Config file for instances self.INSTANCE_PATH = "instances.json" self.INSTANCE_START_DATE = datetime(2018, 6, 19, 0, 0, 0)
def run(self, argv): # parse args, setup logging and prepare keyrings args = self.parser.parse_args(argv) self.setup_logging(args.verbose) inkr = CryptFileKeyring() outkr = CryptFileKeyring() outkr.aesmode = args.aesmode # prepare infile infile = args.infile if not infile: infile = inkr.file_path else: inkr.file_path = infile inkr.filename = os.path.basename(infile) if not os.path.exists(infile): self.errexit('%s not found' % infile) if not inkr._check_file(): self.errexit('Failed to parse %s' % infile) log.info('infile %s: %s', infile, inkr.scheme) # prepare outfile outfile = args.outfile if not outfile: outfile = infile + '.%d' % os.getpid() if os.path.exists(outfile): if os.path.samefile(infile, outfile): self.errexit('infile and outfile must NOT be the same file') # outfile exists: rename os.rename(outfile, outfile + '~') log.info('%s renamed to %s~', outfile, outfile) outkr.file_path = outfile outkr.filename = os.path.basename(outfile) log.info('outfile %s: %s', outfile, outkr.scheme) # unlock the infile keyring try: inkr.keyring_key except ValueError as e: self.errexit('Unlock %s: %s' % (infile, e)) # keep old password or request password for new keyring if args.keep: outkr._get_new_password = lambda: inkr.keyring_key else: outkr.keyring_key # process infile config = configparser.RawConfigParser() config.read(infile) for section in config.sections(): log.debug('process section: [%s]', section) if section != escape('keyring-setting'): for username in config.options(section): username = unescape(username) section = unescape(section) log.info('process: [%s] %s', section, username) password = inkr.get_password(section, username) if password: outkr.set_password(section, username, password) log.debug('[%s] %s: %s', section, username, password) else: log.error('invalid entry: [%s]%s', section, username) return 0