aboutsummaryrefslogtreecommitdiff
path: root/memberof.go
blob: cd0686a15dff6c0b35d50a020d598275e7944675 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main

import (
	"sort"
)

func (server *Server) memberOfAdd(member string, group string) {
	// Retreive previous memberOf value
	memberGroups, err := server.getAttribute(member, ATTR_MEMBEROF)
	if err != nil {
		server.logger.Warnf("Could not add %s to memberOf of %s: %s", group, member, err)
		return
	}

	// Return early if group is already in memberOf
	for _, mb := range memberGroups {
		if mb == group {
			server.logger.Warnf("Inconsistency detected, %s was memberOf %s at a time when it didn't exist! (not an issue anymore)",
				member, group)
			return
		}
	}

	// Add group to memberOf
	memberGroups = append(memberGroups, group)
	err = server.addElements(member, Entry{
		ATTR_MEMBEROF: memberGroups,
	})
	if err != nil {
		server.logger.Warnf("Could not add %s to memberOf of %s: %s", group, member, err)
	}
}

func (server *Server) memberOfRemove(member string, group string) {
	// Retreive previous memberOf value
	memberOf, err := server.getAttribute(member, ATTR_MEMBEROF)
	if err != nil || memberOf == nil {
		server.logger.Warnf("Could not remove %s from memberOf of %s: %s", group, member, err)
		return
	}

	// Filter out group
	newMemberOf := []string{}
	for _, mb := range memberOf {
		if mb != group {
			newMemberOf = append(newMemberOf, mb)
		}
	}

	// Return early if group already not in memberOf
	if len(newMemberOf) == len(memberOf) {
		server.logger.Warnf("Inconsistency detected, %s was not memberOf %s at a time when it should have been! (not an issue anymore)",
			member, group)
		return
	}

	// Update value of memberOf
	err = server.addElements(member, Entry{
		ATTR_MEMBEROF: newMemberOf,
	})
	if err != nil {
		server.logger.Warnf("Could not remove %s from memberOf of %s: %s", group, member, err)
	}
}

func (server *Server) memberOfResync() error {
	server.logger.Warnf("Starting resync of member/memberOf values")

	// Load the whole LDAP database
	basePath, err := dnToConsul(server.config.Suffix)
	if err != nil {
		return err
	}

	data, _, err := server.kv.List(basePath, nil)
	if err != nil {
		return err
	}

	entries, err := parseConsulResult(data)
	if err != nil {
		return err
	}

	// Check member values for all objects, and remove deleted objects
	// Calculate new memberOf sets for all objects
	newMemberOf := make(map[string][]string)
	for dn := range entries {
		newMemberOf[dn] = []string{}
	}

	for dn, entry := range entries {
		member, ok := entry[ATTR_MEMBER]
		if !ok {
			continue
		}
		keepMember := []string{}
		for _, mem := range member {
			if _, exists := entries[mem]; exists {
				keepMember = append(keepMember, mem)
				newMemberOf[mem] = append(newMemberOf[mem], dn)
			} else {
				server.logger.Warnf("Fixing: %s had as member %s which doesn't exist", dn, mem)
			}
		}
		if len(keepMember) != len(member) {
			server.logger.Warnf("Writing new member list for %s", dn)
			err = server.addElements(dn, Entry{
				ATTR_MEMBER: keepMember,
			})
			if err != nil {
				return err
			}
		}
	}

	// Write updated memberOf values
	for dn, newMemberOfV := range newMemberOf {
		prevMemberOf := []string{}
		if v, ok := entries[dn][ATTR_MEMBEROF]; ok {
			prevMemberOf = v
		}
		different := (len(prevMemberOf) != len(newMemberOfV))
		if !different {
			sort.Strings(newMemberOfV)
			sort.Strings(prevMemberOf)
			for i := range newMemberOfV {
				if newMemberOfV[i] != prevMemberOf[i] {
					different = true
					break
				}
			}
		}
		if different {
			server.logger.Warnf("Fixing memberOf for %s (%d -> %d)",
				dn, len(prevMemberOf), len(newMemberOfV))
			err = server.addElements(dn, Entry{
				ATTR_MEMBEROF: newMemberOfV,
			})
			if err != nil {
				return err
			}
		}
	}

	server.logger.Warnf("Done resyncing member/memberOf values")
	return nil
}