Search in sources :

Example 21 with Attribute

use of javax.naming.directory.Attribute in project nhin-d by DirectProject.

the class LDAPResearchTest method setUp.

@SuppressWarnings("unchecked")
@Override
public void setUp() throws Exception {
    MutablePartitionConfiguration pcfg = new MutablePartitionConfiguration();
    pcfg.setName("lookupTest");
    pcfg.setSuffix("cn=lookupTest");
    // Create some indices
    Set<String> indexedAttrs = new HashSet<String>();
    indexedAttrs.add("objectClass");
    indexedAttrs.add("cn");
    pcfg.setIndexedAttributes(indexedAttrs);
    // Create a first entry associated to the partition
    Attributes attrs = new BasicAttributes(true);
    // First, the objectClass attribute
    Attribute attr = new BasicAttribute("objectClass");
    attr.add("top");
    attrs.put(attr);
    // Associate this entry to the partition
    pcfg.setContextEntry(attrs);
    // As we can create more than one partition, we must store
    // each created partition in a Set before initialization
    Set<MutablePartitionConfiguration> pcfgs = new HashSet<MutablePartitionConfiguration>();
    pcfgs.add(pcfg);
    configuration.setContextPartitionConfigurations(pcfgs);
    this.configuration.setWorkingDirectory(new File("LDAP-TEST"));
    // add the private key schema
    ///
    Set<AbstractBootstrapSchema> schemas = configuration.getBootstrapSchemas();
    schemas.add(new PrivkeySchema());
    configuration.setBootstrapSchemas(schemas);
    super.setUp();
    // import the ldif file
    InputStream stream = LDAPResearchTest.class.getClassLoader().getResourceAsStream("ldifs/privCertsOnly.ldif");
    if (stream == null)
        throw new IOException("Failed to load ldif file");
    importLdif(stream);
    createLdapEntries();
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) InputStream(java.io.InputStream) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) IOException(java.io.IOException) AbstractBootstrapSchema(org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema) MutablePartitionConfiguration(org.apache.directory.server.core.configuration.MutablePartitionConfiguration) File(java.io.File) HashSet(java.util.HashSet)

Example 22 with Attribute

use of javax.naming.directory.Attribute in project nhin-d by DirectProject.

the class LDAPResearchTest method testDummy.

@SuppressWarnings("unchecked")
public void testDummy() throws Exception {
    CertCacheFactory.getInstance().flushAll();
    DirContext dirContext = createContext("cn=lookupTest");
    Attributes attributes = dirContext.getAttributes("");
    assertNotNull(attributes);
    NamingEnumeration<Attribute> namingEnum = (NamingEnumeration<Attribute>) attributes.getAll();
    while (namingEnum.hasMoreElements()) {
        Attribute attr = namingEnum.nextElement();
        System.out.println("Name: " + attr.getID() + "\r\nValue: " + attr.get() + "\r\n\r\n");
    }
    Set<SearchResult> results = searchDNs("(email=gm2552@cerner.com)", "", "ou=privKeys, ou=cerner, ou=com", SearchControls.SUBTREE_SCOPE, dirContext);
    for (SearchResult result : results) {
        System.out.println(result.getName());
        // get the priv cert
        String privKey = (String) result.getAttributes().get("privKeyStore").get();
        System.out.println("Privkey BASE64: " + privKey);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext)

Example 23 with Attribute

use of javax.naming.directory.Attribute in project nhin-d by DirectProject.

the class LDAPPublicCertUtil_ldapSearch_Test method setUp.

@SuppressWarnings("unchecked")
@Override
public void setUp() throws Exception {
    // create the LDAP server
    MutablePartitionConfiguration pcfg = new MutablePartitionConfiguration();
    pcfg.setName("lookupTestPublic");
    pcfg.setSuffix("cn=lookupTestPublic");
    // Create some indices
    Set<String> indexedAttrs = new HashSet<String>();
    indexedAttrs.add("objectClass");
    indexedAttrs.add("cn");
    pcfg.setIndexedAttributes(indexedAttrs);
    // Create a first entry associated to the partition
    Attributes attrs = new BasicAttributes(true);
    // First, the objectClass attribute
    Attribute attr = new BasicAttribute("objectClass");
    attr.add("top");
    attrs.put(attr);
    // Associate this entry to the partition
    pcfg.setContextEntry(attrs);
    // As we can create more than one partition, we must store
    // each created partition in a Set before initialization
    Set<MutablePartitionConfiguration> pcfgs = new HashSet<MutablePartitionConfiguration>();
    pcfgs.add(pcfg);
    configuration.setContextPartitionConfigurations(pcfgs);
    configuration.setWorkingDirectory(new File("LDAP-TEST"));
    Set<AbstractBootstrapSchema> schemas = configuration.getBootstrapSchemas();
    configuration.setBootstrapSchemas(schemas);
    mockLookup = mock(Lookup.class);
    LookupFactory.getFactory().addOverrideImplementation(mockLookup);
    super.setUp();
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) AbstractBootstrapSchema(org.apache.directory.server.core.schema.bootstrap.AbstractBootstrapSchema) MutablePartitionConfiguration(org.apache.directory.server.core.configuration.MutablePartitionConfiguration) Lookup(org.nhindirect.stagent.cert.impl.util.Lookup) File(java.io.File) HashSet(java.util.HashSet)

Example 24 with Attribute

use of javax.naming.directory.Attribute in project gerrit by GerritCodeReview.

the class Helper method queryForGroups.

Set<AccountGroup.UUID> queryForGroups(final DirContext ctx, final String username, LdapQuery.Result account) throws NamingException {
    final LdapSchema schema = getSchema(ctx);
    final Set<String> groupDNs = new HashSet<>();
    if (!schema.groupMemberQueryList.isEmpty()) {
        final HashMap<String, String> params = new HashMap<>();
        if (account == null) {
            try {
                account = findAccount(schema, ctx, username, false);
            } catch (AccountException e) {
                return Collections.emptySet();
            }
        }
        for (String name : schema.groupMemberQueryList.get(0).getParameters()) {
            params.put(name, account.get(name));
        }
        params.put(LdapRealm.USERNAME, username);
        for (LdapQuery groupMemberQuery : schema.groupMemberQueryList) {
            for (LdapQuery.Result r : groupMemberQuery.query(ctx, params)) {
                recursivelyExpandGroups(groupDNs, schema, ctx, r.getDN());
            }
        }
    }
    if (schema.accountMemberField != null) {
        if (account == null || account.getAll(schema.accountMemberField) == null) {
            try {
                account = findAccount(schema, ctx, username, true);
            } catch (AccountException e) {
                return Collections.emptySet();
            }
        }
        final Attribute groupAtt = account.getAll(schema.accountMemberField);
        if (groupAtt != null) {
            final NamingEnumeration<?> groups = groupAtt.getAll();
            try {
                while (groups.hasMore()) {
                    final String nextDN = (String) groups.next();
                    recursivelyExpandGroups(groupDNs, schema, ctx, nextDN);
                }
            } catch (PartialResultException e) {
            // Ignored
            }
        }
    }
    final Set<AccountGroup.UUID> actual = new HashSet<>();
    for (String dn : groupDNs) {
        actual.add(new AccountGroup.UUID(LDAP_UUID + dn));
    }
    if (actual.isEmpty()) {
        return Collections.emptySet();
    }
    return ImmutableSet.copyOf(actual);
}
Also used : HashMap(java.util.HashMap) Attribute(javax.naming.directory.Attribute) PartialResultException(javax.naming.PartialResultException) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) AccountException(com.google.gerrit.server.account.AccountException) AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) HashSet(java.util.HashSet)

Example 25 with Attribute

use of javax.naming.directory.Attribute in project gerrit by GerritCodeReview.

the class Helper method recursivelyExpandGroups.

private void recursivelyExpandGroups(final Set<String> groupDNs, final LdapSchema schema, final DirContext ctx, final String groupDN) {
    if (groupDNs.add(groupDN) && schema.accountMemberField != null && schema.accountMemberExpandGroups) {
        ImmutableSet<String> cachedParentsDNs = parentGroups.getIfPresent(groupDN);
        if (cachedParentsDNs == null) {
            // Recursively identify the groups it is a member of.
            ImmutableSet.Builder<String> dns = ImmutableSet.builder();
            try {
                final Name compositeGroupName = new CompositeName().add(groupDN);
                final Attribute in = ctx.getAttributes(compositeGroupName, schema.accountMemberFieldArray).get(schema.accountMemberField);
                if (in != null) {
                    final NamingEnumeration<?> groups = in.getAll();
                    try {
                        while (groups.hasMore()) {
                            dns.add((String) groups.next());
                        }
                    } catch (PartialResultException e) {
                    // Ignored
                    }
                }
            } catch (NamingException e) {
                LdapRealm.log.warn("Could not find group " + groupDN, e);
            }
            cachedParentsDNs = dns.build();
            parentGroups.put(groupDN, cachedParentsDNs);
        }
        for (String dn : cachedParentsDNs) {
            recursivelyExpandGroups(groupDNs, schema, ctx, dn);
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Attribute(javax.naming.directory.Attribute) CompositeName(javax.naming.CompositeName) PartialResultException(javax.naming.PartialResultException) NamingException(javax.naming.NamingException) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name)

Aggregations

Attribute (javax.naming.directory.Attribute)288 Attributes (javax.naming.directory.Attributes)162 NamingException (javax.naming.NamingException)133 BasicAttribute (javax.naming.directory.BasicAttribute)97 SearchResult (javax.naming.directory.SearchResult)92 ArrayList (java.util.ArrayList)74 BasicAttributes (javax.naming.directory.BasicAttributes)64 NamingEnumeration (javax.naming.NamingEnumeration)56 SearchControls (javax.naming.directory.SearchControls)55 DirContext (javax.naming.directory.DirContext)46 InitialDirContext (javax.naming.directory.InitialDirContext)40 HashSet (java.util.HashSet)38 HashMap (java.util.HashMap)29 IOException (java.io.IOException)24 LdapName (javax.naming.ldap.LdapName)20 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)18 Hashtable (java.util.Hashtable)17 Map (java.util.Map)17 ModificationItem (javax.naming.directory.ModificationItem)17 List (java.util.List)15