aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-01-26 17:45:04 +0100
committerAlex Auvolat <alex@adnab.me>2020-01-26 17:45:04 +0100
commit2ad9bce75cdad01089a02518de4b8137f4a3ffe3 (patch)
treeeb081df8eb3a481faf165c7e5fa2abb7ccfaea2d /main.go
parenta7ccdad378391d6d2f3585e35df84e6756bc90ca (diff)
downloadbottin-2ad9bce75cdad01089a02518de4b8137f4a3ffe3.tar.gz
bottin-2ad9bce75cdad01089a02518de4b8137f4a3ffe3.zip
Apply gofmt & minor refactoring
Diffstat (limited to 'main.go')
-rw-r--r--main.go38
1 files changed, 17 insertions, 21 deletions
diff --git a/main.go b/main.go
index f1be40a..4d1f418 100644
--- a/main.go
+++ b/main.go
@@ -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
}