Skip to content

Instantly share code, notes, and snippets.

@tizzo
Last active May 30, 2019 13:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tizzo/c6e387f7c05fe5a67e38dfd8ee728b31 to your computer and use it in GitHub Desktop.
Parse LDIF files and produce a CSV of group membership
{
"name": "ldif-parse",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"ldif": "^0.5.1"
}
}
const ldif = require("ldif");
const file = "output.ldif";
const input = require("fs").readFileSync(file, "utf8");
const document = ldif.parse(input);
const people = document.entries
.filter(object => {
return object.attributes.find(attribute => {
return (
attribute.attribute.attribute == "objectClass" &&
attribute.value.value == "person"
);
});
})
.reduce((acc, person) => {
acc[person.dn] = person.attributes.find(
attribute => attribute.attribute.attribute == "cn"
).value.value;
return acc;
}, {});
const groups = document.entries
.filter(item => {
return item.attributes.find(attribute => {
return (
attribute.attribute.attribute == "objectClass" &&
attribute.value.value == "groupOfNames"
);
});
})
.reduce((acc, group) => {
acc[group.dn] = {
name: group.attributes.find(
attribute => attribute.attribute.attribute == "cn"
).value.value,
members: group.attributes
.filter(attribute => attribute.attribute.attribute === "member")
.map(attribute => {
return people[attribute.value.value]
? people[attribute.value.value]
: attribute.value.value;
})
};
return acc;
}, {});
const output = groups;
//console.log(JSON.stringify(output, null, 2));
Object.keys(groups).forEach(group =>
groups[group].members.forEach(member =>
process.stdout.write(`${member},${groups[group].name}\n`)
)
);
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
ldif@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/ldif/-/ldif-0.5.1.tgz#2135975a83755df96fc277eeaea6a59cc03c1e83"
integrity sha1-ITWXWoN1XflvwnfurqalnMA8HoM=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment