Search in sources :

Example 11 with Filter

use of net.sourceforge.myvd.types.Filter in project OpenUnison by TremoloSecurity.

the class OktaInsert method search.

@Override
public void search(SearchInterceptorChain chain, DistinguishedName base, Int scope, Filter filter, ArrayList<Attribute> attributes, Bool typesOnly, Results results, LDAPSearchConstraints constraints) throws LDAPException {
    OktaTarget os = null;
    try {
        os = (OktaTarget) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.target).getProvider();
    } catch (ProvisioningException e1) {
        logger.error("Could not retrieve kubernetes target", e1);
        throw new LDAPException("Could not connect to kubernetes", LDAPException.OPERATIONS_ERROR, LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR));
    }
    // base search
    if (scope.getValue() == 0) {
        // dir root
        if (base.getDN().equals(this.baseDN)) {
            ArrayList<Entry> ret = new ArrayList<Entry>();
            ret.add(new Entry(EntryUtil.createBaseEntry(this.baseDN)));
            chain.addResult(results, new IteratorEntrySet(ret.iterator()), base, scope, filter, attributes, typesOnly, constraints);
            return;
        } else {
            if (this.users) {
                String name = ((RDN) base.getDN().getRDNs().get(0)).getValue();
                loadUserFromOkta(chain, base, scope, filter, attributes, typesOnly, results, constraints, os, name, base.getDN().toString(), true);
            } else {
                String name = ((RDN) base.getDN().getRDNs().get(0)).getValue();
                Client okta = os.getOkta();
                GroupList groupList = null;
                Group fromOkta = null;
                try {
                    ArrayList<Entry> ret = new ArrayList<Entry>();
                    loadGroupFromOkta(base, filter, name, okta, ret);
                    chain.addResult(results, new IteratorEntrySet(ret.iterator()), base, scope, filter, attributes, typesOnly, constraints);
                } catch (ResourceException e) {
                    if (e.getStatus() == 404) {
                        throw new LDAPException("group not found", LDAPException.NO_SUCH_OBJECT, LDAPException.resultCodeToString(LDAPException.NO_SUCH_OBJECT));
                    } else {
                        throw new LDAPException("Could not load group", LDAPException.OPERATIONS_ERROR, LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), e);
                    }
                } catch (UnsupportedEncodingException e) {
                    throw new LDAPException("Could not load group", LDAPException.OPERATIONS_ERROR, LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), e);
                } catch (IllegalStateException e) {
                    throw new LDAPException("group not found", LDAPException.NO_SUCH_OBJECT, LDAPException.resultCodeToString(LDAPException.NO_SUCH_OBJECT));
                }
            }
            return;
        }
    } else /*else if (scope.getValue() == 1) {
        	if (base.getDN().equals(this.baseDN)) {
        		
        		if (this.users) {
	        		String name = userFromFilter(filter.getRoot());
	        		
	        		loadUserFromOkta(chain, base, scope, filter, attributes, typesOnly, results, constraints, os, name,new StringBuilder().append("login=").append(name).append(",").append(base.getDN().toString()).toString(),false);
        		}
        		
				return;
        	}
        }*/
    {
        // only subtree left
        // String name = userFromFilter(filter.getRoot());
        // loadUserFromOkta(chain, base, scope, filter, attributes, typesOnly, results, constraints, os, name,new StringBuilder().append("uid=").append(name).append(",").append(this.baseDN.toString()).toString(),false);
        Client okta = os.getOkta();
        Filter newFilter = new Filter(filter.getRoot().toString());
        String finalOktaFilter = null;
        if (this.cleanFilter(newFilter.getRoot())) {
            StringBuffer filterForOkta = new StringBuffer();
            this.stringFilter(newFilter.getRoot(), filterForOkta);
            finalOktaFilter = filterForOkta.toString();
        }
        if (logger.isDebugEnabled()) {
            logger.debug(newFilter.getRoot().toString());
        }
        if (this.users) {
            UserList usersFromOkta = okta.listUsers(null, finalOktaFilter, null, null, null);
            StringBuilder sb = new StringBuilder();
            ArrayList<Entry> ret = new ArrayList<Entry>();
            for (User user : usersFromOkta) {
                if (logger.isDebugEnabled()) {
                    logger.debug(user);
                }
                sb.setLength(0);
                sb.append("login=").append(user.getProfile().getLogin().replace("+", "\\+")).append(",").append(this.baseDN.toString());
                LDAPEntry ldapUser = createLdapUser(sb.toString(), user);
                if (filter.getRoot().checkEntry(ldapUser)) {
                    ret.add(new Entry(ldapUser));
                }
            }
            chain.addResult(results, new IteratorEntrySet(ret.iterator()), base, scope, filter, attributes, typesOnly, constraints);
        } else {
            HashSet<String> groupsToLookup = new HashSet<String>();
            HashSet<String> usersToLookup = new HashSet<String>();
            loadGroups(filter.getRoot(), groupsToLookup, usersToLookup);
            StringBuilder sb = new StringBuilder();
            HashSet<String> processedGroups = new HashSet<String>();
            ArrayList<Entry> ret = new ArrayList<Entry>();
            if (usersToLookup.size() > 0) {
                sb.setLength(0);
                for (String username : usersToLookup) {
                    sb.append("profile.login eq \"").append(username).append("\" or ");
                }
                String searchFilter = sb.toString();
                searchFilter = searchFilter.substring(0, searchFilter.length() - 3);
                UserList users = okta.listUsers(null, searchFilter, null, null, null);
                for (User fromOkta : users) {
                    GroupList memberships = fromOkta.listGroups();
                    for (Group groupFromOkta : memberships) {
                        if (!processedGroups.contains(groupFromOkta.getProfile().getName())) {
                            try {
                                processedGroups.add(groupFromOkta.getProfile().getName());
                                sb.setLength(0);
                                sb.append("name=").append(groupFromOkta.getProfile().getName().replace("+", "\\+")).append(",").append(this.baseDN.toString());
                                LDAPEntry entry = new LDAPEntry(sb.toString());
                                try {
                                    this.oktaGroup2Ldap(filter, ret, groupFromOkta, entry);
                                } catch (UnsupportedEncodingException e) {
                                    throw new LDAPException("Could not load group", LDAPException.OPERATIONS_ERROR, LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), e);
                                }
                            } catch (IllegalStateException e) {
                            // no nothing
                            }
                        }
                    }
                }
            }
            if (groupsToLookup.size() > 0) {
                for (String group : groupsToLookup) {
                    if (!processedGroups.contains(group)) {
                        GroupList groups = okta.listGroups(group, null, null);
                        processedGroups.add(group);
                        try {
                            Group groupFromOkta = groups.single();
                            sb.setLength(0);
                            sb.append("name=").append(groupFromOkta.getProfile().getName().replace("+", "\\+")).append(",").append(this.baseDN.toString());
                            LDAPEntry entry = new LDAPEntry(sb.toString());
                            try {
                                this.oktaGroup2Ldap(filter, ret, groupFromOkta, entry);
                            } catch (UnsupportedEncodingException e) {
                                throw new LDAPException("Could not load group", LDAPException.OPERATIONS_ERROR, LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), e);
                            }
                        } catch (IllegalStateException e) {
                        // no nothing
                        }
                    }
                }
            }
            if (usersToLookup.size() == 0 && groupsToLookup.size() == 0) {
                GroupList groups = okta.listGroups();
                try {
                    for (Group groupFromOkta : groups) {
                        sb.setLength(0);
                        sb.append("name=").append(groupFromOkta.getProfile().getName().replace("+", "\\+")).append(",").append(this.baseDN.toString());
                        LDAPEntry entry = new LDAPEntry(sb.toString());
                        try {
                            this.oktaGroup2Ldap(filter, ret, groupFromOkta, entry);
                        } catch (UnsupportedEncodingException e) {
                            throw new LDAPException("Could not load group", LDAPException.OPERATIONS_ERROR, LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), e);
                        }
                    }
                } catch (IllegalStateException e) {
                // no nothing
                }
            }
            chain.addResult(results, new IteratorEntrySet(ret.iterator()), base, scope, filter, attributes, typesOnly, constraints);
        }
        return;
    }
}
Also used : Group(com.okta.sdk.resource.group.Group) User(com.okta.sdk.resource.user.User) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IteratorEntrySet(net.sourceforge.myvd.util.IteratorEntrySet) Entry(net.sourceforge.myvd.types.Entry) LDAPEntry(com.novell.ldap.LDAPEntry) LDAPEntry(com.novell.ldap.LDAPEntry) LDAPException(com.novell.ldap.LDAPException) GroupList(com.okta.sdk.resource.group.GroupList) Filter(net.sourceforge.myvd.types.Filter) OktaTarget(com.tremolosecurity.unison.okta.provisioning.OktaTarget) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ResourceException(com.okta.sdk.resource.ResourceException) AuthenticationClient(com.okta.authn.sdk.client.AuthenticationClient) Client(com.okta.sdk.client.Client) RDN(com.novell.ldap.util.RDN) UserList(com.okta.sdk.resource.user.UserList) HashSet(java.util.HashSet)

Aggregations

Filter (net.sourceforge.myvd.types.Filter)11 ArrayList (java.util.ArrayList)10 LDAPException (com.novell.ldap.LDAPException)8 LDAPAttribute (com.novell.ldap.LDAPAttribute)6 FilterNode (net.sourceforge.myvd.types.FilterNode)6 LDAPEntry (com.novell.ldap.LDAPEntry)5 Entry (net.sourceforge.myvd.types.Entry)5 Attribute (net.sourceforge.myvd.types.Attribute)4 LDAPSearchConstraints (com.novell.ldap.LDAPSearchConstraints)3 RDN (com.novell.ldap.util.RDN)3 SearchInterceptorChain (net.sourceforge.myvd.chain.SearchInterceptorChain)3 Bool (net.sourceforge.myvd.types.Bool)3 DistinguishedName (net.sourceforge.myvd.types.DistinguishedName)3 Int (net.sourceforge.myvd.types.Int)3 Results (net.sourceforge.myvd.types.Results)3 Item (com.amazonaws.services.simpledb.model.Item)2 LDAPSearchResults (com.novell.ldap.LDAPSearchResults)2 DN (com.novell.ldap.util.DN)2 IteratorEntrySet (net.sourceforge.myvd.util.IteratorEntrySet)2 Gson (com.google.gson.Gson)1