Skip to content

Instantly share code, notes, and snippets.

@hasithaa
Last active December 15, 2022 07:03
Show Gist options
  • Save hasithaa/9597687 to your computer and use it in GitHub Desktop.
Save hasithaa/9597687 to your computer and use it in GitHub Desktop.
A utility to generate LDIF (LDAP Data Interchange Format)
/**
* Copyright (c) 2014.
* This file is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Author : Hasitha Aravinda
*/
public class LDIFGenerator {
//LDAP related constants:
public static final String DN = "dn";
public static final String C = "c";
public static final String O = "o";
public static final String OU = "ou";
//Attributes
public static final String OBJECT_CLASS = "objectClass";
public static final String CN = "cn";
public static final String SN = "sn";
public static final String GIVENNAME = "givenname";
public static final String MAIL = "mail";
public static final String UID = "uid";
public static final String USER_PASSWORD = "userpassword";
// Attributes' values
public static final String TOP = "top";
public static final String COUNTY = "country";
public static final String ORGANIZATION = "organization";
public static final String ORGANIZATIONAL_UNIT = "organizationalUnit";
public static final String ORGANIZATIONAL_ROLE = "organizationalRole";
public static final String INET_ORG_PERSON = "inetOrgPerson";
public static final String PERSON = "person";
public static final String ORGANIZATIONAL_PERSON = "organizationalPerson";
public static final String GROUP_OF_NAME = "groupOfNames";
public static final String MEMBER = "member";
public static final String EQ = "=";
public static final String COMMA = ",";
public static final String NL = "\n"; // New line
public static final String COLON = ": ";
private String currentCountryDN;
private String currentOrganizationDN;
private String currentOrganizationalUnitDN;
private String currentOrganizationalRoleDN;
public String getCurrentCountryDN() {
if (currentCountryDN == null) {
throw new RuntimeException("County DN is not generated yet.");
}
return currentCountryDN;
}
public String getCurrentOrganizationDN() {
if (currentOrganizationDN == null) {
throw new RuntimeException("organization DN is not generated yet.");
}
return currentOrganizationDN;
}
public String getCurrentOrganizationalUnitDN() {
if (currentOrganizationalUnitDN == null) {
throw new RuntimeException("organizationalUnit DN is not generated yet");
}
return currentOrganizationalUnitDN;
}
public String getCurrentOrganizationalRoleDN() {
if (currentOrganizationalRoleDN == null) {
throw new RuntimeException("organizationalRole DN is not generated yet");
}
return currentOrganizationalRoleDN;
}
public String generateUser(String attribute, String username, String password, String email, String parentDN) {
return DN + COLON + attribute + EQ + username + COMMA + parentDN + NL
+ OBJECT_CLASS + COLON + TOP + NL
+ OBJECT_CLASS + COLON + INET_ORG_PERSON + NL
+ OBJECT_CLASS + COLON + PERSON + NL
+ OBJECT_CLASS + COLON + ORGANIZATIONAL_PERSON + NL
+ CN + COLON + username + NL
+ SN + COLON + username + NL
+ GIVENNAME + COLON + username + NL
+ MAIL + COLON + email + NL
+ UID + COLON + username + NL
+ USER_PASSWORD + COLON + password + NL
+ NL
;
}
public String generateCountry(String country) {
this.currentCountryDN = C + EQ + country;
return DN + COLON + currentCountryDN + NL
+ OBJECT_CLASS + COLON + TOP + NL
+ OBJECT_CLASS + COLON + COUNTY + NL
+ C + COLON + country + NL
+ NL;
}
public String generateOrganization(String organization, String countryDN) {
this.currentOrganizationDN = O + EQ + organization + COMMA + countryDN;
return DN + COLON + currentOrganizationDN + NL
+ OBJECT_CLASS + COLON + TOP + NL
+ OBJECT_CLASS + COLON + ORGANIZATION + NL
+ O + COLON + organization + NL
+ NL;
}
public String generateOrganizationalUnit(String organizationUnit, String organizationDN) {
this.currentOrganizationalUnitDN = OU + EQ + organizationUnit + COMMA + organizationDN;
return DN + COLON + currentOrganizationalUnitDN + NL
+ OBJECT_CLASS + COLON + TOP + NL
+ OBJECT_CLASS + COLON + ORGANIZATIONAL_UNIT + NL
+ OU + COLON + organizationUnit + NL
+ NL;
}
public String generateOrganizationalRole(String name, String parentDN) {
this.currentOrganizationalRoleDN = CN + EQ + name + COMMA + parentDN;
return DN + COLON + currentOrganizationalRoleDN + NL
+ OBJECT_CLASS + COLON + TOP + NL
+ OBJECT_CLASS + COLON + ORGANIZATIONAL_ROLE + NL
+ CN + COLON + name + NL
+ NL;
}
public String generateGroup(String attribute, String groupName, String parentDN) {
return DN + COLON + attribute + EQ + groupName + COMMA + parentDN + NL
+ OBJECT_CLASS + COLON + TOP + NL
+ OBJECT_CLASS + COLON + GROUP_OF_NAME + NL
+ attribute + COLON + groupName + NL
;
}
public String generateMember(String membershipAttribute, String attribute, String userID, String userParentDN) {
return MEMBER + COLON + attribute + EQ + userID + COMMA + userParentDN + NL;
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class Main {
/**
* Example LDIF Generator
*
*/
public static void main(String[] args) throws Exception {
LDIFGenerator ldiffGenerator = new LDIFGenerator();
File file = new File("/tmp/GroupsGen1.ldif");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
// configure following values
final int TOTAL_USER_COUNT = 20000;
final int TOP_GROUPS_COUNT = 5;
final int SUB_GROUPS_COUNT = 20;
final int CHILD_GROUPS_COUNT = 10;
final int USERS_PER_GROUP = 50;
// Names
final String COUNTRY = "LK";
final String ORGANIZATION = "example";
final String ORGANIZATION_UNIT = "org1";
//Organizational Roles names
final String USERS = "users";
final String GROUPS = "groups";
final String ADMINS = "admins";
//Admin Details
final String ADMIN_NAME = "admin@example.com";
final String ADMIN_PASSWORD = "admin=";
final String ADMIN_ROLE = "example-admins";
final String USER_NAME_PREFIX = "user";
final String USER_EMAIl_DOMAIN = "@example.com";
final String USER_PASSWORD = "password=";
final String GROUP_PARENT_NAME = "parentGroup";
final String GROUP_SUB__NAME = "subGroup";
final String GROUP_CHILD__NAME = "childGroup";
bw.write("version: 1\n\n");
// creating county
bw.append(ldiffGenerator.generateCountry(COUNTRY));
// creating origination DN
bw.append(ldiffGenerator.generateOrganization(
ORGANIZATION,
ldiffGenerator.getCurrentCountryDN()));
// creating originationUnit DN
bw.append(ldiffGenerator.generateOrganizationalUnit(
ORGANIZATION_UNIT,
ldiffGenerator.getCurrentOrganizationDN()));
// creating ADMIN organizational Role
bw.append(ldiffGenerator.generateOrganizationalRole(
ADMINS,
ldiffGenerator.getCurrentOrganizationalUnitDN()));
// adding ADMIN USER
bw.append(ldiffGenerator.generateUser(
LDIFGenerator.CN,
ADMIN_NAME,
ADMIN_PASSWORD,
ADMIN_NAME,
ldiffGenerator.getCurrentOrganizationalRoleDN()));
bw.append(ldiffGenerator.generateGroup(
LDIFGenerator.CN,
ADMIN_ROLE,
ldiffGenerator.getCurrentOrganizationalRoleDN()));
bw.append(ldiffGenerator.generateMember(
LDIFGenerator.MEMBER,
LDIFGenerator.CN,
ADMIN_NAME,
ldiffGenerator.getCurrentOrganizationalRoleDN()));
bw.append(LDIFGenerator.NL);
// creating Users organizational Role
bw.append(ldiffGenerator.generateOrganizationalRole(
USERS, ldiffGenerator.getCurrentOrganizationalUnitDN()));
String userPath = ldiffGenerator.getCurrentOrganizationalRoleDN();
// creating users OrgRole
for (int i = 1; i <= TOTAL_USER_COUNT; i++) {
bw.append(ldiffGenerator.generateUser(
LDIFGenerator.CN,
USER_NAME_PREFIX + i + USER_EMAIl_DOMAIN,
USER_PASSWORD,
USER_NAME_PREFIX + i + USER_EMAIl_DOMAIN,
ldiffGenerator.getCurrentOrganizationalRoleDN()));
}
System.out.println("Generating users completed !");
// creating groups OrgRole
bw.append(ldiffGenerator.generateOrganizationalRole(
GROUPS,
ldiffGenerator.getCurrentOrganizationalUnitDN()));
String groupsOrgRoleDN = ldiffGenerator.getCurrentOrganizationalRoleDN();
for (int i = 1; i <= TOP_GROUPS_COUNT; i++) {
bw.append(ldiffGenerator.generateOrganizationalRole(
GROUP_PARENT_NAME + i,
groupsOrgRoleDN));
String currentTopGroupDN = ldiffGenerator.getCurrentOrganizationalRoleDN();
for (int j = 1; j <= SUB_GROUPS_COUNT; j++) {
String subGroupID = "_" + i + "_" + j;
bw.append(ldiffGenerator.generateOrganizationalRole(
GROUP_SUB__NAME + subGroupID,
currentTopGroupDN));
String currentSubGroupDN = ldiffGenerator.getCurrentOrganizationalRoleDN();
for (int k = 1; k <= CHILD_GROUPS_COUNT; k++) {
String childGroupID = subGroupID + "_" + k;
bw.append(ldiffGenerator.generateGroup(
LDIFGenerator.CN,
GROUP_CHILD__NAME + childGroupID,
currentSubGroupDN));
//Adding users to above group.
for (int l = 0; l < USERS_PER_GROUP; l++) {
// Adding 1 to avoid getting zero and floor to avoid getting number more than TOTAL_USER_COUNT
int userID = (int) Math.floor(Math.random() * TOTAL_USER_COUNT) + 1;
bw.append(ldiffGenerator.generateMember(
LDIFGenerator.MEMBER,
LDIFGenerator.CN,
USER_NAME_PREFIX + userID + USER_EMAIl_DOMAIN,
userPath));
}
bw.append(LDIFGenerator.NL);
}
}
}
System.out.println("Generating groups completed !");
bw.close();
System.out.println("Done !");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment