Changes to store tokens using memcache #66.

This commit is contained in:
Yogeshwar Srikrishnan 2011-07-25 17:07:47 -05:00 committed by James E. Blair
parent 1181778b43
commit 17743367b6
7 changed files with 168 additions and 0 deletions

View File

@ -106,6 +106,8 @@ $ pip install -r tools/pip-requires-testing
# Install development dependencies
$ pip install -r tools/pip-requires-development
#Install Memcache (If memcache is one of the backends enabled)
Refer #(http://memcached.org/)
</pre>
## Running Keystone

View File

@ -71,6 +71,9 @@ ldap_user = cn=Admin
ldap_password = password
backend_entities = ['Tenant', 'User', 'Group']
[keystone.backends.memcache]
memcache_hosts = 127.0.0.1:11211
backend_entities = ['Token']
[pipeline:admin]
pipeline =

View File

@ -0,0 +1,80 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import ast
import logging
from sqlalchemy import create_engine
from sqlalchemy.orm import joinedload, aliased, sessionmaker
from keystone.common import config
from keystone.backends.alterdb import models
import keystone.utils as utils
import keystone.backends.api as top_api
import keystone.backends.models as top_models
import memcache
MODEL_PREFIX = 'keystone.backends.memcache.models.'
API_PREFIX = 'keystone.backends.memcache.api.'
memcache_server = None
def configure_backend(options):
hosts = options['memcache_hosts']
global memcache_server
if not memcache_server:
memcache_server = Memcache_Server(hosts)
register_models(options)
class Memcache_Server():
def __init__(self, hosts):
self.hosts = hosts
self.server = memcache.Client([self.hosts])
def set(self, key, value, expiry=900):
"""
This method is used to set a new value
in the memcache server.
"""
self.server.set(key.encode('utf-8'), value, expiry)
def get(self, key):
"""
This method is used to retrieve a value
from the memcache server
"""
return self.server.get(key.encode('utf-8'))
def delete(self, key):
"""
This method is used to delete a value from the
memcached server. Lazy delete
"""
self.server.delete(key.encode('utf-8'))
def register_models(options):
"""Register Models and create properties"""
supported_memcache_models = ast.literal_eval(
options["backend_entities"])
for supported_memcache_model in supported_memcache_models:
model = utils.import_module(MODEL_PREFIX + supported_memcache_model)
top_models.set_value(supported_memcache_model, model)
if model.__api__ != None:
model_api = utils.import_module(API_PREFIX + model.__api__)
top_api.set_value(model.__api__, model_api.get())

View File

@ -0,0 +1 @@
import token

View File

@ -0,0 +1,61 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from keystone.backends.memcache import memcache_server, models
from keystone.backends.api import BaseTokenAPI
class TokenAPI(BaseTokenAPI):
def create(self, token):
if token.tenant_id is None:
token_tenant_key = token.id
else:
token_tenant_key = token.id + "::" + token.tenant_id
if token.tenant_id != None:
tenant_user_key = token.tenant_id + "::" + token.user_id
else:
tenant_user_key = token.user_id
#Setting them for a day.
memcache_server.set(token.id, token, 86400)
memcache_server.set(token_tenant_key, token, 86400)
memcache_server.set(tenant_user_key, token, 86400)
def get(self, id, session=None):
return memcache_server.get(id)
def delete(self, id, session=None):
token = memcache_server.get(id)
if token != None:
memcache_server.delete(id)
if token.tenant_id != None:
memcache_server.delete(token.id + "::" + token.tenant_id)
memcache_server.delete(token.tenant_id + "::" + token.user_id)
else:
memcache_server.delete(token.id)
memcache_server.delete(token.user_id)
def get_for_user(self, user_id, session=None):
return memcache_server.get(user_id)
def get_for_user_by_tenant(self, user_id, tenant_id, session=None):
return memcache_server.get(tenant_id + "::" + user_id)
def get():
return TokenAPI()

View File

@ -0,0 +1,20 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2010-2011 OpenStack, LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Not Yet PEP8 standardized
class Token():
__api__ = 'token'

View File

@ -9,3 +9,4 @@ webob
Routes
httplib2
python-ldap==2.3.13 # optional authentication backend (may require OpenLDAP libs)
python-memcached