def parse_config_file(self, config_file): config = SafeConfigParser(self.DEFAULT_CONFIG) config.readfp(config_file) blessconfig = { 'CLIENT_CONFIG': { 'domain_regex': config.get('CLIENT', 'domain_regex'), 'cache_dir': config.get('CLIENT', 'cache_dir'), 'cache_file': config.get('CLIENT', 'cache_file'), 'mfa_cache_dir': config.get('CLIENT', 'mfa_cache_dir'), 'mfa_cache_file': config.get('CLIENT', 'mfa_cache_file'), 'ip_urls': [ s.strip() for s in config.get('CLIENT', 'ip_urls').split(",") ], 'update_script': config.get('CLIENT', 'update_script'), 'user_session_length': int(config.get('CLIENT', 'user_session_length')), 'usebless_role_session_length': int(config.get('CLIENT', 'usebless_role_session_length')), 'update_sshagent': config.getboolean('CLIENT', 'update_sshagent'), }, 'BLESS_CONFIG': { 'userrole': config.get('LAMBDA', 'user_role'), 'accountid': config.get('LAMBDA', 'account_id'), 'functionname': config.get('LAMBDA', 'functionname'), 'functionversion': config.get('LAMBDA', 'functionversion'), 'certlifetime': config.getint('LAMBDA', 'certlifetime'), 'ipcachelifetime': config.getint('LAMBDA', 'ipcachelifetime'), 'timeoutconfig': { 'connect': config.getint('LAMBDA', 'timeout_connect'), 'read': config.getint('LAMBDA', 'timeout_read') } }, 'AWS_CONFIG': { 'bastion_ips': config.get('MAIN', 'bastion_ips'), 'remote_user': config.get('MAIN', 'remote_user') }, 'REGION_ALIAS': {} } regions = config.get('MAIN', 'region_aliases').split(",") regions = [region.strip() for region in regions] for region in regions: region = region.upper() kms_region_key = 'KMSAUTH_CONFIG_{}'.format(region) blessconfig.update( {kms_region_key: self._get_region_kms_config(region, config)}) blessconfig['REGION_ALIAS'].update( {region: blessconfig[kms_region_key]['awsregion']}) return blessconfig
def configure(filename=None): """This function gives to the user application a chance to define where configuration file should live. Subsequent calls to this function will have no effect, unless you call :func:`reconfigure`. :param str filename: Full path to configuration file. """ global retry if getattr(configure, '_configured', False): return filename = filename or DEFAULT_CONFIG_FILENAME _ensure_directory(filename) parser = SafeConfigParser() if os.path.isfile(filename): with open(filename, 'rt') as fp: parser.readfp(fp) if not parser.has_section(RETRY_SECTION): parser.add_section(RETRY_SECTION) parser.set(RETRY_SECTION, 'max_tries', str(constants.BACKOFF_DEFAULT_MAXTRIES)) parser.set(RETRY_SECTION, 'delay', str(constants.BACKOFF_DEFAULT_DELAY)) parser.set(RETRY_SECTION, 'factor', str(constants.BACKOFF_DEFAULT_FACTOR)) with open(filename, 'wt') as fp: parser.write(fp) retry = RetrySettings(max_tries=parser.getint(RETRY_SECTION, 'max_tries'), delay=parser.getint(RETRY_SECTION, 'delay'), factor=parser.getint(RETRY_SECTION, 'factor')) setattr(configure, '_configured', True) setattr(configure, '_configured_filename', filename)
def parse_config_file(self, config_file): config = SafeConfigParser(self.DEFAULT_CONFIG) config.readfp(config_file) blessconfig = { 'CLIENT_CONFIG': { 'domain_regex': config.get('CLIENT', 'domain_regex'), 'cache_dir': config.get('CLIENT', 'cache_dir'), 'cache_file': config.get('CLIENT', 'cache_file'), 'mfa_cache_dir': config.get('CLIENT', 'mfa_cache_dir'), 'mfa_cache_file': config.get('CLIENT', 'mfa_cache_file'), 'ip_urls': [s.strip() for s in config.get('CLIENT', 'ip_urls').split(",")], 'update_script': config.get('CLIENT', 'update_script'), 'user_session_length': int(config.get('CLIENT', 'user_session_length')), 'usebless_role_session_length': int(config.get('CLIENT', 'usebless_role_session_length')), 'update_sshagent': config.getboolean('CLIENT', 'update_sshagent'), 'use_env_creds': config.getboolean('CLIENT', 'use_env_creds'), }, 'BLESS_CONFIG': { 'ca_backend': config.get('MAIN', 'ca_backend'), 'userrole': config.get('LAMBDA', 'user_role'), 'accountid': config.get('LAMBDA', 'account_id'), 'functionname': config.get('LAMBDA', 'functionname'), 'functionversion': config.get('LAMBDA', 'functionversion'), 'certlifetime': config.getint('LAMBDA', 'certlifetime'), 'ipcachelifetime': config.getint('LAMBDA', 'ipcachelifetime'), 'timeoutconfig': { 'connect': config.getint('LAMBDA', 'timeout_connect'), 'read': config.getint('LAMBDA', 'timeout_read') } }, 'AWS_CONFIG': { 'remote_user': config.get('MAIN', 'remote_user') }, 'REGION_ALIAS': {} } if config.has_option('MAIN', 'bastion_ips'): blessconfig['AWS_CONFIG']['bastion_ips'] = config.get('MAIN', 'bastion_ips') if blessconfig['BLESS_CONFIG']['ca_backend'].lower() == 'hashicorp-vault': blessconfig['VAULT_CONFIG'] = { 'vault_addr': config.get('VAULT', 'vault_addr'), 'auth_mount': config.get('VAULT', 'auth_mount'), 'ssh_backend_mount': config.get('VAULT', 'ssh_backend_mount'), 'ssh_backend_role': config.get('VAULT', 'ssh_backend_role'), } regions = config.get('MAIN', 'region_aliases').split(",") regions = [region.strip() for region in regions] for region in regions: region = region.upper() kms_region_key = 'KMSAUTH_CONFIG_{}'.format(region) blessconfig.update({kms_region_key: self._get_region_kms_config(region, config)}) blessconfig['REGION_ALIAS'].update({region: blessconfig[kms_region_key]['awsregion']}) if config.has_section('HOUSEKEEPER'): hk_region_key = 'HOUSEKEEPER_CONFIG_{}'.format(region) blessconfig.update({hk_region_key: self._get_region_housekeeper_config(region, config)}) blessconfig['REGION_ALIAS'].update({region: blessconfig[hk_region_key]['awsregion']}) return blessconfig
def load_conf(): conf_path = os.path.expanduser(config_path) if not os.path.exists(conf_path): logger.warn("{conf} couldn't be found, please use \'coscmd config -h\' to learn how to config coscmd!".format(conf=to_printable_str(conf_path))) raise IOError else: logger.debug('{conf} is found'.format(conf=to_printable_str(conf_path))) try: with open(conf_path, 'r') as f: cp = SafeConfigParser() cp.readfp(fp=f) if not cp.has_section('common'): raise Exception("[common] section could't be found, please check your config file.") if cp.has_option('common', 'part_size'): part_size = cp.getint('common', 'part_size') else: part_size = 1 if cp.has_option('common', 'max_thread'): max_thread = cp.getint('common', 'max_thread') else: max_thread = 5 try: secret_id = cp.get('common', 'secret_id') except Exception: secret_id = cp.get('common', 'access_id') try: appid = cp.get('common', 'appid') bucket = cp.get('common', 'bucket') if bucket.endswith("-"+str(appid)): bucket = bucket.rstrip(appid) bucket = bucket[:-1] except Exception: try: bucket = cp.get('common', 'bucket') appid = bucket.split('-')[-1] bucket = bucket.rstrip(appid) bucket = bucket[:-1] except Exception: # check if user use -b bucket if (pre_bucket == ""): logger.error("The configuration file is wrong. Please reconfirm") try: schema = cp.get('common', 'schema') except Exception: schema = 'https' try: verify = cp.get('common', 'verify') except Exception: verify = 'md5' try: token = cp.get('common', 'token') except Exception: token = None try: error = cp.get('common', 'error') except Exception: error = "cos.err.log" try: success = cp.get('common', 'success') except Exception: success = "cos.suc.log" try: anonymous = cp.get('common', 'anonymous') if anonymous == 'True' or anonymous == 'true': anonymous = True else: anonymous = False except Exception: anonymous = False try: retry = int(cp.get('common', 'retry')) except Exception: retry = 2 try: timeout = int(cp.get('common', 'timeout')) except Exception: timeout = 60 region, endpoint = None, None if cp.has_option('common', 'region'): region = cp.get('common', 'region') if cp.has_option('common', 'endpoint'): endpoint = cp.get('common', 'endpoint') if pre_appid != "": appid = pre_appid if pre_bucket != "": bucket = pre_bucket if pre_region != "": region = pre_region conf = CoscmdConfig( appid=appid, secret_id=secret_id, secret_key=cp.get('common', 'secret_key'), token=token, region=compatible(region), endpoint=endpoint, bucket=bucket, part_size=part_size, max_thread=max_thread, schema=schema, anonymous=anonymous, verify=verify, retry=retry, timeout=timeout, error=error, success=success ) return conf except Exception as e: raise(e)
def load_conf(): conf_path = os.path.expanduser(config_path) if not os.path.exists(conf_path): logger.warn( "{conf} couldn't be found, please use \'coscmd config -h\' to learn how to config coscmd!" .format(conf=to_printable_str(conf_path))) raise IOError else: logger.debug( '{conf} is found'.format(conf=to_printable_str(conf_path))) with open(conf_path, 'r') as f: cp = SafeConfigParser() cp.readfp(fp=f) if cp.has_option('common', 'part_size'): part_size = cp.getint('common', 'part_size') else: part_size = 1 if cp.has_option('common', 'max_thread'): max_thread = cp.getint('common', 'max_thread') else: max_thread = 5 try: secret_id = cp.get('common', 'secret_id') except Exception: secret_id = cp.get('common', 'access_id') try: appid = cp.get('common', 'appid') bucket = cp.get('common', 'bucket') if bucket.endswith("-" + str(appid)): bucket = bucket.rstrip(appid) bucket = bucket[:-1] except Exception: try: bucket = cp.get('common', 'bucket') appid = bucket.split('-')[-1] bucket = bucket.rstrip(appid) bucket = bucket[:-1] except Exception: logger.error( "The configuration file is wrong. Please reconfirm") try: schema = cp.get('common', 'schema') except: schema = 'https' try: anonymous = cp.get('common', 'anonymous') except: anonymous = 'False' region, endpoint = None, None if cp.has_option('common', 'region'): region = cp.get('common', 'region') else: endpoint = cp.get('common', 'endpoint') if pre_appid != "": appid = pre_appid if pre_bucket != "": bucket = pre_bucket if pre_region != "": region = pre_region conf = CosConfig(appid=appid, secret_id=secret_id, secret_key=cp.get('common', 'secret_key'), region=compatible(region), endpoint=endpoint, bucket=bucket, part_size=part_size, max_thread=max_thread, schema=schema, anonymous=anonymous) return conf