Apply gofmt & minor refactoring

This commit is contained in:
Alex 2020-01-26 17:45:04 +01:00
parent a7ccdad378
commit 2ad9bce75c

38
main.go
View file

@ -35,8 +35,8 @@ func dnToConsul(dn string) (string, error) {
return strings.Join(rdns, "/"), nil
}
func consulToDN(pair *consul.KVPair) (string, string, []byte) {
path := strings.Split(pair.Key, "/")
func consulToDN(key string) (string, string) {
path := strings.Split(key, "/")
dn := ""
for _, cpath := range path {
if cpath == "" {
@ -44,14 +44,14 @@ func consulToDN(pair *consul.KVPair) (string, string, []byte) {
}
kv := strings.Split(cpath, "=")
if len(kv) == 2 && kv[0] == "attribute" {
return dn, kv[1], pair.Value
return dn, kv[1]
}
if dn != "" {
dn = "," + dn
}
dn = cpath + dn
}
panic("Consul key " + pair.Key + " does not end with attribute=something")
panic("Consul key " + key + " does not end with attribute=something")
}
func parseValue(value []byte) ([]string, error) {
@ -75,14 +75,11 @@ func parseConsulResult(data []*consul.KVPair) (map[string]Entry, error) {
for _, kv := range data {
log.Printf("(parseConsulResult) %s %s", kv.Key, string(kv.Value))
dn, attr, val := consulToDN(kv)
if attr == "" || val == nil {
continue
}
dn, attr := consulToDN(kv.Key)
if _, exists := aggregator[dn]; !exists {
aggregator[dn] = Entry{}
}
value, err := parseValue(val)
value, err := parseValue(kv.Value)
if err != nil {
return nil, err
}
@ -274,7 +271,7 @@ func (server *Server) getAttribute(dn string, attr string) ([]string, error) {
return nil, err
}
pair, _, err := server.kv.Get(path + "/attribute=" + attr, nil)
pair, _, err := server.kv.Get(path+"/attribute="+attr, nil)
if err != nil {
return nil, err
}
@ -292,7 +289,7 @@ func (server *Server) objectExists(dn string) (bool, error) {
return false, err
}
data, _, err := server.kv.List(prefix + "/", nil)
data, _, err := server.kv.List(prefix+"/", nil)
if err != nil {
return false, err
}
@ -382,7 +379,7 @@ func (server *Server) handleSearchInternal(state *State, w ldap.ResponseWriter,
return ldap.LDAPResultInvalidDNSyntax, err
}
data, _, err := server.kv.List(basePath + "/", nil)
data, _, err := server.kv.List(basePath+"/", nil)
if err != nil {
return ldap.LDAPResultOperationsError, err
}
@ -391,7 +388,7 @@ func (server *Server) handleSearchInternal(state *State, w ldap.ResponseWriter,
if err != nil {
return ldap.LDAPResultOperationsError, err
}
log.Printf("in %s: %#v", basePath + "/", data)
log.Printf("in %s: %#v", basePath+"/", data)
log.Printf("%#v", entries)
for dn, entry := range entries {
@ -673,7 +670,7 @@ func (server *Server) handleDeleteInternal(state *State, r *message.DelRequest)
return ldap.LDAPResultInvalidDNSyntax, err
}
items, _, err := server.kv.List(path + "/", nil)
items, _, err := server.kv.List(path+"/", nil)
if err != nil {
return ldap.LDAPResultOperationsError, err
}
@ -682,7 +679,7 @@ func (server *Server) handleDeleteInternal(state *State, r *message.DelRequest)
return ldap.LDAPResultNoSuchObject, fmt.Errorf("Not found: %s", dn)
}
for _, item := range items {
itemDN, _, _ := consulToDN(item)
itemDN, _ := consulToDN(item.Key)
if itemDN != dn {
return ldap.LDAPResultNotAllowedOnNonLeaf, fmt.Errorf(
"Cannot delete %d as it has children", dn)
@ -696,7 +693,7 @@ func (server *Server) handleDeleteInternal(state *State, r *message.DelRequest)
}
// Delete the LDAP entry
_, err = server.kv.DeleteTree(path + "/", nil)
_, err = server.kv.DeleteTree(path+"/", nil)
if err != nil {
return ldap.LDAPResultOperationsError, err
}
@ -704,7 +701,7 @@ func (server *Server) handleDeleteInternal(state *State, r *message.DelRequest)
// Delete it from the member list of all the groups it was a member of
if memberOf != nil {
for _, group := range memberOf {
groupMembers , err := server.getAttribute(dn, "member")
groupMembers, err := server.getAttribute(dn, "member")
if err != nil {
return ldap.LDAPResultOperationsError, err
}
@ -728,7 +725,6 @@ func (server *Server) handleDeleteInternal(state *State, r *message.DelRequest)
return ldap.LDAPResultSuccess, nil
}
func (server *Server) handleModify(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {
state := s.(*State)
r := m.GetModifyRequest()
@ -758,7 +754,7 @@ func (server *Server) handleModifyInternal(state *State, r *message.ModifyReques
return ldap.LDAPResultInvalidDNSyntax, err
}
items, _, err := server.kv.List(path + "/attribute=", nil)
items, _, err := server.kv.List(path+"/attribute=", nil)
if err != nil {
return ldap.LDAPResultOperationsError, err
}
@ -769,11 +765,11 @@ func (server *Server) handleModifyInternal(state *State, r *message.ModifyReques
prevEntry := Entry{}
for _, item := range items {
itemDN, attr, val := consulToDN(item)
itemDN, attr := consulToDN(item.Key)
if itemDN != dn {
panic("itemDN != dn in handleModifyInternal")
}
vals, err := parseValue(val)
vals, err := parseValue(item.Value)
if err != nil {
return ldap.LDAPResultOperationsError, err
}