pyceo/ceod/api/app_factory.py

121 lines
3.8 KiB
Python
Raw Normal View History

2021-08-03 19:19:33 -04:00
import importlib.resources
import os
2021-07-24 17:09:10 -04:00
import socket
from flask import Flask
from zope import component
2021-08-03 19:19:33 -04:00
from .error_handlers import register_error_handlers
2021-07-24 17:09:10 -04:00
from ceo_common.interfaces import IConfig, IKerberosService, ILDAPService, IFileService, \
IMailmanService, IMailService, IUWLDAPService, IHTTPClient, IDatabaseService
2021-07-24 17:09:10 -04:00
from ceo_common.model import Config, HTTPClient, RemoteMailmanService
2021-08-21 02:27:33 -04:00
from ceod.api.spnego import init_spnego
2021-07-24 17:09:10 -04:00
from ceod.model import KerberosService, LDAPService, FileService, \
MailmanService, MailService, UWLDAPService
from ceod.db import MySQLService, PostgreSQLService
2021-07-24 17:09:10 -04:00
def create_app(flask_config={}):
app = Flask(__name__)
app.config.from_mapping(flask_config)
2021-08-13 20:11:56 -04:00
if not app.config.get('TESTING'):
register_services(app)
cfg = component.getUtility(IConfig)
2021-08-21 02:27:33 -04:00
init_spnego('ceod')
2021-08-13 20:11:56 -04:00
hostname = socket.gethostname()
# Only ceod_admin_host should serve the /api/members endpoints because
# it needs to run kadmin
if hostname == cfg.get('ceod_admin_host'):
from ceod.api import members
app.register_blueprint(members.bp, url_prefix='/api/members')
2021-08-18 19:48:17 -04:00
# Only offer mailman API if this host is running Mailman
2021-08-13 20:11:56 -04:00
if hostname == cfg.get('ceod_mailman_host'):
from ceod.api import mailman
app.register_blueprint(mailman.bp, url_prefix='/api/mailman')
if hostname == cfg.get('ceod_database_host'):
from ceod.api import database
app.register_blueprint(database.bp, url_prefix='/api/db')
2021-08-18 19:48:17 -04:00
from ceod.api import groups
app.register_blueprint(groups.bp, url_prefix='/api/groups')
from ceod.api import positions
app.register_blueprint(positions.bp, url_prefix='/api/positions')
2021-08-13 20:11:56 -04:00
from ceod.api import uwldap
app.register_blueprint(uwldap.bp, url_prefix='/api/uwldap')
register_error_handlers(app)
@app.route('/ping')
def ping():
"""Health check"""
return 'pong\n'
return app
def register_services(app):
# Config
2021-08-03 19:19:33 -04:00
if app.config.get('ENV') == 'development' and 'CEOD_CONFIG' not in os.environ:
2021-08-13 20:11:56 -04:00
with importlib.resources.path('tests', 'ceod_dev.ini') as p:
2021-08-03 19:19:33 -04:00
config_file = p.__fspath__()
2021-07-24 17:09:10 -04:00
else:
2021-08-03 19:19:33 -04:00
config_file = None
cfg = Config(config_file)
2021-07-24 17:09:10 -04:00
component.provideUtility(cfg, IConfig)
2021-08-13 20:11:56 -04:00
# KerberosService
2021-07-24 17:09:10 -04:00
hostname = socket.gethostname()
2021-08-13 20:11:56 -04:00
fqdn = socket.getfqdn()
2021-07-24 17:09:10 -04:00
# Only ceod_admin_host has the ceod/admin key in its keytab
if hostname == cfg.get('ceod_admin_host'):
2021-08-13 20:11:56 -04:00
principal = cfg.get('ldap_admin_principal')
2021-07-24 17:09:10 -04:00
else:
2021-08-13 20:11:56 -04:00
principal = f'ceod/{fqdn}'
krb_srv = KerberosService(principal)
2021-07-24 17:09:10 -04:00
component.provideUtility(krb_srv, IKerberosService)
2021-08-13 20:11:56 -04:00
# LDAPService
2021-07-24 17:09:10 -04:00
ldap_srv = LDAPService()
component.provideUtility(ldap_srv, ILDAPService)
2021-08-13 20:11:56 -04:00
# HTTPService
2021-07-24 17:09:10 -04:00
http_client = HTTPClient()
component.provideUtility(http_client, IHTTPClient)
2021-08-13 20:11:56 -04:00
# FileService
2021-08-03 19:19:33 -04:00
if hostname == cfg.get('ceod_fs_root_host'):
2021-07-24 17:09:10 -04:00
file_srv = FileService()
component.provideUtility(file_srv, IFileService)
2021-08-13 20:11:56 -04:00
# MailmanService
2021-08-03 19:19:33 -04:00
if hostname == cfg.get('ceod_mailman_host'):
2021-07-24 17:09:10 -04:00
mailman_srv = MailmanService()
else:
mailman_srv = RemoteMailmanService()
2021-08-13 20:11:56 -04:00
component.provideUtility(mailman_srv, IMailmanService)
2021-07-24 17:09:10 -04:00
2021-08-13 20:11:56 -04:00
# MailService
2021-07-24 17:09:10 -04:00
mail_srv = MailService()
component.provideUtility(mail_srv, IMailService)
2021-08-13 20:11:56 -04:00
# UWLDAPService
2021-07-24 17:09:10 -04:00
uwldap_srv = UWLDAPService()
component.provideUtility(uwldap_srv, IUWLDAPService)
# MySQLService
if hostname == cfg.get('ceod_database_host'):
mysql_srv = MySQLService()
component.provideUtility(mysql_srv, IDatabaseService, 'mysql')
# PostgreSQLService
if hostname == cfg.get('ceod_database_host'):
psql_srv = PostgreSQLService()
component.provideUtility(psql_srv, IDatabaseService, 'postgresql')