User Tools

Site Tools


ldap

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ldap [2020/07/20 10:52] skipidarldap [2020/12/27 20:35] (current) – external edit 127.0.0.1
Line 365: Line 365:
     contextSource.setUserDn(env.getRequiredProperty("ldap.user"));     contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
     contextSource.setPassword(env.getRequiredProperty("ldap.password"));     contextSource.setPassword(env.getRequiredProperty("ldap.password"));
-    contextSource.setPooled(false); // Robert improved the performance by 100x for PARALLEL requests by enabling that+    contextSource.setPooled(false); // pooling is enabled on the wrapper
  
     Map<String, Object> environment = new HashMap<>();     Map<String, Object> environment = new HashMap<>();
Line 410: Line 410:
  
  
-===== BaseClass =====+===== BaseClass for Spring Data =====
  
 <sxh java> <sxh java>
Line 1042: Line 1042:
  
 </sxh> </sxh>
 +
 +
 +
 +
 +==== LDAP tools ====
 +
 +<sxh java>
 +
 +@Component
 +public class Tools {
 +
 +    private static Logger LOG = LoggerFactory.getLogger(Tools.class);
 +
 +    private final LdapName baseLdapPath;
 +
 +    @Autowired
 +    public Tools(Environment env) {
 +        this.baseLdapPath = LdapUtils.newLdapName(env.getRequiredProperty("ldap.base"));
 +    }
 +
 +    public static final <T> List<T> toList(Iterable<T> i) {
 +        return StreamSupport.stream(i.spliterator(), true).collect(Collectors.toList());
 +    }
 +
 +    public LdapName toAbsoluteDn(Name relativeName) {
 +        // check if already absolute
 +        if (relativeName.startsWith(baseLdapPath)) {
 +            LOG.info(String.format("The dn %s already contains the baseLdapPath %s so skip reappending it", relativeName, baseLdapPath));
 +            return LdapNameBuilder.newInstance(relativeName)
 +                    .build();
 +        } else {
 +            return LdapNameBuilder.newInstance(baseLdapPath)
 +                    .add(relativeName)
 +                    .build();
 +        }
 +    }
 +
 +    public static Optional<LdapName> toLdapId(String str) {
 +        try {
 +            return Optional.of(LdapNameBuilder.newInstance(str).build());
 +        } catch (NullPointerException | org.springframework.ldap.InvalidNameException e) {
 +            return Optional.empty();
 +        }
 +    }
 +
 +    public static boolean isLdapId(String str) {
 +        try {
 +            LdapName name = LdapNameBuilder.newInstance(str).build();
 +            return true;
 +        } catch (NullPointerException | org.springframework.ldap.InvalidNameException e) {
 +            return false;
 +        }
 +    }
 +
 +    public LdapName toRelativeDn(Name absoluteName) {
 +        Name name = absoluteName;
 +        if (absoluteName.startsWith(baseLdapPath)) {
 +            name = absoluteName.getSuffix(baseLdapPath.size());
 +        }
 +        LOG.info(String.format("The dn %s doesnt contains the baseLdapPath %s so skip reappending it", absoluteName, baseLdapPath));
 +        return LdapNameBuilder.newInstance(name).build();
 +    }
 +
 +
 +    /**
 +     * The relative DN is typically what is needed to perform lookups and searches in
 +     * the LDAP tree, whereas the absolute DN is needed when authenticating and when
 +     * an LDAP entry is referred to in e.g. a group. This wrapper class contains
 +     * both of these representations.
 +     * <p>
 +     * See LdapEntryIdentification.class comment
 +     *
 +     * @param ldapIds
 +     * @return
 +     */
 +    public List<Name> toAbsLdapIds(Collection<Name> ldapIds) {
 +        final ArrayList<Name> list = new ArrayList<>();
 +        ldapIds.forEach(ldapId -> {
 +            final LdapName absLdapId = toAbsoluteDn(ldapId);
 +            list.add(absLdapId);
 +        });
 +        return list;
 +    }
 +
 +
 +    public boolean ldapNameContainsSegment(final Name ldapName, final Name fragment) {
 +        if (ldapName == null || fragment == null || ldapName.size() < fragment.size()) {
 +            return false;
 +        }
 +
 +        // Create an empty list
 +        final List<String> listLdapName = new ArrayList<>();
 +        ldapName.getAll().asIterator().forEachRemaining(listLdapName::add);
 +
 +        final List<String> listFragment = new ArrayList<>();
 +        fragment.getAll().asIterator().forEachRemaining(listFragment::add);
 +
 +        // is the fragment like "ou=People" present on the path?
 +        return (Collections.indexOfSubList(listLdapName, listFragment) != -1);
 +    }
 +
 +    /**
 +     * Assumes that tags are separated by empty spaces
 +     *
 +     * @param tags
 +     * @return
 +     */
 +    public List<String> toTagsList(final String tags) {
 +        if (tags == null ) return null;
 +        if (tags.isEmpty() ) return Collections.EMPTY_LIST;
 +        final String[] tagsList = tags.trim().split("[ ]+");
 +        return Arrays.asList(tagsList);
 +    }
 +
 +
 +
 +
 +
 +    /**
 +     * Takes two ldap names and replaces the suffix
 +     * by cutting off the length of suffix from the ldapName and appending the suffix
 +     *
 +     * The suffix will be appended on the right side of String.
 +     * Eg in "cn=dudidum,ou=People,ou=BIGFISH,ou=Tenants" the suffix with size 2 is "ou=BIGFISH,ou=Tenants"
 +     *
 +     * @param ldapName  - name, the suffix of which sould be replaced
 +     * @param newSuffix - suffix to append.
 +     * @return name with new suffix
 +     */
 +    public LdapName replaceSuffix(final LdapName ldapName, final LdapName newSuffix) {
 +        Assert.isTrue(ldapName.size() >= newSuffix.size(), String.format("The length of the ldapName %s must be bigger, than that of suffix %s", ldapName, newSuffix));
 +        if (newSuffix.size() == 0) return ldapName;
 +
 +        try {
 +            // cut off the the initial suffix, prepare merge with new suffix
 +            final LdapName copyName = LdapUtils.newLdapName(ldapName);
 +            for (int i = 0; i < newSuffix.size(); i++) {
 +                copyName.remove(0);
 +            }
 +            copyName.addAll(0, newSuffix);
 +            return copyName;
 +
 +        } catch (InvalidNameException e) {
 +            throw new RuntimeException(e);
 +        }
 +    }
 +}
 +
 +</sxh>
 +
  
ldap.1595242352.txt.gz · Last modified: (external edit)