| 1 | """TurboGears middleware initialization""" |
|---|
| 2 | from sipbmp3web.config.app_cfg import base_config |
|---|
| 3 | from sipbmp3web.config.environment import load_environment |
|---|
| 4 | import subprocess, os |
|---|
| 5 | from pylons import config |
|---|
| 6 | |
|---|
| 7 | #Use base_config to setup the necessary WSGI App factory. |
|---|
| 8 | #make_base_app will wrap the TG2 app with all the middleware it needs. |
|---|
| 9 | make_base_app = base_config.setup_tg_wsgi_app(load_environment) |
|---|
| 10 | |
|---|
| 11 | class FastCGIFixMiddleware(object): |
|---|
| 12 | """Remove dispatch.fcgi from the SCRIPT_NAME |
|---|
| 13 | |
|---|
| 14 | mod_rewrite doesn't do a perfect job of hiding it's actions to the |
|---|
| 15 | underlying script, which causes TurboGears to get confused and tack |
|---|
| 16 | on dispatch.fcgi when it really shouldn't. This fixes that problem as a |
|---|
| 17 | Middleware that fiddles with the appropriate environment variable |
|---|
| 18 | before any processing takes place. |
|---|
| 19 | """ |
|---|
| 20 | def __init__(self, app, global_conf=None): |
|---|
| 21 | self.app = app |
|---|
| 22 | def __call__(self, environ, start_response): |
|---|
| 23 | environ['SCRIPT_NAME'] = environ['SCRIPT_NAME'].replace('/dispatch.fcgi', '') |
|---|
| 24 | return self.app(environ, start_response) |
|---|
| 25 | |
|---|
| 26 | class KinitMiddleware(object): |
|---|
| 27 | """Performs Kerberos authentication with a keytab""" |
|---|
| 28 | def __init__(self, app, global_conf=None): |
|---|
| 29 | self.app = app |
|---|
| 30 | try: |
|---|
| 31 | self.keytab = config["keytab"] |
|---|
| 32 | self.krbname = config["krbname"] |
|---|
| 33 | except KeyError: |
|---|
| 34 | self.keytab = None |
|---|
| 35 | def __call__(self, environ, start_response): |
|---|
| 36 | if self.keytab: |
|---|
| 37 | try: |
|---|
| 38 | subprocess.call(["kinit", self.krbname, "-k", "-t", self.keytab]) |
|---|
| 39 | except OSError: |
|---|
| 40 | subprocess.call(["/usr/kerberos/bin/kinit", self.krbname, "-k", "-t", self.keytab]) |
|---|
| 41 | return self.app(environ, start_response) |
|---|
| 42 | |
|---|
| 43 | def make_app(global_conf, full_stack=True, **app_conf): |
|---|
| 44 | app = make_base_app(global_conf, full_stack=True, **app_conf) |
|---|
| 45 | app = FastCGIFixMiddleware(app, global_conf) |
|---|
| 46 | app = KinitMiddleware(app, global_conf) |
|---|
| 47 | return app |
|---|
| 48 | |
|---|
| 49 | |
|---|