test_automatique fini et fonctionnel

This commit is contained in:
MrArmonius 2021-07-08 16:22:30 +02:00
parent 32cce4c4d3
commit f294b66874

View file

@ -7,10 +7,19 @@ import (
"math/rand" "math/rand"
"strings" "strings"
"errors" "errors"
"os"
) )
const bindusername = "cn=admin,dc=deuxfleurs,dc=fr"
const adresse = "127.0.0.1"
const port = 1389
var bindpassword string
var all_names = make(map[string]struct{}) var all_names = make(map[string]struct{})
func printError(LDAPError error) { func printError(LDAPError error) {
if LDAPError != nil { if LDAPError != nil {
log.Fatal(LDAPError) log.Fatal(LDAPError)
@ -165,8 +174,9 @@ func test_attributes(l *ldap.Conn, tab_AddRequest []ldap.AddRequest, filter_obje
return nil return nil
} }
func clean(l *ldap.Conn, AddReq_users, AddReq_groups []ldap.AddRequest) (err error){ func clean(l *ldap.Conn, AddReq_users, AddReq_groups []ldap.AddRequest,user, group bool) (err error){
log.Debug("Debut clean") log.Debug("Debut clean")
if(user) {
for _,req := range AddReq_users { for _,req := range AddReq_users {
delReq := ldap.NewDelRequest(req.DN,nil) delReq := ldap.NewDelRequest(req.DN,nil)
err = l.Del(delReq) err = l.Del(delReq)
@ -174,14 +184,15 @@ func clean(l *ldap.Conn, AddReq_users, AddReq_groups []ldap.AddRequest) (err err
return return
} }
} }
log.Debug("Fin users - Debut groups") }
if group {
for _,req := range AddReq_groups { for _,req := range AddReq_groups {
delReq := ldap.NewDelRequest(req.DN, nil) delReq := ldap.NewDelRequest(req.DN, nil)
err = l.Del(delReq) err = l.Del(delReq)
if err != nil { if err != nil {
return return
} }
}
} }
defer log.Debug("Fin clean") defer log.Debug("Fin clean")
return return
@ -205,14 +216,103 @@ func test_modify_attributes(l *ldap.Conn, r *rand.Rand, tab_AddReq []ldap.AddReq
return return
} }
func add_user_in_groups(l *ldap.Conn, r *rand.Rand, users, groups []ldap.AddRequest) (err error) {
for _,group := range groups {
numberUsers := r.Intn(19) + 1 //Always a minimum of 1 user
list_users := []string{}
for i:=0; i < numberUsers; i++ {
list_users = append(list_users, users[i].DN)
}
modifyReq := ldap.NewModifyRequest( group.DN, nil)
modifyReq.Add("member", list_users)
err = l.Modify(modifyReq)
if err != nil {
log.Warn(fmt.Sprintf("Error: ModifyReq failed, func:add_users_in_groups from group:\n %d",group))
return
}
}
return
}
func delete_groups(l *ldap.Conn, groups []ldap.AddRequest) (list map[string][]string ,err error) {
list = make(map[string][]string)
for _, group := range groups {
//Get lists_users
cn := strings.Split(group.DN,",")[0]
search_req := ldap.NewSearchRequest(
"ou=groups,dc=deuxfleurs,dc=fr",
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(&(objectclass=groupOfNames)(%s))",cn),
[]string{"member"},
nil,
)
res , err := l.Search(search_req)
if err != nil {
log.Warn(fmt.Sprintf("Error Search: func: delete_groups_and_check_memberOf, from group: \n %d", group))
return list, err
}
if len(res.Entries) != 1 {
err = errors.New(fmt.Sprintf("SearchResult get: %s, SearchResult wanted: 1", len(res.Entries)))
return list, err
}
EntryAtt := res.Entries[0].Attributes
list_users := []string{}
for _, att := range EntryAtt {
list_users = append(list_users ,att.Values[0])
}
//Del group
del := ldap.NewDelRequest( group.DN, nil)
err = l.Del(del)
if err != nil {
return list, err
}
list[group.DN] = list_users
}
return
}
func check_memberOf(l *ldap.Conn, list map[string][]string) (err error) {
//Check the memberOf of all users
for groupeDN,_ := range list{
search_req := ldap.NewSearchRequest(
"ou=users,dc=deuxfleurs,dc=fr",
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases,0 ,0, false,
fmt.Sprintf("(&(objectclass=inetOrgPerson)(memberOf=%s))",groupeDN),
[]string{"cn"},
nil,
)
res, err := l.Search(search_req)
if err != nil {
return err
}
if len(res.Entries) != 0 {
err = errors.New(fmt.Sprintf("L'user '%s' a encore le DN d'un groupe supprimé: %s",res.Entries[0].Attributes[0].Values[0],groupeDN))
return err
}
}
return err
}
func reconnect(l *ldap.Conn) (l_nouv *ldap.Conn, err error){
l.Close()
l_nouv, err = ldap.Dial("tcp", fmt.Sprintf("%s:%d",adresse,port))
if err != nil {
return
}
err = l_nouv.Bind(bindusername, bindpassword)
return
}
func main() { func main() {
bindusername :="cn=admin,dc=deuxfleurs,dc=fr" bindpassword = "FhvuwPzk0ko"
bindpassword := "FhvuwPzk0ko" if len(os.Args)==2 {
bindpassword = os.Args[1]
}
adresse := "127.0.0.1" //log.SetLevel(log.TraceLevel)
port := 1389
log.SetLevel(log.TraceLevel)
//Create a connection with Bottin server //Create a connection with Bottin server
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", adresse, port)) l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", adresse, port))
@ -259,13 +359,8 @@ func main() {
//Close the connection and open an other. If we don't do this, bottin server send a wrong answer. Comment this part if you want to try this //Close the connection and open an other. If we don't do this, bottin server send a wrong answer. Comment this part if you want to try this
l.Close() l,err = reconnect(l)
l, err = ldap.Dial("tcp", fmt.Sprintf("%s:%d",adresse, port))
printError(err) printError(err)
err = l.Bind(bindusername, bindpassword)
printError(err)
defer l.Close()
//Modify attributes users and groups. //Modify attributes users and groups.
//Modify users' attributes and check them //Modify users' attributes and check them
@ -294,11 +389,33 @@ func main() {
printError(err) printError(err)
log.Info("Les modifications ont bien été prises en compte") log.Info("Les modifications ont bien été prises en compte")
//Close the connection
l, err = reconnect(l)
printError(err)
//Add users in group, search them, delete several samples and search again to be sur it's good //Add users in group, search them, delete several samples and search again to be sur it's good
err = add_user_in_groups(l, r, tab_AddRequest_users, tab_AddRequest_groups)
printError(err)
log.Info("Ajout d'users dans les groupes fait")
//Close the connection
l, err = reconnect(l)
printError(err)
list, err := delete_groups(l, tab_AddRequest_groups)
printError(err)
log.Info("groupe supprimé")
l,err = reconnect(l)
printError(err)
err = check_memberOf(l, list)
printError(err)
log.Info("Le memberOf a été correctement vidé")
//Clean: Delete all users and groups (not OU users and groups) //Clean: Delete all users and groups (not OU users and groups)
err = clean(l, tab_AddRequest_users, tab_AddRequest_groups) err = clean(l, tab_AddRequest_users, tab_AddRequest_groups, true, false)
printError(err) printError(err)
log.Info("Clean succes") log.Info("Clean succes")