From 4f33f581cb86819dd202d120989e4a6ac06ef6d2 Mon Sep 17 00:00:00 2001 From: Christian Pape Date: Tue, 26 Apr 2022 20:16:33 +0200 Subject: [PATCH] Added user defined mappings --- .../de/hsfulda/informatik/AccountSource.java | 8 +-- .../java/de/hsfulda/informatik/LdapSync.java | 52 +++++++++++++++++-- .../de/hsfulda/informatik/LdapSyncTest.java | 37 +++++++++++++ 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/hsfulda/informatik/AccountSource.java b/src/main/java/de/hsfulda/informatik/AccountSource.java index b35eb14..55bbda8 100644 --- a/src/main/java/de/hsfulda/informatik/AccountSource.java +++ b/src/main/java/de/hsfulda/informatik/AccountSource.java @@ -72,10 +72,10 @@ public class AccountSource { if (ldapResult.getResultCode().intValue() == 0) { System.out.print("Ok\n"); } else { - System.out.print("Failed (" + ldapResult.getResultCode() + ")\n"); + System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n"); } } catch (LDAPException e) { - System.out.print("Failed (" + e.getMessage() + ")\n"); + System.out.print("Fehler (" + e.getMessage() + ")\n"); } } } @@ -91,10 +91,10 @@ public class AccountSource { if (ldapResult.getResultCode().intValue() == 0) { System.out.print("Ok\n"); } else { - System.out.print("Failed (" + ldapResult.getResultCode() + ")\n"); + System.out.print("Fehler (" + ldapResult.getResultCode() + ")\n"); } } catch (LDAPException e) { - System.out.print("Failed (" + e.getMessage() + ")\n"); + System.out.print("Fehler (" + e.getMessage() + ")\n"); } } } diff --git a/src/main/java/de/hsfulda/informatik/LdapSync.java b/src/main/java/de/hsfulda/informatik/LdapSync.java index 4f9bb87..5c6620d 100644 --- a/src/main/java/de/hsfulda/informatik/LdapSync.java +++ b/src/main/java/de/hsfulda/informatik/LdapSync.java @@ -1,5 +1,6 @@ package de.hsfulda.informatik; +import com.google.common.base.Strings; import com.unboundid.ldap.sdk.Attribute; import com.unboundid.ldap.sdk.Entry; import com.unboundid.ldap.sdk.LDAPException; @@ -8,9 +9,12 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.security.GeneralSecurityException; +import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.TreeMap; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -20,14 +24,17 @@ import java.util.stream.Collectors; * Ldap sync utility */ public class LdapSync { - final Properties properties = new Properties(); - final static Pattern pattern = Pattern.compile("^fd([a-z][a-z])?([0-9]*?)$"); + private final Properties properties = new Properties(); + private final static Pattern pattern = Pattern.compile("^fd([a-z][a-z])?([0-9]*?)$"); + private Map userDefinedMappings = Collections.emptyMap(); public LdapSync() throws IOException, LDAPException, GeneralSecurityException { // lade Konfiguration properties.load(new FileReader("ldap-sync.properties")); System.out.print("Abfrage der Benutzer im eDirectory..."); + this.userDefinedMappings = parseUserDefinedMappings(properties.getProperty("sync.src.map", "")); + // lade Daten des Remote-Systems final AccountSource remote = new AccountSource( properties.getProperty("sync.src.host"), @@ -57,6 +64,37 @@ public class LdapSync { sync(remote, local); } + static Map parseUserDefinedMappings(final String property) { + if (Strings.isNullOrEmpty(property)) { + return Collections.emptyMap(); + } + + final Map mappingsMap = new TreeMap<>(); + final String mappingArr[] = property.split("\\s*,\\s*"); + + for (final String mapping : mappingArr) { + final String accountUidArr[] = mapping.split(":"); + if (accountUidArr.length == 2) { + final String uid = accountUidArr[0].toLowerCase(); + final int uidNumber; + + try { + uidNumber = Integer.valueOf(accountUidArr[1]); + } catch (NumberFormatException e) { + System.out.println("Fehler beim Parsen der uidNumber für uid '" + uid + "'"); + continue; + } + + System.out.println("Benutzerdefinierte Zuordnung '" + uid + "' -> " + uidNumber); + mappingsMap.put(uid, uidNumber); + } else { + System.out.println("Fehler beim Parsen der Zuordnung '" + mapping + "'"); + } + } + + return mappingsMap; + } + LdapSync(final AccountSource remote, final AccountSource local) throws IOException, LDAPException, GeneralSecurityException { sync(remote, local); } @@ -82,7 +120,7 @@ public class LdapSync { e.addAttribute(new Attribute("sn", s.getAttributeValue("sn"))); e.addAttribute(new Attribute("givenname", s.getAttributeValue("givenName"))); e.addAttribute(new Attribute("uid", cn)); - e.addAttribute(new Attribute("uidNumber", String.valueOf(computeUid(cn)))); + e.addAttribute(new Attribute("uidNumber", String.valueOf(computeUid(cn, this.userDefinedMappings)))); e.addAttribute(new Attribute("gidNumber", "20")); e.addAttribute(new Attribute("loginShell", "/bin/zsh")); e.addAttribute(new Attribute("homeDirectory", "/Users/" + cn)); @@ -98,6 +136,14 @@ public class LdapSync { } static Integer computeUid(final String cn) { + return computeUid(cn, Collections.emptyMap()); + } + + static Integer computeUid(final String cn, final Map mappings) { + if (mappings.containsKey(cn)) { + return mappings.get(cn); + } + final Matcher m = pattern.matcher(cn); if (m.find()) { diff --git a/src/test/java/de/hsfulda/informatik/LdapSyncTest.java b/src/test/java/de/hsfulda/informatik/LdapSyncTest.java index c6b040e..7d6065a 100644 --- a/src/test/java/de/hsfulda/informatik/LdapSyncTest.java +++ b/src/test/java/de/hsfulda/informatik/LdapSyncTest.java @@ -4,6 +4,7 @@ import com.unboundid.ldap.sdk.Attribute; import com.unboundid.ldap.sdk.Entry; import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.ldap.sdk.SearchResultEntry; + import org.junit.Test; import java.io.IOException; @@ -11,6 +12,7 @@ import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -95,6 +97,41 @@ public class LdapSyncTest { assertEquals(10149999, (int) LdapSync.computeUid("fdaa9999")); assertEquals(262641000, (int) LdapSync.computeUid("fdzz1000")); assertEquals(262649999, (int) LdapSync.computeUid("fdzz9999")); + + // test user defined mappings + Map mappings; + + mappings = LdapSync.parseUserDefinedMappings("fdg00244:300000244,fdbla:300009999, fdfoo:300009777 , fdbar:300009888"); + assertEquals(300000244, (int) LdapSync.computeUid("fdg00244", mappings)); + assertEquals(300009999, (int) LdapSync.computeUid("fdbla", mappings)); + assertEquals(300009777, (int) LdapSync.computeUid("fdfoo", mappings)); + assertEquals(300009888, (int) LdapSync.computeUid("fdbar", mappings)); + assertEquals(41000, (int) LdapSync.computeUid("fd1000", mappings)); + assertEquals(49999, (int) LdapSync.computeUid("fd9999", mappings)); + assertEquals(10141000, (int) LdapSync.computeUid("fdaa1000", mappings)); + assertEquals(10149999, (int) LdapSync.computeUid("fdaa9999", mappings)); + assertEquals(262641000, (int) LdapSync.computeUid("fdzz1000", mappings)); + assertEquals(262649999, (int) LdapSync.computeUid("fdzz9999", mappings)); + + mappings = LdapSync.parseUserDefinedMappings("fdg00244:300000244"); + assertEquals(300000244, (int) LdapSync.computeUid("fdg00244", mappings)); + assertEquals(41000, (int) LdapSync.computeUid("fd1000", mappings)); + assertEquals(49999, (int) LdapSync.computeUid("fd9999", mappings)); + assertEquals(10141000, (int) LdapSync.computeUid("fdaa1000", mappings)); + assertEquals(10149999, (int) LdapSync.computeUid("fdaa9999", mappings)); + assertEquals(262641000, (int) LdapSync.computeUid("fdzz1000", mappings)); + assertEquals(262649999, (int) LdapSync.computeUid("fdzz9999", mappings)); + + mappings = LdapSync.parseUserDefinedMappings("aaabbb"); + assertEquals(0, mappings.size()); + mappings = LdapSync.parseUserDefinedMappings("aaa:123"); + assertEquals(1, mappings.size()); + mappings = LdapSync.parseUserDefinedMappings("aaa::123"); + assertEquals(0, mappings.size()); + mappings = LdapSync.parseUserDefinedMappings("aaa:123,,bbb:456"); + assertEquals(2, mappings.size()); + mappings = LdapSync.parseUserDefinedMappings("aaa:123, bbb:ccc"); + assertEquals(1, mappings.size()); } private SearchResultEntry searchResultEntry(final String dn, final String cn, final String sn, final String givenname) {