~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to nova/auth/fakeldap.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
# Copyright [2010] [Anso Labs, LLC]
 
3
#
 
4
#    Licensed under the Apache License, Version 2.0 (the "License");
 
5
#    you may not use this file except in compliance with the License.
 
6
#    You may obtain a copy of the License at
 
7
#
 
8
#        http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS,
 
12
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
#    See the License for the specific language governing permissions and
 
14
#    limitations under the License.
 
15
 
 
16
"""
 
17
  Fake LDAP server for test harnesses.
 
18
"""
 
19
 
 
20
import logging
 
21
 
 
22
from nova import datastore
 
23
 
 
24
SCOPE_SUBTREE  = 1
 
25
 
 
26
 
 
27
class NO_SUCH_OBJECT(Exception):
 
28
    pass
 
29
 
 
30
 
 
31
def initialize(uri):
 
32
    return FakeLDAP(uri)
 
33
 
 
34
 
 
35
class FakeLDAP(object):
 
36
    def __init__(self, _uri):
 
37
        self.keeper = datastore.Keeper('fakeldap')
 
38
        if self.keeper['objects'] is None:
 
39
            self.keeper['objects'] = {}
 
40
 
 
41
    def simple_bind_s(self, dn, password):
 
42
        pass
 
43
 
 
44
    def unbind_s(self):
 
45
        pass
 
46
 
 
47
    def search_s(self, dn, scope, query=None, fields=None):
 
48
        logging.debug("searching for %s" % dn)
 
49
        filtered = {}
 
50
        d = self.keeper['objects'] or {}
 
51
        for cn, attrs in d.iteritems():
 
52
            if cn[-len(dn):] == dn:
 
53
                filtered[cn] = attrs
 
54
        if query:
 
55
            k,v = query[1:-1].split('=')
 
56
            objects = {}
 
57
            for cn, attrs in filtered.iteritems():
 
58
                if attrs.has_key(k) and (v in attrs[k] or
 
59
                    v == attrs[k]):
 
60
                    objects[cn] = attrs
 
61
        if objects == {}:
 
62
            raise NO_SUCH_OBJECT()
 
63
        return objects.items()
 
64
 
 
65
    def add_s(self, cn, attr):
 
66
        logging.debug("adding %s" % cn)
 
67
        stored = {}
 
68
        for k, v in attr:
 
69
            if type(v) is list:
 
70
                stored[k] = v
 
71
            else:
 
72
                stored[k] = [v]
 
73
        d = self.keeper['objects']
 
74
        d[cn] = stored
 
75
        self.keeper['objects'] = d
 
76
 
 
77
    def delete_s(self, cn):
 
78
        logging.debug("creating for %s" % cn)
 
79
        d = self.keeper['objects'] or {}
 
80
        del d[cn]
 
81
        self.keeper['objects'] = d