From 2159920ec5002851bb86a7bd780df69efbe74e67 Mon Sep 17 00:00:00 2001 From: thoscut Date: Wed, 5 Feb 2020 21:58:01 +0100 Subject: [PATCH 1/3] Move dependencies to start of dockerfile --- Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index d9ce3bd..4bddbe9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,12 @@ FROM golang:1.13-alpine3.10 AS build-env +#Build deps +RUN apk --no-cache add build-base git + #Setup COPY . /src/gitea-group-sync WORKDIR /src/gitea-group-sync -#Build deps -RUN apk --no-cache add build-base git - RUN go get gopkg.in/ldap.v3 && go get gopkg.in/robfig/cron.v3 && go build FROM alpine:3.10 From ba0245b47c606e752ff83f963283b42e43fad983 Mon Sep 17 00:00:00 2001 From: thoscut Date: Wed, 5 Feb 2020 22:01:45 +0100 Subject: [PATCH 2/3] Add LDAP_USER_LOGIN_ATTRIBUTE parameter --- Dockerfile | 3 +++ docker-compose.yml | 3 ++- gitea-group-sync.go | 13 ++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4bddbe9..cae3f1a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,8 +9,11 @@ WORKDIR /src/gitea-group-sync RUN go get gopkg.in/ldap.v3 && go get gopkg.in/robfig/cron.v3 && go build +# Final FROM alpine:3.10 COPY --from=build-env /src/gitea-group-sync/gitea-group-sync /app/gitea-group-sync/gitea-group-sync + RUN ln -s /app/gitea-group-sync/gitea-group-sync /usr/local/bin/gitea-group-sync + ENTRYPOINT ["/usr/local/bin/gitea-group-sync"] diff --git a/docker-compose.yml b/docker-compose.yml index 3184b8f..e3f6082 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ services: group-sync: container_name: gitea-group-sync build: . - image: localhost:5000/gitea-group-sync + image: gitea-group-sync:latest environment: GITEA_TOKEN: c00c810bb668c63ce7cd8057411d2f560eac469c GITEA_URL: http://192.168.2.2:3000 @@ -13,4 +13,5 @@ services: BIND_PASSWORD: GoodNewsEveryone LDAP_FILTER: (&(objectClass=person)(memberOf=cn=%s,ou=people,dc=planetexpress,dc=com)) LDAP_USER_SEARCH_BASE: 'ou=people,dc=planetexpress,dc=com' + LDAP_USER_LOGIN_ATTRIBUTE: uid REP_TIME: '@every 1m' diff --git a/gitea-group-sync.go b/gitea-group-sync.go index 24540e3..9cf241b 100644 --- a/gitea-group-sync.go +++ b/gitea-group-sync.go @@ -137,6 +137,13 @@ func mainJob() { ldapUserSearchBase = os.Getenv("LDAP_USER_SEARCH_BASE") } + var ldapUserLoginAttribute string + if len(os.Getenv("LDAP_USER_LOGIN_ATTRIBUTE")) == 0 { + log.Println("LDAP_USER_LOGIN_ATTRIBUTE is empty") + } else { + ldapUserLoginAttribute = os.Getenv("LDAP_USER_LOGIN_ATTRIBUTE") + } + l, err := ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", ldapUrl, ldapPort), &tls.Config{InsecureSkipVerify: true}) if err != nil { fmt.Println(err) @@ -178,7 +185,7 @@ func mainJob() { ldapUserSearchBase, // The base dn to search ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, filter, // The filter to apply - []string{"cn", "uid", "mailPrimaryAddress, sn"}, // A list attributes to retrieve + []string{"cn", "uid", "mailPrimaryAddress, sn", ldapUserLoginAttribute}, // A list attributes to retrieve nil, ) // make request to ldap server @@ -193,9 +200,9 @@ func mainJob() { log.Printf("The LDAP %s has %d users corresponding to team %s", ldapUrl, len(sr.Entries), teamList[j].Name) for _, entry := range sr.Entries { - AccountsLdap[entry.GetAttributeValue("uid")] = Account{ + AccountsLdap[entry.GetAttributeValue(ldapUserLoginAttribute)] = Account{ Full_name: entry.GetAttributeValue("sn"), //change to cn if you need it - Login: entry.GetAttributeValue("uid"), + Login: entry.GetAttributeValue(ldapUserLoginAttribute), } } From f7619d6a9f526816de79e3d3b90c82f06990d530 Mon Sep 17 00:00:00 2001 From: Thomas Scheel Date: Thu, 6 Feb 2020 12:49:25 +0100 Subject: [PATCH 3/3] Search by login instead of full name --- gitea-group-sync.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitea-group-sync.go b/gitea-group-sync.go index 9cf241b..8c686b9 100644 --- a/gitea-group-sync.go +++ b/gitea-group-sync.go @@ -18,8 +18,8 @@ func AddUsersToTeam(apiKeys GiteaKeys, users []Account, team int) bool { for i := 0; i < len(users); i++ { - fullusername := url.PathEscape(fmt.Sprintf("%s", users[i].Full_name)) - apiKeys.Command = "/api/v1/users/search?q=" + fullusername + "&access_token=" + userlogin := url.PathEscape(fmt.Sprintf("%s", users[i].Login)) + apiKeys.Command = "/api/v1/users/search?q=" + userlogin + "&access_token=" foundUsers := RequestSearchResults(apiKeys) for j := 0; j < len(foundUsers.Data); j++ {