Search in sources :

Example 6 with LdapEntryManager

use of org.gluu.site.ldap.persistence.LdapEntryManager in project oxAuth by GluuFederation.

the class AppInitializer method recreateLdapAuthEntryManagers.

public void recreateLdapAuthEntryManagers(List<GluuLdapConfiguration> newLdapAuthConfigs) {
    // Get existing application scoped instance
    List<LdapEntryManager> oldLdapAuthEntryManagers = CdiUtil.getContextBean(beanManager, new ParameterizedTypeImpl(List.class, LdapEntryManager.class), LDAP_AUTH_ENTRY_MANAGER_NAME);
    // Recreate components
    createAuthConnectionProviders(newLdapAuthConfigs);
    // Close existing connections
    closeLdapAuthEntryManagers(oldLdapAuthEntryManagers);
    // Destroy old Ldap auth entry managers
    for (LdapEntryManager oldLdapAuthEntryManager : oldLdapAuthEntryManagers) {
        log.debug("Attempting to destroy {}: {}", LDAP_AUTH_ENTRY_MANAGER_NAME, oldLdapAuthEntryManager);
        oldLdapAuthEntryManager.destroy();
        log.debug("Destroyed {}: {}", LDAP_AUTH_ENTRY_MANAGER_NAME, oldLdapAuthEntryManager);
    }
    // Force to create new bean
    List<LdapEntryManager> ldapAuthEntryManagers = ldapAuthEntryManagerInstance.get();
    ldapAuthEntryManagerInstance.destroy(ldapAuthEntryManagers);
    log.info("Recreated instance {}: {}", LDAP_AUTH_ENTRY_MANAGER_NAME, ldapAuthEntryManagers);
}
Also used : LdapEntryManager(org.gluu.site.ldap.persistence.LdapEntryManager) List(java.util.List) ArrayList(java.util.ArrayList) ParameterizedTypeImpl(org.jboss.weld.util.reflection.ParameterizedTypeImpl)

Example 7 with LdapEntryManager

use of org.gluu.site.ldap.persistence.LdapEntryManager in project oxAuth by GluuFederation.

the class AuthenticationService method externalAuthenticate.

private boolean externalAuthenticate(String keyValue, String password) {
    for (int i = 0; i < this.ldapAuthConfigs.size(); i++) {
        GluuLdapConfiguration ldapAuthConfig = this.ldapAuthConfigs.get(i);
        LdapEntryManager ldapAuthEntryManager = this.ldapAuthEntryManagers.get(i);
        String primaryKey = "uid";
        if (StringHelper.isNotEmpty(ldapAuthConfig.getPrimaryKey())) {
            primaryKey = ldapAuthConfig.getPrimaryKey();
        }
        String localPrimaryKey = "uid";
        if (StringHelper.isNotEmpty(ldapAuthConfig.getLocalPrimaryKey())) {
            localPrimaryKey = ldapAuthConfig.getLocalPrimaryKey();
        }
        boolean authenticated = authenticate(ldapAuthConfig, ldapAuthEntryManager, keyValue, password, primaryKey, localPrimaryKey);
        if (authenticated) {
            return authenticated;
        }
    }
    return false;
}
Also used : LdapEntryManager(org.gluu.site.ldap.persistence.LdapEntryManager) GluuLdapConfiguration(org.xdi.model.ldap.GluuLdapConfiguration)

Example 8 with LdapEntryManager

use of org.gluu.site.ldap.persistence.LdapEntryManager in project oxCore by GluuFederation.

the class LdapSampleBatchJob method main.

public static void main(String[] args) {
    // Prepare sample connection details
    LdapSampleEntryManager ldapSampleEntryManager = new LdapSampleEntryManager();
    // Create LDAP entry manager
    final LdapEntryManager ldapEntryManager = ldapSampleEntryManager.createLdapEntryManager();
    BatchOperation<SimpleTokenLdap> tokenLdapBatchOperation = new BatchOperation<SimpleTokenLdap>(ldapEntryManager) {

        private int processedCount = 0;

        @Override
        protected List<SimpleTokenLdap> getChunkOrNull(int batchSize) {
            log.info("Processed: " + processedCount);
            final Filter filter = Filter.createPresenceFilter("oxAuthExpiration");
            return ldapEntryManager.findEntries("o=gluu", SimpleTokenLdap.class, filter, SearchScope.SUB, new String[] { "oxAuthExpiration" }, this, 0, batchSize, batchSize);
        }

        @Override
        protected void performAction(List<SimpleTokenLdap> objects) {
            for (SimpleTokenLdap simpleTokenLdap : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute("oxAuthExpiration", simpleTokenLdap.getAttribute("oxAuthExpiration"));
                    simpleTokenLdap.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    ldapEntryManager.merge(simpleTokenLdap);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    log.error("Failed to update entry", ex);
                }
            }
        }
    };
    tokenLdapBatchOperation.iterateAllByChunks(100);
    BatchOperation<SimpleSession> sessionBatchOperation = new BatchOperation<SimpleSession>(ldapEntryManager) {

        private int processedCount = 0;

        @Override
        protected List<SimpleSession> getChunkOrNull(int batchSize) {
            log.info("Processed: " + processedCount);
            final Filter filter = Filter.createPresenceFilter("oxLastAccessTime");
            return ldapEntryManager.findEntries("o=gluu", SimpleSession.class, filter, SearchScope.SUB, new String[] { "oxLastAccessTime" }, this, 0, batchSize, batchSize);
        }

        @Override
        protected void performAction(List<SimpleSession> objects) {
            for (SimpleSession simpleSession : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute("oxLastAccessTime", simpleSession.getAttribute("oxLastAccessTime"));
                    simpleSession.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    ldapEntryManager.merge(simpleSession);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    log.error("Failed to update entry", ex);
                }
            }
        }
    };
    sessionBatchOperation.iterateAllByChunks(100);
}
Also used : LdapEntryManager(org.gluu.site.ldap.persistence.LdapEntryManager) Filter(com.unboundid.ldap.sdk.Filter) CustomAttribute(org.xdi.ldap.model.CustomAttribute) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) List(java.util.List) BatchOperation(org.gluu.site.ldap.persistence.BatchOperation)

Example 9 with LdapEntryManager

use of org.gluu.site.ldap.persistence.LdapEntryManager in project oxCore by GluuFederation.

the class LdapSampleSimpleSessionSample method main.

public static void main(String[] args) throws InterruptedException {
    // Prepare sample connection details
    LdapSampleEntryManager ldapSampleEntryManager = new LdapSampleEntryManager();
    final LdapEntryManager ldapEntryManager = ldapSampleEntryManager.createLdapEntryManager();
    try {
        // Create LDAP entry manager
        String sessionId = "xyzcyzxy-a41a-45ad-8a83-61485dbad561";
        final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163,o=gluu";
        final String userDn = "inum=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163!0000!A8F2.DE1E.D7FB,ou=people,o=@!E8F2.853B.1E7B.ACE2!0001!39A4.C163,o=gluu";
        final SimpleSessionState simpleSessionState = new SimpleSessionState();
        simpleSessionState.setDn(sessionDn);
        simpleSessionState.setId(sessionId);
        simpleSessionState.setLastUsedAt(new Date());
        ldapEntryManager.persist(simpleSessionState);
        System.out.println("Persisted");
        int threadCount = 500;
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount, daemonThreadFactory());
        for (int i = 0; i < threadCount; i++) {
            final int count = i;
            executorService.execute(new Runnable() {

                @Override
                public void run() {
                    final SimpleSessionState simpleSessionStateFromLdap = ldapEntryManager.find(SimpleSessionState.class, sessionDn);
                    String beforeUserDn = simpleSessionStateFromLdap.getUserDn();
                    String randomUserDn = count % 2 == 0 ? userDn : "";
                    try {
                        simpleSessionStateFromLdap.setUserDn(randomUserDn);
                        simpleSessionStateFromLdap.setLastUsedAt(new Date());
                        ldapEntryManager.merge(simpleSessionStateFromLdap);
                        System.out.println("Merged thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn);
                    } catch (Throwable e) {
                        System.out.println("ERROR !!!, thread: " + count + ", userDn: " + randomUserDn + ", before userDn: " + beforeUserDn + ", error:" + e.getMessage());
                    //							e.printStackTrace();
                    }
                }
            });
        }
        Thread.sleep(5000L);
    } finally {
        ldapEntryManager.getLdapOperationService().getConnectionPool().close();
    }
}
Also used : LdapEntryManager(org.gluu.site.ldap.persistence.LdapEntryManager) ExecutorService(java.util.concurrent.ExecutorService) Date(java.util.Date)

Example 10 with LdapEntryManager

use of org.gluu.site.ldap.persistence.LdapEntryManager in project oxTrust by GluuFederation.

the class Configuration method createLdapEntryManager.

private LdapEntryManager createLdapEntryManager() {
    Properties connectionProperties = (Properties) this.ldapConfiguration.getProperties();
    Properties decryptedConnectionProperties = PropertiesDecrypter.decryptProperties(connectionProperties, this.cryptoConfigurationSalt);
    LDAPConnectionProvider connectionProvider = new LDAPConnectionProvider(decryptedConnectionProperties);
    LdapEntryManager ldapEntryManager = new LdapEntryManager(new OperationsFacade(connectionProvider, null));
    logger.debug("Created LdapEntryManager: {}", ldapEntryManager);
    return ldapEntryManager;
}
Also used : LdapEntryManager(org.gluu.site.ldap.persistence.LdapEntryManager) OperationsFacade(org.gluu.site.ldap.OperationsFacade) Properties(java.util.Properties) LDAPConnectionProvider(org.gluu.site.ldap.LDAPConnectionProvider)

Aggregations

LdapEntryManager (org.gluu.site.ldap.persistence.LdapEntryManager)33 OperationsFacade (org.gluu.site.ldap.OperationsFacade)8 Filter (com.unboundid.ldap.sdk.Filter)6 ArrayList (java.util.ArrayList)6 GluuSimplePerson (org.gluu.oxtrust.ldap.cache.model.GluuSimplePerson)5 ApplicationScoped (javax.enterprise.context.ApplicationScoped)4 Produces (javax.enterprise.inject.Produces)4 Named (javax.inject.Named)4 LdapMappingException (org.gluu.site.ldap.persistence.exception.LdapMappingException)4 GluuLdapConfiguration (org.xdi.model.ldap.GluuLdapConfiguration)4 Properties (java.util.Properties)3 GluuInumMap (org.gluu.oxtrust.ldap.cache.model.GluuInumMap)3 LDAPConnectionProvider (org.gluu.site.ldap.LDAPConnectionProvider)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 CacheCompoundKey (org.gluu.oxtrust.ldap.cache.model.CacheCompoundKey)2 CustomAttribute (org.xdi.ldap.model.CustomAttribute)2