Beispiel #1
0
 def makeDirectoryProxyClient(self):
     self.directory = DirectoryProxyClientService(config.DirectoryRealmName)
     if config.DirectoryProxy.InProcessCachingSeconds:
         self.directory = CachingDirectoryService(
             self.directory,
             expireSeconds=config.DirectoryProxy.InProcessCachingSeconds
         )
    def setUp(self):
        yield super(CacheTest, self).setUp()

        self.cachingDirectory = CachingDirectoryService(
            self.directory,
            expireSeconds=10,
            lookupsBetweenPurges=LOOKUPS_BETWEEN_PURGES,
            negativeCaching=True,
        )
        self.storeUnderTest().setDirectoryService(self.cachingDirectory)
 def makeDirectoryProxyClient(self):
     if config.DirectoryProxy.Enabled:
         self.directory = DirectoryProxyClientService(
             config.DirectoryRealmName)
         if config.DirectoryCaching.CachingSeconds:
             self.directory = CachingDirectoryService(
                 self.directory,
                 expireSeconds=config.DirectoryCaching.CachingSeconds,
                 lookupsBetweenPurges=config.DirectoryCaching.
                 LookupsBetweenPurges,
                 negativeCaching=config.DirectoryCaching.
                 NegativeCachingEnabled,
             )
    def test_differentCacheKeys(self):
        """
        Verify records are purged from cache after a certain amount of requests
        """
        dir = CachingDirectoryService(
            DPSClientDirectoryService(None),
            expireSeconds=10,
            lookupsBetweenPurges=LOOKUPS_BETWEEN_PURGES,
            negativeCaching=True,
        )

        self.patch(config.Memcached.Pools.Default, "ClientEnabled", True)

        self.patch(config.DirectoryProxy, "Enabled", True)
        dir.resetCache()
        key1 = dir._memcacher.generateMemcacheKey(IndexType.uid, "abc")

        self.patch(config.DirectoryProxy, "Enabled", False)
        dir.resetCache()
        key2 = dir._memcacher.generateMemcacheKey(IndexType.uid, "abc")

        self.assertNotEqual(key1, key2)
    def setUp(self):
        yield super(CacheTest, self).setUp()

        self.cachingDirectory = CachingDirectoryService(self.directory,
                                                        expireSeconds=10)
        self.storeUnderTest().setDirectoryService(self.cachingDirectory)
Beispiel #6
0
def buildDirectory(
    store,
    dataRoot,
    servicesInfo,
    augmentServiceInfo,
    wikiServiceInfo,
    serversDB=None,
    cachingSeconds=0,
    filterStartsWith=False,
    lookupsBetweenPurges=0,
    negativeCaching=True,
):
    """
    Return a directory without using a config object; suitable for tests
    which need to have mulitple directory instances.

    @param store: The store.
    @param dataRoot: The path to the directory containing xml files for any xml
        based services.
    @param servicesInfo:  An interable of ConfigDicts mirroring the
        DirectoryService and ResourceService sections of stdconfig
    @param augmentServiceInfo: A ConfigDict mirroring the AugmentService section
        of stdconfig
    @param wikiServiceInfo: A ConfigDict mirroring the Wiki section of stdconfig
    @param serversDB: A ServersDB object to assign to the directory
    """

    aggregatedServices = []
    cachingServices = []
    ldapService = None  # LDAP DS has extra stats (see augment.py)

    for serviceValue in servicesInfo:

        if not serviceValue.Enabled:
            continue

        directoryType = serviceValue.type.lower()
        params = serviceValue.params

        if "xml" in directoryType:
            xmlFile = params.xmlFile
            xmlFile = fullServerPath(dataRoot, xmlFile)
            fp = FilePath(xmlFile)
            if not fp.exists():
                fp.setContent(DEFAULT_XML_CONTENT)
            directory = XMLDirectoryService(fp)

        elif "opendirectory" in directoryType:
            from txdav.who.opendirectory import (DirectoryService as
                                                 ODDirectoryService)
            # We don't want system accounts returned in lookups, so tell
            # the service to suppress them.
            node = params.node
            directory = ODDirectoryService(nodeName=node,
                                           suppressSystemRecords=True)

        elif "ldap" in directoryType:
            from twext.who.ldap import (DirectoryService as
                                        LDAPDirectoryService, FieldName as
                                        LDAPFieldName, RecordTypeSchema)

            if params.credentials.dn and params.credentials.password:
                creds = UsernamePassword(params.credentials.dn,
                                         params.credentials.password)
            else:
                creds = None
            mapping = params.mapping
            extraFilters = params.extraFilters
            directory = LDAPDirectoryService(
                params.uri,
                params.rdnSchema.base,
                useTLS=params.useTLS,
                credentials=creds,
                fieldNameToAttributesMap=MappingProxyType({
                    BaseFieldName.uid:
                    mapping.uid,
                    BaseFieldName.guid:
                    mapping.guid,
                    BaseFieldName.shortNames:
                    mapping.shortNames,
                    BaseFieldName.fullNames:
                    mapping.fullNames,
                    BaseFieldName.emailAddresses:
                    mapping.emailAddresses,
                    LDAPFieldName.memberDNs:
                    mapping.memberDNs,
                    CalFieldName.readOnlyProxy:
                    mapping.readOnlyProxy,
                    CalFieldName.readWriteProxy:
                    mapping.readWriteProxy,
                    CalFieldName.loginAllowed:
                    mapping.loginAllowed,
                    CalFieldName.hasCalendars:
                    mapping.hasCalendars,
                    CalFieldName.autoScheduleMode:
                    mapping.autoScheduleMode,
                    CalFieldName.autoAcceptGroup:
                    mapping.autoAcceptGroup,
                    CalFieldName.serviceNodeUID:
                    mapping.serviceNodeUID,
                    CalFieldName.associatedAddress:
                    mapping.associatedAddress,
                    CalFieldName.geographicLocation:
                    mapping.geographicLocation,
                    CalFieldName.streetAddress:
                    mapping.streetAddress,
                }),
                recordTypeSchemas=MappingProxyType({
                    RecordType.user:
                    RecordTypeSchema(
                        relativeDN=params.rdnSchema.users,
                        attributes=(),
                    ),
                    RecordType.group:
                    RecordTypeSchema(
                        relativeDN=params.rdnSchema.groups,
                        attributes=(),
                    ),
                    CalRecordType.location:
                    RecordTypeSchema(
                        relativeDN=params.rdnSchema.locations,
                        attributes=(),
                    ),
                    CalRecordType.resource:
                    RecordTypeSchema(
                        relativeDN=params.rdnSchema.resources,
                        attributes=(),
                    ),
                    CalRecordType.address:
                    RecordTypeSchema(
                        relativeDN=params.rdnSchema.addresses,
                        attributes=(),
                    ),
                }),
                extraFilters={
                    RecordType.user: extraFilters.get("users", ""),
                    RecordType.group: extraFilters.get("groups", ""),
                    CalRecordType.location: extraFilters.get("locations", ""),
                    CalRecordType.resource: extraFilters.get("resources", ""),
                    CalRecordType.address: extraFilters.get("addresses", ""),
                },
                threadPoolMax=params.get("threadPoolMax", 10),
                authConnectionMax=params.get("authConnectionMax", 5),
                queryConnectionMax=params.get("queryConnectionMax", 5),
                tries=params.get("tries", 3),
                warningThresholdSeconds=params.get("warningThresholdSeconds",
                                                   5),
            )
            ldapService = directory

        elif "inmemory" in directoryType:
            from txdav.who.test.support import CalendarInMemoryDirectoryService
            directory = CalendarInMemoryDirectoryService()

        else:
            log.error("Invalid DirectoryType: {dt}", dt=directoryType)
            raise DirectoryConfigurationError

        # Set the appropriate record types on each service
        types = []
        fieldNames = []
        for recordTypeName in params.recordTypes:
            recordType = {
                "users": RecordType.user,
                "groups": RecordType.group,
                "locations": CalRecordType.location,
                "resources": CalRecordType.resource,
                "addresses": CalRecordType.address,
            }.get(recordTypeName, None)

            if recordType is None:
                log.error("Invalid Record Type: {rt}", rt=recordTypeName)
                raise DirectoryConfigurationError

            if recordType in types:
                log.error("Duplicate Record Type: {rt}", rt=recordTypeName)
                raise DirectoryConfigurationError

            types.append(recordType)

        directory.recordType = ConstantsContainer(types)
        directory.fieldName = ConstantsContainer(
            (directory.fieldName, CalFieldName))
        fieldNames.append(directory.fieldName)

        if cachingSeconds:
            directory = CachingDirectoryService(
                directory,
                expireSeconds=cachingSeconds,
                lookupsBetweenPurges=lookupsBetweenPurges,
                negativeCaching=negativeCaching,
            )
            cachingServices.append(directory)

        aggregatedServices.append(directory)

    #
    # Setup the Augment Service
    #
    serviceClass = {
        "xml": "twistedcaldav.directory.augment.AugmentXMLDB",
    }

    for augmentFile in augmentServiceInfo.params.xmlFiles:
        augmentFile = fullServerPath(dataRoot, augmentFile)
        augmentFilePath = FilePath(augmentFile)
        if not augmentFilePath.exists():
            augmentFilePath.setContent(DEFAULT_AUGMENT_CONTENT)

    augmentClass = namedClass(serviceClass[augmentServiceInfo.type])
    log.info("Configuring augment service of type: {augmentClass}",
             augmentClass=augmentClass)
    try:
        augmentService = augmentClass(**augmentServiceInfo.params)
    except IOError:
        log.error("Could not start augment service")
        raise

    userDirectory = None
    for directory in aggregatedServices:
        if RecordType.user in directory.recordTypes():
            userDirectory = directory
            break
    else:
        log.error("No directory service set up for users")
        raise DirectoryConfigurationError

    # Delegate service
    delegateDirectory = DelegateDirectoryService(userDirectory.realmName,
                                                 store)
    # (put at front of list so we don't try to ask the actual DS services
    # about the delegate-related principals, for performance)
    aggregatedServices.insert(0, delegateDirectory)

    # Wiki service
    if wikiServiceInfo.Enabled:
        aggregatedServices.append(
            WikiDirectoryService(
                userDirectory.realmName,
                wikiServiceInfo.EndpointDescriptor,
            ))

    # Aggregate service
    aggregateDirectory = AggregateDirectoryService(userDirectory.realmName,
                                                   aggregatedServices)

    # Augment service
    try:
        fieldNames.append(CalFieldName)
        augmented = AugmentedDirectoryService(aggregateDirectory, store,
                                              augmentService)
        augmented.fieldName = ConstantsContainer(fieldNames)

        # The delegate directory needs a way to look up user/group records
        # so hand it a reference to the augmented directory.
        # FIXME: is there a better pattern to use here?
        delegateDirectory.setMasterDirectory(augmented)

        # Tell each caching service what method to use when reporting
        # times and cache stats
        for cachingService in cachingServices:
            cachingService.setTimingMethod(augmented._addTiming)

        # LDAP has additional stats to report
        augmented._ldapDS = ldapService

    except Exception as e:
        log.error("Could not create directory service", error=e)
        raise

    if serversDB is not None:
        augmented.setServersDB(serversDB)

    if filterStartsWith:
        augmented.setFilter(startswithFilter)

    return augmented