Skip to content

Commit e8d8ed2

Browse files
committed
Merge branch 'master' of github.com:KubeOperator/KubePi
2 parents c9fad71 + a90aa75 commit e8d8ed2

File tree

11 files changed

+95
-81
lines changed

11 files changed

+95
-81
lines changed

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ RUN ARCH=$(uname -m) && case $ARCH in aarch64) ARCH="arm64";; x86_64) ARCH="amd6
5151
rm -rf /tmp/* /var/tmp/* /var/cache/apk/* && \
5252
chmod -R 755 /tmp && mkdir -p /opt/webkubectl
5353

54-
55-
ENV TZ='Asia/Shanghai';
54+
RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
55+
&& echo "Asia/Shanghai" > /etc/timezone \
56+
&& apk del tzdata
5657

5758
COPY vimrc.local /etc/vim
5859

internal/api/v1/ldap/ldap.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,13 @@ func (h *Handler) TestConnect() iris.Handler {
8282

8383
func (h *Handler) SyncLdapUser() iris.Handler {
8484
return func(ctx *context.Context) {
85-
uuid := ctx.Params().Get("id")
86-
err := h.ldapService.Sync(uuid, common.DBOptions{})
85+
users, err := h.ldapService.GetLdapUser()
8786
if err != nil {
8887
ctx.StatusCode(iris.StatusInternalServerError)
8988
ctx.Values().Set("message", err.Error())
9089
return
9190
}
92-
ctx.Values().Set("data", "")
91+
ctx.Values().Set("data", users)
9392
}
9493
}
9594

@@ -132,7 +131,7 @@ func Install(parent iris.Party) {
132131
sp.Get("/", handler.ListLdap())
133132
sp.Post("/", handler.AddLdap())
134133
sp.Put("/", handler.UpdateLdap())
135-
sp.Post("/sync/:id", handler.SyncLdapUser())
134+
sp.Post("/sync", handler.SyncLdapUser())
136135
sp.Post("/test/connect", handler.TestConnect())
137136
sp.Post("/test/login", handler.TestLogin())
138137
sp.Post("/import", handler.ImportUser())

internal/api/v1/v1.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ func logHandler() iris.Handler {
141141
log.Operator = profile.Name
142142
log.Operation = method
143143

144+
//handle ldap operate
145+
if strings.Contains(path,"ldap") {
146+
if strings.Contains(path,"import") {
147+
log.Operation = "import"
148+
}
149+
if strings.Contains(path,"sync") {
150+
log.Operation = "sync"
151+
}
152+
if strings.Contains(path,"connect") {
153+
log.Operation = "testConnect"
154+
}
155+
if strings.Contains(path,"login") {
156+
log.Operation = "testLogin"
157+
}
158+
}
159+
144160
pathResource := strings.Split(path, "/")
145161
if strings.HasPrefix(currentPath, "clusters/:name") {
146162
if len(pathResource) < 3 {

internal/service/v1/ldap/ldap.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ type Service interface {
3030
Delete(id string, options common.DBOptions) error
3131
Sync(id string, options common.DBOptions) error
3232
Login(user v1User.User, password string, options common.DBOptions) error
33-
TestConnect(ldap *v1Ldap.Ldap) ([]v1User.ImportUser, error)
33+
TestConnect(ldap *v1Ldap.Ldap) (int, error)
3434
TestLogin(username string, password string) error
3535
ImportUsers(users []v1User.ImportUser) (v1User.ImportResult, error)
3636
CheckStatus() bool
37+
GetLdapUser() ([]v1User.ImportUser, error)
3738
}
3839

3940
func NewService() Service {
@@ -128,17 +129,19 @@ func (l *service) Delete(id string, options common.DBOptions) error {
128129
return db.DeleteStruct(ldap)
129130
}
130131

131-
//func (l *service) GetLdapUser() ([]v1User.ImportUser, error) {
132-
// users := []v1User.ImportUser{}
133-
//
134-
//}
135-
136-
func (l *service) TestConnect(ldap *v1Ldap.Ldap) ([]v1User.ImportUser, error) {
132+
func (l *service) GetLdapUser() ([]v1User.ImportUser, error) {
137133
users := []v1User.ImportUser{}
134+
ldaps, err := l.List(common.DBOptions{})
135+
if err != nil {
136+
return users, err
137+
}
138+
if len(ldaps) == 0 {
139+
return users, errors.New("请先保存LDAP配置")
140+
}
141+
ldap := ldaps[0]
138142
if !ldap.Enable {
139143
return users, errors.New("请先启用LDAP")
140144
}
141-
142145
lc := ldapClient.NewLdapClient(ldap.Address, ldap.Port, ldap.Username, ldap.Password, ldap.TLS)
143146
if err := lc.Connect(); err != nil {
144147
return users, err
@@ -162,7 +165,6 @@ func (l *service) TestConnect(ldap *v1Ldap.Ldap) ([]v1User.ImportUser, error) {
162165
us := new(v1User.ImportUser)
163166
us.Available = true
164167
rv := reflect.ValueOf(&us).Elem().Elem()
165-
166168
for _, at := range entry.Attributes {
167169
for k, v := range mappings {
168170
if v == at.Name && len(at.Values) > 0 {
@@ -173,23 +175,41 @@ func (l *service) TestConnect(ldap *v1Ldap.Ldap) ([]v1User.ImportUser, error) {
173175
}
174176
}
175177
}
176-
if us.Email == "" || us.Name == "" {
178+
if us.Name == "" {
177179
continue
178180
}
179-
if us.NickName == "" {
180-
us.NickName = us.Name
181-
}
182181
_, err = l.userService.GetByNameOrEmail(us.Name, common.DBOptions{})
183182
if err == nil {
184183
us.Available = false
185184
}
186185
users = append(users, *us)
187186
}
188-
if len(users) == 0 && len(entries) > 0 {
189-
return users, errors.New("Mapping 映射失败!")
187+
return users, nil
188+
}
189+
190+
func (l *service) TestConnect(ldap *v1Ldap.Ldap) (int, error) {
191+
users := 0
192+
if !ldap.Enable {
193+
return users, errors.New("请先启用LDAP")
190194
}
191195

192-
return users, nil
196+
lc := ldapClient.NewLdapClient(ldap.Address, ldap.Port, ldap.Username, ldap.Password, ldap.TLS)
197+
if err := lc.Connect(); err != nil {
198+
return users, err
199+
}
200+
attributes, err := ldap.GetAttributes()
201+
if err != nil {
202+
return users, err
203+
}
204+
entries, err := lc.Search(ldap.Dn, ldap.Filter, ldap.SizeLimit, ldap.TimeLimit, attributes)
205+
if err != nil {
206+
return users, err
207+
}
208+
if len(entries) == 0 {
209+
return users, nil
210+
}
211+
212+
return len(entries), nil
193213
}
194214

195215
func (l *service) CheckStatus() bool {
@@ -263,6 +283,13 @@ func (l *service) ImportUsers(users []v1User.ImportUser) (v1User.ImportResult, e
263283
Type: v1User.LDAP,
264284
Email: imp.Email,
265285
}
286+
if us.Email == "" {
287+
us.Email = us.Name + "@example.com"
288+
}
289+
if us.NickName == "" {
290+
us.NickName = us.Name
291+
}
292+
266293
result.Failures = append(result.Failures, us.Name)
267294
tx, err := server.DB().Begin(true)
268295
if err != nil {

pkg/util/ldap/ldap_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (l *Ldap) Connect() error {
4747

4848
func (l *Ldap) Search(dn, filter string, sizeLimit, timeLimit int, attributes []string) ([]*ldap.Entry, error) {
4949
searchRequest := ldap.NewSearchRequest(dn,
50-
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, timeLimit, false,
50+
ldap.ScopeWholeSubtree, ldap.DerefAlways, 0, timeLimit, false,
5151
filter,
5252
attributes,
5353
nil)

pkg/util/podtool/copytopod.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os"
1111
"path"
1212
"path/filepath"
13-
"strings"
1413
)
1514

1615
func (p *PodTool) CopyToContainer(destPath string) error {
@@ -26,18 +25,17 @@ func (p *PodTool) CopyToContainer(destPath string) error {
2625
var stderr bytes.Buffer
2726
p.ExecConfig.Stderr = &stderr
2827
err := p.Exec(Exec)
28+
var stdout bytes.Buffer
29+
p.ExecConfig.Stdout = &stdout
2930
if err != nil {
30-
return fmt.Errorf(err.Error(), stderr)
31-
}
32-
if len(stderr.Bytes()) != 0 {
33-
for _, line := range strings.Split(stderr.String(), "\n") {
34-
if len(strings.TrimSpace(line)) == 0 {
35-
continue
36-
}
37-
if !strings.Contains(strings.ToLower(line), "removing") {
38-
return fmt.Errorf(line)
39-
}
31+
result := ""
32+
if len(stdout.Bytes()) != 0 {
33+
result = stdout.String()
4034
}
35+
if len(stderr.Bytes()) != 0 {
36+
result = stderr.String()
37+
}
38+
return fmt.Errorf(err.Error(), result)
4139
}
4240
return nil
4341
}
@@ -77,16 +75,6 @@ func (p *PodTool) CopyToPod(srcPath, destPath string) error {
7775
}
7876
return fmt.Errorf(err.Error(), result)
7977
}
80-
if len(stderr.Bytes()) != 0 {
81-
for _, line := range strings.Split(stderr.String(), "\n") {
82-
if len(strings.TrimSpace(line)) == 0 {
83-
continue
84-
}
85-
if !strings.Contains(strings.ToLower(line), "removing") {
86-
return fmt.Errorf(line)
87-
}
88-
}
89-
}
9078
return nil
9179
}
9280

web/dashboard/src/business/workloads/pods/podfilebrowser/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
<div slot="tip" class="el-upload__tip">{{ $t("business.pod.upload_tip") }}</div>
137137
</el-upload>
138138
<span slot="footer" class="dialog-footer">
139-
<el-button @click="openUpload=false" :disabled="loading">{{ $t("commons.button.cancel") }}</el-button>
139+
<el-button @click="handleUploadClose" :disabled="loading">{{ $t("commons.button.cancel") }}</el-button>
140140
<el-button type="primary" @click="upload" :loading="loading">{{ $t("commons.button.confirm") }}</el-button>
141141
</span>
142142
</el-dialog>

web/kubepi/src/api/ldap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export function updateLdap(data){
1414
return put(`${baseUrl}`, data)
1515
}
1616

17-
export function syncLdap(id,data) {
18-
return post(`${baseUrl}/sync/${id}`, data)
17+
export function syncLdap(data) {
18+
return post(`${baseUrl}/sync`, data)
1919
}
2020

2121
export function testConnect(data) {

web/kubepi/src/business/user-management/ldap/index.vue

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@
6767
$t("business.user.ldap_remake")
6868
}}
6969
</el-button>
70-
<!-- <el-button @click="sync" :disabled="isSubmitGoing" v-has-permissions="{resource:'ldap',verb:'create'}">{{-->
71-
<!-- $t("commons.button.sync")-->
72-
<!-- }}-->
73-
<!-- </el-button>-->
7470
<el-button type="primary" @click="onSubmit" :disabled="isSubmitGoing"
7571
v-has-permissions="{resource:'ldap',verb:'create'}">{{ $t("commons.button.confirm") }}
7672
</el-button>
@@ -104,6 +100,7 @@
104100
</el-form>
105101
</el-dialog>
106102
<el-dialog :visible.sync="importUserPageOpen" :title="$t('business.user.import_user')" style="height: 900px">
103+
<span>{{ $t("business.user.ldap_helper") }}</span>
107104
<div style="text-align: right;margin-bottom: 10px">
108105
<el-input v-model="searchName" suffix-icon="el-icon-search" style="width: 30%" size="mini" clearable @change="handleSearch" />
109106
</div>
@@ -159,7 +156,7 @@ export default {
159156
return {
160157
form: {
161158
mapping: "{\n" +
162-
" \"Name\":\"sAMAccountName\",\n" +
159+
" \"Name\":\"cn\",\n" +
163160
" \"NickName\":\"cn\",\n" +
164161
" \"Email\":\"mail\"\n" +
165162
"}",
@@ -206,24 +203,6 @@ export default {
206203
}
207204
},
208205
methods: {
209-
sync () {
210-
if (this.form.uuid === undefined || this.form.uuid === "") {
211-
this.$message({
212-
type: "warning",
213-
message: this.$t("business.user.ldap_sync_error")
214-
})
215-
return
216-
}
217-
this.isSubmitGoing = true
218-
syncLdap(this.form.uuid, {}).then(() => {
219-
this.$message({
220-
type: "success",
221-
message: this.$t("business.user.ldap_sync")
222-
})
223-
}).finally(() => {
224-
this.isSubmitGoing = false
225-
})
226-
},
227206
connectTest () {
228207
let isFormReady = false
229208
this.$refs["form"].validate((valid) => {
@@ -234,15 +213,12 @@ export default {
234213
if (!isFormReady) {
235214
return
236215
}
237-
this.tableUsers = []
238216
this.loading = true
239217
this.connectLoading = true
240218
testConnect(this.form).then(res => {
241-
this.users = res.data
242-
this.tableUsers = this.users
243219
this.$message({
244220
type: "success",
245-
message: this.$t("business.user.test_result", { count: res.data.length })
221+
message: this.$t("business.user.test_result", { count: res.data })
246222
})
247223
}).finally(() => {
248224
this.loading = false
@@ -254,12 +230,15 @@ export default {
254230
this.loginForm = {}
255231
},
256232
openImportPage () {
257-
this.importUserPageOpen = true
258233
this.searchName = ""
259-
if (this.users.length === 0) {
260-
this.connectTest()
261-
}
262-
this.tableUsers = this.users
234+
this.loading = true
235+
syncLdap({}).then(res => {
236+
this.users = res.data
237+
this.tableUsers = this.users
238+
this.importUserPageOpen = true
239+
}).finally(() => {
240+
this.loading = false
241+
})
263242
},
264243
importAvailable (row) {
265244
return row.available

web/kubepi/src/i18n/lang/en-US.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ const message = {
189189
ldap_password: "Password",
190190
ldap_filter_dn: "User Filtering DN",
191191
ldap_filter_rule: "User Filtering Rules",
192-
ldap_helper: "Note: Users without mailboxes will not be synchronized, and those with the same login name as local users will not be synchronized!",
192+
ldap_helper: "Note: Users who cannot get the Name mapping attribute will not be matched",
193193
ldap_sync: "Start syncing, please check the user list later",
194194
ldap_sync_error: "Please save first",
195195
type: "Type",

0 commit comments

Comments
 (0)