2019-12-30 08:22:43 +00:00
|
|
|
import yaml
|
2020-01-05 13:15:02 +00:00
|
|
|
import sys
|
2020-03-10 08:19:11 +00:00
|
|
|
from .logger import getDebugLogger
|
|
|
|
DEBUG = getDebugLogger()
|
2020-01-05 13:15:02 +00:00
|
|
|
|
|
|
|
default = {
|
|
|
|
'AccessTokenLifeTime': 1800,
|
|
|
|
'Mail': {
|
|
|
|
'URL': '',
|
|
|
|
'port': 0,
|
|
|
|
'user': '',
|
|
|
|
'passwd': '',
|
2020-02-26 21:13:44 +00:00
|
|
|
'email': '',
|
|
|
|
'crypt': 'STARTTLS'
|
2020-01-05 13:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 08:22:43 +00:00
|
|
|
|
2020-03-10 10:08:24 +00:00
|
|
|
|
2019-12-30 08:22:43 +00:00
|
|
|
class ConifgParser():
|
|
|
|
def __init__(self, file='config.yml'):
|
|
|
|
self.file = file
|
|
|
|
with open(file, 'r') as f:
|
2020-01-05 13:15:02 +00:00
|
|
|
self.config = yaml.safe_load(f)
|
|
|
|
|
|
|
|
if 'Database' not in self.config:
|
2020-03-10 10:08:24 +00:00
|
|
|
self.__error__(
|
|
|
|
'Wrong Configuration for Database. You should configure databaseconfig with "URL", "user", "passwd", "database"')
|
2020-01-05 13:15:02 +00:00
|
|
|
if 'URL' not in self.config['Database'] or 'user' not in self.config['Database'] or 'passwd' not in self.config['Database'] or 'database' not in self.config['Database']:
|
2020-03-10 10:08:24 +00:00
|
|
|
self.__error__(
|
|
|
|
'Wrong Configuration for Database. You should configure databaseconfig with "URL", "user", "passwd", "database"')
|
2019-12-30 08:22:43 +00:00
|
|
|
|
|
|
|
self.db = self.config['Database']
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.debug("Set Databaseconfig: {}".format(self.db))
|
2020-01-05 13:15:02 +00:00
|
|
|
|
|
|
|
if 'LDAP' not in self.config:
|
2020-03-10 10:08:24 +00:00
|
|
|
self.__error__(
|
2020-06-28 10:59:06 +00:00
|
|
|
'Wrong Configuration for LDAP. You should configure ldapconfig with "URL" and "BIND_DN"')
|
|
|
|
if 'URL' not in self.config['LDAP'] or 'DN' not in self.config['LDAP']:
|
2020-03-10 10:08:24 +00:00
|
|
|
self.__error__(
|
2020-06-28 10:59:06 +00:00
|
|
|
'Wrong Configuration for LDAP. You should configure ldapconfig with "URL" and "BIND_DN"')
|
|
|
|
if 'PORT' not in self.config['LDAP']:
|
2020-03-10 10:08:24 +00:00
|
|
|
DEBUG.info(
|
|
|
|
'No Config for port in LDAP found. Set it to default: {}'.format(389))
|
2020-06-28 10:59:06 +00:00
|
|
|
self.config['LDAP']['PORT'] = 389
|
2020-06-28 10:31:58 +00:00
|
|
|
if 'ADMIN_DN' not in self.config['LDAP']:
|
|
|
|
DEBUG.info(
|
|
|
|
'No Config for ADMIN_DN in LDAP found. Set it to default {}. (Maybe Password reset not working)'.format(None)
|
|
|
|
)
|
|
|
|
self.config['LDAP']['ADMIN_DN'] = None
|
|
|
|
if 'ADMIN_SECRET' not in self.config['LDAP']:
|
|
|
|
DEBUG.info(
|
|
|
|
'No Config for ADMIN_SECRET in LDAP found. Set it to default {}. (Maybe Password reset not working)'.format(None)
|
|
|
|
)
|
|
|
|
self.config['LDAP']['ADMIN_SECRET'] = None
|
|
|
|
if 'USER_DN' not in self.config['LDAP']:
|
|
|
|
DEBUG.info(
|
|
|
|
'No Config for USER_DN in LDAP found. Set it to default {}. (Maybe Password reset not working)'.format(None)
|
|
|
|
)
|
|
|
|
self.config['LDAP']['USER_DN'] = None
|
2020-06-28 10:59:06 +00:00
|
|
|
if 'BIND_DN' not in self.config['LDAP']:
|
|
|
|
DEBUG.info(
|
|
|
|
'No Config for BIND_DN in LDAP found. Set it to default {}. (Maybe Password reset not working)'.format(None)
|
|
|
|
)
|
|
|
|
self.config['LDAP']['BIND_DN'] = None
|
|
|
|
if 'BIND_SECRET' not in self.config['LDAP']:
|
|
|
|
DEBUG.info(
|
|
|
|
'No Config for BIND_SECRET in LDAP found. Set it to default {}. (Maybe Password reset not working)'.format(None)
|
|
|
|
)
|
|
|
|
self.config['LDAP']['BIND_SECRET'] = None
|
|
|
|
if 'SSL' not in self.config['LDAP']:
|
|
|
|
DEBUG.info(
|
|
|
|
'No Config for SSL in LDAP found. Set it to default {}. (Maybe Password reset not working)'.format(False)
|
|
|
|
)
|
|
|
|
self.config['LDAP']['SSL'] = False
|
|
|
|
else:
|
|
|
|
self.config['LDAP']['SSL'] = bool(self.config['LDAP']['SSL'])
|
2019-12-30 08:22:43 +00:00
|
|
|
self.ldap = self.config['LDAP']
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.info("Set LDAPconfig: {}".format(self.ldap))
|
2020-01-05 13:15:02 +00:00
|
|
|
if 'AccessTokenLifeTime' in self.config:
|
2020-01-18 23:37:40 +00:00
|
|
|
self.accessTokenLifeTime = int(self.config['AccessTokenLifeTime'])
|
2020-03-10 10:08:24 +00:00
|
|
|
DEBUG.info("Set AccessTokenLifeTime: {}".format(
|
|
|
|
self.accessTokenLifeTime))
|
2020-01-05 13:15:02 +00:00
|
|
|
else:
|
|
|
|
self.accessTokenLifeTime = default['AccessTokenLifeTime']
|
2020-03-10 10:08:24 +00:00
|
|
|
DEBUG.info("No Config for AccessTokenLifetime found. Set it to default: {}".format(
|
|
|
|
self.accessTokenLifeTime))
|
2020-01-05 13:15:02 +00:00
|
|
|
|
|
|
|
if 'Mail' not in self.config:
|
|
|
|
self.config['Mail'] = default['Mail']
|
2020-03-10 10:08:24 +00:00
|
|
|
DEBUG.info('No Conifg for Mail found. Set it to defaul: {}'.format(
|
|
|
|
self.config['Mail']))
|
2020-01-05 13:15:02 +00:00
|
|
|
if 'URL' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['URL'] = default['Mail']['URL']
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.info("No Config for URL in Mail found. Set it to default")
|
2020-01-05 13:15:02 +00:00
|
|
|
if 'port' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['port'] = default['Mail']['port']
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.info("No Config for port in Mail found. Set it to default")
|
2020-01-05 13:15:02 +00:00
|
|
|
else:
|
|
|
|
self.config['Mail']['port'] = int(self.config['Mail']['port'])
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.info("No Conifg for port in Mail found. Set it to default")
|
2020-01-05 13:15:02 +00:00
|
|
|
if 'user' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['user'] = default['Mail']['user']
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.info("No Config for user in Mail found. Set it to default")
|
2020-01-05 13:15:02 +00:00
|
|
|
if 'passwd' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['passwd'] = default['Mail']['passwd']
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.info("No Config for passwd in Mail found. Set it to default")
|
2020-01-05 13:15:02 +00:00
|
|
|
if 'email' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['email'] = default['Mail']['email']
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.info("No Config for email in Mail found. Set it to default")
|
2020-02-26 21:13:44 +00:00
|
|
|
if 'crypt' not in self.config['Mail']:
|
|
|
|
self.config['Mail']['crypt'] = default['Mail']['crypt']
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.info("No Config for crypt in Mail found. Set it to default")
|
2020-01-05 13:15:02 +00:00
|
|
|
self.mail = self.config['Mail']
|
2020-03-09 18:54:51 +00:00
|
|
|
DEBUG.info('Set Mailconfig: {}'.format(self.mail))
|
2020-01-05 13:15:02 +00:00
|
|
|
|
2019-12-30 08:22:43 +00:00
|
|
|
def getLDAP(self):
|
|
|
|
return self.ldap
|
|
|
|
|
|
|
|
def getDatabase(self):
|
|
|
|
return self.db
|
|
|
|
|
|
|
|
def getAccessToken(self):
|
|
|
|
return self.accessTokenLifeTime
|
|
|
|
|
2020-01-05 13:15:02 +00:00
|
|
|
def getMail(self):
|
|
|
|
return self.mail
|
|
|
|
|
|
|
|
def __error__(self, msg):
|
2020-03-10 08:19:11 +00:00
|
|
|
DEBUG.error(msg, exc_info=True)
|
2020-01-05 13:15:02 +00:00
|
|
|
sys.exit(-1)
|
|
|
|
|
2020-03-10 10:08:24 +00:00
|
|
|
|
2019-12-30 08:22:43 +00:00
|
|
|
if __name__ == '__main__':
|
2020-03-10 10:08:24 +00:00
|
|
|
ConifgParser()
|