1. admin_api 流水线
在上一章中, 我们讲了WEB服务器的创建及使用。这一章我们将进入Keystone的实际业务认证流程。
我们以admin_api做为一个实例,从Paste Deployment配置文件的流水线开始。
先来看看配置的流水线过程。
[pipeline:admin_api]
pipeline = access_log sizelimit url_normalize token_auth admin_token_auth xml_body json_body ec2_extension s3_extension crud_extension admin_service
可以看出, 这里有很多的流水线处理, 我们一个一个来简单过一下。
1.1 access_log
[filter:access_log]
paste.filter_factory = keystone.contrib.access:AccessLogMiddleware.factory
从这里可以看出, 这个点处理的是AccessLogMiddleware, 打开这个类, 所有的代码如下:
import webob.dec
from keystone.common import wsgi
from keystone import config
from keystone.openstack.common import log as logging
from keystone.openstack.common import timeutils
CONF = config.CONF
LOG = logging.getLogger('access')
APACHE_TIME_FORMAT = '%d/%b/%Y:%H:%M:%S'
APACHE_LOG_FORMAT = (
'%(remote_addr)s - %(remote_user)s [%(datetime)s] "%(method)s %(url)s '
'%(http_version)s" %(status)s %(content_length)s')
class AccessLogMiddleware(wsgi.Middleware):
"""Writes an access log to INFO."""
@webob.dec.wsgify
def __call__(self, request):
data = {
'remote_addr': request.remote_addr,
'remote_user': request.remote_user or '-',
'method': request.method,
'url': request.url,
'http_version': request.http_version,
'status': 500,
'content_length': '-'}
try:
response = request.get_response(self.application)
data['status'] = response.status_int
data['content_length'] = len(response.body) or '-'
finally:
# must be calculated *after* the application has been called
now = timeutils.utcnow()
# timeutils may not return UTC, so we can't hardcode +0000
data['datetime'] = '%s %s' % (now.strftime(APACHE_TIME_FORMAT),
now.strftime('%z') or '+0000')
LOG.info(APACHE_LOG_FORMAT % data)
return response
可以看出, 它里面就只是定义一个__call__ 的方法,然后写一条info log到日志文件中。但是这里, 却没有factory的方法。 我们继续打开它的父类。
class Middleware(Application):
@classmethod
def factory(cls, global_config, **local_config):
def _factory(app):
conf = global_config.copy()
conf.update(local_config)
return cls(app, **local_config)
return _factory
...
在这个类中, 我们找到了factory这个类的静态方法,它返回一个用于创建本类的一个方法。到此为止, access_log就已经结束。它的作用就是写一条info log.