From ecefc619e4a890a2ec8265e73d0b024b07ecb8e7 Mon Sep 17 00:00:00 2001 From: Sangbum Kim Date: Mon, 14 Mar 2016 02:36:32 +0900 Subject: [PATCH] removed logger init func added initial ldap support --- main.go | 3 +- route/api.go | 12 ++++++- util/ldap.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/logger.go | 11 +++--- 4 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 util/ldap.go diff --git a/main.go b/main.go index 82658d7..238bc5d 100644 --- a/main.go +++ b/main.go @@ -33,9 +33,8 @@ var maxProcs = flag.Int("max_procs", 0, "max number of CPUs that can be used sim func main() { flag.Parse() setMaxProcs() - util.InitLogger() - gin.SetMode(gin.ReleaseMode) + //gin.SetMode(gin.ReleaseMode) r := gin.New() // middleware settings r.Use(util.Ginrus(time.RFC3339, true)) diff --git a/route/api.go b/route/api.go index a790e11..37cfe66 100644 --- a/route/api.go +++ b/route/api.go @@ -1,8 +1,18 @@ package route -import "github.com/gin-gonic/gin" +import ( + "amuz.es/go/mnemonics/util" + "github.com/gin-gonic/gin" +) func Api(c *gin.Context) { content := gin.H{"Hello": "World"} + + conn := util.NewAccoauntSource() + conn.Connect() + defer conn.Close() + conn.Search() + util.Log().Error("hello") + c.JSON(200, content) } diff --git a/util/ldap.go b/util/ldap.go new file mode 100644 index 0000000..f4e4c41 --- /dev/null +++ b/util/ldap.go @@ -0,0 +1,90 @@ +// ldap +package util + +import ( + "fmt" + + "github.com/nmcclain/ldap" +) + +type ldapUnit struct { + dn string + filter string + uniqueAttributeName string +} + +type ldapSource struct { + host string + port int16 + user ldapUnit + group ldapUnit + connection *ldap.Conn +} + +type AccountSource interface { + Connect() + Search() + Close() +} + +//ldapSourceBool +func (l *ldapSource) Connect() { + addr := fmt.Sprintf("%s:%d", l.host, l.port) + conn, err := ldap.DialTLS("tcp", addr, nil) + if err == nil { + // (*conn).Debug = true + l.connection = conn + } else { + Log().Error(err.Error()) + panic(err) + } +} + +func (l *ldapSource) Close() { + if l.connection != nil { + l.connection.Close() + l.connection = nil + } +} + +func (l ldapSource) Search() { + + search := ldap.NewSearchRequest( + l.user.dn, + ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, + l.user.filter, + nil, + nil) + searchResults, err := l.connection.Search(search) + // be sure to add error checking! + if err == nil { + for _, v := range searchResults.Entries { + + m := make(map[string][]string) + for _, attr := range v.Attributes { + m[attr.Name] = append(m[attr.Name], attr.Values...) + } + Log().Error(m) + } + } else { + Log().Error(err.Error()) + panic(err) + } +} + +func NewAccoauntSource() AccountSource { + user := ldapUnit{ + dn: "ou=User,dc=amuz,dc=es", + filter: "(objectClass=person)", + uniqueAttributeName: "uid"} + group := ldapUnit{ + dn: "ou=Group,dc=amuz,dc=es", + filter: "|((objectClass=groupOfNames)(objectClass=groupOfUniqueNames))", + uniqueAttributeName: "cn"} + return &ldapSource{ + host: "keeper.amuz.es", + port: 636, + user: user, + group: group} + +} diff --git a/util/logger.go b/util/logger.go index 0b6cddd..4b1e50b 100644 --- a/util/logger.go +++ b/util/logger.go @@ -14,13 +14,12 @@ import ( var logger = logrus.New() -func InitLogger() { - // logging framework - logger.Level = logrus.DebugLevel - logger.Out = os.Stderr -} - func Log() *logrus.Logger { + if logger == nil { + logger = logrus.New() + logger.Level = logrus.DebugLevel + logger.Out = os.Stderr + } // logging framework return logger }