[CalendarServer-changes] [12337] twext/trunk/twext/who/ldap

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:22:00 PDT 2014


Revision: 12337
          http://trac.calendarserver.org//changeset/12337
Author:   wsanchez at apple.com
Date:     2014-01-14 12:30:17 -0800 (Tue, 14 Jan 2014)
Log Message:
-----------
Some basic queries work now.

Modified Paths:
--------------
    twext/trunk/twext/who/ldap/_constants.py
    twext/trunk/twext/who/ldap/_service.py
    twext/trunk/twext/who/ldap/test/test_service.py

Modified: twext/trunk/twext/who/ldap/_constants.py
===================================================================
--- twext/trunk/twext/who/ldap/_constants.py	2014-01-14 20:29:48 UTC (rev 12336)
+++ twext/trunk/twext/who/ldap/_constants.py	2014-01-14 20:30:17 UTC (rev 12337)
@@ -22,10 +22,8 @@
     Names, NamedConstant, Values, ValueConstant
 )
 
-from ..idirectory import (
-    FieldName as BaseFieldName, RecordType as BaseRecordType
-)
 from ..expression import MatchType
+from ..util import ConstantsContainer
 
 
 
@@ -119,25 +117,25 @@
     See U{RFC 4519, section 2<http://tools.ietf.org/html/rfc4519#section-2>}.
     """
     businessCategory = ValueConstant(u"businessCategory")
-    countryName = ValueConstant(u"c")
-    commonName = ValueConstant(u"cn")
-    domainComponent = ValueConstant(u"dc")
+    c = ValueConstant(u"c")  # country name
+    cn = ValueConstant(u"cn")  # common name
+    domainComponent = ValueConstant(u"dc")  # domain component
     description = ValueConstant(u"description")
     destinationIndicator = ValueConstant(u"destinationIndicator")
     distinguishedName = ValueConstant(u"distinguishedName")
     dnQualifier = ValueConstant(u"dnQualifier")
-    enhancedSearchGuide = ValueConstant(u"enhanced search guide")
+    enhancedSearchGuide = ValueConstant(u"enhancedSearchGuide")
     facsimileTelephoneNumber = ValueConstant(u"facsimileTelephoneNumber")
     generationQualifier = ValueConstant(u"generationQualifier")
     givenName = ValueConstant(u"givenName")
     houseIdentifier = ValueConstant(u"houseIdentifier")
     initials = ValueConstant(u"initials")
     internationalISDNNumber = ValueConstant(u"internationalISDNNumber")
-    localityName = ValueConstant(u"l")
+    l = ValueConstant(u"l")  # location name
     member = ValueConstant(u"member")
     name = ValueConstant(u"name")
-    organizationName = ValueConstant(u"o")
-    organizationalUnitName = ValueConstant(u"ou")
+    o = ValueConstant(u"o")  # organization name
+    ou = ValueConstant(u"ou")  # organizational unit name
     owner = ValueConstant(u"owner")
     physicalDeliveryOfficeName = ValueConstant(u"physicalDeliveryOfficeName")
     postalAddress = ValueConstant(u"postalAddress")
@@ -149,25 +147,21 @@
     searchGuide = ValueConstant(u"searchGuide")
     seeAlso = ValueConstant(u"seeAlso")
     serialNumber = ValueConstant(u"serialNumber")
-    surname = ValueConstant(u"sn")
-    stateOrProvinceName = ValueConstant(u"st")
+    sn = ValueConstant(u"sn")  # surname
+    st = ValueConstant(u"st")  # state or province name
     street = ValueConstant(u"street")
     telephoneNumber = ValueConstant(u"telephoneNumber")
     teletexTerminalIdentifier = ValueConstant(u"teletexTerminalIdentifier")
     telexNumber = ValueConstant(u"telexNumber")
     title = ValueConstant(u"title")
-    userID = ValueConstant(u"uid")
+    uid = ValueConstant(u"uid")  # user id
     uniqueMember = ValueConstant(u"uniqueMember")
     userPassword = ValueConstant(u"userPassword")
     x121Address = ValueConstant(u"x121Address")
     x500UniqueIdentifier = ValueConstant(u"x500UniqueIdentifier")
 
 
-for c in RFC4519Attribute.iterconstants():
-    if c.name != c.value:
-        setattr(RFC4519Attribute, c.value, c)
 
-
 class RFC4519ObjectClass(Values):
     """
     See U{RFC 4519, section 3<http://tools.ietf.org/html/rfc4519#section-2>}.
@@ -188,28 +182,30 @@
     uidObject = ValueConstant(u"uidObject")
 
 
-# http://tools.ietf.org/html/rfc4524
-# http://tools.ietf.org/html/rfc3112 auth schemes
-# http://tools.ietf.org/html/rfc2307
-# http://tools.ietf.org/html/rfc2798
-# http://tools.ietf.org/html/rfc2739 calendar
 
+class WhoAttribute(Values):
+    """
+    Attributes needed internally that have no standard name.
+    """
+    who_uid = ValueConstant(u"__who_uid")
+    generatedUUID = ValueConstant(u"__who_guid")
+    objectClass = ValueConstant(u"__who_objectClass")
+    mail = ValueConstant(u"__who_mail")
 
 
-# Maps field name -> LDAP attribute name
-DEFAULT_FIELDNAME_MAP = {
-    BaseFieldName.uid: u"uid",  # FIXME
-    BaseFieldName.guid: u"entryUUID",  # FIXME
-    BaseFieldName.recordType: u"objectClass",
-    BaseFieldName.shortNames: RFC4519Attribute.userID.value,
-    BaseFieldName.fullNames: RFC4519Attribute.commonName.value,
-    BaseFieldName.emailAddresses: u"mail",  # FIXME
-    BaseFieldName.password: u"userPassword",  # FIXME
-}
 
+LDAPAttribute = ConstantsContainer((
+    RFC4519Attribute,
+    WhoAttribute,
+))
 
-# Maps record type -> LDAP organizational unit name
-DEFAULT_RECORDTYPE_MAP = {
-    BaseRecordType.user: u"People",  # FIXME
-    BaseRecordType.group: u"Group",  # FIXME
-}
+LDAPObjectClass = ConstantsContainer((
+    RFC4519ObjectClass,
+))
+
+
+# http://tools.ietf.org/html/rfc4524
+# http://tools.ietf.org/html/rfc3112 auth schemes
+# http://tools.ietf.org/html/rfc2307
+# http://tools.ietf.org/html/rfc2798
+# http://tools.ietf.org/html/rfc2739 calendar

Modified: twext/trunk/twext/who/ldap/_service.py
===================================================================
--- twext/trunk/twext/who/ldap/_service.py	2014-01-14 20:29:48 UTC (rev 12336)
+++ twext/trunk/twext/who/ldap/_service.py	2014-01-14 20:30:17 UTC (rev 12337)
@@ -21,6 +21,8 @@
 LDAP directory service implementation.
 """
 
+from uuid import UUID
+
 import ldap
 
 # from zope.interface import implementer
@@ -36,7 +38,7 @@
     DirectoryServiceError, DirectoryAvailabilityError,
     # InvalidDirectoryRecordError,
     # QueryNotSupportedError,
-    # FieldName as BaseFieldName,
+    FieldName as BaseFieldName,
     RecordType as BaseRecordType,
     # IPlaintextPasswordVerifier, IHTTPDigestVerifier,
 )
@@ -52,7 +54,7 @@
     # iterFlags,
     ConstantsContainer,
 )
-from ._constants import DEFAULT_FIELDNAME_MAP, DEFAULT_RECORDTYPE_MAP
+from ._constants import LDAPAttribute, LDAPObjectClass
 from ._util import (
     ldapQueryStringFromMatchExpression,
     ldapQueryStringFromCompoundExpression,
@@ -61,6 +63,27 @@
 
 
 
+# Maps field name -> LDAP attribute name
+# FIXME: Use constants
+DEFAULT_FIELDNAME_MAP = {
+    BaseFieldName.uid: LDAPAttribute.who_uid.value,
+    BaseFieldName.guid: LDAPAttribute.generatedUUID.value,
+    BaseFieldName.recordType: LDAPAttribute.objectClass.value,
+    BaseFieldName.shortNames: LDAPAttribute.uid.value,
+    BaseFieldName.fullNames: LDAPAttribute.cn.value,
+    BaseFieldName.emailAddresses: LDAPAttribute.mail.value,
+    BaseFieldName.password: LDAPAttribute.userPassword.value,
+}
+
+
+# Maps record type -> LDAP object class name
+DEFAULT_RECORDTYPE_MAP = {
+    BaseRecordType.user: LDAPObjectClass.person.value,
+    BaseRecordType.group: LDAPObjectClass.groupOfUniqueNames.value,
+}
+
+
+
 #
 # Exceptions
 #
@@ -76,6 +99,13 @@
 
 
 
+class LDAPConfigurationError(ValueError):
+    """
+    LDAP configuration error.
+    """
+
+
+
 class LDAPConnectionError(DirectoryAvailabilityError):
     """
     LDAP connection error.
@@ -134,8 +164,9 @@
         tlsCACertificateDirectory=None,
         useTLS=False,
         debug=False,
-        fieldNameMap=DEFAULT_FIELDNAME_MAP,
-        recordTypeMap=DEFAULT_RECORDTYPE_MAP,
+        fieldNameToAttributeMap=DEFAULT_FIELDNAME_MAP,
+        recordTypeToObjectClassMap=DEFAULT_RECORDTYPE_MAP,
+        uidField=BaseFieldName.uid,
     ):
         self.url = url
         self._baseDN = baseDN
@@ -159,10 +190,29 @@
         else:
             self._debug = None
 
-        self._fieldNameMap = fieldNameMap
-        self._recordTypeMap = recordTypeMap
+        def reverseDict(source):
+            new = {}
 
+            for k, v in source.iteritems():
+                if v in new:
+                    raise LDAPConfigurationError(
+                        u"Field name map has duplicate values: {0}".format(v)
+                    )
+                new[v] = k
 
+            return new
+
+        self._fieldNameToAttributeMap = fieldNameToAttributeMap
+        self._attributeToFieldNameMap = reverseDict(fieldNameToAttributeMap)
+
+        self._recordTypeToObjectClassMap = recordTypeToObjectClassMap
+        self._objectClassToRecordTypeMap = reverseDict(
+            recordTypeToObjectClassMap
+        )
+
+        self._uidField = uidField
+
+
     @property
     def realmName(self):
         return u"{self.url}".format(self=self)
@@ -236,28 +286,90 @@
     def _recordsFromQueryString(self, queryString):
         connection = yield self._connect()
 
-        # print("+" * 80)
-        # print("Query:", repr(queryString))
+        self.log.debug("Performing LDAP query: {query}", query=queryString)
 
         reply = connection.search_s(
             self._baseDN, ldap.SCOPE_SUBTREE, queryString  # attrs
         )
 
-        # print("Reply:", repr(reply))
-        # print("+" * 80)
-
         records = []
 
-        for recordData in reply:
-            raise NotImplementedError(reply)
+        # Note: self._uidField is the name of the field in
+        # self._fieldNameToAttributeMap that tells us which LDAP attribute
+        # we are using to determine the UID of the record.
 
+        uidField = self.fieldName.uid
+        uidAttribute = self._fieldNameToAttributeMap[self._uidField]
+
+        recordTypeField = self.fieldName.recordType
+        recordTypeAttribute = (
+            self._fieldNameToAttributeMap[self.fieldName.recordType]
+        )
+
+        for dn, recordData in reply:
+
+            if recordTypeAttribute not in recordData:
+                self.log.debug(
+                    "Ignoring LDAP record data with no record type attribute "
+                    "{source.fieldName.recordType!r}: {recordData!r}",
+                    self=self, recordData=recordData
+                )
+                continue
+
+            # Make a dict of fields -> values from the incoming dict of
+            # attributes -> values.
+
+            fields = dict([
+                (self._attributeToFieldNameMap[k], v)
+                for k, v in recordData.iteritems()
+            ])
+
+            # Make sure the UID is populated
+
+            try:
+                fields[uidField] = recordData[uidAttribute]
+            except KeyError:
+                self.log.debug(
+                    "Ignoring LDAP record data with no UID attribute "
+                    "{source._uidField!r}: {recordData!r}",
+                    self=self, recordData=recordData
+                )
+                continue
+
+
+            # Coerce data to the correct type
+
+            for fieldName, value in fields.iteritems():
+                valueType = self.fieldName.valueType(fieldName)
+
+                if fieldName is recordTypeField:
+                    value = self._objectClassToRecordTypeMap[value]
+                elif valueType in (unicode, UUID):
+                    value = valueType(value)
+                else:
+                    raise LDAPConfigurationError(
+                        "Unknown value type {0} for field {1}".format(
+                            valueType, fieldName
+                        )
+                    )
+
+                fields[fieldName] = value
+
+            # Make a record object from fields.
+
+            record = DirectoryRecord(self, fields)
+            records.append(record)
+
+        self.log.debug("LDAP results: {records}", records=records)
+
         returnValue(records)
 
 
     def recordsFromNonCompoundExpression(self, expression, records=None):
         if isinstance(expression, MatchExpression):
             queryString = ldapQueryStringFromMatchExpression(
-                expression, self._fieldNameMap, self._recordTypeMap
+                expression,
+                self._fieldNameToAttributeMap, self._recordTypeToObjectClassMap
             )
             return self._recordsFromQueryString(queryString)
 
@@ -271,7 +383,8 @@
             return ()
 
         queryString = ldapQueryStringFromCompoundExpression(
-            expression, self._fieldNameMap, self._recordTypeMap
+            expression,
+            self._fieldNameToAttributeMap, self._recordTypeToObjectClassMap
         )
         return self._recordsFromQueryString(queryString)
 

Modified: twext/trunk/twext/who/ldap/test/test_service.py
===================================================================
--- twext/trunk/twext/who/ldap/test/test_service.py	2014-01-14 20:29:48 UTC (rev 12336)
+++ twext/trunk/twext/who/ldap/test/test_service.py	2014-01-14 20:30:17 UTC (rev 12337)
@@ -53,7 +53,7 @@
     """
 
     url = "ldap://localhost/"
-    baseDN = u"o=org"
+    baseDN = u"ou=calendarserver,o=org"
     realmName = unicode(url)
 
 
@@ -84,22 +84,6 @@
 class DirectoryServiceConvenienceTestMixIn(
     BaseDirectoryServiceConvenienceTestMixIn
 ):
-    def test_recordWithUID(self):
-        return (
-            BaseDirectoryServiceConvenienceTestMixIn.test_recordWithUID(self)
-        )
-
-    test_recordWithUID.todo = "needs a seed?"
-
-
-    def test_recordWithGUID(self):
-        return (
-            BaseDirectoryServiceConvenienceTestMixIn.test_recordWithGUID(self)
-        )
-
-    test_recordWithGUID.todo = "needs a seed?"
-
-
     def test_recordsWithRecordType(self):
         return (
             BaseDirectoryServiceConvenienceTestMixIn
@@ -109,25 +93,7 @@
     test_recordsWithRecordType.todo = "needs a seed?"
 
 
-    def test_recordWithShortName(self):
-        return (
-            BaseDirectoryServiceConvenienceTestMixIn
-            .test_recordWithShortName(self)
-        )
 
-    test_recordWithShortName.todo = "needs a seed?"
-
-
-    def test_recordsWithEmailAddress(self):
-        return (
-            BaseDirectoryServiceConvenienceTestMixIn
-            .test_recordsWithEmailAddress(self)
-        )
-
-    test_recordsWithEmailAddress.todo = "needs a seed?"
-
-
-
 class DirectoryServiceConnectionTestMixIn(object):
     @inlineCallbacks
     def test_connect_defaults(self):
@@ -155,7 +121,7 @@
         Connect with UsernamePassword credentials.
         """
         credentials = UsernamePassword(
-            u"uid=wsanchez,cn=user,dc=calendarserver,dc=org",
+            u"uid=wsanchez,cn=user,ou=calendarserver,o=org",
             u"__password__"
         )
         service = self.service(credentials=credentials)
@@ -168,7 +134,7 @@
         Connect with UsernamePassword credentials.
         """
         credentials = UsernamePassword(
-            u"uid=wsanchez,cn=user,dc=calendarserver,dc=org",
+            u"uid=wsanchez,cn=user,ou=calendarserver,o=org",
             u"zehcnasw"
         )
         service = self.service(credentials=credentials)
@@ -249,7 +215,7 @@
 
     data = {
         u"o={o}".format(o=o): dict(o=o),
-        u"ou={ou}".format(ou=ou): dict(ou=ou),
+        u"ou={ou},o={o}".format(ou=ou, o=o): dict(ou=ou),
     }
 
     def toUnicode(obj):
@@ -273,7 +239,7 @@
 
     for records in service.index[service.fieldName.uid].itervalues():
         for record in records:
-            dn = u"uid={uid},cn={cn},dc={ou},dc={o}".format(
+            dn = u"uid={uid},cn={cn},ou={ou},o={o}".format(
                 uid=record.shortNames[0], cn=record.recordType.name, ou=ou, o=o
             )
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/296582cb/attachment.html>


More information about the calendarserver-changes mailing list