forked from gitea-group-sync/gitea-group-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitea-group-sync.go
267 lines (220 loc) · 7.77 KB
/
gitea-group-sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"crypto/tls"
"fmt"
"log"
"net/url"
"os"
"strconv"
"strings"
"time"
)
import "gopkg.in/ldap.v3"
import "github.com/robfig/cron/v3"
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="
foundUsers := RequestSearchResults(apiKeys)
for j := 0; j < len(foundUsers.Data); j++ {
if strings.EqualFold(users[i].Login, foundUsers.Data[j].Login) {
apiKeys.Command = "/api/v1/teams/" + fmt.Sprintf("%d", team) + "/members/" + foundUsers.Data[j].Login + "?access_token="
error := RequestPut(apiKeys)
if len(error) > 0 {
log.Println("Error (Team does not exist or Not Found User) :", parseJson(error).(map[string]interface{})["message"])
}
}
}
}
return true
}
func DelUsersFromTeam(apiKeys GiteaKeys, Users []Account, team int) bool {
for i := 0; i < len(Users); i++ {
apiKeys.Command = "/api/v1/users/search?uid=" + fmt.Sprintf("%d", Users[i].Id) + "&access_token="
foundUser := RequestSearchResults(apiKeys)
apiKeys.Command = "/api/v1/teams/" + fmt.Sprintf("%d", team) + "/members/" + foundUser.Data[0].Login + "?access_token="
RequestDel(apiKeys)
}
return true
}
func main() {
mainJob() // First run for check settings
var repTime string
if len(os.Getenv("REP_TIME")) == 0 {
} else {
repTime = os.Getenv("REP_TIME")
}
c := cron.New()
c.AddFunc(repTime, mainJob)
c.Start()
fmt.Println(c.Entries())
for true {
time.Sleep(100*time.Second)
}
}
func mainJob() {
//------------------------------
// Check and Set input settings
//------------------------------
var apiKeys GiteaKeys
if len(os.Getenv("GITEA_TOKEN")) < 40 { // get on https://[web_site_url]/user/settings/applications
log.Println("GITEA_TOKEN is empty or invalid.")
} else {
apiKeys.TokenKey = strings.Split(os.Getenv("GITEA_TOKEN"), ",")
}
if len(os.Getenv("GITEA_URL")) == 0 {
log.Println("GITEA_URL is empty")
} else {
apiKeys.BaseUrl = os.Getenv("GITEA_URL")
}
var ldapUrl string = "ucs.totalwebservices.net"
if len(os.Getenv("LDAP_URL")) == 0 {
log.Println("LDAP_URL is empty")
} else {
ldapUrl = os.Getenv("LDAP_URL")
}
var ldapPort int
var ldapTls bool
if len(os.Getenv("LDAP_TLS_PORT")) > 0 {
port, err := strconv.Atoi(os.Getenv("LDAP_TLS_PORT"))
ldapPort = port
ldapTls = true
log.Printf("DialTLS:=%v:%d", ldapUrl, ldapPort)
if err != nil {
log.Println("LDAP_TLS_PORT is invalid.")
}
} else {
if len(os.Getenv("LDAP_PORT")) > 0 {
port, err := strconv.Atoi(os.Getenv("LDAP_PORT"))
ldapPort = port
ldapTls = false
log.Printf("Dial:=%v:%d", ldapUrl, ldapPort)
if err != nil {
log.Println("LDAP_PORT is invalid.")
}
}
}
var ldapbindDN string
if len(os.Getenv("BIND_DN")) == 0 {
log.Println("BIND_DN is empty")
} else {
ldapbindDN = os.Getenv("BIND_DN")
}
var ldapbindPassword string
if len(os.Getenv("BIND_PASSWORD")) == 0 {
log.Println("BIND_PASSWORD is empty")
} else {
ldapbindPassword = os.Getenv("BIND_PASSWORD")
}
var ldapUserFilter string
if len(os.Getenv("LDAP_FILTER")) == 0 {
log.Println("LDAP_FILTER is empty")
} else {
ldapUserFilter = os.Getenv("LDAP_FILTER")
}
var ldapUserSearchBase string
if len(os.Getenv("LDAP_USER_SEARCH_BASE")) == 0 {
log.Println("LDAP_USER_SEARCH_BASE is empty")
} else {
ldapUserSearchBase = os.Getenv("LDAP_USER_SEARCH_BASE")
}
var ldapUserIdentityAttribute string
if len(os.Getenv("LDAP_USER_IDENTITY_ATTRIBUTE")) == 0 {
ldapUserIdentityAttribute = "uid"
log.Println("By default LDAP_USER_IDENTITY_ATTRIBUTE = 'uid'")
} else {
ldapUserIdentityAttribute = os.Getenv("LDAP_USER_IDENTITY_ATTRIBUTE")
}
var ldapUserFullName string
if len(os.Getenv("LDAP_USER_FULL_NAME")) == 0 {
ldapUserFullName = "sn" //change to cn if you need it
log.Println("By default LDAP_USER_FULL_NAME = 'sn'")
} else {
ldapUserFullName = os.Getenv("LDAP_USER_FULL_NAME")
}
var l *ldap.Conn
var err error
if ldapTls {
l, err = ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", ldapUrl, ldapPort), &tls.Config{InsecureSkipVerify: true})
} else {
l, err = ldap.Dial("tcp", fmt.Sprintf("%s:%d", ldapUrl, ldapPort))
}
if err != nil {
fmt.Println(err)
fmt.Println("Please set the correct values for all specifics.")
os.Exit(1)
}
defer l.Close()
err = l.Bind(ldapbindDN, ldapbindPassword)
if err != nil {
log.Fatal(err)
}
page := 1
apiKeys.BruteforceTokenKey = 0
apiKeys.Command = "/api/v1/admin/orgs?page=" + fmt.Sprintf("%d", page) + "&limit=20&access_token=" // List all organizations
organizationList := RequestOrganizationList(apiKeys)
log.Printf("%d organizations were found on the server: %s", len(organizationList), apiKeys.BaseUrl)
for 1 < len(organizationList) {
for i := 0; i < len(organizationList); i++ {
log.Println(organizationList)
log.Printf("Begin an organization review: OrganizationName= %v, OrganizationId= %d \n", organizationList[i].Name, organizationList[i].Id)
apiKeys.Command = "/api/v1/orgs/" + organizationList[i].Name + "/teams?access_token="
teamList := RequestTeamList(apiKeys)
log.Printf("%d teams were found in %s organization", len(teamList), organizationList[i].Name)
log.Printf("Skip synchronization in the Owners team")
apiKeys.BruteforceTokenKey = 0
for j := 1; j < len(teamList); j++ {
// preparing request to ldap server
filter := fmt.Sprintf(ldapUserFilter, teamList[j].Name)
searchRequest := ldap.NewSearchRequest(
ldapUserSearchBase, // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
filter, // The filter to apply
[]string{"cn", "uid", "mailPrimaryAddress, sn", ldapUserIdentityAttribute}, // A list attributes to retrieve
nil,
)
// make request to ldap server
sr, err := l.Search(searchRequest)
if err != nil {
log.Fatal(err)
}
AccountsLdap := make(map[string]Account)
AccountsGitea := make(map[string]Account)
var addUserToTeamList, delUserToTeamlist []Account
if len(sr.Entries) > 0 {
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(ldapUserIdentityAttribute)] = Account{
Full_name: entry.GetAttributeValue(ldapUserFullName),
Login: entry.GetAttributeValue(ldapUserIdentityAttribute),
}
}
apiKeys.Command = "/api/v1/teams/" + fmt.Sprintf("%d", teamList[j].Id) + "/members?access_token="
AccountsGitea, apiKeys.BruteforceTokenKey = RequestUsersList(apiKeys)
log.Printf("The gitea %s has %d users corresponding to team %s Teamid=%d", apiKeys.BaseUrl, len(AccountsGitea), teamList[j].Name, teamList[j].Id)
for k, v := range AccountsLdap {
if AccountsGitea[k].Login != v.Login {
addUserToTeamList = append(addUserToTeamList, v)
}
}
log.Printf("can be added users list %v", addUserToTeamList)
AddUsersToTeam(apiKeys, addUserToTeamList, teamList[j].Id)
for k, v := range AccountsGitea {
if AccountsLdap[k].Login != v.Login {
delUserToTeamlist = append(delUserToTeamlist, v)
}
}
log.Printf("must be del users list %v", delUserToTeamlist)
DelUsersFromTeam(apiKeys, delUserToTeamlist, teamList[j].Id)
} else {
log.Printf("The LDAP %s not found users corresponding to team %s", ldapUrl, teamList[j].Name)
}
}
}
page++
apiKeys.BruteforceTokenKey = 0
apiKeys.Command = "/api/v1/admin/orgs?page=" + fmt.Sprintf("%d", page) + "&limit=20&access_token=" // List all organizations
organizationList = RequestOrganizationList(apiKeys)
log.Printf("%d organizations were found on the server: %s", len(organizationList), apiKeys.BaseUrl)
}
}