“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”
In the last post we shared the idea of how to convert LDIF data into JSON data and draw the corresponding schematic diagram
This time, let’s do it
The implementation is as follows:
-
Connect to the LDAP server and query the data structure on the LDAP server. How does Goalng obtain the data from the LDAP server? There are said to the
-
Iterate through each entry
-
When processing each entry, the RDN (corresponding key and value) is obtained from right to left, and a node in the multi-fork tree is created for each RDN
-
Nodes corresponding to the BASedN and node addresses corresponding to each OU are stored in a map (key is string, value is node address) to facilitate subsequent traversal of other entries. You can directly obtain the node address based on the OU name
-
For users under a node, simply attach to that node
Let’s look at the data structure and the main function
The data structure is the necessary information for the node
// Node information
type lNode struct {
Name string
Path string
Children []*lNode
User []string
}
// Create a node
func NewNode(name, path string) *lNode {
return &lNode{
Name: name,
Path: path,
Children: []*lNode{},
User: []string{},}}Copy the code
The execution flow of main function is as follows:
- Connect to the LDAP server and query the corresponding data
- Process the data and generate a tree (default DC is the root node, /)
- Convert the tree to JSON format and print it out
func main(a) {
data := connectLdap(
"ldap://xxxx"."dc=xiaomotong,dc=com"."cn=admin,dc=xiaomotong,dc=com"."123123"."(&(objectClass=*))")
if len(data) <= 0 {
fmt.Println("search no data !!")
}
mp := make(map[string]*lNode)
root := NewNode("dc=xiaomotong,dc=com"."/")
mp["dc=xiaomotong,dc=com"] = root
// Generate a tree
CreateLdapTree(mp, data, "dc=xiaomotong,dc=com")
b, err := json.Marshal(root)
iferr ! =nil {
fmt.Println("json.Marshal error !!!")
return
}
fmt.Println(string(b))
}
Copy the code
Get data from the LDAP server
Let’s simply implement it in a main.go file. The code structure looks like this
Func connectLdap(addr, baseDB, username, passwd, filter String) []* ldap.entry {Goalng how to obtain ldap server data? Yes, we just adjusted the parameters this time
Process data for LDAP responses
The data returned by LDAP is in LDIF format, which returns 0 to multiple entries. You need to parse the data in each entry one by one
An entry is a DN, and a DN contains multiple RDN. An RDN is a key-value pair
- Create the root node with the message BASEDN:
dc=xiaomotong,dc=com
And put the information into the map
- Each DN of data is parsed, and each RDN in the DN is created corresponding node, and the RDN is connected from right to left through the DN sequence
- A group has subgroups in the Children of a node, a User in a group in the User, the name of the current node in the name, and the absolute path of the current node in the path
Func CreateLdapTree(mp map[string]*lNode, Entries []*ldap.Entry, BASEDN String) {function
// Create a tree
func CreateLdapTree(mp map[string]*lNode, Entries []*ldap.Entry, BASEDN string) {
/ / traverse Entries
for _, Entry := range Entries {
if BASEDN == Entry.DN {
continue
}
ProcessDN(Entry.DN, mp, BASEDN)
}
}
Copy the code
The specific implementation of CreateLdapTree is to iterate through all ldap entries and call ProcessDN function to parse DN data and generate the corresponding multi-fork tree fragment according to DN
Specific processing of DN data
Func ProcessDN(DN String, mp map[string]*lNode, BASEDN String)
- Mainly do is to parse a DN data, and generate a multi-fork tree fragment
- Ou node addresses are recorded in the MAP for future use
The processing logic determines whether the key of the RDN is DC, CN, or OU. If the key is OU, a node is created and its address is recorded in the map
Json serialization
Finally, serialize the data structure to JSON and print it out as a string
The above code logic is also relatively simple, is to convert LDIF into a tree, the code flow is
The entire main.go file, after execution, the result is as follows: lDIF is successfully converted into a multi-fork tree, which has been displayed in JSON mode
{
"Name": "dc=xiaomotong,dc=com"."Path": "/"."Children": [{"Name": "People"."Path": "/People/"."Children": []."User": [
"xiaozhupeiqi"] {},"Name": "dev"."Path": "/dev/"."Children": [{"Name": "golang"."Path": "/dev/golang/"."Children": []."User": [
"xiaoppp"] {},"Name": "clang"."Path": "/dev/clang/"."Children": [{"Name": "woshixiaozhu"."Path": "/dev/clang/woshixiaozhu/"."Children": []."User": [
"xiaopang2"]}],"User": []}, {"Name": "java"."Path": "/dev/java/"."Children": []."User": []}],"User": []}],"User": [
"admin"."zhangsan"."xiaopang"."xiaopang2"]}Copy the code
Learning, if there is a deviation, please also give advice, careful friends will find the above logic pit, see you next time
Welcome to like, follow and favorites
Friends, your support and encouragement, I insist on sharing, improve the quality of the power
All right, that’s it for this time
Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.
I am Nezha, welcome to like, see you next time ~