Refactoring

This commit is contained in:
Alex 2021-03-09 18:30:59 +01:00
parent dc3fd4df65
commit 1a20a64eff
4 changed files with 87 additions and 82 deletions

79
main.go
View file

@ -10,7 +10,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
ldap "bottin/ldapserver" ldap "bottin/ldapserver"
@ -358,84 +357,6 @@ func (server *Server) init() error {
return nil return nil
} }
func (server *Server) putAttributes(dn string, attrs Entry) error {
prefix, err := dnToConsul(dn)
if err != nil {
return err
}
for k, valuesNC := range attrs {
path := prefix + "/attribute=" + k
// Trim spaces and remove empty values
values := []string{}
for _, v := range valuesNC {
vv := strings.TrimSpace(v)
if len(vv) > 0 {
values = append(values, vv)
}
}
// If we have zero values, delete associated k/v pair
// Otherwise, write new values
if len(values) == 0 {
_, err := server.kv.Delete(path, nil)
if err != nil {
return err
}
} else {
json, err := json.MarshalIndent(values, "", " ")
if err != nil {
return err
}
pair := &consul.KVPair{Key: path, Value: json}
_, err = server.kv.Put(pair, nil)
if err != nil {
return err
}
}
}
return nil
}
func (server *Server) getAttribute(dn string, attr string) ([]string, error) {
path, err := dnToConsul(dn)
if err != nil {
return nil, err
}
pairs, _, err := server.kv.List(path+"/attribute=", &server.readOpts)
if err != nil {
return nil, err
}
values := []string{}
for _, pair := range pairs {
if strings.EqualFold(pair.Key, path+"/attribute="+attr) {
newVals, err := parseValue(pair.Value)
if err != nil {
return nil, err
}
values = append(values, newVals...)
}
}
return values, nil
}
func (server *Server) objectExists(dn string) (bool, error) {
prefix, err := dnToConsul(dn)
if err != nil {
return false, err
}
data, _, err := server.kv.List(prefix+"/attribute=", &server.readOpts)
if err != nil {
return false, err
}
return len(data) > 0, nil
}
func (server *Server) checkDN(dn string, allow_extend bool) (string, error) { func (server *Server) checkDN(dn string, allow_extend bool) (string, error) {
// 1. Canonicalize: remove spaces between things and put all in lower case // 1. Canonicalize: remove spaces between things and put all in lower case
dn, err := canonicalDN(dn) dn, err := canonicalDN(dn)

40
read.go
View file

@ -9,6 +9,46 @@ import (
message "github.com/lor00x/goldap/message" message "github.com/lor00x/goldap/message"
) )
// Generic read utility functions ----------
func (server *Server) getAttribute(dn string, attr string) ([]string, error) {
path, err := dnToConsul(dn)
if err != nil {
return nil, err
}
pairs, _, err := server.kv.List(path+"/attribute=", &server.readOpts)
if err != nil {
return nil, err
}
values := []string{}
for _, pair := range pairs {
if strings.EqualFold(pair.Key, path+"/attribute="+attr) {
newVals, err := parseValue(pair.Value)
if err != nil {
return nil, err
}
values = append(values, newVals...)
}
}
return values, nil
}
func (server *Server) objectExists(dn string) (bool, error) {
prefix, err := dnToConsul(dn)
if err != nil {
return false, err
}
data, _, err := server.kv.List(prefix+"/attribute=", &server.readOpts)
if err != nil {
return false, err
}
return len(data) > 0, nil
}
// Compare request ------------------------- // Compare request -------------------------
func (server *Server) handleCompare(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) { func (server *Server) handleCompare(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {

View file

@ -64,15 +64,15 @@ func parseValue(value []byte) ([]string, error) {
func parseConsulResult(data []*consul.KVPair) (map[string]Entry, error) { func parseConsulResult(data []*consul.KVPair) (map[string]Entry, error) {
aggregator := map[string]Entry{} aggregator := map[string]Entry{}
for _, kv := range data { for _, pair := range data {
dn, attr, err := consulToDN(kv.Key) dn, attr, err := consulToDN(pair.Key)
if err != nil { if err != nil {
continue continue
} }
if _, exists := aggregator[dn]; !exists { if _, exists := aggregator[dn]; !exists {
aggregator[dn] = Entry{} aggregator[dn] = Entry{}
} }
value, err := parseValue(kv.Value) value, err := parseValue(pair.Value)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,14 +1,58 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
ldap "bottin/ldapserver" ldap "bottin/ldapserver"
consul "github.com/hashicorp/consul/api"
message "github.com/lor00x/goldap/message" message "github.com/lor00x/goldap/message"
) )
// Generic item modification function --------
func (server *Server) putAttributes(dn string, attrs Entry) error {
prefix, err := dnToConsul(dn)
if err != nil {
return err
}
for k, valuesNC := range attrs {
path := prefix + "/attribute=" + k
// Trim spaces and remove empty values
values := []string{}
for _, v := range valuesNC {
vv := strings.TrimSpace(v)
if len(vv) > 0 {
values = append(values, vv)
}
}
// If we have zero values, delete associated k/v pair
// Otherwise, write new values
if len(values) == 0 {
_, err := server.kv.Delete(path, nil)
if err != nil {
return err
}
} else {
json, err := json.MarshalIndent(values, "", " ")
if err != nil {
return err
}
pair := &consul.KVPair{Key: path, Value: json}
_, err = server.kv.Put(pair, nil)
if err != nil {
return err
}
}
}
return nil
}
// Add request ------------------------ // Add request ------------------------
func (server *Server) handleAdd(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) { func (server *Server) handleAdd(s ldap.UserState, w ldap.ResponseWriter, m *ldap.Message) {